View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   // Generated by Modello 2.3.0,
3   // any modifications will be overwritten.
4   // ==============================================================
5   
6   package org.codehaus.modello.test.features.io.stax;
7   
8     //---------------------------------/
9    //- Imported classes and packages -/
10  //---------------------------------/
11  
12  import javax.xml.namespace.NamespaceContext;
13  import javax.xml.stream.XMLStreamException;
14  import javax.xml.stream.XMLStreamWriter;
15  
16  /**
17   * Class IndentingXMLStreamWriter.
18   * 
19   * @version $Revision$ $Date$
20   */
21  @SuppressWarnings( "all" )
22  class IndentingXMLStreamWriter
23      implements XMLStreamWriter
24  {
25  
26        //--------------------------/
27       //- Class/Member Variables -/
28      //--------------------------/
29  
30      /**
31       * Field out.
32       */
33      private XMLStreamWriter out;
34  
35      /**
36       * Field NEW_LINE.
37       */
38      private static final String NEW_LINE = "\n";
39  
40      /**
41       * Field newLine.
42       */
43      private String newLine = NEW_LINE;
44  
45      /**
46       * Field indent.
47       */
48      private String indent = "  ";
49  
50      /**
51       * Field linePrefix.
52       */
53      private char[] linePrefix = "                        ".toCharArray();
54  
55      /**
56       * Field depth.
57       */
58      private int depth;
59  
60      /**
61       * Field states.
62       */
63      private byte[] states = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
64  
65      /**
66       * Field ELEMENT_HAS_DATA.
67       */
68      private static final int ELEMENT_HAS_DATA = 0x1;
69  
70      /**
71       * Field ELEMENT_HAS_MARKUP.
72       */
73      private static final int ELEMENT_HAS_MARKUP = 0x2;
74  
75  
76        //----------------/
77       //- Constructors -/
78      //----------------/
79  
80      public IndentingXMLStreamWriter(XMLStreamWriter out)
81      {
82          this.out = out;
83      } //-- org.codehaus.modello.test.features.io.stax.IndentingXMLStreamWriter(XMLStreamWriter)
84  
85  
86        //-----------/
87       //- Methods -/
88      //-----------/
89  
90      /**
91       * Method close.
92       * 
93       * @throws XMLStreamException XMLStreamException if any.
94       */
95      public void close()
96          throws XMLStreamException
97      {
98          out.close();
99      } //-- void close()
100 
101     /**
102      * Method flush.
103      * 
104      * @throws XMLStreamException XMLStreamException if any.
105      */
106     public void flush()
107         throws XMLStreamException
108     {
109         out.flush();
110     } //-- void flush()
111 
112     /**
113      * Method getNamespaceContext.
114      * 
115      * @return NamespaceContext
116      */
117     public NamespaceContext getNamespaceContext()
118     {
119         return out.getNamespaceContext();
120     } //-- NamespaceContext getNamespaceContext()
121 
122     /**
123      * Method getPrefix.
124      * 
125      * @param uri a uri object.
126      * @throws XMLStreamException XMLStreamException if any.
127      * @return String
128      */
129     public String getPrefix( String uri )
130         throws XMLStreamException
131     {
132         return out.getPrefix( uri );
133     } //-- String getPrefix( String )
134 
135     /**
136      * Method getProperty.
137      * 
138      * @param name a name object.
139      * @throws IllegalArgumentException IllegalArgumentException if
140      * any.
141      * @return Object
142      */
143     public Object getProperty( String name )
144         throws IllegalArgumentException
145     {
146         return out.getProperty( name );
147     } //-- Object getProperty( String )
148 
149     /**
150      * Method setDefaultNamespace.
151      * 
152      * @param uri a uri object.
153      * @throws XMLStreamException XMLStreamException if any.
154      */
155     public void setDefaultNamespace( String uri )
156         throws XMLStreamException
157     {
158         out.setDefaultNamespace( uri );
159     } //-- void setDefaultNamespace( String )
160 
161     /**
162      * Method setNamespaceContext.
163      * 
164      * @param context a context object.
165      * @throws XMLStreamException XMLStreamException if any.
166      */
167     public void setNamespaceContext( NamespaceContext context )
168         throws XMLStreamException
169     {
170         out.setNamespaceContext( context );
171     } //-- void setNamespaceContext( NamespaceContext )
172 
173     /**
174      * Method setPrefix.
175      * 
176      * @param prefix a prefix object.
177      * @param uri a uri object.
178      * @throws XMLStreamException XMLStreamException if any.
179      */
180     public void setPrefix( String prefix, String uri )
181         throws XMLStreamException
182     {
183         out.setPrefix( prefix, uri );
184     } //-- void setPrefix( String, String )
185 
186     /**
187      * Method writeAttribute.
188      * 
189      * @param localName a localName object.
190      * @param value a value object.
191      * @throws XMLStreamException XMLStreamException if any.
192      */
193     public void writeAttribute( String localName, String value )
194         throws XMLStreamException
195     {
196         out.writeAttribute( localName, value );
197     } //-- void writeAttribute( String, String )
198 
199     /**
200      * Method writeAttribute.
201      * 
202      * @param namespaceURI a namespaceURI object.
203      * @param value a value object.
204      * @param localName a localName object.
205      * @throws XMLStreamException XMLStreamException if any.
206      */
207     public void writeAttribute( String namespaceURI, String localName, String value )
208         throws XMLStreamException
209     {
210         out.writeAttribute( namespaceURI, localName, value );
211     } //-- void writeAttribute( String, String, String )
212 
213     /**
214      * Method writeAttribute.
215      * 
216      * @param prefix a prefix object.
217      * @param value a value object.
218      * @param localName a localName object.
219      * @param namespaceURI a namespaceURI object.
220      * @throws XMLStreamException XMLStreamException if any.
221      */
222     public void writeAttribute( String prefix, String namespaceURI, String localName, String value )
223         throws XMLStreamException
224     {
225         out.writeAttribute( prefix, namespaceURI, localName, value );
226     } //-- void writeAttribute( String, String, String, String )
227 
228     /**
229      * Method writeCData.
230      * 
231      * @param data a data object.
232      * @throws XMLStreamException XMLStreamException if any.
233      */
234     public void writeCData( String data )
235         throws XMLStreamException
236     {
237         out.writeCData( data );
238         afterData();
239     } //-- void writeCData( String )
240 
241     /**
242      * Method writeCharacters.
243      * 
244      * @param text a text object.
245      * @throws XMLStreamException XMLStreamException if any.
246      */
247     public void writeCharacters( String text )
248         throws XMLStreamException
249     {
250         out.writeCharacters( text );
251         afterData();
252     } //-- void writeCharacters( String )
253 
254     /**
255      * Method writeCharacters.
256      * 
257      * @param text a text object.
258      * @param len a len object.
259      * @param start a start object.
260      * @throws XMLStreamException XMLStreamException if any.
261      */
262     public void writeCharacters( char[] text, int start, int len )
263         throws XMLStreamException
264     {
265         out.writeCharacters( text, start, len );
266         afterData();
267     } //-- void writeCharacters( char[], int, int )
268 
269     /**
270      * Method writeComment.
271      * 
272      * @param data a data object.
273      * @throws XMLStreamException XMLStreamException if any.
274      */
275     public void writeComment( String data )
276         throws XMLStreamException
277     {
278         beforeMarkup();
279         out.writeComment( data );
280         afterMarkup();
281     } //-- void writeComment( String )
282 
283     /**
284      * Method writeDTD.
285      * 
286      * @param dtd a dtd object.
287      * @throws XMLStreamException XMLStreamException if any.
288      */
289     public void writeDTD( String dtd )
290         throws XMLStreamException
291     {
292         beforeMarkup();
293         out.writeDTD( dtd );
294         afterMarkup();
295     } //-- void writeDTD( String )
296 
297     /**
298      * Method writeDefaultNamespace.
299      * 
300      * @param namespaceURI a namespaceURI object.
301      * @throws XMLStreamException XMLStreamException if any.
302      */
303     public void writeDefaultNamespace( String namespaceURI )
304         throws XMLStreamException
305     {
306         out.writeDefaultNamespace( namespaceURI );
307     } //-- void writeDefaultNamespace( String )
308 
309     /**
310      * Method writeEmptyElement.
311      * 
312      * @param localName a localName object.
313      * @throws XMLStreamException XMLStreamException if any.
314      */
315     public void writeEmptyElement( String localName )
316         throws XMLStreamException
317     {
318         beforeMarkup();
319         out.writeEmptyElement( localName );
320         afterMarkup();
321     } //-- void writeEmptyElement( String )
322 
323     /**
324      * Method writeEmptyElement.
325      * 
326      * @param namespaceURI a namespaceURI object.
327      * @param localName a localName object.
328      * @throws XMLStreamException XMLStreamException if any.
329      */
330     public void writeEmptyElement( String namespaceURI, String localName )
331         throws XMLStreamException
332     {
333         beforeMarkup();
334         out.writeEmptyElement( namespaceURI, localName );
335         afterMarkup();
336     } //-- void writeEmptyElement( String, String )
337 
338     /**
339      * Method writeEmptyElement.
340      * 
341      * @param prefix a prefix object.
342      * @param localName a localName object.
343      * @param namespaceURI a namespaceURI object.
344      * @throws XMLStreamException XMLStreamException if any.
345      */
346     public void writeEmptyElement( String prefix, String namespaceURI, String localName )
347         throws XMLStreamException
348     {
349         beforeMarkup();
350         out.writeEmptyElement( prefix, namespaceURI, localName );
351         afterMarkup();
352     } //-- void writeEmptyElement( String, String, String )
353 
354     /**
355      * Method writeEndDocument.
356      * 
357      * @throws XMLStreamException XMLStreamException if any.
358      */
359     public void writeEndDocument()
360         throws XMLStreamException
361     {
362         out.writeEndDocument(  );
363     } //-- void writeEndDocument()
364 
365     /**
366      * Method writeEndElement.
367      * 
368      * @throws XMLStreamException XMLStreamException if any.
369      */
370     public void writeEndElement()
371         throws XMLStreamException
372     {
373         beforeEndElement();
374         out.writeEndElement(  );
375         afterEndElement();
376     } //-- void writeEndElement()
377 
378     /**
379      * Method writeEntityRef.
380      * 
381      * @param name a name object.
382      * @throws XMLStreamException XMLStreamException if any.
383      */
384     public void writeEntityRef( String name )
385         throws XMLStreamException
386     {
387         out.writeEntityRef( name );
388         afterData();
389     } //-- void writeEntityRef( String )
390 
391     /**
392      * Method writeNamespace.
393      * 
394      * @param prefix a prefix object.
395      * @param namespaceURI a namespaceURI object.
396      * @throws XMLStreamException XMLStreamException if any.
397      */
398     public void writeNamespace( String prefix, String namespaceURI )
399         throws XMLStreamException
400     {
401         out.writeNamespace( prefix, namespaceURI );
402     } //-- void writeNamespace( String, String )
403 
404     /**
405      * Method writeProcessingInstruction.
406      * 
407      * @param target a target object.
408      * @throws XMLStreamException XMLStreamException if any.
409      */
410     public void writeProcessingInstruction( String target )
411         throws XMLStreamException
412     {
413         beforeMarkup();
414         out.writeProcessingInstruction( target );
415         afterMarkup();
416     } //-- void writeProcessingInstruction( String )
417 
418     /**
419      * Method writeProcessingInstruction.
420      * 
421      * @param target a target object.
422      * @param data a data object.
423      * @throws XMLStreamException XMLStreamException if any.
424      */
425     public void writeProcessingInstruction( String target, String data )
426         throws XMLStreamException
427     {
428         beforeMarkup();
429         out.writeProcessingInstruction( target, data );
430         afterMarkup();
431     } //-- void writeProcessingInstruction( String, String )
432 
433     /**
434      * Method writeStartDocument.
435      * 
436      * @throws XMLStreamException XMLStreamException if any.
437      */
438     public void writeStartDocument()
439         throws XMLStreamException
440     {
441         beforeMarkup();
442         out.writeStartDocument(  );
443         afterMarkup();
444     } //-- void writeStartDocument()
445 
446     /**
447      * Method writeStartDocument.
448      * 
449      * @param version a version object.
450      * @throws XMLStreamException XMLStreamException if any.
451      */
452     public void writeStartDocument( String version )
453         throws XMLStreamException
454     {
455         beforeMarkup();
456         out.writeStartDocument( version );
457         afterMarkup();
458     } //-- void writeStartDocument( String )
459 
460     /**
461      * Method writeStartDocument.
462      * 
463      * @param encoding a encoding object.
464      * @param version a version object.
465      * @throws XMLStreamException XMLStreamException if any.
466      */
467     public void writeStartDocument( String encoding, String version )
468         throws XMLStreamException
469     {
470         beforeMarkup();
471         out.writeStartDocument( encoding, version );
472         afterMarkup();
473     } //-- void writeStartDocument( String, String )
474 
475     /**
476      * Method writeStartElement.
477      * 
478      * @param localName a localName object.
479      * @throws XMLStreamException XMLStreamException if any.
480      */
481     public void writeStartElement( String localName )
482         throws XMLStreamException
483     {
484         beforeStartElement();
485         out.writeStartElement( localName );
486         afterStartElement();
487     } //-- void writeStartElement( String )
488 
489     /**
490      * Method writeStartElement.
491      * 
492      * @param namespaceURI a namespaceURI object.
493      * @param localName a localName object.
494      * @throws XMLStreamException XMLStreamException if any.
495      */
496     public void writeStartElement( String namespaceURI, String localName )
497         throws XMLStreamException
498     {
499         beforeStartElement();
500         out.writeStartElement( namespaceURI, localName );
501         afterStartElement();
502     } //-- void writeStartElement( String, String )
503 
504     /**
505      * Method writeStartElement.
506      * 
507      * @param prefix a prefix object.
508      * @param namespaceURI a namespaceURI object.
509      * @param localName a localName object.
510      * @throws XMLStreamException XMLStreamException if any.
511      */
512     public void writeStartElement( String prefix, String localName, String namespaceURI )
513         throws XMLStreamException
514     {
515         beforeStartElement();
516         out.writeStartElement( prefix, localName, namespaceURI );
517         afterStartElement();
518     } //-- void writeStartElement( String, String, String )
519 
520     /**
521      * Method afterData.
522      */
523     private void afterData()
524     {
525         states[depth] |= ELEMENT_HAS_DATA;
526     } //-- void afterData()
527 
528     /**
529      * Method afterEndElement.
530      */
531     private void afterEndElement()
532     {
533         if ( depth > 0 )
534         {
535             depth--;
536             if ( depth <= 0 )
537             {
538                 newLine( 0 );
539             }
540         }
541     } //-- void afterEndElement()
542 
543     /**
544      * Method afterMarkup.
545      */
546     private void afterMarkup()
547     {
548         states[depth] |= ELEMENT_HAS_MARKUP;
549     } //-- void afterMarkup()
550 
551     /**
552      * Method afterStartElement.
553      */
554     private void afterStartElement()
555     {
556         afterMarkup();
557         depth++;
558     } //-- void afterStartElement()
559 
560     /**
561      * Method beforeEndElement.
562      */
563     private void beforeEndElement()
564     {
565         if ( depth > 0 && states[depth] == ELEMENT_HAS_MARKUP )
566         {
567             newLine( depth - 1 );
568         }
569     } //-- void beforeEndElement()
570 
571     /**
572      * Method beforeMarkup.
573      */
574     private void beforeMarkup()
575     {
576         int state = states[depth];
577         if ( ( state & ELEMENT_HAS_DATA ) == 0 && ( depth > 0 || state != 0 ) )
578         {
579             newLine( depth );
580             if ( depth > 0 && indent.length() > 0 )
581             {
582                 afterMarkup();
583             }
584         }
585     } //-- void beforeMarkup()
586 
587     /**
588      * Method beforeStartElement.
589      */
590     private void beforeStartElement()
591     {
592         beforeMarkup();
593         if ( states.length <= depth + 1 )
594         {
595             byte[] tmp = new byte[states.length * 2];
596             System.arraycopy( states, 0, tmp, 0, states.length );
597             states = tmp;
598         }
599         states[depth + 1] = 0;
600     } //-- void beforeStartElement()
601 
602     /**
603      * Method getLineSeparator.
604      * 
605      * @return String
606      */
607     public String getLineSeparator()
608     {
609         try
610         {
611             return System.getProperty( "line.separator", NEW_LINE );
612         }
613         catch ( Exception e )
614         {
615             return NEW_LINE;
616         }
617     } //-- String getLineSeparator()
618 
619     /**
620      * Method newLine.
621      * 
622      * @param depth a depth object.
623      */
624     private void newLine( int depth )
625     {
626         try
627         {
628             out.writeCharacters( newLine );
629             int prefixLength = depth * indent.length();
630             while ( linePrefix.length < prefixLength )
631             {
632                 char[] tmp = new char[linePrefix.length * 2];
633                 System.arraycopy( linePrefix, 0, tmp, 0, linePrefix.length );
634                 System.arraycopy( linePrefix, 0, tmp, linePrefix.length, linePrefix.length );
635                 linePrefix = tmp;
636             }
637             out.writeCharacters( linePrefix, 0, prefixLength );
638         }
639         catch ( Exception e )
640         {
641         }
642     } //-- void newLine( int )
643 
644     /**
645      * Method setNewLine.
646      * 
647      * @param newLine a newLine object.
648      */
649     public void setNewLine( String newLine )
650     {
651         this.newLine = newLine;
652     } //-- void setNewLine( String )
653 
654 }