KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > XSAnnotationImpl


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.InputSource JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Document JavaDoc;
30 import java.io.StringReader JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 /**
34  * This is an implementation of the XSAnnotation schema component.
35  *
36  * @xerces.internal
37  */

38 public class XSAnnotationImpl implements XSAnnotation {
39
40     // Data
41

42     // the content of the annotation node, including all children, along
43
// with any non-schema attributes from its parent
44
private String JavaDoc fData = null;
45
46     // the grammar which owns this annotation; we get parsers
47
// from here when we need them
48
private SchemaGrammar fGrammar = null;
49
50     // constructors
51
public XSAnnotationImpl(String JavaDoc contents, SchemaGrammar grammar) {
52         fData = contents;
53         fGrammar = grammar;
54     }
55
56     /**
57      * Write contents of the annotation to the specified DOM object. If the
58      * specified <code>target</code> object is a DOM in-scope namespace
59      * declarations for <code>annotation</code> element are added as
60      * attributes nodes of the serialized <code>annotation</code>, otherwise
61      * the corresponding events for all in-scope namespace declaration are
62      * sent via specified document handler.
63      * @param target A target pointer to the annotation target object, i.e.
64      * <code>org.w3c.dom.Document</code>,
65      * <code>org.xml.sax.ContentHandler</code>.
66      * @param targetType A target type.
67      * @return If the <code>target</code> is recognized type and supported by
68      * this implementation return true, otherwise return false.
69      */

70     public boolean writeAnnotation(Object JavaDoc target,
71                                    short targetType) {
72         if(targetType == XSAnnotation.W3C_DOM_ELEMENT || targetType == XSAnnotation.W3C_DOM_DOCUMENT) {
73             writeToDOM((Node JavaDoc)target, targetType);
74             return true;
75         } else if (targetType == SAX_CONTENTHANDLER) {
76             writeToSAX((ContentHandler JavaDoc)target);
77             return true;
78         }
79         return false;
80     }
81
82     /**
83      * A text representation of annotation.
84      */

85     public String JavaDoc getAnnotationString() {
86         return fData;
87     }
88
89     // XSObject methods
90

91     /**
92      * The <code>type</code> of this object, i.e.
93      * <code>ELEMENT_DECLARATION</code>.
94      */

95     public short getType() {
96         return XSConstants.ANNOTATION;
97     }
98
99     /**
100      * The name of type <code>NCName</code> of this declaration as defined in
101      * XML Namespaces.
102      */

103     public String JavaDoc getName() {
104         return null;
105     }
106
107     /**
108      * The [target namespace] of this object, or <code>null</code> if it is
109      * unspecified.
110      */

111     public String JavaDoc getNamespace() {
112         return null;
113     }
114
115     /**
116      * A namespace schema information item corresponding to the target
117      * namespace of the component, if it's globally declared; or null
118      * otherwise.
119      */

120     public XSNamespaceItem getNamespaceItem() {
121         return null;
122     }
123
124     // private methods
125
private synchronized void writeToSAX(ContentHandler JavaDoc handler) {
126         // nothing must go wrong with this parse...
127
SAXParser parser = fGrammar.getSAXParser();
128         StringReader JavaDoc aReader = new StringReader JavaDoc(fData);
129         InputSource JavaDoc aSource = new InputSource JavaDoc(aReader);
130         parser.setContentHandler(handler);
131         try {
132             parser.parse(aSource);
133         } catch (SAXException JavaDoc e) {
134             // this should never happen!
135
// REVISIT: what to do with this?; should really not
136
// eat it...
137
} catch (IOException JavaDoc i) {
138             // ditto with above
139
}
140     }
141
142     // this creates the new Annotation element as the first child
143
// of the Node
144
private synchronized void writeToDOM(Node JavaDoc target, short type){
145         Document JavaDoc futureOwner = (type == XSAnnotation.W3C_DOM_ELEMENT)?target.getOwnerDocument():(Document JavaDoc)target;
146         DOMParser parser = fGrammar.getDOMParser();
147         StringReader JavaDoc aReader = new StringReader JavaDoc(fData);
148         InputSource JavaDoc aSource = new InputSource JavaDoc(aReader);
149         try {
150             parser.parse(aSource);
151         } catch (SAXException JavaDoc e) {
152             // this should never happen!
153
// REVISIT: what to do with this?; should really not
154
// eat it...
155
} catch (IOException JavaDoc i) {
156             // ditto with above
157
}
158         Document JavaDoc aDocument = parser.getDocument();
159         Element JavaDoc annotation = aDocument.getDocumentElement();
160         Node JavaDoc newElem = futureOwner.importNode(annotation, true);
161         target.insertBefore(newElem, target.getFirstChild());
162     }
163
164 }
165
Popular Tags