View Javadoc

1   package org.apache.maven.release;
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 org.dom4j.Document;
21  import org.dom4j.io.SAXReader;
22  import org.jaxen.XPath;
23  import org.jaxen.dom4j.Dom4jXPath;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  /**
30   * This is a simple POM manipulator that doesn't take into account any sort of
31   * POM inheritance. When the interpolation and inheritance is cleaned up in the
32   * Maven we will be able to serialize a POM directly with greater ease. But we
33   * may stick with this Jaxen solution anyway because it's so easy. Just turn the
34   * POM in memory into a Dom4j document and perform the same operation as we are
35   * here.
36   */
37  public class PomNodeSelector
38  {
39      /**
40       * POM document
41       */
42      private File pom;
43  
44      /**
45       * Xpath expression.
46       */
47      private String xpathExpression;
48  
49      /**
50       * Snapshot dependencies found.
51       */
52      private List nodes;
53  
54      /**
55       * Constructor.
56       */
57      public PomNodeSelector()
58      {
59          nodes = new ArrayList();
60      }
61  
62      // -------------------------------------------------------------------------
63      // Accessors
64      // -------------------------------------------------------------------------
65  
66      public void setXpathExpression( String xpathExpression )
67      {
68          this.xpathExpression = xpathExpression;
69      }
70  
71      public String getXpathExpression()
72      {
73          return xpathExpression;
74      }
75  
76      /**
77       * Sets the nodes attribute of the PomManipulator object
78       */
79      public void setNodes( List nodes )
80      {
81          this.nodes = nodes;
82      }
83  
84      /**
85       * Gets the nodes attribute of the PomManipulator object
86       */
87      public List getNodes()
88      {
89          return nodes;
90      }
91  
92      public void setPom( File pom )
93      {
94          this.pom = pom;
95      }
96  
97      public File getPom()
98      {
99          return pom;
100     }
101 
102     /**
103      * Update the snapshot version identifiers with actual timestamp versions
104      * and write out the POM in its updated form.
105      *
106      * @throws Exception
107      */
108     public void execute()
109         throws Exception
110     {
111         SAXReader reader = new SAXReader();
112         Document doc = reader.read( getPom() );
113 
114         // The selecting nodes with the xpath expression will give us a list
115         // of dependencies elements where the version element is equal to 'SNAPSHOT'.
116         // So we can get any information we need, and alter anything we need to before writing
117         // the dom4j document back out.
118         XPath xpath = new Dom4jXPath( getXpathExpression() );
119         setNodes( xpath.selectNodes( doc ) );
120     }
121 }