Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/com.ibm.uma/com/ibm/uma/om/parser/Parser.java
6005 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2017 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.uma.om.parser;
23
24
import java.io.File;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.net.URL;
28
import java.util.Hashtable;
29
import java.util.Vector;
30
31
import javax.xml.XMLConstants;
32
import javax.xml.parsers.DocumentBuilder;
33
import javax.xml.parsers.DocumentBuilderFactory;
34
import javax.xml.parsers.ParserConfigurationException;
35
import javax.xml.validation.Schema;
36
import javax.xml.validation.SchemaFactory;
37
38
import org.w3c.dom.Document;
39
import org.w3c.dom.NamedNodeMap;
40
import org.w3c.dom.Node;
41
import org.w3c.dom.NodeList;
42
import org.xml.sax.EntityResolver;
43
import org.xml.sax.InputSource;
44
import org.xml.sax.SAXException;
45
46
import com.ibm.uma.IConfiguration;
47
import com.ibm.uma.UMA;
48
import com.ibm.uma.UMAException;
49
import com.ibm.uma.om.Artifact;
50
import com.ibm.uma.om.Module;
51
import com.ibm.uma.om.Predicate;
52
import com.ibm.uma.om.PredicateList;
53
import com.ibm.uma.om.SubdirArtifact;
54
import com.ibm.uma.util.Logger;
55
56
public class Parser implements EntityResolver {
57
58
static String fileCurrentlyBeingParsed;
59
static public String getFileCurrentlyBeingParsed() {
60
return fileCurrentlyBeingParsed;
61
}
62
63
UMA generator;
64
Vector<Module> modules = new Vector<Module>();
65
Hashtable<String,Module> modulesByFullName = new Hashtable<String,Module>();
66
DocumentBuilder domParser;
67
68
public Parser(UMA generator) {
69
this.generator = generator;
70
71
try {
72
URL schemaURL = getClass().getResource("module.xsd");
73
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
74
Schema schema = schemaFactory.newSchema(schemaURL);
75
76
77
DocumentBuilderFactory domParserFactory = DocumentBuilderFactory.newInstance();
78
domParserFactory.setNamespaceAware(true);
79
domParserFactory.setSchema(schema);
80
domParserFactory.setXIncludeAware(true);
81
82
domParser = domParserFactory.newDocumentBuilder();
83
//domParser.setEntityResolver(this);
84
domParser.setErrorHandler(new ParserErrorHandler());
85
} catch (ParserConfigurationException e) {
86
// TODO Auto-generated catch block
87
e.printStackTrace();
88
} catch (SAXException e) {
89
// TODO Auto-generated catch block
90
e.printStackTrace();
91
}
92
93
94
}
95
96
IConfiguration getConfiguration() {
97
return generator.getConfiguration();
98
}
99
100
public Vector<Module> getModules() {
101
return modules;
102
}
103
104
Document parseFile(String xmlFile) throws SAXException, IOException {
105
fileCurrentlyBeingParsed = xmlFile;
106
Document retval = domParser.parse(xmlFile);
107
fileCurrentlyBeingParsed = null;
108
return retval;
109
}
110
111
void resolveModuleHierarchy() throws UMAException {
112
113
generator.validateArtifactLocations();
114
115
// Make sure that all modules have parents all the way to the root.
116
// If a hole is found and a module with artifacts of type subdir.
117
//System.out.println("resolveModuleHierarchy");
118
boolean modulesToBeChecked = true;
119
while (modulesToBeChecked) {
120
//System.out.println("resolveModuleHierarchy - modulesToBeChecked");
121
// Store modules in a hashtable by full name.
122
// e.g., root/gc/module.xml is 'gc'
123
// root/gc/modron/base/module.xml is 'gc/modron/base'
124
for ( Module module : modules ) {
125
if ( !module.evaluate() ) continue;
126
addModuleByLogicalFullname(module);
127
}
128
Vector<Module> newModules = new Vector<Module>();
129
modulesToBeChecked = false;
130
131
for ( Module module : modules) {
132
if ( !module.evaluate() ) continue;
133
// ensure each parent of a module exists.
134
if ( module.isTopLevel() ) continue;
135
String[] parents = module.getParents();
136
for ( int depth=0; depth<module.getModuleDepth(); depth++ ) {
137
Module parentMod = getModuleByLogicalFullname(parents[depth]);
138
if ( parentMod == null ) {
139
// need to create the parent
140
String moduleXmlFilename = UMA.getUma().getRootDirectory();
141
for ( int d=1; d<=depth; d++ ) {
142
moduleXmlFilename = moduleXmlFilename + module.moduleNameAtDepth(d) + "/";
143
}
144
moduleXmlFilename = moduleXmlFilename + getConfiguration().getMetadataFilename();
145
parentMod = new Module(moduleXmlFilename);
146
addModuleByLogicalFullname(parentMod);
147
newModules.add(parentMod);
148
modulesToBeChecked = true;
149
}
150
}
151
}
152
modules.addAll(newModules);
153
for ( Module module : modules) {
154
if ( !module.evaluate() ) continue;
155
// ensure each parent of a module exists.
156
if ( module.isTopLevel()) continue;
157
String[] parents = module.getParents();
158
for ( int depth=0; depth<module.getModuleDepth(); depth++ ) {
159
Module parentMod = getModuleByLogicalFullname(parents[depth]);
160
boolean found = false;
161
String child = module.moduleNameAtDepth(depth+1);
162
for ( Artifact artifact : parentMod.getArtifacts() ){
163
if ( !artifact.evaluate()) continue;
164
if ( artifact.getType() == Artifact.TYPE_SUBDIR ) {
165
if ( artifact.getTargetName().equalsIgnoreCase(child) ) {
166
found = true;
167
}
168
}
169
}
170
if ( !found && !child.equalsIgnoreCase(UMA.getUma().getConfiguration().getMetadataFilename())) {
171
Module subdirModule = getModuleByLogicalFullname(parentMod.getLogicalFullName()+"/"+child);
172
Artifact artifact = new SubdirArtifact(child, subdirModule, parentMod);
173
parentMod.addArtifact(artifact);
174
}
175
}
176
}
177
}
178
179
}
180
181
void addAllModulesFoundInDiretory( File dir, Vector<String> moduleFilenames ) {
182
String rootDir = generator.getRootDirectory();
183
File [] dirListing = dir.listFiles();
184
Vector<File> directories = new Vector<File>();
185
for ( File file : dirListing ) {
186
if ( file.isDirectory() ) {
187
directories.add(file);
188
} else if ( file.getName().equalsIgnoreCase(generator.getConfiguration().getMetadataFilename()) ) {
189
String modulePath = file.getParent();
190
if ( modulePath.equalsIgnoreCase(rootDir) || rootDir.equalsIgnoreCase(modulePath+File.separator)) {
191
modulePath = "";
192
} else {
193
modulePath = file.getParent().substring(rootDir.length());
194
}
195
modulePath = modulePath.replace(File.separator, "/");
196
moduleFilenames.add(modulePath);
197
Logger.getLogger().println(Logger.InformationL2Log, "Adding " + modulePath);
198
}
199
}
200
for ( File file : directories ) {
201
addAllModulesFoundInDiretory(file, moduleFilenames);
202
}
203
}
204
205
public Vector<String> getModuleFilenames(Vector<String> moduleFilenames) {
206
String rootDir = generator.getRootDirectory();
207
String metaDataFileName = generator.getConfiguration().getMetadataFilename();
208
Vector<String> filenames = new Vector<String>();
209
for ( String name : moduleFilenames ) {
210
filenames.add( rootDir + name + "/" + metaDataFileName);
211
}
212
return filenames;
213
}
214
215
216
public boolean parse() throws UMAException {
217
Vector<String> moduleFilenames = new Vector<String>();
218
File rootDirectory = new File(generator.getRootDirectory());
219
addAllModulesFoundInDiretory(rootDirectory, moduleFilenames);
220
221
// parse it to determine directories to search for module.xml files.
222
moduleFilenames = getModuleFilenames(moduleFilenames);
223
224
// parse all found module.xml files.
225
for (String moduleXmlFilename : moduleFilenames) {
226
try {
227
Logger.getLogger().println(Logger.InformationL2Log, "Parsing " + moduleXmlFilename);
228
Document doc = parseFile(moduleXmlFilename);
229
Module module = ModuleParser.parse(doc, moduleXmlFilename);
230
modules.add( module );
231
} catch (SAXException e) {
232
throw new UMAException("Error: Module " + moduleXmlFilename + " failed to parse.", e);
233
} catch (IOException e) {
234
throw new UMAException("Error: " + e.getMessage(), e);
235
}
236
}
237
238
resolveModuleHierarchy();
239
240
return true;
241
}
242
243
static public void populatePredicateList(NodeList nodeList, PredicateList predicates) {
244
for ( int j=0; j<nodeList.getLength(); j++ ) {
245
Node item = nodeList.item(j);
246
NamedNodeMap attributes = item.getAttributes();
247
if ( attributes == null ) continue;
248
Node conditionNode = attributes.getNamedItem("condition");
249
int predicateType = Predicate.EXCLUDE_IF;
250
if ( item.getNodeName().equalsIgnoreCase("include-if") ) {
251
predicateType = Predicate.INCLUDE_IF;
252
} else if ( item.getNodeName().equalsIgnoreCase("exclude-if") ) {
253
predicateType = Predicate.EXCLUDE_IF;
254
} else continue;
255
predicates.add(new Predicate(predicateType,conditionNode.getNodeValue()));
256
}
257
}
258
259
260
261
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
262
if (!systemId.endsWith("module.xsd")) {
263
throw new SAXException("Unable to resolve schema named [" + systemId + "]");
264
}
265
266
InputStream stream = getClass().getResourceAsStream("module.xsd");
267
return new InputSource(stream);
268
}
269
270
public Module getModuleByLogicalFullname(String name) {
271
return modulesByFullName.get(name);
272
}
273
274
public void addModuleByLogicalFullname(Module module ) {
275
//System.out.println("adding logical name: " + module.getLogicalFullName());
276
modulesByFullName.put(module.getLogicalFullName(), module);
277
}
278
279
280
281
}
282
283