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