Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/io/BuildInfoParser.java
6005 views
/*******************************************************************************1* Copyright (c) 2007, 2011 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21package com.ibm.j9tools.om.io;2223import java.io.File;24import java.io.FileInputStream;25import java.io.IOException;26import java.io.InputStream;27import java.text.ParseException;28import java.text.SimpleDateFormat;29import java.util.Calendar;30import java.util.Date;31import java.util.TimeZone;3233import org.xml.sax.Attributes;34import org.xml.sax.InputSource;35import org.xml.sax.SAXException;36import org.xml.sax.SAXParseException;3738import com.ibm.j9tools.om.BuildInfo;39import com.ibm.j9tools.om.InvalidBuildInfoException;4041public class BuildInfoParser extends AbstractParser {4243/** Build Information */44private BuildInfo _buildInfo;4546/**47* Parse passed input stream. Expects well formed XML adhering to the specified48* schema. This method should not be called directly but rather via the49* FlagDefinitionIO.load method.50*51* @param input InputStream containing flag definitions XML to be parsed52* @return a HashMap of FlagDefinitions keyed by Flag ID Strings.53*54* @throws InvalidBuildInfoException55*/56public BuildInfo parse(InputStream input) throws InvalidBuildInfoException, IOException {57_buildInfo = new BuildInfo();5859try {60_parser.getXMLReader().parse(new InputSource(input));61} catch (SAXException e) {62error(new SAXParseException(e.getMessage(), "", "", 0, 0)); //$NON-NLS-1$ //$NON-NLS-2$63} finally {64if (hasErrors() || hasWarnings()) {65throw new InvalidBuildInfoException(getErrors(), getWarnings(), "buildInfo"); //$NON-NLS-1$66}67}6869return _buildInfo;70}7172/**73* Parses the specified file.74*75* @param file file to be parsed76* @return the flag definitions found in the given file77*78* @throws InvalidBuildInfoException79*/80public BuildInfo parse(File file) throws InvalidBuildInfoException, IOException {81setDocumentLocatorFile(file);8283FileInputStream fis = new FileInputStream(file);84BuildInfo result = parse(fis);85fis.close();8687return result;88}8990/**91* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)92*/93@Override94@SuppressWarnings("nls")95public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {96/* We are starting to parse a new element, reset the parsed value */97_parsedValue = "";9899if (qName.equalsIgnoreCase("build")) {100_buildInfo = new BuildInfo();101102/* Remember where this build spec was defined */103_buildInfo.setLocation(_documentLocatorFile, _documentLocator);104105String schemaSource = _parser.getProperty(AbstractParser.JAXP_SCHEMA_SOURCE).toString();106int schemaVersion = Integer.parseInt(schemaSource.substring(schemaSource.length() - 5, schemaSource.length() - 4));107108if (schemaVersion >= 4) {109_buildInfo.setValidateDefaultSizes(true);110}111} else if (qName.equalsIgnoreCase("product")) {112_buildInfo.setProductName(attributes.getValue("name"));113_buildInfo.setProductRelease(attributes.getValue("release"));114} else if (qName.equalsIgnoreCase("repositoryBranch")) {115_buildInfo.addRepositoryBranch(attributes.getValue("id"), attributes.getValue("name"));116} else if (qName.equalsIgnoreCase("fsroot")) {117_buildInfo.addFSRoot(attributes.getValue("id"), attributes.getValue("root"));118} else if (qName.equalsIgnoreCase("defaultSize")) {119_buildInfo.addDefaultSize(attributes.getValue("id"), attributes.getValue("value"));120} else if (qName.equalsIgnoreCase("jcl")) {121_buildInfo.addJCL(attributes.getValue("id"));122} else if (qName.equalsIgnoreCase("project")) {123_buildInfo.addSource(attributes.getValue("id"));124} else if (qName.equalsIgnoreCase("builder")) {125_buildInfo.addASMBuilder(attributes.getValue("id"));126}127}128129/**130* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)131*/132@Override133public void endElement(String uri, String localName, String qName) {134if (qName.equalsIgnoreCase("major")) { //$NON-NLS-1$135_buildInfo.setMajorVersion(_parsedValue);136} else if (qName.equalsIgnoreCase("minor")) { //$NON-NLS-1$137_buildInfo.setMinorVersion(_parsedValue);138} else if (qName.equalsIgnoreCase("prefix")) { //$NON-NLS-1$139_buildInfo.setPrefix(_parsedValue);140} else if (qName.equalsIgnoreCase("parentStream")) { //$NON-NLS-1$141_buildInfo.setParentStream(_parsedValue);142} else if (qName.equalsIgnoreCase("streamSplitDate")) { //$NON-NLS-1$143try {144SimpleDateFormat formater = new SimpleDateFormat(BuildInfo.DATE_FORMAT_PATTERN);145StringBuilder sb = new StringBuilder();146147// Switch sign since Etc/GMT notation is inversed GMT-0500 is TimeZone of ID = Etc/GMT+5148char sign = _parsedValue.charAt(_parsedValue.length() - 6);149if (sign == '-') {150sb.append("+"); //$NON-NLS-1$151} else {152sb.append("-"); //$NON-NLS-1$153}154155if (_parsedValue.charAt(_parsedValue.length() - 5) == '0') {156sb.append(_parsedValue.charAt(_parsedValue.length() - 4));157} else {158sb.append(_parsedValue.substring(_parsedValue.length() - 5, _parsedValue.length() - 4));159}160161// Get generic timezone by ID162TimeZone tz = TimeZone.getTimeZone("Etc/GMT" + sb.toString()); //$NON-NLS-1$163// Set formater timezone to ensure date is not converted to local time164formater.setTimeZone(tz);165166// Create calendar date167Date date = formater.parse(_parsedValue);168Calendar calendar = Calendar.getInstance(tz);169calendar.setTime(date);170171_buildInfo.setStreamSplitDate(calendar);172} catch (ParseException e) {173// No need to throw error because XML schema already validates date format174}175} else if (qName.equalsIgnoreCase("runtime")) { //$NON-NLS-1$176_buildInfo.setRuntime(_parsedValue);177}178}179180/**181* @return The name of the schema expected by this parser.182*/183@Override184public String getSchemaName() {185return "build-v6.xsd"; //$NON-NLS-1$186}187188}189190191