View Javadoc

1   package org.apache.maven;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  import java.io.FileWriter;
21  import java.io.File;
22  
23  import org.dom4j.Document;
24  import org.dom4j.io.OutputFormat;
25  import org.dom4j.io.XMLWriter;
26  import org.dom4j.io.SAXReader;
27  
28  /**
29   * A little XML document formatter. Used to format a POM in XML format
30   * after it has been transformed.
31   *
32   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33   * @author <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
34   * @version $Id: XmlPomFormatter.java 170200 2005-05-15 06:24:19Z brett $
35   */
36  public class XmlPomFormatter
37  {
38      /** Input xml */
39      private File input;
40      
41      /** Output xml */
42      private File output;
43      
44      /**
45       * Set input file.
46       * @param input the {@link File} to read the unformatted project object
47       *      model from
48       */
49      public void setInput(File input)
50      {
51          this.input = input;
52      }        
53      
54      /**
55       * Get input file.
56       * @return the file the unformatted project object model will read from
57       */
58      public File getInput()
59      {
60          return input;
61      }
62  
63      /**
64       * Set output file.
65       * @param output the {@link File} to write the formatted project object
66       *      object model to
67       */
68      public void setOutput(File output)
69      {
70          this.output = output;
71      }        
72      
73      /**
74       * Get output file.
75       * @return the file the formatted project object model will be written to
76       */
77      public File getOutput()
78      {
79          return output;
80      }
81  
82  
83      /**
84       * Format the project descriptor.
85       * @throws Exception when any error occurs 
86       */
87      public void execute() throws Exception
88      {
89          Document document = createDocument();
90          OutputFormat format = new OutputFormat();
91          format.setIndentSize(2);
92          format.setNewlines(true);
93          format.setTrimText(true);
94  
95          FileWriter out = new FileWriter(output);
96          XMLWriter writer = new XMLWriter(out, format);
97          writer.write(document);
98          out.close();
99      }
100 
101     /**
102      * Description of the Method
103      * @return a {@link Document} from parsing the {@link #input input file}
104      * @throws Exception when any error occurs 
105      */
106     protected Document createDocument() throws Exception
107     {
108         SAXReader reader = new SAXReader();
109         return reader.read(input);
110     }
111 
112     /** Left over testing method - to be removed
113      * @param args command line arguments
114      * @throws Exception when any error occurs 
115      */
116     public static void main(String[] args) throws Exception
117     {
118         System.out.println(args[0]);
119         System.out.println(args[1]);
120         
121         XmlPomFormatter formatter = new XmlPomFormatter();
122         formatter.setInput(new File(args[0]));
123         formatter.setOutput(new File(args[1]));
124         formatter.execute();
125     }
126 }