Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/io/FlagDefinitionParser.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.Collection;
29
import java.util.HashSet;
30
31
import org.xml.sax.Attributes;
32
import org.xml.sax.InputSource;
33
import org.xml.sax.SAXException;
34
import org.xml.sax.SAXParseException;
35
36
import com.ibm.j9tools.om.FlagDefinition;
37
import com.ibm.j9tools.om.FlagDefinitions;
38
import com.ibm.j9tools.om.InvalidFlagDefinitionsException;
39
import com.ibm.j9tools.om.InvalidFlagException;
40
41
public class FlagDefinitionParser extends AbstractParser {
42
43
/** Used by the SAXParser as an intermediary between start and end element handlers. */
44
private FlagDefinition _flag;
45
46
/** List of flag definitions */
47
private FlagDefinitions _flags;
48
49
private final Collection<Throwable> flagErrors = new HashSet<Throwable>();
50
51
/**
52
* Parse passed input stream. Expects well formed XML adhering to the specified
53
* schema. This method should not be called directly but rather via the
54
* FlagDefinitionIO.load method.
55
*
56
* @param input InputStream containing flag definitions XML to be parsed
57
* @return a HashMap of FlagDefinitions keyed by Flag ID Strings.
58
*
59
* @throws InvalidFlagDefinitionsException
60
* @throws IOException
61
*/
62
public FlagDefinitions parse(InputStream input) throws InvalidFlagDefinitionsException, IOException {
63
_flags = new FlagDefinitions();
64
65
try {
66
_parser.getXMLReader().parse(new InputSource(input));
67
} catch (SAXException e) {
68
error(new SAXParseException(e.getMessage(), "", "", 0, 0)); //$NON-NLS-1$//$NON-NLS-2$
69
} finally {
70
if (hasErrors() || hasWarnings()) {
71
throw new InvalidFlagDefinitionsException(getErrors(), getWarnings(), "flags"); //$NON-NLS-1$
72
} else if (flagErrors.size() > 0) {
73
throw new InvalidFlagDefinitionsException(flagErrors, _flags);
74
}
75
}
76
77
return _flags;
78
}
79
80
/**
81
* Parses the specified file.
82
*
83
* @param file file to be parsed
84
* @return the flag definitions found in the given file
85
*
86
* @throws InvalidFlagDefinitionsException
87
* @throws IOException
88
*/
89
public FlagDefinitions parse(File file) throws InvalidFlagDefinitionsException, IOException {
90
setDocumentLocatorFile(file);
91
92
FileInputStream fis = new FileInputStream(file);
93
FlagDefinitions result = parse(fis);
94
fis.close();
95
96
return result;
97
}
98
99
/**
100
* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
101
*/
102
@Override
103
public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, Attributes attributes) {
104
/* We are starting to parse a new element, reset the parsed value */
105
_parsedValue = ""; //$NON-NLS-1$
106
107
if (qName.equalsIgnoreCase("flags")) { //$NON-NLS-1$
108
/* Remember where this build spec was defined */
109
_flags.setLocation(_documentLocatorFile, _documentLocator);
110
}
111
112
if (qName.equalsIgnoreCase("flag") && _flags != null) { //$NON-NLS-1$
113
String id = attributes.getValue("id"); //$NON-NLS-1$
114
_flag = _flags.getFlagDefinition(id);
115
116
if (_flag == null) {
117
_flag = new FlagDefinition(id, "", ""); //$NON-NLS-1$ //$NON-NLS-2$
118
_flag.setLocation(_documentLocatorFile, _documentLocator);
119
} else if (_flag.isComplete()) {
120
InvalidFlagException error = new InvalidFlagException("Flag definition has already been defined.", _flag); //$NON-NLS-1$
121
error.setLineNumber(_documentLocator.getLineNumber());
122
error.setColumn(_documentLocator.getColumnNumber());
123
124
if (_documentLocatorFile != null) {
125
error.setFileName(_documentLocatorFile.getAbsolutePath());
126
}
127
128
flagErrors.add(error);
129
130
_flag = null;
131
}
132
}
133
134
/* Parse the required flags list */
135
else if (qName.equalsIgnoreCase("require") && _flag != null) { //$NON-NLS-1$
136
String flagId = attributes.getValue("flag"); //$NON-NLS-1$
137
138
if (flagId.equalsIgnoreCase(_flag.getId())) {
139
_flag.addRequires(_flag);
140
} else {
141
FlagDefinition flagDef = _flags.getFlagDefinition(flagId);
142
143
if (flagDef == null) {
144
flagDef = new FlagDefinition(flagId, "", ""); //$NON-NLS-1$ //$NON-NLS-2$
145
_flags.addFlagDefinition(flagDef);
146
}
147
148
_flag.addRequires(flagDef);
149
}
150
151
}
152
153
/* Parse the precludes flag list */
154
else if (qName.equalsIgnoreCase("preclude") && _flag != null) { //$NON-NLS-1$
155
String flagId = attributes.getValue("flag"); //$NON-NLS-1$
156
157
if (flagId.equalsIgnoreCase(_flag.getId())) {
158
_flag.addPrecludes(_flag);
159
} else {
160
FlagDefinition flagDef = _flags.getFlagDefinition(flagId);
161
162
if (flagDef == null) {
163
flagDef = new FlagDefinition(flagId, "", ""); //$NON-NLS-1$ //$NON-NLS-2$
164
_flags.addFlagDefinition(flagDef);
165
}
166
167
_flag.addPrecludes(flagDef);
168
}
169
170
}
171
172
return;
173
}
174
175
/**
176
* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
177
*/
178
@Override
179
public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) {
180
if (qName.equalsIgnoreCase("description") && !_flag.isComplete()) { //$NON-NLS-1$
181
_flag.setDescription(_parsedValue);
182
} else if (qName.equalsIgnoreCase("ifRemoved") && !_flag.isComplete()) { //$NON-NLS-1$
183
_flag.setIfRemoved(_parsedValue);
184
} else if (qName.equalsIgnoreCase("flag") && _flag != null) { //$NON-NLS-1$
185
_flag.setComplete(true);
186
_flags.addFlagDefinition(_flag);
187
}
188
189
}
190
191
/**
192
* @return The name of the schema expected by this parser.
193
*/
194
@Override
195
public String getSchemaName() {
196
return "flags-v1.xsd"; //$NON-NLS-1$
197
}
198
199
}
200
201