FileDocCategorySizeDatePackage
SchemaContentHandler.javaAPI DocJava SE 6 API13670Tue Jun 10 00:22:46 BST 2008com.sun.org.apache.xerces.internal.impl.xs.traversers

SchemaContentHandler

public final class SchemaContentHandler extends Object implements ContentHandler

SchemaContentHandler converts SAX events into XNI and passes them directly to the SchemaDOMParser.

xerces.internal
author
Michael Glavassevich, IBM
author
Jack Z. Wang, IBM
version
$Id: SchemaContentHandler.java,v 1.1.4.1 2005/09/09 07:49:28 sunithareddy Exp $

Fields Summary
private SymbolTable
fSymbolTable
Symbol table
private SchemaDOMParser
fSchemaDOMParser
SchemaDOMParser, events will be delegated to SchemaDOMParser to pass
private final SAXLocatorWrapper
fSAXLocatorWrapper
XML Locator wrapper for SAX.
private NamespaceSupport
fNamespaceContext
The namespace context of this document: stores namespaces in scope
private boolean
fNeedPushNSContext
Indicate if push NamespaceContest is needed
private boolean
fNamespacePrefixes
Flag used to track whether namespace declarations are reported as attributes.
private boolean
fStringsInternalized
Flag used to track whether XML names and Namespace URIs have been internalized.
private final QName
fElementQName
Fields for start element, end element and characters.
private final QName
fAttributeQName
private final XMLAttributesImpl
fAttributes
private final XMLString
fTempString
Constructors Summary
public SchemaContentHandler()

Constructs an SchemaContentHandler.

    
            
      
Methods Summary
private voidaddNamespaceDeclarations(int prefixCount)

        String prefix = null;
        String localpart = null;
        String rawname = null;
        String nsPrefix = null;
        String nsURI = null;
        for (int i = 0; i < prefixCount; ++i) {
            nsPrefix = fNamespaceContext.getDeclaredPrefixAt(i);
            nsURI = fNamespaceContext.getURI(nsPrefix);
            if (nsPrefix.length() > 0) {
                prefix = XMLSymbols.PREFIX_XMLNS;
                localpart = nsPrefix;
                rawname = fSymbolTable.addSymbol(prefix + ":" + localpart);
            }
            else {
                prefix = XMLSymbols.EMPTY_STRING;
                localpart = XMLSymbols.PREFIX_XMLNS;
                rawname = XMLSymbols.PREFIX_XMLNS;
            }
            fAttributeQName.setValues(prefix, localpart, rawname, NamespaceContext.XMLNS_URI);
            fAttributes.addAttribute(fAttributeQName, XMLSymbols.fCDATASymbol, nsURI);
        }
    
public voidcharacters(char[] ch, int start, int length)

        try {
            fTempString.setValues(ch, start, length);
            fSchemaDOMParser.characters(fTempString, null);
        }
        catch (XMLParseException e) {
            convertToSAXParseException(e);
        }
        catch (XNIException e) {
            convertToSAXException(e);
        }
    
static voidconvertToSAXException(com.sun.org.apache.xerces.internal.xni.XNIException e)

        Exception ex = e.getException();
        if (ex == null) {
            throw new SAXException(e.getMessage());
        }
        if (ex instanceof SAXException) {
            throw (SAXException) ex;
        }
        throw new SAXException(ex);
    
static voidconvertToSAXParseException(com.sun.org.apache.xerces.internal.xni.parser.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;
        }
        throw new SAXException(ex);
    
public voidendDocument()

        fSAXLocatorWrapper.setLocator(null);
        try {
            fSchemaDOMParser.endDocument(null);
        }
        catch (XMLParseException e) {
            convertToSAXParseException(e);
        }
        catch (XNIException e) {
            convertToSAXException(e);
        }
    
public voidendElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)

        fillQName(fElementQName, uri, localName, qName);
        try {
            fSchemaDOMParser.endElement(fElementQName, null);
        }
        catch (XMLParseException e) {
            convertToSAXParseException(e);
        }
        catch (XNIException e) {
            convertToSAXException(e);
        }
        finally {
            fNamespaceContext.popContext();
        }
    
public voidendPrefixMapping(java.lang.String prefix)

        // do nothing
    
