1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.xerces.internal.impl.xs; 23 24 import java.io.IOException; 25 import java.io.StringReader; 26 27 import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl; 28 import com.sun.org.apache.xerces.internal.parsers.DOMParser; 29 import com.sun.org.apache.xerces.internal.parsers.SAXParser; 30 import com.sun.org.apache.xerces.internal.xs.XSAnnotation; 31 import com.sun.org.apache.xerces.internal.xs.XSConstants; 32 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem; 33 34 import org.w3c.dom.Document; 35 import org.w3c.dom.Element; 36 import org.w3c.dom.Node; 37 import org.xml.sax.ContentHandler; 38 import org.xml.sax.InputSource; 39 import org.xml.sax.SAXException; 40 /** 41 * This is an implementation of the XSAnnotation schema component. 42 * 43 * @xerces.internal 44 */ 45 public class XSAnnotationImpl implements XSAnnotation { 46 47 // Data 48 49 // the content of the annotation node, including all children, along 50 // with any non-schema attributes from its parent 51 private String fData = null; 52 53 // the grammar which owns this annotation; we get parsers 54 // from here when we need them 55 private SchemaGrammar fGrammar = null; 56 57 // constructors XSAnnotationImpl(String contents, SchemaGrammar grammar)58 public XSAnnotationImpl(String contents, SchemaGrammar grammar) { 59 fData = contents; 60 fGrammar = grammar; 61 } 62 63 /** 64 * Write contents of the annotation to the specified DOM object. If the 65 * specified <code>target</code> object is a DOM in-scope namespace 66 * declarations for <code>annotation</code> element are added as 67 * attributes nodes of the serialized <code>annotation</code>, otherwise 68 * the corresponding events for all in-scope namespace declaration are 69 * sent via specified document handler. 70 * @param target A target pointer to the annotation target object, i.e. 71 * <code>org.w3c.dom.Document</code>, 72 * <code>org.xml.sax.ContentHandler</code>. 73 * @param targetType A target type. 74 * @return If the <code>target</code> is recognized type and supported by 75 * this implementation return true, otherwise return false. 76 */ writeAnnotation(Object target, short targetType)77 public boolean writeAnnotation(Object target, 78 short targetType) { 79 if(targetType == XSAnnotation.W3C_DOM_ELEMENT || targetType == XSAnnotation.W3C_DOM_DOCUMENT) { 80 writeToDOM((Node)target, targetType); 81 return true; 82 } else if (targetType == SAX_CONTENTHANDLER) { 83 writeToSAX((ContentHandler)target); 84 return true; 85 } 86 return false; 87 } 88 89 /** 90 * A text representation of annotation. 91 */ getAnnotationString()92 public String getAnnotationString() { 93 return fData; 94 } 95 96 // XSObject methods 97 98 /** 99 * The <code>type</code> of this object, i.e. 100 * <code>ELEMENT_DECLARATION</code>. 101 */ getType()102 public short getType() { 103 return XSConstants.ANNOTATION; 104 } 105 106 /** 107 * The name of type <code>NCName</code> of this declaration as defined in 108 * XML Namespaces. 109 */ getName()110 public String getName() { 111 return null; 112 } 113 114 /** 115 * The [target namespace] of this object, or <code>null</code> if it is 116 * unspecified. 117 */ getNamespace()118 public String getNamespace() { 119 return null; 120 } 121 122 /** 123 * A namespace schema information item corresponding to the target 124 * namespace of the component, if it's globally declared; or null 125 * otherwise. 126 */ getNamespaceItem()127 public XSNamespaceItem getNamespaceItem() { 128 return null; 129 } 130 131 // private methods writeToSAX(ContentHandler handler)132 private synchronized void writeToSAX(ContentHandler handler) { 133 // nothing must go wrong with this parse... 134 SAXParser parser = fGrammar.getSAXParser(); 135 StringReader aReader = new StringReader(fData); 136 InputSource aSource = new InputSource(aReader); 137 parser.setContentHandler(handler); 138 try { 139 parser.parse(aSource); 140 } 141 catch (SAXException e) { 142 // this should never happen! 143 // REVISIT: what to do with this?; should really not 144 // eat it... 145 } 146 catch (IOException i) { 147 // ditto with above 148 } 149 // Release the reference to the user's ContentHandler. 150 parser.setContentHandler(null); 151 } 152 153 // this creates the new Annotation element as the first child 154 // of the Node writeToDOM(Node target, short type)155 private synchronized void writeToDOM(Node target, short type) { 156 Document futureOwner = (type == XSAnnotation.W3C_DOM_ELEMENT) ? 157 target.getOwnerDocument() : (Document)target; 158 DOMParser parser = fGrammar.getDOMParser(); 159 StringReader aReader = new StringReader(fData); 160 InputSource aSource = new InputSource(aReader); 161 try { 162 parser.parse(aSource); 163 } 164 catch (SAXException e) { 165 // this should never happen! 166 // REVISIT: what to do with this?; should really not 167 // eat it... 168 } 169 catch (IOException i) { 170 // ditto with above 171 } 172 Document aDocument = parser.getDocument(); 173 parser.dropDocumentReferences(); 174 Element annotation = aDocument.getDocumentElement(); 175 Node newElem = null; 176 if (futureOwner instanceof CoreDocumentImpl) { 177 newElem = futureOwner.adoptNode(annotation); 178 // adoptNode will return null when the DOM implementations are not compatible. 179 if (newElem == null) { 180 newElem = futureOwner.importNode(annotation, true); 181 } 182 } 183 else { 184 newElem = futureOwner.importNode(annotation, true); 185 } 186 target.insertBefore(newElem, target.getFirstChild()); 187 } 188 189 } 190