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.Element;
21  import org.dom4j.Node;
22  
23  /**
24   *
25   *
26   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
27   *
28   * @version $Id: VersionTransformer.java 170200 2005-05-15 06:24:19Z brett $
29   */
30  public class VersionTransformer
31      extends AbstractPomTransformer
32  {
33      private String version;
34      private String tag;
35  
36      public VersionTransformer( String version, String tag )
37      {
38          this.version = version;
39          this.tag = tag;
40      }
41  
42      // -------------------------------------------------------------------------
43      // Accessors
44      // -------------------------------------------------------------------------
45  
46      public String selectNodesXPathExpression()
47      {
48          return "/project";
49      }
50  
51      public void transformNode( Node node )
52          throws Exception
53      {
54          Element project = ( Element ) node;
55  
56          Node currentVersion = node.selectSingleNode( "currentVersion" );
57          if ( currentVersion != null )
58          {
59              currentVersion.setText( version );
60          }
61          else
62          {
63              project.addElement( "currentVersion" ).addText( version );
64          }
65  
66          Element version = ( Element ) node.selectSingleNode( "versions/version[tag='" + tag + "']" );
67          if ( version != null )
68          {
69              // tag exists - overwrite
70              Node id = version.selectSingleNode( "id" );
71              if ( id != null )
72              {
73                  id.setText( this.version );
74              }
75              else
76              {
77                  version.addElement( "id" ).addText( this.version );
78              }
79              Node name = version.selectSingleNode( "name" );
80              if ( name != null )
81              {
82                  name.setText( this.version );
83              }
84              else
85              {
86                  version.addElement( "name" ).addText( this.version );
87              }
88          }
89          else
90          {
91              // tag doesn't exist - add
92              Element versions = ( Element ) project.selectSingleNode( "versions" );
93              if ( versions == null )
94              {
95                  versions = project.addElement( "versions" );
96              }
97              Element v = versions.addElement( "version" );
98              v.addElement( "id" ).addText( this.version );
99              v.addElement( "name" ).addText( this.version );
100             v.addElement( "tag" ).addText( tag );
101         }
102     }
103 
104     public Node getTransformedNode( Node node )
105         throws Exception
106     {
107         throw new UnsupportedOperationException( "getTransformedNode not implemented" );
108     }
109 
110     public boolean transformRequired()
111     {
112         Node node = ( Node ) getSelectedNodes().get( 0 );
113         if ( node == null )
114         {
115             return true;
116         }
117 
118         Node currentVersion = node.selectSingleNode( "currentVersion" );
119         if ( currentVersion == null || !currentVersion.getText().equals( version ))
120         {
121             return true;
122         }
123 
124         
125         Node v = node.selectSingleNode( "versions/version[tag='" + tag + "' and id='" + version + "' and name='" + version + "']" );
126         return ( v == null );
127     }
128 }
129