private voidfillQName(com.sun.org.apache.xerces.internal.xni.QName toFill, java.lang.String uri, java.lang.String localpart, java.lang.String rawname)

        if (!fStringsInternalized) {
            uri = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null;
            localpart = (localpart != null) ? fSymbolTable.addSymbol(localpart) : XMLSymbols.EMPTY_STRING;
            rawname = (rawname != null) ? fSymbolTable.addSymbol(rawname) : XMLSymbols.EMPTY_STRING;
        }
        else {
            if (uri != null && uri.length() == 0) {
                uri = null;
            }
            if (localpart == null) {
                localpart = XMLSymbols.EMPTY_STRING;
            }
            if (rawname == null) {
                rawname = XMLSymbols.EMPTY_STRING;
            }
        }
        String prefix = XMLSymbols.EMPTY_STRING;
        int prefixIdx = rawname.indexOf(':");
        if (prefixIdx != -1) {
            prefix = fSymbolTable.addSymbol(rawname.substring(0, prefixIdx));
            // local part may be an empty string if this is a namespace declaration
            if (localpart == XMLSymbols.EMPTY_STRING) {
                localpart = fSymbolTable.addSymbol(rawname.substring(prefixIdx + 1));
            }
        }
        // local part may be an empty string if this is a namespace declaration
        else if (localpart == XMLSymbols.EMPTY_STRING) {
            localpart = rawname;
        }
        toFill.setValues(prefix, localpart, rawname, uri);
    
private voidfillXMLAttributes(org.xml.sax.Attributes atts)

        fAttributes.removeAllAttributes();
        final int attrCount = atts.getLength();
        for (int i = 0; i < attrCount; ++i) {
            fillQName(fAttributeQName, atts.getURI(i), atts.getLocalName(i), atts.getQName(i));
            String type = atts.getType(i);
            fAttributes.addAttributeNS(fAttributeQName, (type != null) ? type : XMLSymbols.fCDATASymbol, atts.getValue(i));
            fAttributes.setSpecified(i, true);
        }
    
public org.w3c.dom.DocumentgetDocument()

        return fSchemaDOMParser.getDocument();
    
public voidignorableWhitespace(char[] ch, int start, int length)

        try {
            fTempString.setValues(ch, start, length);
            fSchemaDOMParser.ignorableWhitespace(fTempString, null);
        }
        catch (XMLParseException e) {
            convertToSAXParseException(e);
        }
        catch (XNIException e) {
            convertToSAXException(e);
        }
    
public voidprocessingInstruction(java.lang.String target, java.lang.String data)

        try {
            fTempString.setValues(data.toCharArray(), 0, data.length());
            fSchemaDOMParser.processingInstruction(target, fTempString, null);
        }
        catch (XMLParseException e) {
            convertToSAXParseException(e);
        }
        catch (XNIException e) {
            convertToSAXException(e);
        }
    
public voidreset(com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaDOMParser schemaDOMParser, com.sun.org.apache.xerces.internal.util.SymbolTable symbolTable, boolean namespacePrefixes, boolean stringsInternalized)

        fSchemaDOMParser = schemaDOMParser;
        fSymbolTable = symbolTable;
        fNamespacePrefixes = namespacePrefixes;
        fStringsInternalized = stringsInternalized;
    
public voidsetDocumentLocator(org.xml.sax.Locator locator)

        fSAXLocatorWrapper.setLocator(locator);
    
public voidskippedEntity(java.lang.String arg)

        // do-nothing
    
public voidstartDocument()

        fNeedPushNSContext = true;
        try {
            fSchemaDOMParser.startDocument(fSAXLocatorWrapper, null, fNamespaceContext, null);
        }
        catch (XMLParseException e) {
            convertToSAXParseException(e);
        }
        catch (XNIException e) {
            convertToSAXException(e);
        }
    
public voidstartElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)

        if (fNeedPushNSContext) {
            fNamespaceContext.pushContext();
        }
        fNeedPushNSContext = true;
        
        // Fill element QName and XMLAttributes
        fillQName(fElementQName, uri, localName, qName);
        fillXMLAttributes(atts);
        
        // Add namespace declarations if necessary
        if (!fNamespacePrefixes) {
            final int prefixCount = fNamespaceContext.getDeclaredPrefixCount();
            if (prefixCount > 0) {
                addNamespaceDeclarations(prefixCount);
            }
        }
        
        try {
            fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
        }
        catch (XMLParseException e) {
            convertToSAXParseException(e);
        }
        catch (XNIException e) {
            convertToSAXException(e);
        }
    
public voidstartPrefixMapping(java.lang.String prefix, java.lang.String uri)

        if (fNeedPushNSContext) {
            fNeedPushNSContext = false;
            fNamespaceContext.pushContext();
        }
        if (!fStringsInternalized) {
            prefix = (prefix != null) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING;
            uri = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null;
        }
        else {
            if (prefix == null) {
                prefix = XMLSymbols.EMPTY_STRING;
            }
            if (uri != null && uri.length() == 0) {
                uri = null;
            }
        }
        fNamespaceContext.declarePrefix(prefix, uri);