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.common.monitor.ProgressMonitor;
028 import org.jboss.dna.sequencer.java.AbstractJavaMetadata;
029 import org.jboss.dna.sequencer.java.CompilationUnitParser;
030 import org.jboss.dna.sequencer.java.JavaMetadataUtil;
031
032 /**
033 * @author Serge Pagop
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 * @param progressMonitor - The basic <code>ProgressMonitor</code> that facilitates the updating and monitoring of progress
056 * towards the completion of an activity.
057 * @return the new instace of <code>JavaMetadata</code>
058 * @see java.io.File#length()
059 */
060 public static JavaMetadata instance( InputStream inputStream,
061 long length,
062 String encoding,
063 ProgressMonitor progressMonitor ) {
064
065 JavaMetadata javaMetadata = new JavaMetadata();
066 char[] source = null;
067 try {
068 source = JavaMetadataUtil.getJavaSourceFromTheInputStream(inputStream, length, encoding);
069 } catch (Exception e) {
070 e.printStackTrace();
071 return null;
072 }
073
074 CompilationUnit unit = (CompilationUnit)CompilationUnitParser.runJLS3Conversion(source, true);
075 if (unit != null) {
076 javaMetadata.packageMetadata = javaMetadata.createPackageMetadata(unit);
077 javaMetadata.imports = javaMetadata.createImportMetadata(unit);
078 javaMetadata.types = javaMetadata.createTypeMetadata(unit);
079
080 }
081
082 return javaMetadata;
083 }
084
085 /**
086 * Gets the {@link PackageMetadata} from the unit.
087 *
088 * @return the PackageMetadata or null if there is not package declaration for the unit.
089 */
090 public final PackageMetadata getPackageMetadata() {
091 return packageMetadata;
092 }
093
094 /**
095 * Gets a list of {@linkImportMetadata} from the unit.
096 *
097 * @return all imports of this unit if there is one.
098 */
099 public List<ImportMetadata> getImports() {
100 return imports;
101 }
102
103 /**
104 * Gets the list for type declarations (class/interface/enum/annotation) of this unit.
105 *
106 * @return all typeMetadata of this unit.
107 */
108 public List<TypeMetadata> getTypeMetadata() {
109 return types;
110 }
111 }