Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/io/BuildInfoParser.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.text.ParseException;
29
import java.text.SimpleDateFormat;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.TimeZone;
33
34
import org.xml.sax.Attributes;
35
import org.xml.sax.InputSource;
36
import org.xml.sax.SAXException;
37
import org.xml.sax.SAXParseException;
38
39
import com.ibm.j9tools.om.BuildInfo;
40
import com.ibm.j9tools.om.InvalidBuildInfoException;
41
42
public class BuildInfoParser extends AbstractParser {
43
44
/** Build Information */
45
private BuildInfo _buildInfo;
46
47
/**
48
* Parse passed input stream. Expects well formed XML adhering to the specified
49
* schema. This method should not be called directly but rather via the
50
* FlagDefinitionIO.load method.
51
*
52
* @param input InputStream containing flag definitions XML to be parsed
53
* @return a HashMap of FlagDefinitions keyed by Flag ID Strings.
54
*
55
* @throws InvalidBuildInfoException
56
*/
57
public BuildInfo parse(InputStream input) throws InvalidBuildInfoException, IOException {
58
_buildInfo = new BuildInfo();
59
60
try {
61
_parser.getXMLReader().parse(new InputSource(input));
62
} catch (SAXException e) {
63
error(new SAXParseException(e.getMessage(), "", "", 0, 0)); //$NON-NLS-1$ //$NON-NLS-2$
64
} finally {
65
if (hasErrors() || hasWarnings()) {
66
throw new InvalidBuildInfoException(getErrors(), getWarnings(), "buildInfo"); //$NON-NLS-1$
67
}
68
}
69
70
return _buildInfo;
71
}
72
73
/**
74
* Parses the specified file.
75
*
76
* @param file file to be parsed
77
* @return the flag definitions found in the given file
78
*
79
* @throws InvalidBuildInfoException
80
*/
81
public BuildInfo parse(File file) throws InvalidBuildInfoException, IOException {
82
setDocumentLocatorFile(file);
83
84
FileInputStream fis = new FileInputStream(file);
85
BuildInfo result = parse(fis);
86
fis.close();
87
88
return result;
89
}
90
91
/**
92
* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
93
*/
94
@Override
95
@SuppressWarnings("nls")
96
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
97
/* We are starting to parse a new element, reset the parsed value */
98
_parsedValue = "";
99
100
if (qName.equalsIgnoreCase("build")) {
101
_buildInfo = new BuildInfo();
102
103
/* Remember where this build spec was defined */
104
_buildInfo.setLocation(_documentLocatorFile, _documentLocator);
105
106
String schemaSource = _parser.getProperty(AbstractParser.JAXP_SCHEMA_SOURCE).toString();
107
int schemaVersion = Integer.parseInt(schemaSource.substring(schemaSource.length() - 5, schemaSource.length() - 4));
108
109
if (schemaVersion >= 4) {
110
_buildInfo.setValidateDefaultSizes(true);
111
}
112
} else if (qName.equalsIgnoreCase("product")) {
113
_buildInfo.setProductName(attributes.getValue("name"));
114
_buildInfo.setProductRelease(attributes.getValue("release"));
115
} else if (qName.equalsIgnoreCase("repositoryBranch")) {
116
_buildInfo.addRepositoryBranch(attributes.getValue("id"), attributes.getValue("name"));
117
} else if (qName.equalsIgnoreCase("fsroot")) {
118
_buildInfo.addFSRoot(attributes.getValue("id"), attributes.getValue("root"));
119
} else if (qName.equalsIgnoreCase("defaultSize")) {
120
_buildInfo.addDefaultSize(attributes.getValue("id"), attributes.getValue("value"));
121
} else if (qName.equalsIgnoreCase("jcl")) {
122
_buildInfo.addJCL(attributes.getValue("id"));
123
} else if (qName.equalsIgnoreCase("project")) {
124
_buildInfo.addSource(attributes.getValue("id"));
125
} else if (qName.equalsIgnoreCase("builder")) {
126
_buildInfo.addASMBuilder(attributes.getValue("id"));
127
}
128
}
129
130
/**
131
* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
132
*/
133
@Override
134
public void endElement(String uri, String localName, String qName) {
135
if (qName.equalsIgnoreCase("major")) { //$NON-NLS-1$
136
_buildInfo.setMajorVersion(_parsedValue);
137
} else if (qName.equalsIgnoreCase("minor")) { //$NON-NLS-1$
138
_buildInfo.setMinorVersion(_parsedValue);
139
} else if (qName.equalsIgnoreCase("prefix")) { //$NON-NLS-1$
140
_buildInfo.setPrefix(_parsedValue);
141
} else if (qName.equalsIgnoreCase("parentStream")) { //$NON-NLS-1$
142
_buildInfo.setParentStream(_parsedValue);
143
} else if (qName.equalsIgnoreCase("streamSplitDate")) { //$NON-NLS-1$
144
try {
145
SimpleDateFormat formater = new SimpleDateFormat(BuildInfo.DATE_FORMAT_PATTERN);
146
StringBuilder sb = new StringBuilder();
147
148
// Switch sign since Etc/GMT notation is inversed GMT-0500 is TimeZone of ID = Etc/GMT+5
149
char sign = _parsedValue.charAt(_parsedValue.length() - 6);
150
if (sign == '-') {
151
sb.append("+"); //$NON-NLS-1$
152
} else {
153
sb.append("-"); //$NON-NLS-1$
154
}
155
156
if (_parsedValue.charAt(_parsedValue.length() - 5) == '0') {
157
sb.append(_parsedValue.charAt(_parsedValue.length() - 4));
158
} else {
159
sb.append(_parsedValue.substring(_parsedValue.length() - 5, _parsedValue.length() - 4));
160
}
161
162
// Get generic timezone by ID
163
TimeZone tz = TimeZone.getTimeZone("Etc/GMT" + sb.toString()); //$NON-NLS-1$
164
// Set formater timezone to ensure date is not converted to local time
165
formater.setTimeZone(tz);
166
167
// Create calendar date
168
Date date = formater.parse(_parsedValue);
169
Calendar calendar = Calendar.getInstance(tz);
170
calendar.setTime(date);
171
172
_buildInfo.setStreamSplitDate(calendar);
173
} catch (ParseException e) {
174
// No need to throw error because XML schema already validates date format
175
}
176
} else if (qName.equalsIgnoreCase("runtime")) { //$NON-NLS-1$
177
_buildInfo.setRuntime(_parsedValue);
178
}
179
}
180
181
/**
182
* @return The name of the schema expected by this parser.
183
*/
184
@Override
185
public String getSchemaName() {
186
return "build-v6.xsd"; //$NON-NLS-1$
187
}
188
189
}
190
191