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