KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xs > XSAnnotationImpl


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
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 JavaDoc;
66 import org.xml.sax.SAXException JavaDoc;
67 import org.xml.sax.InputSource JavaDoc;
68 import org.w3c.dom.Node JavaDoc;
69 import org.w3c.dom.Element JavaDoc;
70 import org.w3c.dom.Document JavaDoc;
71 import java.io.StringReader JavaDoc;
72 import java.io.IOException JavaDoc;
73
74 /**
75  * This is an implementation of the XSAnnotation schema component.
76  */

77 public class XSAnnotationImpl implements XSAnnotation {
78
79     // Data
80

81     // the content of the annotation node, including all children, along
82
// with any non-schema attributes from its parent
83
private String JavaDoc fData = null;
84
85     // the grammar which owns this annotation; we get parsers
86
// from here when we need them
87
private SchemaGrammar fGrammar = null;
88
89     // constructors
90
public XSAnnotationImpl(String JavaDoc contents, SchemaGrammar grammar) {
91         fData = contents;
92         fGrammar = grammar;
93     }
94
95     /**
96      * Write contents of the annotation to the specified DOM object. If the
97      * specified <code>target</code> object is a DOM in-scope namespace
98      * declarations for <code>annotation</code> element are added as
99      * attributes nodes of the serialized <code>annotation</code>, otherwise
100      * the corresponding events for all in-scope namespace declaration are
101      * sent via specified document handler.
102      * @param target A target pointer to the annotation target object, i.e.
103      * <code>org.w3c.dom.Document</code>,
104      * <code>org.xml.sax.ContentHandler</code>.
105      * @param targetType A target type.
106      * @return If the <code>target</code> is recognized type and supported by
107      * this implementation return true, otherwise return false.
108      */

109     public boolean writeAnnotation(Object JavaDoc target,
110                                    short targetType) {
111         if(targetType == XSAnnotation.W3C_DOM_ELEMENT || targetType == XSAnnotation.W3C_DOM_DOCUMENT) {
112             writeToDOM((Node JavaDoc)target, targetType);
113             return true;
114         } else if (targetType == SAX_CONTENTHANDLER) {
115             writeToSAX((ContentHandler JavaDoc)target);
116             return true;
117         }
118         return false;
119     }
120
121     /**
122      * A text representation of annotation.
123      */

124     public String JavaDoc getAnnotationString() {
125         return fData;
126     }
127
128     // XSObject methods
129

130     /**
131      * The <code>type</code> of this object, i.e.
132      * <code>ELEMENT_DECLARATION</code>.
133      */

134     public short getType() {
135         return XSConstants.ANNOTATION;
136     }
137
138     /**
139      * The name of type <code>NCName</code> of this declaration as defined in
140      * XML Namespaces.
141      */

142     public String JavaDoc getName() {
143         return null;
144     }
145
146     /**
147      * The [target namespace] of this object, or <code>null</code> if it is
148      * unspecified.
149      */

150     public String JavaDoc getNamespace() {
151         return null;
152     }
153
154     /**
155      * A namespace schema information item corresponding to the target
156      * namespace of the component, if it's globally declared; or null
157      * otherwise.
158      */

159     public XSNamespaceItem getNamespaceItem() {
160         return null;
161     }
162
163     // private methods
164
private synchronized void writeToSAX(ContentHandler JavaDoc handler) {
165         // nothing must go wrong with this parse...
166
SAXParser parser = fGrammar.getSAXParser();
167         StringReader JavaDoc aReader = new StringReader JavaDoc(fData);
168         InputSource JavaDoc aSource = new InputSource JavaDoc(aReader);
169         parser.setContentHandler(handler);
170         try {
171             parser.parse(aSource);
172         } catch (SAXException JavaDoc e) {
173             // this should never happen!
174
// REVISIT: what to do with this?; should really not
175
// eat it...
176
} catch (IOException JavaDoc i) {
177             // ditto with above
178
}
179     }
180
181     // this creates the new Annotation element as the first child
182
// of the Node
183
private synchronized void writeToDOM(Node JavaDoc target, short type){
184         Document JavaDoc futureOwner = (type == XSAnnotation.W3C_DOM_ELEMENT)?target.getOwnerDocument():(Document JavaDoc)target;
185         DOMParser parser = fGrammar.getDOMParser();
186         StringReader JavaDoc aReader = new StringReader JavaDoc(fData);
187         InputSource JavaDoc aSource = new InputSource JavaDoc(aReader);
188         try {
189             parser.parse(aSource);
190         } catch (SAXException JavaDoc e) {
191             // this should never happen!
192
// REVISIT: what to do with this?; should really not
193
// eat it...
194
} catch (IOException JavaDoc i) {
195             // ditto with above
196
}
197         Document JavaDoc aDocument = parser.getDocument();
198         Element JavaDoc annotation = aDocument.getDocumentElement();
199         Node JavaDoc newElem = futureOwner.importNode(annotation, true);
200         target.insertBefore(newElem, target.getFirstChild());
201     }
202
203 }
204
Popular Tags