FileDocCategorySizeDatePackage
SchemaDOMParser.javaAPI DocApache Xerces 3.0.123365Fri Sep 14 20:33:54 BST 2007org.apache.xerces.impl.xs.opti

SchemaDOMParser

public class SchemaDOMParser extends DefaultXMLDocumentHandler
xerces.internal
author
Rahul Srivastava, Sun Microsystems Inc.
author
Sandy Gao, IBM
version
$Id: SchemaDOMParser.java 446728 2006-09-15 20:43:46Z mrglavas $

Fields Summary
public static final String
ERROR_REPORTER
Property identifier: error reporter.
public static final String
GENERATE_SYNTHETIC_ANNOTATION
Feature identifier: generate synthetic annotations.
protected org.apache.xerces.xni.XMLLocator
fLocator
protected org.apache.xerces.xni.NamespaceContext
fNamespaceContext
SchemaDOM
schemaDOM
org.apache.xerces.xni.parser.XMLParserConfiguration
config
private ElementImpl
fCurrentAnnotationElement
private int
fAnnotationDepth
private int
fInnerAnnotationDepth
private int
fDepth
org.apache.xerces.impl.XMLErrorReporter
fErrorReporter
private boolean
fGenerateSyntheticAnnotation
private BooleanStack
fHasNonSchemaAttributes
private BooleanStack
fSawAnnotation
private org.apache.xerces.xni.XMLAttributes
fEmptyAttr
Constructors Summary
public SchemaDOMParser(org.apache.xerces.xni.parser.XMLParserConfiguration config)
Default constructor.

    
    //
    // Constructors
    //
    
       
       
        this.config = config;
    
Methods Summary
public voidcharacters(org.apache.xerces.xni.XMLString text, org.apache.xerces.xni.Augmentations augs)
Character content.

param
text The content.
param
augs Additional information that may include infoset augmentations
exception
XNIException Thrown by handler to signal an error.

        // when it's not within xs:appinfo or xs:documentation
        if (fInnerAnnotationDepth == -1 ) {
            for (int i=text.offset; i<text.offset+text.length; i++) {
                // and there is a non-whitespace character
                if (!XMLChar.isSpace(text.ch[i])) {
                    // the string we saw: starting from the first non-whitespace character.
                    String txt = new String(text.ch, i, text.length+text.offset-i);
                    // report an error
                    fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
                            "s4s-elt-character",
                            new Object[]{txt},
                            XMLErrorReporter.SEVERITY_ERROR);
                    break;
                }
            }
            // don't call super.characters() when it's not within one of the 2
            // annotation elements: the traversers ignore them anyway. We can
            // save time/memory creating the text nodes.
        }
        // when it's within either of the 2 elements, characters are allowed
        // and we need to store them.
        else {
            schemaDOM.characters(text);
        }
        
    
public voidcomment(org.apache.xerces.xni.XMLString text, org.apache.xerces.xni.Augmentations augs)
A comment.

param
text The text in the comment.
param
augs Additional information that may include infoset augmentations
exception
XNIException Thrown by application to signal an error.

        if(fAnnotationDepth > -1) {
            schemaDOM.comment(text);
        }
    
public voidemptyElement(org.apache.xerces.xni.QName element, org.apache.xerces.xni.XMLAttributes attributes, org.apache.xerces.xni.Augmentations augs)
An empty element.

