FileDocCategorySizeDatePackage
DOMParser.javaAPI DocJava SE 5 API19983Fri Aug 26 14:55:54 BST 2005com.sun.org.apache.xerces.internal.parsers

DOMParser

public class DOMParser extends AbstractDOMParser
This is the main Xerces DOM parser class. It uses the abstract DOM parser with a document scanner, a dtd scanner, and a validator, as well as a grammar pool.
author
Arnaud Le Hors, IBM
author
Andy Clark, IBM
version
$Id: DOMParser.java,v 1.69 2004/02/17 07:14:49 neeraj Exp $

Fields Summary
protected static final String
SYMBOL_TABLE
Property identifier: symbol table.
protected static final String
XMLGRAMMAR_POOL
Property identifier: XML grammar pool.
private static final String[]
RECOGNIZED_PROPERTIES
Recognized properties.
Constructors Summary
public DOMParser(XMLParserConfiguration config)
Constructs a DOM parser using the specified parser configuration.

    
    //
    // Constructors
    //
    
                  
       
        super(config);
    
public DOMParser()
Constructs a DOM parser using the dtd/xml schema parser configuration.

        this(null, null);
    
public DOMParser(SymbolTable symbolTable)
Constructs a DOM parser using the specified symbol table.

        this(symbolTable, null);
    
public DOMParser(SymbolTable symbolTable, XMLGrammarPool grammarPool)
Constructs a DOM parser using the specified symbol table and grammar pool.

        super((XMLParserConfiguration)ObjectFactory.createObject(
        "com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration",
        "com.sun.org.apache.xerces.internal.parsers.XIncludeParserConfiguration"
        ));
        
        // set properties
        fConfiguration.addRecognizedProperties(RECOGNIZED_PROPERTIES);
        if (symbolTable != null) {
            fConfiguration.setProperty(SYMBOL_TABLE, symbolTable);
        }
        if (grammarPool != null) {
            fConfiguration.setProperty(XMLGRAMMAR_POOL, grammarPool);
        }
        
    
Methods Summary
public org.xml.sax.EntityResolvergetEntityResolver()
Return the current entity resolver.

return
The current entity resolver, or null if none has been registered.
see
#setEntityResolver

        
        EntityResolver entityResolver = null;
        try {
            XMLEntityResolver xmlEntityResolver =
            (XMLEntityResolver)fConfiguration.getProperty(ENTITY_RESOLVER);
            if (xmlEntityResolver != null){
                if(xmlEntityResolver instanceof EntityResolverWrapper) {
                    entityResolver = ((EntityResolverWrapper)xmlEntityResolver).getEntityResolver();
                }else if(xmlEntityResolver instanceof EntityResolver2Wrapper){
                    entityResolver = ((EntityResolver2Wrapper)xmlEntityResolver).getEntityResolver();
                }
            }
        }catch (XMLConfigurationException e) {
            // do nothing
        }
        return entityResolver;
        
    
public org.xml.sax.ErrorHandlergetErrorHandler()
Return the current error handler.

return
The current error handler, or null if none has been registered.
see
#setErrorHandler

        
        ErrorHandler errorHandler = null;
        try {
            XMLErrorHandler xmlErrorHandler =
            (XMLErrorHandler)fConfiguration.getProperty(ERROR_HANDLER);
            if (xmlErrorHandler != null &&
            xmlErrorHandler instanceof ErrorHandlerWrapper) {
                errorHandler = ((ErrorHandlerWrapper)xmlErrorHandler).getErrorHandler();
            }
        }
        catch (XMLConfigurationException e) {
            // do nothing
        }
        return errorHandler;
        
    
public booleangetFeature(java.lang.String featureId)
Query the state of a feature. Query the current state of any feature in a SAX2 parser. The parser might not recognize the feature.

