1 57 58 package com.sun.org.apache.xerces.internal.impl.xs.opti; 59 60 61 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 62 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols; 63 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter; 64 import com.sun.org.apache.xerces.internal.util.XMLChar; 65 import com.sun.org.apache.xerces.internal.xni.Augmentations; 66 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 67 import com.sun.org.apache.xerces.internal.xni.QName; 68 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 69 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 70 import com.sun.org.apache.xerces.internal.xni.XMLString; 71 import com.sun.org.apache.xerces.internal.xni.XNIException; 72 import org.w3c.dom.Document ; 73 74 88 public class SchemaDOMParser extends DefaultXMLDocumentHandler { 89 90 94 protected XMLLocator fLocator; 96 97 protected NamespaceContext fNamespaceContext = null; 100 101 SchemaDOM schemaDOM; 102 103 104 108 public SchemaDOMParser(XMLErrorReporter errorReporter) { 109 fErrorReporter = errorReporter; 110 } 111 112 private int fAnnotationDepth = -1; 115 private int fInnerAnnotationDepth = -1; 118 private int fDepth = -1; 120 XMLErrorReporter fErrorReporter; 122 123 124 128 public void startDocument(XMLLocator locator, String encoding, 129 NamespaceContext namespaceContext, Augmentations augs) 130 throws XNIException { 131 schemaDOM = new SchemaDOM(); 132 fAnnotationDepth = -1; 133 fInnerAnnotationDepth = -1; 134 fDepth = -1; 135 fLocator = locator; 136 fNamespaceContext = namespaceContext; 137 } 139 145 public void endDocument(Augmentations augs) throws XNIException { 146 } 150 151 160 public void comment(XMLString text, Augmentations augs) throws XNIException { 161 if(fAnnotationDepth > -1) { 162 schemaDOM.comment(text); 163 } 164 } 165 166 184 public void processingInstruction(String target, XMLString data, Augmentations augs) 185 throws XNIException { 186 if(fAnnotationDepth > -1) { 187 schemaDOM.processingInstruction(target, data.toString()); 188 } 189 } 190 191 200 public void characters(XMLString text, Augmentations augs) throws XNIException { 201 if (fInnerAnnotationDepth == -1 ) { 203 for (int i=text.offset; i<text.offset+text.length; i++) { 204 if (!XMLChar.isSpace(text.ch[i])) { 206 String txt = new String (text.ch, i, text.length+text.offset-i); 208 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 210 "s4s-elt-character", 211 new Object []{txt}, 212 XMLErrorReporter.SEVERITY_ERROR); 213 break; 214 } 215 } 216 } 220 else { 223 schemaDOM.characters(text); 224 } 225 226 } 227 228 229 239 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) 240 throws XNIException { 241 242 fDepth++; 243 if (fAnnotationDepth == -1) { 249 if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA && 250 element.localpart == SchemaSymbols.ELT_ANNOTATION) { 251 fAnnotationDepth = fDepth; 252 schemaDOM.startAnnotation(element, attributes, fNamespaceContext); 253 } 254 } else if(fDepth == fAnnotationDepth+1) { 255 fInnerAnnotationDepth = fDepth; 256 schemaDOM.startAnnotationElement(element, attributes); 257 } else { 258 schemaDOM.startAnnotationElement(element, attributes); 259 return; 261 } 262 schemaDOM.startElement(element, attributes, 263 fLocator.getLineNumber(), 264 fLocator.getColumnNumber()); 265 266 } 267 268 269 279 public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) 280 throws XNIException { 281 if (fAnnotationDepth == -1) { 293 if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA && 295 element.localpart == SchemaSymbols.ELT_ANNOTATION) { 296 schemaDOM.startAnnotation(element, attributes, fNamespaceContext); 297 } 298 } else { 299 schemaDOM.startAnnotationElement(element, attributes); 300 } 301 302 schemaDOM.emptyElement(element, attributes, 303 fLocator.getLineNumber(), 304 fLocator.getColumnNumber()); 305 306 if (fAnnotationDepth == -1) { 307 if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA && 309 element.localpart == SchemaSymbols.ELT_ANNOTATION) { 310 schemaDOM.endAnnotationElement(element, true); 311 } 312 } else { 313 schemaDOM.endAnnotationElement(element, false); 314 } 315 } 316 317 318 327 public void endElement(QName element, Augmentations augs) throws XNIException { 328 329 if(fAnnotationDepth > -1) { 332 if (fInnerAnnotationDepth == fDepth) { 333 fInnerAnnotationDepth = -1; 334 schemaDOM.endAnnotationElement(element, false); 335 schemaDOM.endElement(); 336 } else if (fAnnotationDepth == fDepth) { 337 fAnnotationDepth = -1; 338 schemaDOM.endAnnotationElement(element, true); 339 schemaDOM.endElement(); 340 } else { schemaDOM.endAnnotationElement(element, false); 342 } 343 } else { schemaDOM.endElement(); 345 } 346 fDepth--; 347 348 } 349 350 364 public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { 365 if (fAnnotationDepth != -1 ) { 367 schemaDOM.characters(text); 368 } 369 } 370 371 379 public void startCDATA(Augmentations augs) throws XNIException { 380 if (fAnnotationDepth != -1) { 382 schemaDOM.startAnnotationCDATA(); 383 } 384 } 385 386 394 public void endCDATA(Augmentations augs) throws XNIException { 395 if (fAnnotationDepth != -1) { 397 schemaDOM.endAnnotationCDATA(); 398 } 399 } 400 401 402 406 409 public Document getDocument() { 410 return schemaDOM; 411 } 412 413 } 414 | Popular Tags |