Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/io/FeatureParser.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.Vector;2829import org.xml.sax.Attributes;30import org.xml.sax.InputSource;31import org.xml.sax.SAXException;32import org.xml.sax.SAXParseException;3334import com.ibm.j9tools.om.ConfigDirectory;35import com.ibm.j9tools.om.FeatureDefinition;36import com.ibm.j9tools.om.Flag;37import com.ibm.j9tools.om.FlagDefinition;38import com.ibm.j9tools.om.FlagDefinitions;39import com.ibm.j9tools.om.InvalidFeatureDefinitionException;40import com.ibm.j9tools.om.Property;41import com.ibm.j9tools.om.Source;4243/**44* Parses a feature file given an input stream. An initialized instance of Feature45* is returned. The feature xml must adhere to the feature schema.46*47* @author mac48*/49public class FeatureParser extends AbstractParser {50private FlagDefinitions flagDefinitions;5152private Vector<Property> _properties; // List of properties53private Vector<Flag> _flags; // List of flags54private Vector<Source> _sources; // List of sources5556private FeatureDefinition _feature = null; // Feature instance to be returned once parsed and initialized5758/**59* Parse passed input stream. Expects well formed XML adhering to the specified60* schema. This method should not be called directly but rather via the FeatureIO.load61* method.62*63* @param input InputStream containing build spec XML to be parsed64* @return initialized instance of a Feature65*66* @throws InvalidFeatureDefinitionException67*/68public FeatureDefinition parse(InputStream input, String objectId, FlagDefinitions flagDefinitions) throws InvalidFeatureDefinitionException, IOException {69this.flagDefinitions = flagDefinitions;70this._feature = new FeatureDefinition();7172try {73_parser.getXMLReader().parse(new InputSource(input));74} catch (SAXException e) {75error(new SAXParseException(e.getMessage(), "", "", 0, 0)); //$NON-NLS-1$ //$NON-NLS-2$76} finally {77if (hasErrors() || hasWarnings()) {78throw new InvalidFeatureDefinitionException(getErrors(), getWarnings(), objectId);79}80}8182return _feature;83}8485/**86* Parses the specified file.87*88* @param file the feature file to be parsed89* @return the parsed feature90*91* @throws InvalidFeatureDefinitionException92*/93public FeatureDefinition parse(File file, FlagDefinitions flagDefinitions) throws InvalidFeatureDefinitionException, IOException {94setDocumentLocatorFile(file);9596FileInputStream fis = new FileInputStream(file);97FeatureDefinition result = parse(fis, file.getName().substring(0, file.getName().length() - ConfigDirectory.FEATURE_FILE_EXTENSION.length()), flagDefinitions);98fis.close();99100return result;101}102103/**104* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)105*/106@Override107public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, Attributes attributes) {108/* We are starting to parse a new element, reset the parsed value */109_parsedValue = ""; //$NON-NLS-1$110111if (qName.equalsIgnoreCase("feature")) { //$NON-NLS-1$112/* Reset the flags list */113_flags = new Vector<Flag>();114115/* Set the build spec id */116_feature.setId(attributes.getValue("id")); //$NON-NLS-1$117_feature.setLocation(_documentLocatorFile, _documentLocator);118}119120else if (qName.equalsIgnoreCase("properties")) { //$NON-NLS-1$121_properties = new Vector<Property>();122} else if (qName.equalsIgnoreCase("requiredProperty") && _properties != null) { //$NON-NLS-1$123Property property = new Property(attributes.getValue("name"), null); //$NON-NLS-1$124property.setLocation(_documentLocatorFile, _documentLocator);125126_properties.add(property);127}128129/* Parse sources of the feature */130else if (qName.equalsIgnoreCase("source")) { //$NON-NLS-1$131_sources = new Vector<Source>();132} else if (qName.equalsIgnoreCase("project") && _sources != null) { //$NON-NLS-1$133Source source = new Source(attributes.getValue("id")); //$NON-NLS-1$134source.setLocation(_documentLocatorFile, _documentLocator);135136_sources.add(source);137}138139/* Parse flags of the feature */140else if (qName.equalsIgnoreCase("flags")) { //$NON-NLS-1$141/* Reset the flags list */142_flags = new Vector<Flag>();143} else if (qName.equalsIgnoreCase("flag") && _flags != null) { //$NON-NLS-1$144String flagName = attributes.getValue("id"); //$NON-NLS-1$145Boolean flagState = Boolean.valueOf(attributes.getValue("value")); //$NON-NLS-1$146FlagDefinition flagDefinition = flagDefinitions.getFlagDefinition(flagName);147148Flag flag = new Flag(flagName, flagState);149flag.setLocation(_documentLocatorFile, _documentLocator);150151if (flagDefinition != null) {152flag.setDefinition(flagDefinition);153}154155_flags.add(flag);156}157}158159/**160* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)161*/162@Override163public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) {164if (qName.equalsIgnoreCase("id")) { //$NON-NLS-1$165_feature.setId(_parsedValue);166} else if (qName.equalsIgnoreCase("name")) { //$NON-NLS-1$167_feature.setName(_parsedValue);168} else if (qName.equalsIgnoreCase("description")) { //$NON-NLS-1$169_feature.setDescription(_parsedValue);170} else if (qName.equalsIgnoreCase("properties")) { //$NON-NLS-1$171for (Property p : _properties) {172_feature.addProperty(p);173}174} else if (qName.equalsIgnoreCase("source")) { //$NON-NLS-1$175for (Source s : _sources) {176_feature.addSource(s);177}178} else if (qName.equalsIgnoreCase("flags")) { //$NON-NLS-1$179for (Flag f : _flags) {180_feature.addFlag(f);181}182}183}184185/**186* @return The name of the schema expected by this parser.187*/188@Override189public String getSchemaName() {190return "feature-v2.xsd"; //$NON-NLS-1$191}192}193194195