001/*
002 =================== DO NOT EDIT THIS FILE ====================
003 Generated by Modello 1.9.2-SNAPSHOT,
004 any modifications will be overwritten.
005 ==============================================================
006 */
007
008package org.codehaus.modello.test.features.io.stax;
009
010  //---------------------------------/
011 //- Imported classes and packages -/
012//---------------------------------/
013
014import javax.xml.namespace.NamespaceContext;
015import javax.xml.stream.XMLStreamException;
016import javax.xml.stream.XMLStreamWriter;
017
018/**
019 * Class IndentingXMLStreamWriter.
020 * 
021 * @version $Revision$ $Date$
022 */
023@SuppressWarnings( "all" )
024class IndentingXMLStreamWriter
025    implements XMLStreamWriter
026{
027
028      //--------------------------/
029     //- Class/Member Variables -/
030    //--------------------------/
031
032    /**
033     * Field out.
034     */
035    private XMLStreamWriter out;
036
037    /**
038     * Field NEW_LINE.
039     */
040    private static final String NEW_LINE = "\n";
041
042    /**
043     * Field newLine.
044     */
045    private String newLine = NEW_LINE;
046
047    /**
048     * Field indent.
049     */
050    private String indent = "  ";
051
052    /**
053     * Field linePrefix.
054     */
055    private char[] linePrefix = "                        ".toCharArray();
056
057    /**
058     * Field depth.
059     */
060    private int depth;
061
062    /**
063     * Field states.
064     */
065    private byte[] states = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
066
067    /**
068     * Field ELEMENT_HAS_DATA.
069     */
070    private static final int ELEMENT_HAS_DATA = 0x1;
071
072    /**
073     * Field ELEMENT_HAS_MARKUP.
074     */
075    private static final int ELEMENT_HAS_MARKUP = 0x2;
076
077
078      //----------------/
079     //- Constructors -/
080    //----------------/
081
082    public IndentingXMLStreamWriter(XMLStreamWriter out)
083    {
084        this.out = out;
085    } //-- org.codehaus.modello.test.features.io.stax.IndentingXMLStreamWriter(XMLStreamWriter)
086
087
088      //-----------/
089     //- Methods -/
090    //-----------/
091
092    /**
093     * Method close.
094     * 
095     * @throws XMLStreamException
096     */
097    public void close()
098        throws XMLStreamException
099    {
100        out.close();
101    } //-- void close()
102
103    /**
104     * Method flush.
105     * 
106     * @throws XMLStreamException
107     */
108    public void flush()
109        throws XMLStreamException
110    {
111        out.flush();
112    } //-- void flush()
113
114    /**
115     * Method getNamespaceContext.
116     * 
117     * @return NamespaceContext
118     */
119    public NamespaceContext getNamespaceContext()
120    {
121        return out.getNamespaceContext();
122    } //-- NamespaceContext getNamespaceContext()
123
124    /**
125     * Method getPrefix.
126     * 
127     * @param uri
128     * @throws XMLStreamException
129     * @return String
130     */
131    public String getPrefix( String uri )
132        throws XMLStreamException
133    {
134        return out.getPrefix( uri );
135    } //-- String getPrefix( String )
136
137    /**
138     * Method getProperty.
139     * 
140     * @param name
141     * @throws IllegalArgumentException
142     * @return Object
143     */
144    public Object getProperty( String name )
145        throws IllegalArgumentException
146    {
147        return out.getProperty( name );
148    } //-- Object getProperty( String )
149
150    /**
151     * Method setDefaultNamespace.
152     * 
153     * @param uri
154     * @throws XMLStreamException
155     */
156    public void setDefaultNamespace( String uri )
157        throws XMLStreamException
158    {
159        out.setDefaultNamespace( uri );
160    } //-- void setDefaultNamespace( String )
161
162    /**
163     * Method setNamespaceContext.
164     * 
165     * @param context
166     * @throws XMLStreamException
167     */
168    public void setNamespaceContext( NamespaceContext context )
169        throws XMLStreamException
170    {
171        out.setNamespaceContext( context );
172    } //-- void setNamespaceContext( NamespaceContext )
173
174    /**
175     * Method setPrefix.
176     * 
177     * @param prefix
178     * @param uri
179     * @throws XMLStreamException
180     */
181    public void setPrefix( String prefix, String uri )
182        throws XMLStreamException
183    {
184        out.setPrefix( prefix, uri );
185    } //-- void setPrefix( String, String )
186
187    /**
188     * Method writeAttribute.
189     * 
190     * @param localName
191     * @param value
192     * @throws XMLStreamException
193     */
194    public void writeAttribute( String localName, String value )
195        throws XMLStreamException
196    {
197        out.writeAttribute( localName, value );
198    } //-- void writeAttribute( String, String )
199
200    /**
201     * Method writeAttribute.
202     * 
203     * @param namespaceURI
204     * @param value
205     * @param localName
206     * @throws XMLStreamException
207     */
208    public void writeAttribute( String namespaceURI, String localName, String value )
209        throws XMLStreamException
210    {
211        out.writeAttribute( namespaceURI, localName, value );
212    } //-- void writeAttribute( String, String, String )
213
214    /**
215     * Method writeAttribute.
216     * 
217     * @param prefix
218     * @param value
219     * @param localName
220     * @param namespaceURI
221     * @throws XMLStreamException
222     */
223    public void writeAttribute( String prefix, String namespaceURI, String localName, String value )
224        throws XMLStreamException
225    {
226        out.writeAttribute( prefix, namespaceURI, localName, value );
227    } //-- void writeAttribute( String, String, String, String )
228
229    /**
230     * Method writeCData.
231     * 
232     * @param data
233     * @throws XMLStreamException
234     */
235    public void writeCData( String data )
236        throws XMLStreamException
237    {
238        out.writeCData( data );
239        afterData();
240    } //-- void writeCData( String )
241
242    /**
243     * Method writeCharacters.
244     * 
245     * @param text
246     * @throws XMLStreamException
247     */
248    public void writeCharacters( String text )
249        throws XMLStreamException
250    {
251        out.writeCharacters( text );
252        afterData();
253    } //-- void writeCharacters( String )
254
255    /**
256     * Method writeCharacters.
257     * 
258     * @param text
259     * @param len
260     * @param start
261     * @throws XMLStreamException
262     */
263    public void writeCharacters( char[] text, int start, int len )
264        throws XMLStreamException
265    {
266        out.writeCharacters( text, start, len );
267        afterData();
268    } //-- void writeCharacters( char[], int, int )
269
270    /**
271     * Method writeComment.
272     * 
273     * @param data
274     * @throws XMLStreamException
275     */
276    public void writeComment( String data )
277        throws XMLStreamException
278    {
279        beforeMarkup();
280        out.writeComment( data );
281        afterMarkup();
282    } //-- void writeComment( String )
283
284    /**
285     * Method writeDTD.
286     * 
287     * @param dtd
288     * @throws XMLStreamException
289     */
290    public void writeDTD( String dtd )
291        throws XMLStreamException
292    {
293        beforeMarkup();
294        out.writeDTD( dtd );
295        afterMarkup();
296    } //-- void writeDTD( String )
297
298    /**
299     * Method writeDefaultNamespace.
300     * 
301     * @param namespaceURI
302     * @throws XMLStreamException
303     */
304    public void writeDefaultNamespace( String namespaceURI )
305        throws XMLStreamException
306    {
307        out.writeDefaultNamespace( namespaceURI );
308    } //-- void writeDefaultNamespace( String )
309
310    /**
311     * Method writeEmptyElement.
312     * 
313     * @param localName
314     * @throws XMLStreamException
315     */
316    public void writeEmptyElement( String localName )
317        throws XMLStreamException
318    {
319        beforeMarkup();
320        out.writeEmptyElement( localName );
321        afterMarkup();
322    } //-- void writeEmptyElement( String )
323
324    /**
325     * Method writeEmptyElement.
326     * 
327     * @param namespaceURI
328     * @param localName
329     * @throws XMLStreamException
330     */
331    public void writeEmptyElement( String namespaceURI, String localName )
332        throws XMLStreamException
333    {
334        beforeMarkup();
335        out.writeEmptyElement( namespaceURI, localName );
336        afterMarkup();
337    } //-- void writeEmptyElement( String, String )
338
339    /**
340     * Method writeEmptyElement.
341     * 
342     * @param prefix
343     * @param localName
344     * @param namespaceURI
345     * @throws XMLStreamException
346     */
347    public void writeEmptyElement( String prefix, String namespaceURI, String localName )
348        throws XMLStreamException
349    {
350        beforeMarkup();
351        out.writeEmptyElement( prefix, namespaceURI, localName );
352        afterMarkup();
353    } //-- void writeEmptyElement( String, String, String )
354
355    /**
356     * Method writeEndDocument.
357     * 
358     * @throws XMLStreamException
359     */
360    public void writeEndDocument()
361        throws XMLStreamException
362    {
363        out.writeEndDocument(  );
364    } //-- void writeEndDocument()
365
366    /**
367     * Method writeEndElement.
368     * 
369     * @throws XMLStreamException
370     */
371    public void writeEndElement()
372        throws XMLStreamException
373    {
374        beforeEndElement();
375        out.writeEndElement(  );
376        afterEndElement();
377    } //-- void writeEndElement()
378
379    /**
380     * Method writeEntityRef.
381     * 
382     * @param name
383     * @throws XMLStreamException
384     */
385    public void writeEntityRef( String name )
386        throws XMLStreamException
387    {
388        out.writeEntityRef( name );
389        afterData();
390    } //-- void writeEntityRef( String )
391
392    /**
393     * Method writeNamespace.
394     * 
395     * @param prefix
396     * @param namespaceURI
397     * @throws XMLStreamException
398     */
399    public void writeNamespace( String prefix, String namespaceURI )
400        throws XMLStreamException
401    {
402        out.writeNamespace( prefix, namespaceURI );
403    } //-- void writeNamespace( String, String )
404
405    /**
406     * Method writeProcessingInstruction.
407     * 
408     * @param target
409     * @throws XMLStreamException
410     */
411    public void writeProcessingInstruction( String target )
412        throws XMLStreamException
413    {
414        beforeMarkup();
415        out.writeProcessingInstruction( target );
416        afterMarkup();
417    } //-- void writeProcessingInstruction( String )
418
419    /**
420     * Method writeProcessingInstruction.
421     * 
422     * @param target
423     * @param data
424     * @throws XMLStreamException
425     */
426    public void writeProcessingInstruction( String target, String data )
427        throws XMLStreamException
428    {
429        beforeMarkup();
430        out.writeProcessingInstruction( target, data );
431        afterMarkup();
432    } //-- void writeProcessingInstruction( String, String )
433
434    /**
435     * Method writeStartDocument.
436     * 
437     * @throws XMLStreamException
438     */
439    public void writeStartDocument()
440        throws XMLStreamException
441    {
442        beforeMarkup();
443        out.writeStartDocument(  );
444        afterMarkup();
445    } //-- void writeStartDocument()
446
447    /**
448     * Method writeStartDocument.
449     * 
450     * @param version
451     * @throws XMLStreamException
452     */
453    public void writeStartDocument( String version )
454        throws XMLStreamException
455    {
456        beforeMarkup();
457        out.writeStartDocument( version );
458        afterMarkup();
459    } //-- void writeStartDocument( String )
460
461    /**
462     * Method writeStartDocument.
463     * 
464     * @param encoding
465     * @param version
466     * @throws XMLStreamException
467     */
468    public void writeStartDocument( String encoding, String version )
469        throws XMLStreamException
470    {
471        beforeMarkup();
472        out.writeStartDocument( encoding, version );
473        afterMarkup();
474    } //-- void writeStartDocument( String, String )
475
476    /**
477     * Method writeStartElement.
478     * 
479     * @param localName
480     * @throws XMLStreamException
481     */
482    public void writeStartElement( String localName )
483        throws XMLStreamException
484    {
485        beforeStartElement();
486        out.writeStartElement( localName );
487        afterStartElement();
488    } //-- void writeStartElement( String )
489
490    /**
491     * Method writeStartElement.
492     * 
493     * @param namespaceURI
494     * @param localName
495     * @throws XMLStreamException
496     */
497    public void writeStartElement( String namespaceURI, String localName )
498        throws XMLStreamException
499    {
500        beforeStartElement();
501        out.writeStartElement( namespaceURI, localName );
502        afterStartElement();
503    } //-- void writeStartElement( String, String )
504
505    /**
506     * Method writeStartElement.
507     * 
508     * @param prefix
509     * @param namespaceURI
510     * @param localName
511     * @throws XMLStreamException
512     */
513    public void writeStartElement( String prefix, String localName, String namespaceURI )
514        throws XMLStreamException
515    {
516        beforeStartElement();
517        out.writeStartElement( prefix, localName, namespaceURI );
518        afterStartElement();
519    } //-- void writeStartElement( String, String, String )
520
521    /**
522     * Method afterData.
523     */
524    private void afterData()
525    {
526        states[depth] |= ELEMENT_HAS_DATA;
527    } //-- void afterData()
528
529    /**
530     * Method afterEndElement.
531     */
532    private void afterEndElement()
533    {
534        if ( depth > 0 )
535        {
536            depth--;
537            if ( depth <= 0 )
538            {
539                newLine( 0 );
540            }
541        }
542    } //-- void afterEndElement()
543
544    /**
545     * Method afterMarkup.
546     */
547    private void afterMarkup()
548    {
549        states[depth] |= ELEMENT_HAS_MARKUP;
550    } //-- void afterMarkup()
551
552    /**
553     * Method afterStartElement.
554     */
555    private void afterStartElement()
556    {
557        afterMarkup();
558        depth++;
559    } //-- void afterStartElement()
560
561    /**
562     * Method beforeEndElement.
563     */
564    private void beforeEndElement()
565    {
566        if ( depth > 0 && states[depth] == ELEMENT_HAS_MARKUP )
567        {
568            newLine( depth - 1 );
569        }
570    } //-- void beforeEndElement()
571
572    /**
573     * Method beforeMarkup.
574     */
575    private void beforeMarkup()
576    {
577        int state = states[depth];
578        if ( ( state & ELEMENT_HAS_DATA ) == 0 && ( depth > 0 || state != 0 ) )
579        {
580            newLine( depth );
581            if ( depth > 0 && indent.length() > 0 )
582            {
583                afterMarkup();
584            }
585        }
586    } //-- void beforeMarkup()
587
588    /**
589     * Method beforeStartElement.
590     */
591    private void beforeStartElement()
592    {
593        beforeMarkup();
594        if ( states.length <= depth + 1 )
595        {
596            byte[] tmp = new byte[states.length * 2];
597            System.arraycopy( states, 0, tmp, 0, states.length );
598            states = tmp;
599        }
600        states[depth + 1] = 0;
601    } //-- void beforeStartElement()
602
603    /**
604     * Method getLineSeparator.
605     * 
606     * @return String
607     */
608    public String getLineSeparator()
609    {
610        try
611        {
612            return System.getProperty( "line.separator", NEW_LINE );
613        }
614        catch ( Exception e )
615        {
616            return NEW_LINE;
617        }
618    } //-- String getLineSeparator()
619
620    /**
621     * Method newLine.
622     * 
623     * @param depth
624     */
625    private void newLine( int depth )
626    {
627        try
628        {
629            out.writeCharacters( newLine );
630            int prefixLength = depth * indent.length();
631            while ( linePrefix.length < prefixLength )
632            {
633                char[] tmp = new char[linePrefix.length * 2];
634                System.arraycopy( linePrefix, 0, tmp, 0, linePrefix.length );
635                System.arraycopy( linePrefix, 0, tmp, linePrefix.length, linePrefix.length );
636                linePrefix = tmp;
637            }
638            out.writeCharacters( linePrefix, 0, prefixLength );
639        }
640        catch ( Exception e )
641        {
642        }
643    } //-- void newLine( int )
644
645    /**
646     * Method setNewLine.
647     * 
648     * @param newLine
649     */
650    public void setNewLine( String newLine )
651    {
652        this.newLine = newLine;
653    } //-- void setNewLine( String )
654
655}