param
element The name of the element.
param
attributes The element attributes.
param
augs Additional information that may include infoset augmentations
exception
XNIException Thrown by handler to signal an error.

        
        if (fGenerateSyntheticAnnotation && fAnnotationDepth == -1 && 
                element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA && element.localpart != SchemaSymbols.ELT_ANNOTATION && hasNonSchemaAttributes(element, attributes)) { 
            
            schemaDOM.startElement(element, attributes,
                    fLocator.getLineNumber(),
                    fLocator.getColumnNumber(),
                    fLocator.getCharacterOffset());
            
            attributes.removeAllAttributes();
            String schemaPrefix = fNamespaceContext.getPrefix(SchemaSymbols.URI_SCHEMAFORSCHEMA);
            QName annQName = new QName(schemaPrefix, SchemaSymbols.ELT_ANNOTATION, schemaPrefix + (schemaPrefix.length() == 0?"":":") + SchemaSymbols.ELT_ANNOTATION, SchemaSymbols.URI_SCHEMAFORSCHEMA);
            schemaDOM.startAnnotation(annQName, attributes, fNamespaceContext);
            QName elemQName = new QName(schemaPrefix, SchemaSymbols.ELT_DOCUMENTATION, schemaPrefix + (schemaPrefix.length() == 0?"":":") + SchemaSymbols.ELT_DOCUMENTATION, SchemaSymbols.URI_SCHEMAFORSCHEMA);
            schemaDOM.startAnnotationElement(elemQName, attributes);
            schemaDOM.characters(new XMLString("SYNTHETIC_ANNOTATION".toCharArray(), 0, 20 ));     
            schemaDOM.endSyntheticAnnotationElement(elemQName, false);
            schemaDOM.endSyntheticAnnotationElement(annQName, true);
            
            schemaDOM.endElement();
            
            return;
        }
        // the order of events that occurs here is:
        //   schemaDOM.startAnnotation/startAnnotationElement (if applicable)
        //   schemaDOM.emptyElement  (basically the same as startElement then endElement)
        //   schemaDOM.endAnnotationElement (if applicable)
        // the order of events that would occur if this was <element></element>:
        //   schemaDOM.startAnnotation/startAnnotationElement (if applicable)
        //   schemaDOM.startElement
        //   schemaDOM.endAnnotationElement (if applicable)
        //   schemaDOM.endElementElement
        // Thus, we can see that the order of events isn't the same.  However, it doesn't
        // seem to matter.  -- PJM
        if (fAnnotationDepth == -1) {
            // this is messed up, but a case to consider:
            if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA &&
                    element.localpart == SchemaSymbols.ELT_ANNOTATION) {
                schemaDOM.startAnnotation(element, attributes, fNamespaceContext);
            }
        } 
        else {
            schemaDOM.startAnnotationElement(element, attributes);
        }
        
        ElementImpl newElem = schemaDOM.emptyElement(element, attributes, 
                fLocator.getLineNumber(),
                fLocator.getColumnNumber(),
                fLocator.getCharacterOffset());
        
        if (fAnnotationDepth == -1) {
            // this is messed up, but a case to consider:
            if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA &&
                    element.localpart == SchemaSymbols.ELT_ANNOTATION) {
                schemaDOM.endAnnotation(element, newElem);
            }
        } 
        else {
            schemaDOM.endAnnotationElement(element);
        } 
    
public voidendCDATA(org.apache.xerces.xni.Augmentations augs)
The end of a CDATA section.

param
augs Additional information that may include infoset augmentations
exception
XNIException Thrown by handler to signal an error.

        // only deal with CDATA boundaries within an annotation.
        if (fAnnotationDepth != -1) {
            schemaDOM.endAnnotationCDATA();
        }
    
public voidendDocument(org.apache.xerces.xni.Augmentations augs)
The end of the document.

param
augs Additional information that may include infoset augmentations
throws
XNIException Thrown by handler to signal an error.

        // To debug the DOM created uncomment the line below
        // schemaDOM.printDOM();
    
public voidendElement(org.apache.xerces.xni.QName element, org.apache.xerces.xni.Augmentations augs)
The end of an element.

