View Javadoc

1   package org.apache.maven.plugins.scm.release;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import org.dom4j.Element;
22  import org.dom4j.Node;
23  
24  /**
25   *
26   *
27   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
28   *
29   * @version $Id: VersionTransformer.java 521198 2007-03-22 10:39:43Z ltheussl $
30   */
31  public class VersionTransformer
32      extends AbstractPomTransformer
33  {
34      private String version;
35      private String tag;
36  
37      public VersionTransformer( String version, String tag )
38      {
39          this.version = version;
40          this.tag = tag;
41      }
42  
43      // -------------------------------------------------------------------------
44      // Accessors
45      // -------------------------------------------------------------------------
46  
47      public String selectNodesXPathExpression()
48      {
49          return "/project";
50      }
51  
52      public void transformNode( Node node )
53          throws Exception
54      {
55          Element project = ( Element ) node;
56  
57          Node currentVersion = node.selectSingleNode( "currentVersion" );
58          if ( currentVersion != null )
59          {
60              currentVersion.setText( version );
61          }
62          else
63          {
64              project.addElement( "currentVersion" ).addText( version );
65          }
66  
67          Element version = ( Element ) node.selectSingleNode( "versions/version[tag='" + tag + "']" );
68          if ( version != null )
69          {
70              // tag exists - overwrite
71              Node id = version.selectSingleNode( "id" );
72              if ( id != null )
73              {
74                  id.setText( this.version );
75              }
76              else
77              {
78                  version.addElement( "id" ).addText( this.version );
79              }
80              Node name = version.selectSingleNode( "name" );
81              if ( name != null )
82              {
83                  name.setText( this.version );
84              }
85              else
86              {
87                  version.addElement( "name" ).addText( this.version );
88              }
89          }
90          else
91          {
92              // tag doesn't exist - add
93              Element versions = ( Element ) project.selectSingleNode( "versions" );
94              if ( versions == null )
95              {
96                  versions = project.addElement( "versions" );
97              }
98              Element v = versions.addElement( "version" );
99              v.addElement( "id" ).addText( this.version );
100             v.addElement( "name" ).addText( this.version );
101             v.addElement( "tag" ).addText( tag );
102         }
103     }
104 
105     public Node getTransformedNode( Node node )
106         throws Exception
107     {
108         throw new UnsupportedOperationException( "getTransformedNode not implemented" );
109     }
110 
111     public boolean transformRequired()
112     {
113         Node node = ( Node ) getSelectedNodes().get( 0 );
114         if ( node == null )
115         {
116             return true;
117         }
118 
119         Node currentVersion = node.selectSingleNode( "currentVersion" );
120         if ( currentVersion == null || !currentVersion.getText().equals( version ))
121         {
122             return true;
123         }
124 
125         
126         Node v = node.selectSingleNode( "versions/version[tag='" + tag + "' and id='" + version + "' and name='" + version + "']" );
127         return ( v == null );
128     }
129 }
130