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.metadata;
023    
024    import java.io.InputStream;
025    import java.util.List;
026    import org.eclipse.jdt.core.dom.CompilationUnit;
027    import org.jboss.dna.sequencer.java.AbstractJavaMetadata;
028    import org.jboss.dna.sequencer.java.CompilationUnitParser;
029    import org.jboss.dna.sequencer.java.JavaMetadataUtil;
030    
031    /**
032     * @author Serge Pagop
033     * @author John Verhaeg
034     */
035    public class JavaMetadata extends AbstractJavaMetadata {
036    
037        /** The package representation of a compilation unit. */
038        private PackageMetadata packageMetadata;
039    
040        /** All the import declarations of a compilation unit. */
041        private List<ImportMetadata> imports;
042    
043        /** Types of unit */
044        private List<TypeMetadata> types;
045    
046        private JavaMetadata() {
047        }
048    
049        /**
050         * Creates a new instance of <code>JavaMetadata</code>, that will be used to get informations of a compilation unit.
051         * 
052         * @param inputStream - the <code>InputStream</code> in our case a <code>FileInputStream</code> of the java file.
053         * @param length - the length of the java file.
054         * @param encoding - the encoding that can be used.
055         * @return the new instace of <code>JavaMetadata</code>
056         * @see java.io.File#length()
057         */
058        public static JavaMetadata instance( InputStream inputStream,
059                                             long length,
060                                             String encoding ) {
061    
062            JavaMetadata javaMetadata = new JavaMetadata();
063            char[] source = null;
064            try {
065                source = JavaMetadataUtil.getJavaSourceFromTheInputStream(inputStream, length, encoding);
066            } catch (Exception e) {
067                e.printStackTrace();
068                return null;
069            }
070    
071            CompilationUnit unit = (CompilationUnit)CompilationUnitParser.runJLS3Conversion(source, true);
072            if (unit != null) {
073                javaMetadata.packageMetadata = javaMetadata.createPackageMetadata(unit);
074                javaMetadata.imports = javaMetadata.createImportMetadata(unit);
075                javaMetadata.types = javaMetadata.createTypeMetadata(unit);
076    
077            }
078    
079            return javaMetadata;
080        }
081    
082        /**
083         * Gets the {@link PackageMetadata} from the unit.
084         * 
085         * @return the PackageMetadata or null if there is not package declaration for the unit.
086         */
087        public final PackageMetadata getPackageMetadata() {
088            return packageMetadata;
089        }
090    
091        /**
092         * Gets a list of {@linkImportMetadata} from the unit.
093         * 
094         * @return all imports of this unit if there is one.
095         */
096        public List<ImportMetadata> getImports() {
097            return imports;
098        }
099    
100        /**
101         * Gets the list for type declarations (class/interface/enum/annotation) of this unit.
102         * 
103         * @return all typeMetadata of this unit.
104         */
105        public List<TypeMetadata> getTypeMetadata() {
106            return types;
107        }
108    }