001// =================== DO NOT EDIT THIS FILE ====================
002// Generated by Modello 2.3.0,
003// any modifications will be overwritten.
004// ==============================================================
005
006package org.codehaus.modello.test.features.io.stax;
007
008  //---------------------------------/
009 //- Imported classes and packages -/
010//---------------------------------/
011
012import javax.xml.namespace.NamespaceContext;
013import javax.xml.stream.XMLStreamException;
014import javax.xml.stream.XMLStreamWriter;
015
016/**
017 * Class IndentingXMLStreamWriter.
018 * 
019 * @version $Revision$ $Date$
020 */
021@SuppressWarnings( "all" )
022class IndentingXMLStreamWriter
023    implements XMLStreamWriter
024{
025
026      //--------------------------/
027     //- Class/Member Variables -/
028    //--------------------------/
029
030    /**
031     * Field out.
032     */
033    private XMLStreamWriter out;
034
035    /**
036     * Field NEW_LINE.
037     */
038    private static final String NEW_LINE = "\n";
039
040    /**
041     * Field newLine.
042     */
043    private String newLine = NEW_LINE;
044
045    /**
046     * Field indent.
047     */
048    private String indent = "  ";
049
050    /**
051     * Field linePrefix.
052     */
053    private char[] linePrefix = "                        ".toCharArray();
054
055    /**
056     * Field depth.
057     */
058    private int depth;
059
060    /**
061     * Field states.
062     */
063    private byte[] states = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
064
065    /**
066     * Field ELEMENT_HAS_DATA.
067     */
068    private static final int ELEMENT_HAS_DATA = 0x1;
069
070    /**
071     * Field ELEMENT_HAS_MARKUP.
072     */
073    private static final int ELEMENT_HAS_MARKUP = 0x2;
074
075
076      //----------------/
077     //- Constructors -/
078    //----------------/
079
080    public IndentingXMLStreamWriter(XMLStreamWriter out)
081    {
082        this.out = out;
083    } //-- org.codehaus.modello.test.features.io.stax.IndentingXMLStreamWriter(XMLStreamWriter)
084
085
086      //-----------/
087     //- Methods -/
088    //-----------/
089
090    /**
091     * Method close.
092     * 
093     * @throws XMLStreamException XMLStreamException if any.
094     */
095    public void close()
096        throws XMLStreamException
097    {
098        out.close();
099    } //-- 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}