Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/io/FeatureParser.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.Vector;
29
30
import org.xml.sax.Attributes;
31
import org.xml.sax.InputSource;
32
import org.xml.sax.SAXException;
33
import org.xml.sax.SAXParseException;
34
35
import com.ibm.j9tools.om.ConfigDirectory;
36
import com.ibm.j9tools.om.FeatureDefinition;
37
import com.ibm.j9tools.om.Flag;
38
import com.ibm.j9tools.om.FlagDefinition;
39
import com.ibm.j9tools.om.FlagDefinitions;
40
import com.ibm.j9tools.om.InvalidFeatureDefinitionException;
41
import com.ibm.j9tools.om.Property;
42
import com.ibm.j9tools.om.Source;
43
44
/**
45
* Parses a feature file given an input stream. An initialized instance of Feature
46
* is returned. The feature xml must adhere to the feature schema.
47
*
48
* @author mac
49
*/
50
public class FeatureParser extends AbstractParser {
51
private FlagDefinitions flagDefinitions;
52
53
private Vector<Property> _properties; // List of properties
54
private Vector<Flag> _flags; // List of flags
55
private Vector<Source> _sources; // List of sources
56
57
private FeatureDefinition _feature = null; // Feature instance to be returned once parsed and initialized
58
59
/**
60
* Parse passed input stream. Expects well formed XML adhering to the specified
61
* schema. This method should not be called directly but rather via the FeatureIO.load
62
* method.
63
*
64
* @param input InputStream containing build spec XML to be parsed
65
* @return initialized instance of a Feature
66
*
67
* @throws InvalidFeatureDefinitionException
68
*/
69
public FeatureDefinition parse(InputStream input, String objectId, FlagDefinitions flagDefinitions) throws InvalidFeatureDefinitionException, IOException {
70
this.flagDefinitions = flagDefinitions;
71
this._feature = new FeatureDefinition();
72
73
try {
74
_parser.getXMLReader().parse(new InputSource(input));
75
} catch (SAXException e) {
76
error(new SAXParseException(e.getMessage(), "", "", 0, 0)); //$NON-NLS-1$ //$NON-NLS-2$
77
} finally {
78
if (hasErrors() || hasWarnings()) {
79
throw new InvalidFeatureDefinitionException(getErrors(), getWarnings(), objectId);
80
}
81
}
82
83
return _feature;
84
}
85
86
/**
87
* Parses the specified file.
88
*
89
* @param file the feature file to be parsed
90
* @return the parsed feature
91
*
92
* @throws InvalidFeatureDefinitionException
93
*/
94
public FeatureDefinition parse(File file, FlagDefinitions flagDefinitions) throws InvalidFeatureDefinitionException, IOException {
95
setDocumentLocatorFile(file);
96
97
FileInputStream fis = new FileInputStream(file);
98
FeatureDefinition result = parse(fis, file.getName().substring(0, file.getName().length() - ConfigDirectory.FEATURE_FILE_EXTENSION.length()), flagDefinitions);
99
fis.close();
100
101
return result;
102
}
103
104
/**
105
* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
106
*/
107
@Override
108
public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, Attributes attributes) {
109
/* We are starting to parse a new element, reset the parsed value */
110
_parsedValue = ""; //$NON-NLS-1$
111
112
if (qName.equalsIgnoreCase("feature")) { //$NON-NLS-1$
113
/* Reset the flags list */
114
_flags = new Vector<Flag>();
115
116
/* Set the build spec id */
117
_feature.setId(attributes.getValue("id")); //$NON-NLS-1$
118
_feature.setLocation(_documentLocatorFile, _documentLocator);
119
}
120
121
else if (qName.equalsIgnoreCase("properties")) { //$NON-NLS-1$
122
_properties = new Vector<Property>();
123
} else if (qName.equalsIgnoreCase("requiredProperty") && _properties != null) { //$NON-NLS-1$
124
Property property = new Property(attributes.getValue("name"), null); //$NON-NLS-1$
125
property.setLocation(_documentLocatorFile, _documentLocator);
126
127
_properties.add(property);
128
}
129
130
/* Parse sources of the feature */
131
else if (qName.equalsIgnoreCase("source")) { //$NON-NLS-1$
132
_sources = new Vector<Source>();
133
} else if (qName.equalsIgnoreCase("project") && _sources != null) { //$NON-NLS-1$
134
Source source = new Source(attributes.getValue("id")); //$NON-NLS-1$
135
source.setLocation(_documentLocatorFile, _documentLocator);
136
137
_sources.add(source);
138
}
139
140
/* Parse flags of the feature */
141
else if (qName.equalsIgnoreCase("flags")) { //$NON-NLS-1$
142
/* Reset the flags list */
143
_flags = new Vector<Flag>();
144
} else if (qName.equalsIgnoreCase("flag") && _flags != null) { //$NON-NLS-1$
145
String flagName = attributes.getValue("id"); //$NON-NLS-1$
146
Boolean flagState = Boolean.valueOf(attributes.getValue("value")); //$NON-NLS-1$
147
FlagDefinition flagDefinition = flagDefinitions.getFlagDefinition(flagName);
148
149
Flag flag = new Flag(flagName, flagState);
150
flag.setLocation(_documentLocatorFile, _documentLocator);
151
152
if (flagDefinition != null) {
153
flag.setDefinition(flagDefinition);
154
}
155
156
_flags.add(flag);
157
}
158
}
159
160
/**
161
* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
162
*/
163
@Override
164
public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) {
165
if (qName.equalsIgnoreCase("id")) { //$NON-NLS-1$
166
_feature.setId(_parsedValue);
167
} else if (qName.equalsIgnoreCase("name")) { //$NON-NLS-1$
168
_feature.setName(_parsedValue);
169
} else if (qName.equalsIgnoreCase("description")) { //$NON-NLS-1$
170
_feature.setDescription(_parsedValue);
171
} else if (qName.equalsIgnoreCase("properties")) { //$NON-NLS-1$
172
for (Property p : _properties) {
173
_feature.addProperty(p);
174
}
175
} else if (qName.equalsIgnoreCase("source")) { //$NON-NLS-1$
176
for (Source s : _sources) {
177
_feature.addSource(s);
178
}
179
} else if (qName.equalsIgnoreCase("flags")) { //$NON-NLS-1$
180
for (Flag f : _flags) {
181
_feature.addFlag(f);
182
}
183
}
184
}
185
186
/**
187
* @return The name of the schema expected by this parser.
188
*/
189
@Override
190
public String getSchemaName() {
191
return "feature-v2.xsd"; //$NON-NLS-1$
192
}
193
}
194
195