SchemaDOMParserpublic class SchemaDOMParser extends DefaultXMLDocumentHandler
Fields Summary |
---|
public static final String | ERROR_REPORTERProperty identifier: error reporter. | public static final String | GENERATE_SYNTHETIC_ANNOTATIONFeature identifier: generate synthetic annotations. | protected XMLLocator | fLocator | protected NamespaceContext | fNamespaceContext | SchemaDOM | schemaDOM | XMLParserConfiguration | config | private int | fAnnotationDepth | private int | fInnerAnnotationDepth | private int | fDepth | XMLErrorReporter | fErrorReporter | private boolean | fGenerateSyntheticAnnotation | private BooleanStack | fHasNonSchemaAttributes | private BooleanStack | fSawAnnotation | private XMLAttributes | fEmptyAttr |
Constructors Summary |
---|
public SchemaDOMParser(XMLParserConfiguration config)Default constructor.
//
// Constructors
//
this.config = config;
|
Methods Summary |
---|
public void | characters(com.sun.org.apache.xerces.internal.xni.XMLString text, com.sun.org.apache.xerces.internal.xni.Augmentations augs)Character content.
// 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 void | comment(com.sun.org.apache.xerces.internal.xni.XMLString text, com.sun.org.apache.xerces.internal.xni.Augmentations augs)A comment.
if(fAnnotationDepth > -1) {
schemaDOM.comment(text);
}
| public void | emptyElement(com.sun.org.apache.xerces.internal.xni.QName element, com.sun.org.apache.xerces.internal.xni.XMLAttributes attributes, com.sun.org.apache.xerces.internal.xni.Augmentations augs)An empty element.
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);
}
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.endAnnotationElement(element, true);
}
} else {
schemaDOM.endAnnotationElement(element, false);
}
| public void | endCDATA(com.sun.org.apache.xerces.internal.xni.Augmentations augs)The end of a CDATA section.
// only deal with CDATA boundaries within an annotation.
if (fAnnotationDepth != -1) {
schemaDOM.endAnnotationCDATA();
}
| public void | endDocument(com.sun.org.apache.xerces.internal.xni.Augmentations augs)The end of the document.
// To debug the DOM created uncomment the line below
// schemaDOM.printDOM();
| public void | endElement(com.sun.org.apache.xerces.internal.xni.QName element, com.sun.org.apache.xerces.internal.xni.Augmentations augs)The end of an element.
// 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, false);
schemaDOM.endElement();
} else if (fAnnotationDepth == fDepth) {
fAnnotationDepth = -1;
schemaDOM.endAnnotationElement(element, true);
schemaDOM.endElement();
} else { // inside a child of annotation
schemaDOM.endAnnotationElement(element, false);
}
} 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.Document | getDocument()Returns the DOM document object.
return schemaDOM;
| public org.w3c.dom.Document | getDocument2()Gets the document from SchemaParsingConfig
return ((SchemaParsingConfig)config).getDocument();
| public boolean | getFeature(java.lang.String featureId)Delegates to SchemaParsingConfig.getFeature
return config.getFeature(featureId);
| public java.lang.Object | getProperty(java.lang.String propertyId)Delegates to SchemaParsingConfig.getProperty.
return config.getProperty(propertyId);
| private boolean | hasNonSchemaAttributes(com.sun.org.apache.xerces.internal.xni.QName element, com.sun.org.apache.xerces.internal.xni.XMLAttributes attributes)
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 void | ignorableWhitespace(com.sun.org.apache.xerces.internal.xni.XMLString text, com.sun.org.apache.xerces.internal.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.
// unlikely to be called, but you never know...
if (fAnnotationDepth != -1 ) {
schemaDOM.characters(text);
}
| public void | parse(com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource inputSource)Delegates parsing to SchemaParsingConfig
config.parse(inputSource);
| public void | processingInstruction(java.lang.String target, com.sun.org.apache.xerces.internal.xni.XMLString data, com.sun.org.apache.xerces.internal.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.
if(fAnnotationDepth > -1) {
schemaDOM.processingInstruction(target, data.toString());
}
| public void | reset()Reset SchemaParsingConfig
((SchemaParsingConfig)config).reset();
| public void | resetNodePool()ResetNodePool on SchemaParsingConfig
((SchemaParsingConfig)config).resetNodePool();
| public void | setEntityResolver(com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver er)Delegates to SchemaParsingConfig.setEntityResolver.
config.setEntityResolver(er);
| public void | setFeature(java.lang.String featureId, boolean state)Delegates to SchemaParsingConfig.setFeature
config.setFeature(featureId, state);
| public void | setProperty(java.lang.String propertyId, java.lang.Object value)Delegates to SchemaParsingConfig.setProperty.
config.setProperty(propertyId, value);
| public void | startCDATA(com.sun.org.apache.xerces.internal.xni.Augmentations augs)The start of a CDATA section.
// only deal with CDATA boundaries within an annotation.
if (fAnnotationDepth != -1) {
schemaDOM.startAnnotationCDATA();
}
| public void | startDocument(com.sun.org.apache.xerces.internal.xni.XMLLocator locator, java.lang.String encoding, com.sun.org.apache.xerces.internal.xni.NamespaceContext namespaceContext, com.sun.org.apache.xerces.internal.xni.Augmentations augs)
//
// XMLDocumentHandler methods
//
fErrorReporter = (XMLErrorReporter)config.getProperty(ERROR_REPORTER);
fGenerateSyntheticAnnotation = config.getFeature(GENERATE_SYNTHETIC_ANNOTATION);
fHasNonSchemaAttributes.clear();
fSawAnnotation.clear();
schemaDOM = new SchemaDOM();
fAnnotationDepth = -1;
fInnerAnnotationDepth = -1;
fDepth = -1;
fLocator = locator;
fNamespaceContext = namespaceContext;
schemaDOM.setDocumentURI(locator.getExpandedSystemId());
| public void | startElement(com.sun.org.apache.xerces.internal.xni.QName element, com.sun.org.apache.xerces.internal.xni.XMLAttributes attributes, com.sun.org.apache.xerces.internal.xni.Augmentations augs)The start of an element.
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);
}
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());
|
|