This project has retired. For details please refer to its Attic page.
IndentingXMLStreamWriter xref
View Javadoc

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