Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/io/BuildSpecParser.java
6005 views
1
/*******************************************************************************
2
* Copyright (c) 2007, 2011 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.j9tools.om.io;
23
24
import java.io.File;
25
import java.io.FileInputStream;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.util.HashMap;
29
import java.util.Map;
30
import java.util.Vector;
31
32
import org.xml.sax.Attributes;
33
import org.xml.sax.InputSource;
34
import org.xml.sax.SAXException;
35
import org.xml.sax.SAXParseException;
36
37
import com.ibm.j9tools.om.AsmBuilder;
38
import com.ibm.j9tools.om.BuildSpec;
39
import com.ibm.j9tools.om.ConfigDirectory;
40
import com.ibm.j9tools.om.DefaultSizes;
41
import com.ibm.j9tools.om.Feature;
42
import com.ibm.j9tools.om.FeatureDefinition;
43
import com.ibm.j9tools.om.Flag;
44
import com.ibm.j9tools.om.FlagDefinition;
45
import com.ibm.j9tools.om.FlagDefinitions;
46
import com.ibm.j9tools.om.InvalidBuildSpecException;
47
import com.ibm.j9tools.om.JclConfiguration;
48
import com.ibm.j9tools.om.ObjectFactory;
49
import com.ibm.j9tools.om.Owner;
50
import com.ibm.j9tools.om.Property;
51
import com.ibm.j9tools.om.Source;
52
53
/**
54
* Parses a build spec XML definition into a Java object.
55
*
56
* @author Maciek Klimkowski
57
* @author Gabriel Castro
58
*/
59
public class BuildSpecParser extends AbstractParser {
60
private final Map<String, FeatureDefinition> featureDefinitions = new HashMap<String, FeatureDefinition>();
61
private FlagDefinitions flagDefinitions;
62
63
/** BuildSpec instance to be returned once parsed and initialized */
64
private BuildSpec _buildSpec = null;
65
66
private Vector<Owner> _owners;
67
/** List of owners */
68
private Vector<Feature> _features;
69
/** List of features */
70
private Vector<Flag> _flags;
71
/** List of flags */
72
private Vector<Source> _sources;
73
/** List of sources */
74
private Vector<Property> _properties;
75
76
/** List of properties */
77
78
/**
79
* Parse passed input stream. Expects well formed XML adhering to the specified
80
* schema. This method should not be called directly but rather via {@link ObjectFactory#loadBuildSpec(File)}.
81
*
82
* @param input InputStream containing build spec XML to be parsed
83
* @return initialized instance of a BuildSpec
84
*
85
* @throws InvalidBuildSpecException
86
*/
87
public BuildSpec parse(InputStream input, String objectId, FlagDefinitions flagDefinitions, Map<String, FeatureDefinition> featureDefinitions) throws InvalidBuildSpecException, IOException {
88
this.featureDefinitions.putAll(featureDefinitions);
89
this.flagDefinitions = flagDefinitions;
90
_buildSpec = new BuildSpec();
91
92
try {
93
_parser.getXMLReader().parse(new InputSource(input));
94
} catch (SAXException e) {
95
error(new SAXParseException(e.getMessage(), "", "", 0, 0)); //$NON-NLS-1$ //$NON-NLS-2$
96
} finally {
97
if (hasErrors() || hasWarnings()) {
98
throw new InvalidBuildSpecException(getErrors(), getWarnings(), objectId);
99
}
100
}
101
102
return _buildSpec;
103
}
104
105
/**
106
* Parses the specified file. This method should not be called directly but rather via
107
* {@link ObjectFactory#loadBuildSpec(File)}.
108
*
109
* @param file the file to be loaded
110
* @return A new instance of a BuildSpec
111
*
112
* @throws InvalidBuildSpecException if an error occured while parsing the build spec contained in <code>file</code>
113
*/
114
public BuildSpec parse(File file, FlagDefinitions flagDefinitions, Map<String, FeatureDefinition> featureDefinitions) throws InvalidBuildSpecException, IOException {
115
setDocumentLocatorFile(file);
116
117
FileInputStream fis = new FileInputStream(file);
118
BuildSpec result = parse(fis, file.getName().substring(0, file.getName().length() - ConfigDirectory.BUILD_SPEC_FILE_EXTENSION.length()), flagDefinitions, featureDefinitions);
119
fis.close();
120
121
return result;
122
}
123
124
/**
125
* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
126
*/
127
@Override
128
public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, Attributes attributes) {
129
/* We are starting to parse a new element, reset the parsed value */
130
_parsedValue = ""; //$NON-NLS-1$
131
132
if (qName.equalsIgnoreCase("spec")) { //$NON-NLS-1$
133
/* Reset all lists */
134
_owners = new Vector<Owner>();
135
_features = new Vector<Feature>();
136
_flags = new Vector<Flag>();
137
_sources = new Vector<Source>();
138
_properties = new Vector<Property>();
139
140
/* Set the build spec id */
141
_buildSpec.setId(attributes.getValue("id")); //$NON-NLS-1$
142
143
/* Remember where this build spec was defined */
144
_buildSpec.setLocation(_documentLocatorFile, _documentLocator);
145
}
146
147
/* Parse owners of the build spec */
148
else if (qName.equalsIgnoreCase("owners")) { //$NON-NLS-1$
149
_owners = new Vector<Owner>();
150
}
151
152
/* Parse flags of the build spec */
153
else if (qName.equalsIgnoreCase("flags")) { //$NON-NLS-1$
154
_flags = new Vector<Flag>();
155
} else if (qName.equalsIgnoreCase("flag") && _flags != null) { //$NON-NLS-1$
156
String flagName = attributes.getValue("id"); //$NON-NLS-1$
157
Boolean flagState = Boolean.valueOf(attributes.getValue("value")); //$NON-NLS-1$
158
FlagDefinition flagDefinition = flagDefinitions.getFlagDefinition(flagName);
159
160
Flag flag = new Flag(flagName, flagState);
161
flag.setLocation(_documentLocatorFile, _documentLocator);
162
163
if (flagDefinition != null) {
164
flag.setDefinition(flagDefinition);
165
}
166
167
_flags.add(flag);
168
}
169
170
/* Parse features of the build spec */
171
else if (qName.equalsIgnoreCase("features")) { //$NON-NLS-1$
172
_features = new Vector<Feature>();
173
} else if (qName.equalsIgnoreCase("feature") && _features != null) { //$NON-NLS-1$
174
String featureId = attributes.getValue("id"); //$NON-NLS-1$
175
FeatureDefinition featureDefinition = featureDefinitions.get(featureId);
176
177
Feature feature = new Feature(featureId);
178
feature.setLocation(_documentLocatorFile, _documentLocator);
179
180
if (featureDefinition != null) {
181
feature.setDefinition(featureDefinition);
182
}
183
184
_features.add(feature);
185
}
186
187
/* Parse sources of the build spec */
188
else if (qName.equalsIgnoreCase("source")) { //$NON-NLS-1$
189
_sources = new Vector<Source>();
190
} else if (qName.equalsIgnoreCase("project") && _sources != null) { //$NON-NLS-1$
191
Source source = new Source(attributes.getValue("id")); //$NON-NLS-1$
192
source.setLocation(_documentLocatorFile, _documentLocator);
193
194
_sources.add(source);
195
}
196
197
/* Parse properties of the build spec */
198
else if (qName.equalsIgnoreCase("properties")) { //$NON-NLS-1$
199
_properties = new Vector<Property>();
200
} else if (qName.equalsIgnoreCase("property") && _properties != null) { //$NON-NLS-1$
201
Property property = new Property(attributes.getValue("name"), attributes.getValue("value")); //$NON-NLS-1$ //$NON-NLS-2$
202
property.setLocation(_documentLocatorFile, _documentLocator);
203
204
_properties.add(property);
205
}
206
207
}
208
209
/**
210
* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
211
*/
212
@Override
213
public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) {
214
if (qName.equalsIgnoreCase("name")) { //$NON-NLS-1$
215
_buildSpec.setName(_parsedValue);
216
} else if (qName.equalsIgnoreCase("asmBuilderName")) { //$NON-NLS-1$
217
AsmBuilder asmBuilder = new AsmBuilder(_parsedValue);
218
asmBuilder.setLocation(_documentLocatorFile, _documentLocator);
219
220
_buildSpec.setAsmBuilder(asmBuilder);
221
} else if (qName.equalsIgnoreCase("cpuArchitecture")) { //$NON-NLS-1$
222
_buildSpec.setCpuArchitecture(_parsedValue);
223
} else if (qName.equalsIgnoreCase("os")) { //$NON-NLS-1$
224
_buildSpec.setOs(_parsedValue);
225
} else if (qName.equalsIgnoreCase("defaultJCL")) { //$NON-NLS-1$
226
JclConfiguration jclConfiguration = new JclConfiguration(_parsedValue);
227
jclConfiguration.setLocation(_documentLocatorFile, _documentLocator);
228
229
_buildSpec.setDefaultJCL(jclConfiguration);
230
} else if (qName.equalsIgnoreCase("defaultSizes")) { //$NON-NLS-1$
231
DefaultSizes defaultSizes = new DefaultSizes(_parsedValue);
232
defaultSizes.setLocation(_documentLocatorFile, _documentLocator);
233
234
_buildSpec.setDefaultSizes(defaultSizes);
235
} else if (qName.equalsIgnoreCase("priority")) { //$NON-NLS-1$
236
try {
237
_buildSpec.setPriority(Integer.parseInt(_parsedValue));
238
} catch (NumberFormatException e) {
239
error(new SAXParseException("Invalid priority value, not a valid integer.", _documentLocator)); //$NON-NLS-1$
240
}
241
}
242
243
else if (qName.equalsIgnoreCase("owner")) { //$NON-NLS-1$
244
/* Parsed a single owner, add it to the list of Owners */
245
Owner owner = new Owner(_parsedValue);
246
owner.setLocation(_documentLocatorFile, _documentLocator);
247
248
_buildSpec.addOwner(owner);
249
} else if (qName.equalsIgnoreCase("owners")) { //$NON-NLS-1$
250
/* Parsed all owners, add it to the BuildSpec */
251
_owners.add(new Owner(_parsedValue));
252
}
253
254
/* The <flags> element is being closed. Add all parsed flags to the buildSpec */
255
else if (qName.equalsIgnoreCase("flags")) { //$NON-NLS-1$
256
for (Flag f : _flags) {
257
_buildSpec.addFlag(f);
258
}
259
260
// Once all the flags and features have been added fill the list with the remaining
261
// flags set to a default value of FALSE
262
for (FlagDefinition flagDefinition : flagDefinitions.getFlagDefinitions().values()) {
263
if (!_buildSpec.hasFlag(flagDefinition.getId())) {
264
_buildSpec.addFlag(new Flag(flagDefinition));
265
}
266
}
267
}
268
269
/* The <features> element is being closed. Add all parsed features to the buildSpec */
270
else if (qName.equalsIgnoreCase("features")) { //$NON-NLS-1$
271
for (Feature f : _features) {
272
/* Save the feature id, it is not our responsibility to load the actual feature here */
273
_buildSpec.addFeature(f);
274
}
275
}
276
277
/* The <sources> element is being closed. Add all parsed sources to the buildSpec */
278
else if (qName.equalsIgnoreCase("source")) { //$NON-NLS-1$
279
for (Source s : _sources) {
280
_buildSpec.addSource(s);
281
}
282
}
283
284
/* The <properties> element is being closed. Add all parsed properties to the buildSpec */
285
else if (qName.equalsIgnoreCase("properties")) { //$NON-NLS-1$
286
for (Property p : _properties) {
287
_buildSpec.addProperty(p);
288
}
289
}
290
291
}
292
293
/**
294
* Returns the name of the schema expected from this parser.
295
*
296
* @return The name of the schema expected by this parser.
297
*/
298
@Override
299
public String getSchemaName() {
300
return "spec-v1.xsd"; //$NON-NLS-1$
301
}
302
}
303
304