001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.sequencer.java;
025    
026    import java.io.IOException;
027    import java.io.InputStream;
028    import org.eclipse.jdt.core.dom.Name;
029    import org.eclipse.jdt.internal.compiler.util.Util;
030    import org.jboss.dna.common.util.CheckArg;
031    
032    /**
033     * @author Serge Pagop
034     */
035    public class JavaMetadataUtil {
036        /**
037         * Get the length of the input stream.
038         * 
039         * @param stream - the <code>InputStream</code>
040         * @return the length of the stream.
041         * @throws IOException - exceptional situation during calculating the length.
042         */
043        public static long length( InputStream stream ) throws IOException {
044            return stream.available();
045        }
046    
047        /**
048         * Gets Java source from the <code>InputStream</code>.
049         * 
050         * @param inputStream - the <code>FileInputStream</code>.
051         * @param length - the length of the java file.
052         * @param encoding - the encoding of the source, if there is one.
053         * @return the array character of the java source.
054         * @throws IOException - exceptional error can be thrown during the reading of the file.
055         */
056        public static char[] getJavaSourceFromTheInputStream( InputStream inputStream,
057                                                              long length,
058                                                              String encoding ) throws IOException {
059            char[] source = Util.getInputStreamAsCharArray(inputStream, (int)length, encoding);
060            return source;
061        }
062    
063        /**
064         * Get the fully qualified name from the <code>Name</code>.
065         * 
066         * @param name - the name to process.
067         * @return a FQN of the name.
068         */
069        public static String getName( Name name ) {
070            CheckArg.isNotNull(name, "name");
071            return name.getFullyQualifiedName();
072        }
073        
074        /**
075         * Create a path for the tree with index.
076         * 
077         * @param path the path.
078         * @param index the index begin with 1.
079         * @return the string
080         * @throws IllegalArgumentException if the path is null, blank or empty, or if the index is not a positive value
081         */
082        public static String createPathWithIndex( String path,
083                                            int index ) {
084            CheckArg.isNotEmpty(path, "path");
085            CheckArg.isPositive(index, "index");
086            return path + "[" + index + "]";
087        }
088    
089        /**
090         * Create a path for the tree without index.
091         * 
092         * @param path - the path.
093         * @return the string
094         * @throws IllegalArgumentException if the path is null, blank or empty
095         */
096        public static String createPath( String path ) {
097            CheckArg.isNotEmpty(path, "path");
098            return path;
099        }
100    
101        // prevent construction
102        private JavaMetadataUtil() {
103        }
104    }