1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 2001, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.impl.xs;
  58. import com.sun.org.apache.xerces.internal.xs.XSAnnotation;
  59. import com.sun.org.apache.xerces.internal.xs.XSConstants;
  60. import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
  61. import com.sun.org.apache.xerces.internal.parsers.SAXParser;
  62. import com.sun.org.apache.xerces.internal.parsers.DOMParser;
  63. import org.xml.sax.ContentHandler;
  64. import org.xml.sax.SAXException;
  65. import org.xml.sax.InputSource;
  66. import org.w3c.dom.Node;
  67. import org.w3c.dom.Element;
  68. import org.w3c.dom.Document;
  69. import java.io.StringReader;
  70. import java.io.IOException;
  71. /**
  72. * This is an implementation of the XSAnnotation schema component.
  73. */
  74. public class XSAnnotationImpl implements XSAnnotation {
  75. // Data
  76. // the content of the annotation node, including all children, along
  77. // with any non-schema attributes from its parent
  78. private String fData = null;
  79. // the grammar which owns this annotation; we get parsers
  80. // from here when we need them
  81. private SchemaGrammar fGrammar = null;
  82. // constructors
  83. public XSAnnotationImpl(String contents, SchemaGrammar grammar) {
  84. fData = contents;
  85. fGrammar = grammar;
  86. }
  87. /**
  88. * Write contents of the annotation to the specified DOM object. If the
  89. * specified <code>target</code> object is a DOM in-scope namespace
  90. * declarations for <code>annotation</code> element are added as
  91. * attributes nodes of the serialized <code>annotation</code>, otherwise
  92. * the corresponding events for all in-scope namespace declaration are
  93. * sent via specified document handler.
  94. * @param target A target pointer to the annotation target object, i.e.
  95. * <code>org.w3c.dom.Document</code>,
  96. * <code>org.xml.sax.ContentHandler</code>.
  97. * @param targetType A target type.
  98. * @return If the <code>target</code> is recognized type and supported by
  99. * this implementation return true, otherwise return false.
  100. */
  101. public boolean writeAnnotation(Object target,
  102. short targetType) {
  103. if(targetType == XSAnnotation.W3C_DOM_ELEMENT || targetType == XSAnnotation.W3C_DOM_DOCUMENT) {
  104. writeToDOM((Node)target, targetType);
  105. return true;
  106. } else if (targetType == SAX_CONTENTHANDLER) {
  107. writeToSAX((ContentHandler)target);
  108. return true;
  109. }
  110. return false;
  111. }
  112. /**
  113. * A text representation of annotation.
  114. */
  115. public String getAnnotationString() {
  116. return fData;
  117. }
  118. // XSObject methods
  119. /**
  120. * The <code>type</code> of this object, i.e.
  121. * <code>ELEMENT_DECLARATION</code>.
  122. */
  123. public short getType() {
  124. return XSConstants.ANNOTATION;
  125. }
  126. /**
  127. * The name of type <code>NCName</code> of this declaration as defined in
  128. * XML Namespaces.
  129. */
  130. public String getName() {
  131. return null;
  132. }
  133. /**
  134. * The [target namespace] of this object, or <code>null</code> if it is
  135. * unspecified.
  136. */
  137. public String getNamespace() {
  138. return null;
  139. }
  140. /**
  141. * A namespace schema information item corresponding to the target
  142. * namespace of the component, if it's globally declared; or null
  143. * otherwise.
  144. */
  145. public XSNamespaceItem getNamespaceItem() {
  146. return null;
  147. }
  148. // private methods
  149. private synchronized void writeToSAX(ContentHandler handler) {
  150. // nothing must go wrong with this parse...
  151. SAXParser parser = fGrammar.getSAXParser();
  152. StringReader aReader = new StringReader(fData);
  153. InputSource aSource = new InputSource(aReader);
  154. parser.setContentHandler(handler);
  155. try {
  156. parser.parse(aSource);
  157. } catch (SAXException e) {
  158. // this should never happen!
  159. // REVISIT: what to do with this?; should really not
  160. // eat it...
  161. } catch (IOException i) {
  162. // ditto with above
  163. }
  164. }
  165. // this creates the new Annotation element as the first child
  166. // of the Node
  167. private synchronized void writeToDOM(Node target, short type){
  168. Document futureOwner = (type == XSAnnotation.W3C_DOM_ELEMENT)?target.getOwnerDocument():(Document)target;
  169. DOMParser parser = fGrammar.getDOMParser();
  170. StringReader aReader = new StringReader(fData);
  171. InputSource aSource = new InputSource(aReader);
  172. try {
  173. parser.parse(aSource);
  174. } catch (SAXException e) {
  175. // this should never happen!
  176. // REVISIT: what to do with this?; should really not
  177. // eat it...
  178. } catch (IOException i) {
  179. // ditto with above
  180. }
  181. Document aDocument = parser.getDocument();
  182. Element annotation = aDocument.getDocumentElement();
  183. Node newElem = futureOwner.importNode(annotation, true);
  184. target.insertBefore(newElem, target.getFirstChild());
  185. }
  186. }