001: /*
002: * $Id: XMLEventConsumerDelegate.java,v 1.4 2004/08/19 15:58:17 cniles Exp $
003: *
004: * Copyright (c) 2004, Christian Niles, Unit12
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * * Neither the name of Christian Niles, Unit12, nor the names of its
018: * contributors may be used to endorse or promote products derived from
019: * this software without specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: * POSSIBILITY OF SUCH DAMAGE.
032: *
033: */
034: package javanet.staxutils;
035:
036: import java.util.Iterator;
037:
038: import javax.xml.namespace.NamespaceContext;
039: import javax.xml.namespace.QName;
040: import javax.xml.stream.XMLEventFactory;
041: import javax.xml.stream.XMLStreamException;
042: import javax.xml.stream.events.*;
043: import javax.xml.stream.util.XMLEventConsumer;
044:
045: /**
046: * Writes all events to a wrapped {@link XMLEventConsumer}, and provides
047: * convenience methods for creating events written to the internal consumer.
048: *
049: * @author Christian Niles
050: * @version $Revision: 1.4 $
051: */
052: public class XMLEventConsumerDelegate implements XMLEventConsumer {
053:
054: /**
055: * The consumer instance to which events are written.
056: */
057: private XMLEventConsumer consumer;
058:
059: /**
060: * The factory used to create events.
061: */
062: private XMLEventFactory factory;
063:
064: public XMLEventConsumerDelegate(XMLEventConsumer consumer) {
065:
066: this .consumer = consumer;
067: this .factory = XMLEventFactory.newInstance();
068:
069: }
070:
071: public XMLEventConsumerDelegate(XMLEventConsumer consumer,
072: XMLEventFactory factory) {
073:
074: this .consumer = consumer;
075: this .factory = (factory == null ? XMLEventFactory.newInstance()
076: : factory);
077:
078: }
079:
080: /**
081: * Returns a reference to the underlying {@link XMLEventConsumer} to which
082: * events are added.
083: *
084: * @return The underlying {@link XMLEventConsumer} to which events are
085: * added.
086: */
087: public XMLEventConsumer getConsumer() {
088:
089: return consumer;
090:
091: }
092:
093: /**
094: * Sets the underlying {@link XMLEventConsumer} to which events are added.
095: *
096: * @param consumer The new {@link XMLEventConsumer}.
097: */
098: public void setConsumer(XMLEventConsumer consumer) {
099:
100: this .consumer = consumer;
101:
102: }
103:
104: /**
105: * Returns a reference to the {@link XMLEventFactory} used to construct
106: * events.
107: *
108: * @return The {@link XMLEventFactory} used to construct events.
109: */
110: public XMLEventFactory getEventFactory() {
111:
112: return factory;
113:
114: }
115:
116: /**
117: * Sets the {@link XMLEventFactory} used to construct events.
118: *
119: * @param factory The new {@link XMLEventFactory}.
120: */
121: public void setEventFactory(XMLEventFactory factory) {
122:
123: this .factory = factory;
124:
125: }
126:
127: public void add(XMLEvent event) throws XMLStreamException {
128:
129: consumer.add(event);
130:
131: }
132:
133: /**
134: * Creates and adds a {@link DTD} event.
135: *
136: * @param dtd The DTD content, as per
137: * {@link XMLEventFactory#createDTD(String)}.
138: * @throws XMLStreamException If an error occurs adding the event.
139: */
140: public void addDTD(String dtd) throws XMLStreamException {
141:
142: add(factory.createDTD(dtd));
143:
144: }
145:
146: /**
147: * Creates and adds a CDATA {@link Characters} event.
148: *
149: * @param content The CDATA content, as per
150: * {@link XMLEventFactory#createCData(String)}.
151: * @throws XMLStreamException If an error occurs adding the event.
152: */
153: public void addCData(String content) throws XMLStreamException {
154:
155: add(factory.createCData(content));
156:
157: }
158:
159: /**
160: * Creates and adds a {@link Characters} event.
161: *
162: * @param content The text content, as per
163: * {@link XMLEventFactory#createCharacters(String)}.
164: * @throws XMLStreamException If an error occurs adding the event.
165: */
166: public void addText(String content) throws XMLStreamException {
167:
168: add(factory.createCharacters(content));
169:
170: }
171:
172: /**
173: * Creates and adds an ignorable space {@link Characters} event.
174: *
175: * @param content The ignorable whitespace, as per
176: * {@link XMLEventFactory#createIgnorableSpace(String)}.
177: * @throws XMLStreamException If an error occurs adding the event.
178: */
179: public void addIgnorableSpace(String content)
180: throws XMLStreamException {
181:
182: add(factory.createIgnorableSpace(content));
183:
184: }
185:
186: /**
187: * Creates and adds a whitespace {@link Characters} event.
188: *
189: * @param content The whitespace, as per
190: * {@link XMLEventFactory#createIgnorableSpace(String)}.
191: * @throws XMLStreamException If an error occurs adding the event.
192: */
193: public void addSpace(String content) throws XMLStreamException {
194:
195: add(factory.createSpace(content));
196:
197: }
198:
199: /**
200: * Creates and adds a {@link Comment} event.
201: *
202: * @param comment The comment text, as per
203: * {@link XMLEventFactory#createComment(String)}.
204: * @throws XMLStreamException If an error occurs adding the event.
205: */
206: public void addComment(String comment) throws XMLStreamException {
207:
208: add(factory.createComment(comment));
209:
210: }
211:
212: /**
213: * Creates and adds a {@link StartDocument} event.
214: *
215: * @see XMLEventFactory#createStartDocument()
216: * @throws XMLStreamException If an error occurs adding the event.
217: */
218: public void addStartDocument() throws XMLStreamException {
219:
220: add(factory.createStartDocument());
221:
222: }
223:
224: /**
225: * Creates and adds a {@link StartDocument} event.
226: *
227: * @param encoding The encoding to specify in the xml declaration.
228: * @see XMLEventFactory#createStartDocument(String)
229: * @throws XMLStreamException If an error occurs adding the event.
230: */
231: public void addStartDocument(String encoding)
232: throws XMLStreamException {
233:
234: add(factory.createStartDocument(encoding));
235:
236: }
237:
238: /**
239: * Creates and adds a {@link StartDocument} event.
240: *
241: * @param encoding The encoding to include in the xml declaration.
242: * @param version The XML version to include in the xml declaration.
243: * @see XMLEventFactory#createStartDocument(String, String)
244: * @throws XMLStreamException If an error occurs adding the event.
245: */
246: public void addStartDocument(String encoding, String version)
247: throws XMLStreamException {
248:
249: add(factory.createStartDocument(encoding, version));
250:
251: }
252:
253: /**
254: * Creates and adds a {@link StartDocument} event.
255: *
256: * @param encoding The encoding to include in the xml declaration.
257: * @param version The XML version to include in the xml declaration.
258: * @param standalone The standalone value to include in the xml declaration.
259: * @see XMLEventFactory#createStartDocument(String, String, boolean)
260: * @throws XMLStreamException If an error occurs adding the event.
261: */
262: public void addStartDocument(String encoding, String version,
263: boolean standalone) throws XMLStreamException {
264:
265: add(factory.createStartDocument(encoding, version, standalone));
266:
267: }
268:
269: /**
270: * Creates and adds an {@link EndDocument} event.
271: *
272: * @see XMLEventFactory#createEndDocument()
273: * @throws XMLStreamException If an error occurs adding the event.
274: */
275: public void addEndDocument() throws XMLStreamException {
276:
277: add(factory.createEndDocument());
278:
279: }
280:
281: /**
282: * Creates and adds a {@link StartElement} event.
283: *
284: * @param localName The local name of the element.
285: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
286: * @throws XMLStreamException If an error occurs adding the event.
287: */
288: public void addStartElement(String localName,
289: NamespaceContext context) throws XMLStreamException {
290:
291: addStartElement(localName, null, null, context);
292:
293: }
294:
295: /**
296: * Creates and adds a {@link StartElement} event.
297: *
298: * @param localName The local name of the element.
299: * @param attributes An {@link Iterator} over the element's attributes.
300: * @param namespaces An {@link Iterator} over the element's namespaces.
301: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
302: * @throws XMLStreamException If an error occurs adding the event.
303: */
304: public void addStartElement(String localName, Iterator attributes,
305: Iterator namespaces, NamespaceContext context)
306: throws XMLStreamException {
307:
308: add(factory.createStartElement("", "", localName, attributes,
309: namespaces, context));
310:
311: }
312:
313: /**
314: * Creates and adds a {@link StartElement} event.
315: *
316: * @param ns The element's namespace URI.
317: * @param localName The local name of the element.
318: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
319: * @throws XMLStreamException If an error occurs adding the event.
320: */
321: public void addStartElement(String ns, String localName,
322: NamespaceContext context) throws XMLStreamException {
323:
324: addStartElement(ns, localName, null, null, context);
325:
326: }
327:
328: /**
329: * Creates and adds a {@link StartElement} event.
330: *
331: * @param ns The element's namespace URI.
332: * @param localName The local name of the element.
333: * @param attributes An {@link Iterator} over the element's attributes.
334: * @param namespaces An {@link Iterator} over the element's namespaces.
335: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
336: * @throws XMLStreamException If an error occurs adding the event.
337: */
338: public void addStartElement(String ns, String localName,
339: Iterator attributes, Iterator namespaces,
340: NamespaceContext context) throws XMLStreamException {
341:
342: add(factory.createStartElement("", ns, localName, attributes,
343: namespaces, context));
344:
345: }
346:
347: /**
348: * Creates and adds a {@link StartElement} event.
349: *
350: * @param name The qualified element name.
351: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
352: * @throws XMLStreamException If an error occurs adding the event.
353: */
354: public void addStartElement(QName name, NamespaceContext context)
355: throws XMLStreamException {
356:
357: addStartElement(name, null, null, context);
358:
359: }
360:
361: /**
362: * Creates and adds a {@link StartElement} event.
363: *
364: * @param name The qualified element name.
365: * @param attributes An {@link Iterator} over the element's attributes.
366: * @param namespaces An {@link Iterator} over the element's namespaces.
367: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
368: * @throws XMLStreamException If an error occurs adding the event.
369: */
370: public void addStartElement(QName name, Iterator attributes,
371: Iterator namespaces, NamespaceContext context)
372: throws XMLStreamException {
373:
374: add(factory.createStartElement(name.getPrefix(), name
375: .getNamespaceURI(), name.getLocalPart(), attributes,
376: namespaces, context));
377:
378: }
379:
380: /**
381: * Creates and adds an {@link EndElement} event.
382: *
383: * @param localName The unqualified element name.
384: * @throws XMLStreamException If an error occurs adding the event.
385: */
386: public void addEndElement(String localName)
387: throws XMLStreamException {
388:
389: addEndElement(localName, (Iterator) null);
390:
391: }
392:
393: /**
394: * Creates and adds an {@link EndElement} event.
395: *
396: * @param localName The unqualified element name.
397: * @param namespaces An {@link Iterator} over the element's namespaces that
398: * are going out of scope.
399: * @throws XMLStreamException If an error occurs adding the event.
400: */
401: public void addEndElement(String localName, Iterator namespaces)
402: throws XMLStreamException {
403:
404: add(factory.createEndElement(null, null, localName, namespaces));
405:
406: }
407:
408: /**
409: * Creates and adds an {@link EndElement} event.
410: *
411: * @param ns The element namespace.
412: * @param localName The element name.
413: * @throws XMLStreamException If an error occurs adding the event.
414: */
415: public void addEndElement(String ns, String localName)
416: throws XMLStreamException {
417:
418: addEndElement(ns, localName, (Iterator) null);
419:
420: }
421:
422: /**
423: * Creates and adds an {@link EndElement} event.
424: *
425: * @param ns The element namespace.
426: * @param localName The element name.
427: * @param namespaces An {@link Iterator} over the element's namespaces that
428: * are going out of scope.
429: * @throws XMLStreamException If an error occurs adding the event.
430: */
431: public void addEndElement(String ns, String localName,
432: Iterator namespaces) throws XMLStreamException {
433:
434: add(factory.createEndElement(null, ns, localName, namespaces));
435:
436: }
437:
438: /**
439: * Creates and adds an {@link EndElement} event.
440: *
441: * @param name The element name.
442: * @see XMLEventFactory#createEndElement(QName, Iterator)
443: * @throws XMLStreamException If an error occurs adding the event.
444: */
445: public void addEndElement(QName name) throws XMLStreamException {
446:
447: addEndElement(name, (Iterator) null);
448:
449: }
450:
451: /**
452: * Creates and adds an {@link EndElement} event.
453: *
454: * @param name The element name.
455: * @param namespaces An {@link Iterator} over the element's namespaces that
456: * are going out of scope.
457: * @see XMLEventFactory#createEndElement(QName, Iterator)
458: * @throws XMLStreamException If an error occurs adding the event.
459: */
460: public void addEndElement(QName name, Iterator namespaces)
461: throws XMLStreamException {
462:
463: add(factory.createEndElement(name, namespaces));
464:
465: }
466:
467: /**
468: * Adds a simple text element with no attributes or namespace declarations.
469: *
470: * @param name The unqualified element name.
471: * @param text The text content, which may be <code>null</code>
472: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
473: * @throws XMLStreamException If an error occurs adding an event.
474: */
475: public void addTextElement(String name, String text,
476: NamespaceContext context) throws XMLStreamException {
477:
478: addStartElement(name, context);
479: if (text != null) {
480:
481: addText(text);
482:
483: }
484: addEndElement(name);
485:
486: }
487:
488: /**
489: * Adds a simple text element with no attributes or namespace declarations.
490: *
491: * @param name The element name.
492: * @param text The text content, which may be <code>null</code>
493: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
494: * @throws XMLStreamException If an error occurs adding an event.
495: */
496: public void addTextElement(QName name, String text,
497: NamespaceContext context) throws XMLStreamException {
498:
499: addStartElement(name, context);
500: if (text != null) {
501:
502: addText(text);
503:
504: }
505: addEndElement(name);
506:
507: }
508:
509: /**
510: * Adds a boolean text element with no attributes or namespace declarations.
511: *
512: * @param name The unqualified element name.
513: * @param text The boolean content.
514: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
515: * @throws XMLStreamException If an error occurs adding an event.
516: */
517: public void addTextElement(String name, boolean text,
518: NamespaceContext context) throws XMLStreamException {
519:
520: addTextElement(name, Boolean.toString(text), context);
521:
522: }
523:
524: /**
525: * Adds a boolean text element with no attributes or namespace declarations.
526: *
527: * @param name The element name.
528: * @param text The boolean content.
529: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
530: * @throws XMLStreamException If an error occurs adding an event.
531: */
532: public void addTextElement(QName name, boolean text,
533: NamespaceContext context) throws XMLStreamException {
534:
535: addTextElement(name, Boolean.toString(text), context);
536:
537: }
538:
539: /**
540: * Adds a text element with no attributes or namespace declarations.
541: *
542: * @param name The unqualified element name.
543: * @param text The element content.
544: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
545: * @throws XMLStreamException If an error occurs adding an event.
546: */
547: public void addTextElement(String name, int text,
548: NamespaceContext context) throws XMLStreamException {
549:
550: addTextElement(name, Integer.toString(text), context);
551:
552: }
553:
554: /**
555: * Adds a text element with no attributes or namespace declarations.
556: *
557: * @param name The element name.
558: * @param text The element content.
559: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
560: * @throws XMLStreamException If an error occurs adding an event.
561: */
562: public void addTextElement(QName name, int text,
563: NamespaceContext context) throws XMLStreamException {
564:
565: addTextElement(name, Integer.toString(text), context);
566:
567: }
568:
569: /**
570: * Adds a text element with no attributes or namespace declarations.
571: *
572: * @param name The unqualified element name.
573: * @param text The element content.
574: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
575: * @throws XMLStreamException If an error occurs adding an event.
576: */
577: public void addTextElement(String name, long text,
578: NamespaceContext context) throws XMLStreamException {
579:
580: addTextElement(name, Long.toString(text), context);
581:
582: }
583:
584: /**
585: * Adds a text element with no attributes or namespace declarations.
586: *
587: * @param name The element name.
588: * @param text The element content.
589: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
590: * @throws XMLStreamException If an error occurs adding an event.
591: */
592: public void addTextElement(QName name, long text,
593: NamespaceContext context) throws XMLStreamException {
594:
595: addTextElement(name, Long.toString(text), context);
596:
597: }
598:
599: /**
600: * Adds a text element with no attributes or namespace declarations.
601: *
602: * @param name The unqualified element name.
603: * @param text The element content.
604: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
605: * @throws XMLStreamException If an error occurs adding an event.
606: */
607: public void addTextElement(String name, float text,
608: NamespaceContext context) throws XMLStreamException {
609:
610: addTextElement(name, Float.toString(text), context);
611:
612: }
613:
614: /**
615: * Adds a text element with no attributes or namespace declarations.
616: *
617: * @param name The element name.
618: * @param text The element content.
619: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
620: * @throws XMLStreamException If an error occurs adding an event.
621: */
622: public void addTextElement(QName name, float text,
623: NamespaceContext context) throws XMLStreamException {
624:
625: addTextElement(name, Float.toString(text), context);
626:
627: }
628:
629: /**
630: * Adds a text element with no attributes or namespace declarations.
631: *
632: * @param name The unqualified element name.
633: * @param text The element content.
634: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
635: * @throws XMLStreamException If an error occurs adding an event.
636: */
637: public void addTextElement(String name, double text,
638: NamespaceContext context) throws XMLStreamException {
639:
640: addTextElement(name, Double.toString(text), context);
641:
642: }
643:
644: /**
645: * Adds a text element with no attributes or namespace declarations.
646: *
647: * @param name The element name.
648: * @param text The element content.
649: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
650: * @throws XMLStreamException If an error occurs adding an event.
651: */
652: public void addTextElement(QName name, double text,
653: NamespaceContext context) throws XMLStreamException {
654:
655: addTextElement(name, Double.toString(text), context);
656:
657: }
658:
659: /**
660: * Adds a text element with no attributes or namespace declarations.
661: *
662: * @param name The unqualified element name.
663: * @param text The element content.
664: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
665: * @throws XMLStreamException If an error occurs adding an event.
666: */
667: public void addTextElement(String name, Number text,
668: NamespaceContext context) throws XMLStreamException {
669:
670: if (text != null) {
671:
672: addTextElement(name, text.toString(), context);
673:
674: } else {
675:
676: addTextElement(name, (String) null, context);
677:
678: }
679:
680: }
681:
682: /**
683: * Adds a text element with no attributes or namespace declarations.
684: *
685: * @param name The element name.
686: * @param text The element content.
687: * @param context The element's {@link NamespaceContext}, or <code>null</code>.
688: * @throws XMLStreamException If an error occurs adding an event.
689: */
690: public void addTextElement(QName name, Number text,
691: NamespaceContext context) throws XMLStreamException {
692:
693: if (text != null) {
694:
695: addTextElement(name, text.toString(), context);
696:
697: } else {
698:
699: addTextElement(name, (String) null, context);
700:
701: }
702:
703: }
704:
705: }
|