KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > StAXEventFactory


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.stax;
41
42 import javax.xml.namespace.QName JavaDoc;
43 import javax.xml.namespace.NamespaceContext JavaDoc;
44 import javax.xml.stream.Location;
45 import javax.xml.stream.XMLEventFactory;
46 import javax.xml.stream.events.*;
47 import java.util.Iterator JavaDoc;
48 import com.sun.xml.fastinfoset.stax.events.*;
49
50
51 public class StAXEventFactory extends XMLEventFactory {
52     Location location = null;
53     
54     /** Creates a new instance of StAXEventFactory */
55     public StAXEventFactory() {
56     }
57     /**
58     * This method allows setting of the Location on each event that
59     * is created by this factory. The values are copied by value into
60     * the events created by this factory. To reset the location
61     * information set the location to null.
62     * @param location the location to set on each event created
63     */

64     public void setLocation(Location location) {
65         this.location = location;
66     }
67     
68   /**
69    * Create a new Attribute
70    * @param prefix the prefix of this attribute, may not be null
71    * @param namespaceURI the attribute value is set to this value, may not be null
72    * @param localName the local name of the XML name of the attribute, localName cannot be null
73    * @param value the attribute value to set, may not be null
74    * @return the Attribute with specified values
75    */

76     public Attribute createAttribute(String JavaDoc prefix, String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc value) {
77         AttributeBase attr = new AttributeBase(prefix, namespaceURI, localName, value, null);
78         if(location != null)attr.setLocation(location);
79         return attr;
80     }
81     
82   /**
83    * Create a new Attribute
84    * @param localName the local name of the XML name of the attribute, localName cannot be null
85    * @param value the attribute value to set, may not be null
86    * @return the Attribute with specified values
87    */

88     public Attribute createAttribute(String JavaDoc localName, String JavaDoc value) {
89         AttributeBase attr = new AttributeBase(localName, value);
90         if(location != null)attr.setLocation(location);
91         return attr;
92     }
93     
94     public Attribute createAttribute(QName JavaDoc name, String JavaDoc value) {
95         AttributeBase attr = new AttributeBase(name, value);
96         if(location != null)attr.setLocation(location);
97         return attr;
98     }
99     
100   /**
101    * Create a new default Namespace
102    * @param namespaceURI the default namespace uri
103    * @return the Namespace with the specified value
104    */

105     public Namespace createNamespace(String JavaDoc namespaceURI) {
106         NamespaceBase event = new NamespaceBase(namespaceURI);
107         if(location != null)event.setLocation(location);
108         return event;
109     }
110     
111   /**
112    * Create a new Namespace
113    * @param prefix the prefix of this namespace, may not be null
114    * @param namespaceUri the attribute value is set to this value, may not be null
115    * @return the Namespace with the specified values
116    */

117     public Namespace createNamespace(String JavaDoc prefix, String JavaDoc namespaceURI) {
118         NamespaceBase event = new NamespaceBase(prefix, namespaceURI);
119         if(location != null)event.setLocation(location);
120         return event;
121     }
122         
123   /**
124    * Create a new StartElement.
125    * @param name the qualified name of the attribute, may not be null
126    * @param attributes an optional unordered set of objects that
127    * implement Attribute to add to the new StartElement, may be null
128    * @param namespaces an optional unordered set of objects that
129    * implement Namespace to add to the new StartElement, may be null
130    * @return an instance of the requested StartElement
131    */

132     public StartElement createStartElement(QName JavaDoc name, Iterator JavaDoc attributes, Iterator JavaDoc namespaces) {
133         return createStartElement(name.getPrefix(), name.getNamespaceURI(), name.getLocalPart(), attributes, namespaces);
134     }
135     
136     public StartElement createStartElement(String JavaDoc prefix, String JavaDoc namespaceUri, String JavaDoc localName) {
137         StartElementEvent event = new StartElementEvent(prefix, namespaceUri, localName);
138         if(location != null)event.setLocation(location);
139         return event;
140     }
141     
142     public StartElement createStartElement(String JavaDoc prefix, String JavaDoc namespaceUri, String JavaDoc localName, Iterator JavaDoc attributes, Iterator JavaDoc namespaces) {
143         return createStartElement(prefix, namespaceUri, localName, attributes, namespaces, null);
144     }
145     
146     public StartElement createStartElement(String JavaDoc prefix, String JavaDoc namespaceUri, String JavaDoc localName, Iterator JavaDoc attributes, Iterator JavaDoc namespaces, NamespaceContext JavaDoc context) {
147         StartElementEvent elem = new StartElementEvent(prefix, namespaceUri, localName);
148         elem.addAttributes(attributes);
149         elem.addNamespaces(namespaces);
150         elem.setNamespaceContext(context);
151         if(location != null)elem.setLocation(location);
152         return elem;
153     }
154     
155   /**
156    * Create a new EndElement
157    * @param name the qualified name of the EndElement
158    * @param namespaces an optional unordered set of objects that
159    * implement Namespace that have gone out of scope, may be null
160    * @return an instance of the requested EndElement
161    */

162     public EndElement createEndElement(QName JavaDoc name, Iterator JavaDoc namespaces) {
163         return createEndElement(name.getPrefix(), name.getNamespaceURI(), name.getLocalPart(), namespaces);
164     }
165     
166   /**
167    * Create a new EndElement
168    * @param namespaceUri the uri of the QName of the new StartElement
169    * @param localName the local name of the QName of the new StartElement
170    * @param prefix the prefix of the QName of the new StartElement
171    * @return an instance of the requested EndElement
172    */

173     public EndElement createEndElement(String JavaDoc prefix, String JavaDoc namespaceUri, String JavaDoc localName) {
174         EndElementEvent event = new EndElementEvent(prefix, namespaceUri, localName);
175         if(location != null)event.setLocation(location);
176         return event;
177     }
178     
179   /**
180    * Create a new EndElement
181    * @param namespaceUri the uri of the QName of the new StartElement
182    * @param localName the local name of the QName of the new StartElement
183    * @param prefix the prefix of the QName of the new StartElement
184    * @param namespaces an unordered set of objects that implement
185    * Namespace that have gone out of scope, may be null
186    * @return an instance of the requested EndElement
187    */

188     public EndElement createEndElement(String JavaDoc prefix, String JavaDoc namespaceUri, String JavaDoc localName, Iterator JavaDoc namespaces) {
189         
190         EndElementEvent event = new EndElementEvent(prefix, namespaceUri, localName);
191         if(namespaces!=null){
192             while(namespaces.hasNext())
193                 event.addNamespace((Namespace)namespaces.next());
194         }
195         if(location != null)event.setLocation(location);
196         return event;
197     }
198         
199   /**
200    * Create a Characters event, this method does not check if the content
201    * is all whitespace. To create a space event use #createSpace(String)
202    * @param content the string to create
203    * @return a Characters event
204    */

205     public Characters createCharacters(String JavaDoc content) {
206         CharactersEvent charEvent = new CharactersEvent(content);
207         if(location != null)charEvent.setLocation(location);
208         return charEvent;
209     }
210
211   /**
212    * Create a Characters event with the CData flag set to true
213    * @param content the string to create
214    * @return a Characters event
215    */

216     public Characters createCData(String JavaDoc content) {
217         CharactersEvent charEvent = new CharactersEvent(content, true);
218         if(location != null)charEvent.setLocation(location);
219         return charEvent;
220     }
221
222    /**
223    * Create a Characters event with the isSpace flag set to true
224    * @param content the content of the space to create
225    * @return a Characters event
226    */

227     public Characters createSpace(String JavaDoc content) {
228         CharactersEvent event = new CharactersEvent(content);
229         event.setSpace(true);
230         if(location != null)event.setLocation(location);
231         return event;
232     }
233     /**
234     * Create an ignorable space
235     * @param content the space to create
236     * @return a Characters event
237     */

238     public Characters createIgnorableSpace(String JavaDoc content) {
239         CharactersEvent event = new CharactersEvent(content, false);
240         event.setSpace(true);
241         event.setIgnorable(true);
242         if(location != null)event.setLocation(location);
243         return event;
244     }
245   /**
246    * Creates a new instance of a StartDocument event
247    * @return a StartDocument event
248    */

249     public StartDocument createStartDocument() {
250         StartDocumentEvent event = new StartDocumentEvent();
251         if(location != null)event.setLocation(location);
252         return event;
253     }
254     
255   /**
256    * Creates a new instance of a StartDocument event
257    *
258    * @param encoding the encoding style
259    * @return a StartDocument event
260    */

261     public StartDocument createStartDocument(String JavaDoc encoding) {
262         StartDocumentEvent event = new StartDocumentEvent(encoding);
263         if(location != null)event.setLocation(location);
264         return event;
265     }
266     
267   /**
268    * Creates a new instance of a StartDocument event
269    *
270    * @param encoding the encoding style
271    * @param version the XML version
272    * @return a StartDocument event
273    */

274     public StartDocument createStartDocument(String JavaDoc encoding, String JavaDoc version) {
275         StartDocumentEvent event = new StartDocumentEvent(encoding, version);
276         if(location != null)event.setLocation(location);
277         return event;
278     }
279     
280   /**
281    * Creates a new instance of a StartDocument event
282    *
283    * @param encoding the encoding style
284    * @param version the XML version
285    * @param standalone the status of standalone may be set to "true" or "false"
286    * @return a StartDocument event
287    */

288     public StartDocument createStartDocument(String JavaDoc encoding, String JavaDoc version, boolean standalone) {
289         StartDocumentEvent event = new StartDocumentEvent(encoding, version);
290         event.setStandalone(standalone);
291         if(location != null)event.setLocation(location);
292         return event;
293     }
294
295     public EndDocument createEndDocument() {
296         EndDocumentEvent event =new EndDocumentEvent();
297         if(location != null)event.setLocation(location);
298         return event;
299     }
300     
301     /** Creates a new instance of a EntityReference event
302     *
303     * @param name The name of the reference
304     * @param declaration the declaration for the event
305     * @return an EntityReference event
306     */

307     public EntityReference createEntityReference(String JavaDoc name, EntityDeclaration entityDeclaration) {
308         EntityReferenceEvent event = new EntityReferenceEvent(name, entityDeclaration);
309         if(location != null)event.setLocation(location);
310         return event;
311     }
312         
313     /**
314     * Create a comment
315     * @param text The text of the comment
316     * a Comment event
317     */

318     public Comment createComment(String JavaDoc text) {
319         CommentEvent charEvent = new CommentEvent(text);
320         if(location != null)charEvent.setLocation(location);
321         return charEvent;
322     }
323     
324     /**
325     * Create a document type definition event
326     * This string contains the entire document type declaration that matches
327     * the doctypedecl in the XML 1.0 specification
328     * @param dtd the text of the document type definition
329     * @return a DTD event
330     */

331     public DTD createDTD(String JavaDoc dtd) {
332         DTDEvent dtdEvent = new DTDEvent(dtd);
333         if(location != null)dtdEvent.setLocation(location);
334         return dtdEvent;
335     }
336
337
338     /**
339     * Create a processing instruction
340     * @param target The target of the processing instruction
341     * @param data The text of the processing instruction
342     * @return a ProcessingInstruction event
343     */

344     public ProcessingInstruction createProcessingInstruction(String JavaDoc target, String JavaDoc data) {
345         ProcessingInstructionEvent event = new ProcessingInstructionEvent(target, data);
346         if(location != null)event.setLocation(location);
347         return event;
348     }
349     
350
351
352
353         
354 }
355
Popular Tags