1 16 package org.apache.xerces.impl.xs; 17 18 import org.apache.xerces.xs.XSAnnotation; 19 import org.apache.xerces.xs.XSConstants; 20 import org.apache.xerces.xs.XSNamespaceItem; 21 import org.apache.xerces.parsers.SAXParser; 22 import org.apache.xerces.parsers.DOMParser; 23 24 import org.xml.sax.ContentHandler ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.InputSource ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Document ; 30 import java.io.StringReader ; 31 import java.io.IOException ; 32 33 38 public class XSAnnotationImpl implements XSAnnotation { 39 40 42 private String fData = null; 45 46 private SchemaGrammar fGrammar = null; 49 50 public XSAnnotationImpl(String contents, SchemaGrammar grammar) { 52 fData = contents; 53 fGrammar = grammar; 54 } 55 56 70 public boolean writeAnnotation(Object target, 71 short targetType) { 72 if(targetType == XSAnnotation.W3C_DOM_ELEMENT || targetType == XSAnnotation.W3C_DOM_DOCUMENT) { 73 writeToDOM((Node )target, targetType); 74 return true; 75 } else if (targetType == SAX_CONTENTHANDLER) { 76 writeToSAX((ContentHandler )target); 77 return true; 78 } 79 return false; 80 } 81 82 85 public String getAnnotationString() { 86 return fData; 87 } 88 89 91 95 public short getType() { 96 return XSConstants.ANNOTATION; 97 } 98 99 103 public String getName() { 104 return null; 105 } 106 107 111 public String getNamespace() { 112 return null; 113 } 114 115 120 public XSNamespaceItem getNamespaceItem() { 121 return null; 122 } 123 124 private synchronized void writeToSAX(ContentHandler handler) { 126 SAXParser parser = fGrammar.getSAXParser(); 128 StringReader aReader = new StringReader (fData); 129 InputSource aSource = new InputSource (aReader); 130 parser.setContentHandler(handler); 131 try { 132 parser.parse(aSource); 133 } catch (SAXException e) { 134 } catch (IOException i) { 138 } 140 } 141 142 private synchronized void writeToDOM(Node target, short type){ 145 Document futureOwner = (type == XSAnnotation.W3C_DOM_ELEMENT)?target.getOwnerDocument():(Document )target; 146 DOMParser parser = fGrammar.getDOMParser(); 147 StringReader aReader = new StringReader (fData); 148 InputSource aSource = new InputSource (aReader); 149 try { 150 parser.parse(aSource); 151 } catch (SAXException e) { 152 } catch (IOException i) { 156 } 158 Document aDocument = parser.getDocument(); 159 Element annotation = aDocument.getDocumentElement(); 160 Node newElem = futureOwner.importNode(annotation, true); 161 target.insertBefore(newElem, target.getFirstChild()); 162 } 163 164 } 165 | Popular Tags |