param
featureId The unique identifier (URI) of the feature being set.
return
The current state of the feature.
exception
org.xml.sax.SAXNotRecognizedException If the requested feature is not known.
exception
SAXNotSupportedException If the requested feature is known but not supported.

        
        try {
            return fConfiguration.getFeature(featureId);
        }
        catch (XMLConfigurationException e) {
            String message = e.getMessage();
            if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
                throw new SAXNotRecognizedException(message);
            }
            else {
                throw new SAXNotSupportedException(message);
            }
        }
        
    
public java.lang.ObjectgetProperty(java.lang.String propertyId)
Query the value of a property. Return the current value of a property in a SAX2 parser. The parser might not recognize the property.

param
propertyId The unique identifier (URI) of the property being set.
return
The current value of the property.
exception
org.xml.sax.SAXNotRecognizedException If the requested property is not known.
exception
SAXNotSupportedException If the requested property is known but not supported.

        
        if (propertyId.equals(CURRENT_ELEMENT_NODE)) {
            boolean deferred = false;
            try {
                deferred = getFeature(DEFER_NODE_EXPANSION);
            }
            catch (XMLConfigurationException e){
                // ignore
            }
            if (deferred) {
                throw new SAXNotSupportedException("Current element node cannot be queried when node expansion is deferred.");
            }
            return (fCurrentNode!=null &&
            fCurrentNode.getNodeType() == Node.ELEMENT_NODE)? fCurrentNode:null;
        }
        
        try {
            return fConfiguration.getProperty(propertyId);
        }
        catch (XMLConfigurationException e) {
            String message = e.getMessage();
            if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
                throw new SAXNotRecognizedException(message);
            }
            else {
                throw new SAXNotSupportedException(message);
            }
        }
        
    
public voidparse(java.lang.String systemId)
Parses the input source specified by the given system identifier.

This method is equivalent to the following:

parse(new InputSource(systemId));

param
source The input source.
exception
org.xml.sax.SAXException Throws exception on SAX error.
exception
java.io.IOException Throws exception on i/o error.

        
        // parse document
        XMLInputSource source = new XMLInputSource(null, systemId, null);
        try {
            parse(source);
        }
        
        // wrap XNI exceptions as SAX exceptions
        catch (XMLParseException e) {
            Exception ex = e.getException();
            if (ex == null) {
                // must be a parser exception; mine it for locator info and throw
                // a SAXParseException
                LocatorImpl locatorImpl = new LocatorImpl();
                locatorImpl.setPublicId(e.getPublicId());
                locatorImpl.setSystemId(e.getExpandedSystemId());
                locatorImpl.setLineNumber(e.getLineNumber());
                locatorImpl.setColumnNumber(e.getColumnNumber());
                throw new SAXParseException(e.getMessage(), locatorImpl);
            }
            if (ex instanceof SAXException) {
                // why did we create an XMLParseException?
                throw (SAXException)ex;
            }
            if (ex instanceof IOException) {
                throw (IOException)ex;
            }
            throw new SAXException(ex);
        }
        catch (XNIException e) {
            e.printStackTrace();
            Exception ex = e.getException();
            if (ex == null) {
                throw new SAXException(e.getMessage());
            }
            if (ex instanceof SAXException) {
                throw (SAXException)ex;
            }
            if (ex instanceof IOException) {
                throw (IOException)ex;
            }
            throw new SAXException(ex);
        }
        
    
public voidparse(org.xml.sax.InputSource inputSource)
parse

