View Javadoc

1   package org.apache.maven;
2   
3   /* ====================================================================
4    *   Copyright 2001-2005 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.*;
21  import javax.xml.parsers.*;
22  import org.w3c.dom.*;
23  import org.xml.sax.*;
24  
25  /**
26   * Some utility methods.
27   * @author <a href="mailto:ltheussl@apache.org">Lukas Theussl</a>
28   */
29  public class PomUtils
30  {
31      private File file;
32      private String parent;
33  
34      /** The parent.
35       * @return The parent
36       */
37      public String getParent()
38      {
39        return parent;
40      }
41  
42      /** The file.
43       * @return The file
44       */
45      public File getFile()
46      {
47        return file;
48      }
49  
50      /**
51       * Sets the file.
52       * @param newFile The file to set
53       */
54      public void setFile(File newFile)
55      {
56        this.file = newFile;
57      }
58  
59      /** The file.
60       * @return True if the file contains <extend>.
61       */
62      public boolean extend()
63      {
64          Document doc = parseXmlFile(file, false);
65          NodeList list = doc.getElementsByTagName("extend");
66          boolean extend = false;
67          if ( list.getLength() > 0 )
68          {
69              extend = true;
70              Element element = (Element) list.item(0);
71              try
72              {
73              Node firstChild = element.getFirstChild();
74              parent = firstChild.getNodeValue();
75              }  catch (DOMException e) {
76              }
77          }
78          return extend;
79      }
80  
81      private Document parseXmlFile(File filename, boolean validating)
82      {
83          try
84          {
85              DocumentBuilderFactory factory =
86                  DocumentBuilderFactory.newInstance();
87              factory.setValidating(validating);
88              Document doc = factory.newDocumentBuilder().parse(filename);
89              return doc;
90          } catch (SAXException e) {
91          } catch (ParserConfigurationException e) {
92          } catch (IOException e) {
93          }
94          return null;
95      }
96  }