Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/io/AbstractParser.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.InputStream;25import java.text.MessageFormat;26import java.util.HashSet;27import java.util.Set;2829import javax.xml.XMLConstants;30import javax.xml.parsers.SAXParser;31import javax.xml.parsers.SAXParserFactory;3233import org.xml.sax.InputSource;34import org.xml.sax.Locator;35import org.xml.sax.SAXException;36import org.xml.sax.SAXParseException;37import org.xml.sax.XMLReader;38import org.xml.sax.helpers.DefaultHandler;3940/**41* AbstractParser provides extensions to the standard SAX DefaultHandler for42* error propagation, and schema lookup.43*/44public abstract class AbstractParser extends DefaultHandler {4546/** The JAXP property for schema language. */47static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; //$NON-NLS-1$4849/** The JAXP property for schema language. */50static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; //$NON-NLS-1$5152/**53* Used by the SAXParser as an intermediary between start and end element handlers. Contains the value between54* <element>value</element> once the end handler is invoked.55*/56protected String _parsedValue = ""; //$NON-NLS-1$5758/** The underlying SAX parser that does the walk. */59protected SAXParser _parser;6061/** Parse Errors. */62protected Set<Throwable> _warnings = new HashSet<Throwable>();63protected Set<Throwable> _errors = new HashSet<Throwable>();6465/**66* The Locator instance set via setDocumentLocator handler called once when67* we start parsing a document. Keep track of it in order to be able to retrieve68* parse location data after the initial setDocumentLocator event.69*/70protected Locator _documentLocator;7172/**73* The currently parsed File. Used as part of the SourceLocation data stored74* for each relevant line of parsed XML.75*/76protected File _documentLocatorFile;7778/**79*80*/81public AbstractParser() {8283try {84// Create a parser factory and configure it to enable namespaces/validation.85SAXParserFactory saxFactory = SAXParserFactory.newInstance();86saxFactory.setNamespaceAware(true);87saxFactory.setValidating(true);8889// Create a new parser90_parser = saxFactory.newSAXParser();91_parser.setProperty(JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI);92// _parser.setProperty(JAXP_SCHEMA_SOURCE, getSchemaName());9394// Point all handlers at this object95XMLReader xmlReader = _parser.getXMLReader();96xmlReader.setContentHandler(this);97xmlReader.setErrorHandler(this);98xmlReader.setDTDHandler(this);99xmlReader.setEntityResolver(this);100101} catch (Exception e) {102// Promote the exception to a fatal error.103throw new Error(e);104}105}106107/**108* @see org.xml.sax.ContentHandler#characters(char[], int, int)109*/110@Override111public void characters(char[] ch, int start, int length) {112_parsedValue += new String(ch, start, length);113}114115/**116* @return The name of the schema expected by the parser.117*/118public abstract String getSchemaName();119120/**121* @see org.xml.sax.helpers.DefaultHandler#resolveEntity(java.lang.String, java.lang.String)122*/123@Override124public InputSource resolveEntity(String publicId, String systemId) throws SAXException {125File schemaFile = new File(systemId);126InputStream stream = getClass().getResourceAsStream("/" + schemaFile.getName()); //$NON-NLS-1$127128if (stream == null) {129_errors.add(new SAXException(MessageFormat.format(Messages.getString("AbstractParser.0"), new Object[] { schemaFile.getName() }))); //$NON-NLS-1$130} else {131_parser.setProperty(JAXP_SCHEMA_SOURCE, schemaFile.getName());132}133134return new InputSource(stream);135}136137@Override138public void setDocumentLocator(Locator locator) {139this._documentLocator = locator;140}141142public void setDocumentLocatorFile(File file) {143this._documentLocatorFile = file;144}145146/**147* @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)148*/149@Override150public void warning(SAXParseException exception) {151_warnings.add(exception);152}153154/**155* @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)156*/157@Override158public void error(SAXParseException exception) {159_errors.add(exception);160}161162/**163* @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)164*/165@Override166public void fatalError(SAXParseException exception) {167_errors.add(exception);168}169170/**171* Determines if there has been any SAX parse errors.172*173* @return <code>true</code> if there are SAX errors, <code>false</code> otherwise174*/175public boolean hasErrors() {176return _errors.size() != 0;177}178179/**180* Determines if there has been any SAX parse warnings.181*182* @return <code>true</code> if there are SAX warnings, <code>false</code> otherwise183*/184public boolean hasWarnings() {185return _warnings.size() != 0;186}187188/**189* Retrieves the SAX parse errors.190*191* @return the SAX parse errors.192*/193public Set<Throwable> getErrors() {194return _errors;195}196197/**198* Retrieves the SAX parse warnings.199*200* @return the SAX parse warnings.201*/202public Set<Throwable> getWarnings() {203return _warnings;204}205}206207208