param
inputSource
exception
org.xml.sax.SAXException
exception
java.io.IOException

        
        // parse document
        try {
            XMLInputSource xmlInputSource =
            new XMLInputSource(inputSource.getPublicId(),
            inputSource.getSystemId(),
            null);
            xmlInputSource.setByteStream(inputSource.getByteStream());
            xmlInputSource.setCharacterStream(inputSource.getCharacterStream());
            xmlInputSource.setEncoding(inputSource.getEncoding());
            parse(xmlInputSource);
        }
        
        // wrap XNI exceptions as SAX exceptions
        catch (XMLParseException e) {
            Exception ex = e.getException();
            if (ex == null) {
                // must be a parser exception; mine it for locator info and throw
                // a SAXParseException
                LocatorImpl locatorImpl = new LocatorImpl();
                locatorImpl.setPublicId(e.getPublicId());
                locatorImpl.setSystemId(e.getExpandedSystemId());
                locatorImpl.setLineNumber(e.getLineNumber());
                locatorImpl.setColumnNumber(e.getColumnNumber());
                throw new SAXParseException(e.getMessage(), locatorImpl);
            }
            if (ex instanceof SAXException) {
                // why did we create an XMLParseException?
                throw (SAXException)ex;
            }
            if (ex instanceof IOException) {
                throw (IOException)ex;
            }
            throw new SAXException(ex);
        }
        catch (XNIException e) {
            Exception ex = e.getException();
            if (ex == null) {
                throw new SAXException(e.getMessage());
            }
            if (ex instanceof SAXException) {
                throw (SAXException)ex;
            }
            if (ex instanceof IOException) {
                throw (IOException)ex;
            }
            throw new SAXException(ex);
        }
        
    
public voidsetEntityResolver(org.xml.sax.EntityResolver resolver)
Sets the resolver used to resolve external entities. The EntityResolver interface supports resolution of public and system identifiers.

param
resolver The new entity resolver. Passing a null value will uninstall the currently installed resolver.

        
        try {
            if(resolver instanceof EntityResolver2){
                fConfiguration.setProperty(ENTITY_RESOLVER, new EntityResolver2Wrapper((EntityResolver2)resolver));
            }else{
                fConfiguration.setProperty(ENTITY_RESOLVER, new EntityResolverWrapper(resolver));
            }
        }
        catch (XMLConfigurationException e) {
            // do nothing
        }
        
    
public voidsetErrorHandler(org.xml.sax.ErrorHandler errorHandler)
Allow an application to register an error event handler.

If the application does not register an error handler, all error events reported by the SAX parser will be silently ignored; however, normal processing may not continue. It is highly recommended that all SAX applications implement an error handler to avoid unexpected bugs.

Applications may register a new or different handler in the middle of a parse, and the SAX parser must begin using the new handler immediately.

param
errorHandler The error handler.
exception
java.lang.NullPointerException If the handler argument is null.
see
#getErrorHandler

        
        try {
            fConfiguration.setProperty(ERROR_HANDLER,
            new ErrorHandlerWrapper(errorHandler));
        }
        catch (XMLConfigurationException e) {
            // do nothing
        }
        
    
public voidsetFeature(java.lang.String featureId, boolean state)
Set the state of any feature in a SAX2 parser. The parser might not recognize the feature, and if it does recognize it, it might not be able to fulfill the request.

param
featureId The unique identifier (URI) of the feature.
param
state The requested state of the feature (true or false).
exception
SAXNotRecognizedException If the requested feature is not known.
exception
SAXNotSupportedException If the requested feature is known, but the requested state is not supported.

        
        try {
            fConfiguration.setFeature(featureId, state);
        }
        catch (XMLConfigurationException e) {
            String message = e.getMessage();
            if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
                throw new SAXNotRecognizedException(message);
            }
            else {
                throw new SAXNotSupportedException(message);
            }
        }
        
    
public voidsetProperty(java.lang.String propertyId, java.lang.Object value)
Set the value of any property in a SAX2 parser. The parser might not recognize the property, and if it does recognize it, it might not support the requested value.

param
propertyId The unique identifier (URI) of the property being set.
param
Object The value to which the property is being set.
exception
SAXNotRecognizedException If the requested property is not known.
exception
SAXNotSupportedException If the requested property is known, but the requested value is not supported.

        
        try {
            fConfiguration.setProperty(propertyId, value);
        }
        catch (XMLConfigurationException e) {
            String message = e.getMessage();
            if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
                throw new SAXNotRecognizedException(message);
            }
            else {
                throw new SAXNotSupportedException(message);
            }
        }