Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/io/FlagDefinitionParser.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.util.Collection;28import java.util.HashSet;2930import org.xml.sax.Attributes;31import org.xml.sax.InputSource;32import org.xml.sax.SAXException;33import org.xml.sax.SAXParseException;3435import com.ibm.j9tools.om.FlagDefinition;36import com.ibm.j9tools.om.FlagDefinitions;37import com.ibm.j9tools.om.InvalidFlagDefinitionsException;38import com.ibm.j9tools.om.InvalidFlagException;3940public class FlagDefinitionParser extends AbstractParser {4142/** Used by the SAXParser as an intermediary between start and end element handlers. */43private FlagDefinition _flag;4445/** List of flag definitions */46private FlagDefinitions _flags;4748private final Collection<Throwable> flagErrors = new HashSet<Throwable>();4950/**51* Parse passed input stream. Expects well formed XML adhering to the specified52* schema. This method should not be called directly but rather via the53* FlagDefinitionIO.load method.54*55* @param input InputStream containing flag definitions XML to be parsed56* @return a HashMap of FlagDefinitions keyed by Flag ID Strings.57*58* @throws InvalidFlagDefinitionsException59* @throws IOException60*/61public FlagDefinitions parse(InputStream input) throws InvalidFlagDefinitionsException, IOException {62_flags = new FlagDefinitions();6364try {65_parser.getXMLReader().parse(new InputSource(input));66} catch (SAXException e) {67error(new SAXParseException(e.getMessage(), "", "", 0, 0)); //$NON-NLS-1$//$NON-NLS-2$68} finally {69if (hasErrors() || hasWarnings()) {70throw new InvalidFlagDefinitionsException(getErrors(), getWarnings(), "flags"); //$NON-NLS-1$71} else if (flagErrors.size() > 0) {72throw new InvalidFlagDefinitionsException(flagErrors, _flags);73}74}7576return _flags;77}7879/**80* Parses the specified file.81*82* @param file file to be parsed83* @return the flag definitions found in the given file84*85* @throws InvalidFlagDefinitionsException86* @throws IOException87*/88public FlagDefinitions parse(File file) throws InvalidFlagDefinitionsException, IOException {89setDocumentLocatorFile(file);9091FileInputStream fis = new FileInputStream(file);92FlagDefinitions result = parse(fis);93fis.close();9495return result;96}9798/**99* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)100*/101@Override102public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, Attributes attributes) {103/* We are starting to parse a new element, reset the parsed value */104_parsedValue = ""; //$NON-NLS-1$105106if (qName.equalsIgnoreCase("flags")) { //$NON-NLS-1$107/* Remember where this build spec was defined */108_flags.setLocation(_documentLocatorFile, _documentLocator);109}110111if (qName.equalsIgnoreCase("flag") && _flags != null) { //$NON-NLS-1$112String id = attributes.getValue("id"); //$NON-NLS-1$113_flag = _flags.getFlagDefinition(id);114115if (_flag == null) {116_flag = new FlagDefinition(id, "", ""); //$NON-NLS-1$ //$NON-NLS-2$117_flag.setLocation(_documentLocatorFile, _documentLocator);118} else if (_flag.isComplete()) {119InvalidFlagException error = new InvalidFlagException("Flag definition has already been defined.", _flag); //$NON-NLS-1$120error.setLineNumber(_documentLocator.getLineNumber());121error.setColumn(_documentLocator.getColumnNumber());122123if (_documentLocatorFile != null) {124error.setFileName(_documentLocatorFile.getAbsolutePath());125}126127flagErrors.add(error);128129_flag = null;130}131}132133/* Parse the required flags list */134else if (qName.equalsIgnoreCase("require") && _flag != null) { //$NON-NLS-1$135String flagId = attributes.getValue("flag"); //$NON-NLS-1$136137if (flagId.equalsIgnoreCase(_flag.getId())) {138_flag.addRequires(_flag);139} else {140FlagDefinition flagDef = _flags.getFlagDefinition(flagId);141142if (flagDef == null) {143flagDef = new FlagDefinition(flagId, "", ""); //$NON-NLS-1$ //$NON-NLS-2$144_flags.addFlagDefinition(flagDef);145}146147_flag.addRequires(flagDef);148}149150}151152/* Parse the precludes flag list */153else if (qName.equalsIgnoreCase("preclude") && _flag != null) { //$NON-NLS-1$154String flagId = attributes.getValue("flag"); //$NON-NLS-1$155156if (flagId.equalsIgnoreCase(_flag.getId())) {157_flag.addPrecludes(_flag);158} else {159FlagDefinition flagDef = _flags.getFlagDefinition(flagId);160161if (flagDef == null) {162flagDef = new FlagDefinition(flagId, "", ""); //$NON-NLS-1$ //$NON-NLS-2$163_flags.addFlagDefinition(flagDef);164}165166_flag.addPrecludes(flagDef);167}168169}170171return;172}173174/**175* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)176*/177@Override178public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) {179if (qName.equalsIgnoreCase("description") && !_flag.isComplete()) { //$NON-NLS-1$180_flag.setDescription(_parsedValue);181} else if (qName.equalsIgnoreCase("ifRemoved") && !_flag.isComplete()) { //$NON-NLS-1$182_flag.setIfRemoved(_parsedValue);183} else if (qName.equalsIgnoreCase("flag") && _flag != null) { //$NON-NLS-1$184_flag.setComplete(true);185_flags.addFlagDefinition(_flag);186}187188}189190/**191* @return The name of the schema expected by this parser.192*/193@Override194public String getSchemaName() {195return "flags-v1.xsd"; //$NON-NLS-1$196}197198}199200201