1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.sequencer.java.metadata;
25
26 import java.io.InputStream;
27 import java.util.List;
28 import org.eclipse.jdt.core.dom.CompilationUnit;
29 import org.modeshape.sequencer.java.AbstractJavaMetadata;
30 import org.modeshape.sequencer.java.CompilationUnitParser;
31 import org.modeshape.sequencer.java.JavaMetadataUtil;
32
33 /**
34 * Metadata for a Java source file.
35 */
36 public class JavaMetadata extends AbstractJavaMetadata {
37
38 /** The package representation of a compilation unit. */
39 private PackageMetadata packageMetadata;
40
41 /** All the import declarations of a compilation unit. */
42 private List<ImportMetadata> imports;
43
44 /** Types of unit */
45 private List<TypeMetadata> types;
46
47 private JavaMetadata() {
48 // private constructor to enforce static class pattern
49 }
50
51 /**
52 * Creates a new instance of <code>JavaMetadata</code>, that will be used to get informations of a compilation unit.
53 *
54 * @param inputStream - the <code>InputStream</code> in our case a <code>FileInputStream</code> of the java file.
55 * @param length - the length of the java file.
56 * @param encoding - the encoding that can be used.
57 * @return the new instace of <code>JavaMetadata</code>
58 * @see java.io.File#length()
59 */
60 public static JavaMetadata instance( InputStream inputStream,
61 long length,
62 String encoding ) {
63
64 JavaMetadata javaMetadata = new JavaMetadata();
65 char[] source = null;
66 try {
67 source = JavaMetadataUtil.getJavaSourceFromTheInputStream(inputStream, length, encoding);
68 } catch (Exception e) {
69 e.printStackTrace();
70 return null;
71 }
72
73 CompilationUnit unit = (CompilationUnit)CompilationUnitParser.runJLS3Conversion(source, true);
74 if (unit != null) {
75 javaMetadata.packageMetadata = javaMetadata.createPackageMetadata(unit);
76 javaMetadata.imports = javaMetadata.createImportMetadata(unit);
77 javaMetadata.types = javaMetadata.createTypeMetadata(unit);
78
79 }
80
81 return javaMetadata;
82 }
83
84 /**
85 * Gets the {@link PackageMetadata} from the unit.
86 *
87 * @return the PackageMetadata or null if there is not package declaration for the unit.
88 */
89 public final PackageMetadata getPackageMetadata() {
90 return packageMetadata;
91 }
92
93 /**
94 * Gets a list of {@linkImportMetadata} from the unit.
95 *
96 * @return all imports of this unit if there is one.
97 */
98 public List<ImportMetadata> getImports() {
99 return imports;
100 }
101
102 /**
103 * Gets the list for type declarations (class/interface/enum/annotation) of this unit.
104 *
105 * @return all typeMetadata of this unit.
106 */
107 public List<TypeMetadata> getTypeMetadata() {
108 return types;
109 }
110 }