param
element The name of the element.
param
augs Additional information that may include infoset augmentations
exception
XNIException Thrown by handler to signal an error.

        
        // when we reach the endElement of xs:appinfo or xs:documentation,
        // change fInnerAnnotationDepth to -1
        if(fAnnotationDepth > -1) {
            if (fInnerAnnotationDepth == fDepth) {
                fInnerAnnotationDepth = -1;
                schemaDOM.endAnnotationElement(element);
                schemaDOM.endElement();
            } else if (fAnnotationDepth == fDepth) {
                fAnnotationDepth = -1;
                schemaDOM.endAnnotation(element, fCurrentAnnotationElement);
                schemaDOM.endElement();
            } else { // inside a child of annotation
                schemaDOM.endAnnotationElement(element);
            }
        } else { // not in an annotation at all
            if(element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA && fGenerateSyntheticAnnotation) {
                boolean value = fHasNonSchemaAttributes.pop();
                boolean sawann = fSawAnnotation.pop();
                if (value && !sawann) {
                    String schemaPrefix = fNamespaceContext.getPrefix(SchemaSymbols.URI_SCHEMAFORSCHEMA);
                    QName annQName = new QName(schemaPrefix, SchemaSymbols.ELT_ANNOTATION, schemaPrefix + (schemaPrefix.length() == 0?"":":") + SchemaSymbols.ELT_ANNOTATION, SchemaSymbols.URI_SCHEMAFORSCHEMA);
                    schemaDOM.startAnnotation(annQName, fEmptyAttr, fNamespaceContext);
                    QName elemQName = new QName(schemaPrefix, SchemaSymbols.ELT_DOCUMENTATION, schemaPrefix + (schemaPrefix.length() == 0?"":":") + SchemaSymbols.ELT_DOCUMENTATION, SchemaSymbols.URI_SCHEMAFORSCHEMA);
                    schemaDOM.startAnnotationElement(elemQName, fEmptyAttr);
                    schemaDOM.characters(new XMLString("SYNTHETIC_ANNOTATION".toCharArray(), 0, 20 ));     
                    schemaDOM.endSyntheticAnnotationElement(elemQName, false);
                    schemaDOM.endSyntheticAnnotationElement(annQName, true);
                }
            }
            schemaDOM.endElement();
        }
        fDepth--;
        
    
public org.w3c.dom.DocumentgetDocument()
Returns the DOM document object.

        return schemaDOM;
    
public org.w3c.dom.DocumentgetDocument2()
Gets the document from SchemaParsingConfig

return
Document

    	return ((SchemaParsingConfig)config).getDocument();
    
public booleangetFeature(java.lang.String featureId)
Delegates to SchemaParsingConfig.getFeature

param
featureId
return
boolean

        return config.getFeature(featureId);
    
public java.lang.ObjectgetProperty(java.lang.String propertyId)
Delegates to SchemaParsingConfig.getProperty.

param
propertyId
return
Object

        return config.getProperty(propertyId);
    
private booleanhasNonSchemaAttributes(org.apache.xerces.xni.QName element, org.apache.xerces.xni.XMLAttributes attributes)

param
attributes
return

        final int length = attributes.getLength();
        for (int i = 0; i < length; ++i) {
            String uri = attributes.getURI(i);
            if (uri != null && uri != SchemaSymbols.URI_SCHEMAFORSCHEMA && 
                    uri != NamespaceContext.XMLNS_URI &&
                    !(uri == NamespaceContext.XML_URI && 
                            attributes.getQName(i) == SchemaSymbols.ATT_XML_LANG && element.localpart == SchemaSymbols.ELT_SCHEMA)) {
                return true;
            }
        }
        return false;
    
public voidignorableWhitespace(org.apache.xerces.xni.XMLString text, org.apache.xerces.xni.Augmentations augs)
Ignorable whitespace. For this method to be called, the document source must have some way of determining that the text containing only whitespace characters should be considered ignorable. For example, the validator can determine if a length of whitespace characters in the document are ignorable based on the element content model.

param
text The ignorable whitespace.
param
augs Additional information that may include infoset augmentations
exception
XNIException Thrown by handler to signal an error.

        // unlikely to be called, but you never know...
        if (fAnnotationDepth != -1 ) {
            schemaDOM.characters(text);
        }
    
public voidparse(org.apache.xerces.xni.parser.XMLInputSource inputSource)
Delegates parsing to SchemaParsingConfig

param
inputSource
throws
IOException

        config.parse(inputSource);
    
public voidprocessingInstruction(java.lang.String target, org.apache.xerces.xni.XMLString data, org.apache.xerces.xni.Augmentations augs)
A processing instruction. Processing instructions consist of a target name and, optionally, text data. The data is only meaningful to the application.

Typically, a processing instruction's data will contain a series of pseudo-attributes. These pseudo-attributes follow the form of element attributes but are not parsed or presented to the application as anything other than text. The application is responsible for parsing the data.

param
target The target.
param
data The data or null if none specified.
param
augs Additional information that may include infoset augmentations
exception
XNIException Thrown by handler to signal an error.

        if (fAnnotationDepth > -1) {
            schemaDOM.processingInstruction(target, data);
        }
    
