1 57 package com.sun.org.apache.xerces.internal.impl.xs; 58 59 import com.sun.org.apache.xerces.internal.xs.XSAnnotation; 60 import com.sun.org.apache.xerces.internal.xs.XSConstants; 61 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem; 62 import com.sun.org.apache.xerces.internal.parsers.SAXParser; 63 import com.sun.org.apache.xerces.internal.parsers.DOMParser; 64 65 import org.xml.sax.ContentHandler ; 66 import org.xml.sax.SAXException ; 67 import org.xml.sax.InputSource ; 68 import org.w3c.dom.Node ; 69 import org.w3c.dom.Element ; 70 import org.w3c.dom.Document ; 71 import java.io.StringReader ; 72 import java.io.IOException ; 73 74 77 public class XSAnnotationImpl implements XSAnnotation { 78 79 81 private String fData = null; 84 85 private SchemaGrammar fGrammar = null; 88 89 public XSAnnotationImpl(String contents, SchemaGrammar grammar) { 91 fData = contents; 92 fGrammar = grammar; 93 } 94 95 109 public boolean writeAnnotation(Object target, 110 short targetType) { 111 if(targetType == XSAnnotation.W3C_DOM_ELEMENT || targetType == XSAnnotation.W3C_DOM_DOCUMENT) { 112 writeToDOM((Node )target, targetType); 113 return true; 114 } else if (targetType == SAX_CONTENTHANDLER) { 115 writeToSAX((ContentHandler )target); 116 return true; 117 } 118 return false; 119 } 120 121 124 public String getAnnotationString() { 125 return fData; 126 } 127 128 130 134 public short getType() { 135 return XSConstants.ANNOTATION; 136 } 137 138 142 public String getName() { 143 return null; 144 } 145 146 150 public String getNamespace() { 151 return null; 152 } 153 154 159 public XSNamespaceItem getNamespaceItem() { 160 return null; 161 } 162 163 private synchronized void writeToSAX(ContentHandler handler) { 165 SAXParser parser = fGrammar.getSAXParser(); 167 StringReader aReader = new StringReader (fData); 168 InputSource aSource = new InputSource (aReader); 169 parser.setContentHandler(handler); 170 try { 171 parser.parse(aSource); 172 } catch (SAXException e) { 173 } catch (IOException i) { 177 } 179 } 180 181 private synchronized void writeToDOM(Node target, short type){ 184 Document futureOwner = (type == XSAnnotation.W3C_DOM_ELEMENT)?target.getOwnerDocument():(Document )target; 185 DOMParser parser = fGrammar.getDOMParser(); 186 StringReader aReader = new StringReader (fData); 187 InputSource aSource = new InputSource (aReader); 188 try { 189 parser.parse(aSource); 190 } catch (SAXException e) { 191 } catch (IOException i) { 195 } 197 Document aDocument = parser.getDocument(); 198 Element annotation = aDocument.getDocumentElement(); 199 Node newElem = futureOwner.importNode(annotation, true); 200 target.insertBefore(newElem, target.getFirstChild()); 201 } 202 203 } 204 | Popular Tags |