View Javadoc

1   package org.apache.maven.tools.plugin.generator;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.PrintWriter;
23  import java.io.Writer;
24  
25  /**
26   * Copied from plexus-utils 1.3-SNAPSHOT as we can't upgrade it yet.
27   * This class can be removed when a newer version of plexus-utils is included with Maven
28   *
29   * @see org.codehaus.plexus.util.xml.PrettyPrintXMLWriter
30   */
31  public class PrettyPrintXMLWriter
32      extends org.codehaus.plexus.util.xml.PrettyPrintXMLWriter
33  {
34  
35      private static final String LS = System.getProperty( "line.separator" );
36  
37      private PrintWriter writer;
38  
39      private String lineIndenter;
40  
41      private int depth;
42  
43      public PrettyPrintXMLWriter( PrintWriter writer, String lineIndenter )
44      {
45          this( writer, lineIndenter, null, null );
46      }
47  
48      public PrettyPrintXMLWriter( Writer writer, String lineIndenter )
49      {
50          this( new PrintWriter( writer ), lineIndenter );
51      }
52  
53      public PrettyPrintXMLWriter( PrintWriter writer )
54      {
55          this( writer, null, null );
56      }
57  
58      public PrettyPrintXMLWriter( Writer writer )
59      {
60          this( new PrintWriter( writer ) );
61      }
62  
63      public PrettyPrintXMLWriter( PrintWriter writer, String lineIndenter, String encoding, String doctype )
64      {
65          super( writer, lineIndenter, encoding, doctype );
66  
67          setWriter( writer );
68  
69          setLineIndenter( lineIndenter );
70      }
71  
72      public PrettyPrintXMLWriter( Writer writer, String lineIndenter, String encoding, String doctype )
73      {
74          this( new PrintWriter( writer ), lineIndenter, encoding, doctype );
75      }
76  
77      public PrettyPrintXMLWriter( PrintWriter writer, String encoding, String doctype )
78      {
79          this( writer, "  ", encoding, doctype );
80      }
81  
82      public PrettyPrintXMLWriter( Writer writer, String encoding, String doctype )
83      {
84          this( new PrintWriter( writer ), encoding, doctype );
85      }
86  
87      /**
88       * Write a string to the underlying writer
89       *
90       * @param str
91       */
92      private void write( String str )
93      {
94          getWriter().write( str );
95      }
96  
97      /**
98       * Get the string used as line indenter
99       *
100      * @return the line indenter
101      */
102     protected String getLineIndenter()
103     {
104         return lineIndenter;
105     }
106 
107     /**
108      * Set the string used as line indenter
109      *
110      * @param lineIndenter
111      */
112     protected void setLineIndenter( String lineIndenter )
113     {
114         this.lineIndenter = lineIndenter;
115     }
116 
117     /**
118      * Write the end of line character (using system line separator)
119      * and start new line with indentation
120      */
121     protected void endOfLine()
122     {
123         write( LS );
124 
125         for ( int i = 0; i < getDepth(); i++ )
126         {
127             write( getLineIndenter() );
128         }
129     }
130 
131     /**
132      * Set the underlying writer
133      *
134      * @param writer
135      */
136     protected void setWriter( PrintWriter writer )
137     {
138         this.writer = writer;
139     }
140 
141     /**
142      * Get the underlying writer
143      *
144      * @return the underlying writer
145      */
146     protected PrintWriter getWriter()
147     {
148         return writer;
149     }
150 
151     /**
152      * Set the current depth in the xml indentation
153      *
154      * @param depth
155      */
156     protected void setDepth( int depth )
157     {
158         this.depth = depth;
159     }
160 
161     /**
162      * Get the current depth in the xml indentation
163      *
164      * @return
165      */
166     protected int getDepth()
167     {
168         return depth;
169     }
170 
171     public void startElement( String name )
172     {
173         super.startElement( name );
174 
175         setDepth( getDepth() + 1 );
176     }
177 
178     public void endElement()
179     {
180         super.endElement();
181 
182         setDepth( getDepth() - 1 );
183     }
184 
185 }