public voidreset()
Reset SchemaParsingConfig

    	((SchemaParsingConfig)config).reset();
    
public voidresetNodePool()
ResetNodePool on SchemaParsingConfig

    	((SchemaParsingConfig)config).resetNodePool();
    
public voidsetEntityResolver(org.apache.xerces.xni.parser.XMLEntityResolver er)
Delegates to SchemaParsingConfig.setEntityResolver.

param
er XMLEntityResolver

    	config.setEntityResolver(er);
    
public voidsetFeature(java.lang.String featureId, boolean state)
Delegates to SchemaParsingConfig.setFeature

param
featureId
param
state

    	config.setFeature(featureId, state);
    
public voidsetProperty(java.lang.String propertyId, java.lang.Object value)
Delegates to SchemaParsingConfig.setProperty.

param
propertyId
param
value

        config.setProperty(propertyId, value);
    
public voidstartCDATA(org.apache.xerces.xni.Augmentations augs)
The start of a CDATA section.

param
augs Additional information that may include infoset augmentations
exception
XNIException Thrown by handler to signal an error.

        // only deal with CDATA boundaries within an annotation.
        if (fAnnotationDepth != -1) {
            schemaDOM.startAnnotationCDATA();
        }
    
public voidstartDocument(org.apache.xerces.xni.XMLLocator locator, java.lang.String encoding, org.apache.xerces.xni.NamespaceContext namespaceContext, org.apache.xerces.xni.Augmentations augs)

    
    //
    // XMLDocumentHandler methods
    //
    
          
               
      
        fErrorReporter = (XMLErrorReporter)config.getProperty(ERROR_REPORTER);
        fGenerateSyntheticAnnotation = config.getFeature(GENERATE_SYNTHETIC_ANNOTATION);
        fHasNonSchemaAttributes.clear();
        fSawAnnotation.clear();
        schemaDOM = new SchemaDOM();
        fCurrentAnnotationElement = null;
        fAnnotationDepth = -1;
        fInnerAnnotationDepth = -1;
        fDepth = -1;
        fLocator = locator;
        fNamespaceContext = namespaceContext;
        schemaDOM.setDocumentURI(locator.getExpandedSystemId());
    
public voidstartElement(org.apache.xerces.xni.QName element, org.apache.xerces.xni.XMLAttributes attributes, org.apache.xerces.xni.Augmentations augs)
The start of an element.

param
element The name of the element.
param
attributes The element attributes.
param
augs Additional information that may include infoset augmentations
exception
XNIException Thrown by handler to signal an error.

        
        fDepth++;
        // while it is true that non-whitespace character data
        // may only occur in appInfo or documentation
        // elements, it's certainly legal for comments and PI's to
        // occur as children of annotation; we need
        // to account for these here.
        if (fAnnotationDepth == -1) {
            if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA &&
                    element.localpart == SchemaSymbols.ELT_ANNOTATION) {
                if (fGenerateSyntheticAnnotation) {
                    if (fSawAnnotation.size() > 0) {
                        fSawAnnotation.pop();
                    }
                    fSawAnnotation.push(true);
                }
                fAnnotationDepth = fDepth;
                schemaDOM.startAnnotation(element, attributes, fNamespaceContext);
                fCurrentAnnotationElement = schemaDOM.startElement(element, attributes, 
                        fLocator.getLineNumber(),
                        fLocator.getColumnNumber(),
                        fLocator.getCharacterOffset());
                return;
            } 
            else if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA && fGenerateSyntheticAnnotation) {
                fSawAnnotation.push(false);
                fHasNonSchemaAttributes.push(hasNonSchemaAttributes(element, attributes));
            }
        } 
        else if (fDepth == fAnnotationDepth + 1) {
            fInnerAnnotationDepth = fDepth;
            schemaDOM.startAnnotationElement(element, attributes);
        } 
        else {
            schemaDOM.startAnnotationElement(element, attributes);
            // avoid falling through; don't call startElement in this case
            return;
        }
        schemaDOM.startElement(element, attributes, 
                fLocator.getLineNumber(),
                fLocator.getColumnNumber(),
                fLocator.getCharacterOffset());