View Javadoc
1   package org.apache.maven.shared.release.transform.jdom;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import org.apache.maven.model.Build;
28  import org.apache.maven.model.Dependency;
29  import org.apache.maven.model.DependencyManagement;
30  import org.apache.maven.model.Model;
31  import org.apache.maven.model.Parent;
32  import org.apache.maven.model.Profile;
33  import org.apache.maven.model.Reporting;
34  import org.apache.maven.model.Scm;
35  import org.jdom.Document;
36  import org.jdom.Element;
37  import org.jdom.Text;
38  
39  /**
40   * JDom implementation of poms PROJECT element
41   *
42   * @author Robert Scholte
43   * @since 3.0
44   */
45  public class JDomModel extends Model
46  {
47      private final Element project;
48  
49      private final JDomModelBase modelBase;
50  
51      public JDomModel( Document document )
52      {
53          this( document.getRootElement() );
54      }
55  
56      public JDomModel( Element project )
57      {
58          this.project = project;
59          this.modelBase = new JDomModelBase( project );
60      }
61  
62      @Override
63      public Build getBuild()
64      {
65          return modelBase.getBuild();
66      }
67  
68      @Override
69      public List<Dependency> getDependencies()
70      {
71          return modelBase.getDependencies();
72      }
73  
74      @Override
75      public DependencyManagement getDependencyManagement()
76      {
77          return modelBase.getDependencyManagement();
78      }
79  
80      @Override
81      public Parent getParent()
82      {
83          Element elm = getParentElement();
84          if ( elm == null )
85          {
86              return null;
87          }
88          else
89          {
90              // this way scm setters change DOM tree immediately
91              return new JDomParent( elm );
92          }
93      }
94  
95      private Element getParentElement()
96      {
97          return project.getChild( "parent", project.getNamespace() );
98      }
99  
100     @Override
101     public List<Profile> getProfiles()
102     {
103         Element profilesElm = project.getChild( "profiles", project.getNamespace() );
104         if ( profilesElm == null )
105         {
106             return Collections.emptyList();
107         }
108         else
109         {
110             List<Element> profileElms = profilesElm.getChildren( "profile", project.getNamespace() );
111 
112             List<Profile> profiles = new ArrayList<>( profileElms.size() );
113 
114             for ( Element profileElm : profileElms )
115             {
116                 profiles.add( new JDomProfile( profileElm ) );
117             }
118 
119             return profiles;
120         }
121     }
122 
123 
124     @Override
125     public Properties getProperties()
126     {
127         Element properties = project.getChild( "properties", project.getNamespace() );
128 
129         if ( properties == null )
130         {
131             return null;
132         }
133         else
134         {
135             return new JDomProperties( properties );
136         }
137     }
138 
139     @Override
140     public Reporting getReporting()
141     {
142         Element reporting = project.getChild( "reporting", project.getNamespace() );
143 
144         if ( reporting == null )
145         {
146             return null;
147         }
148         else
149         {
150             return new JDomReporting( reporting );
151         }
152     }
153 
154     @Override
155     public void setScm( Scm scm )
156     {
157         if ( scm == null )
158         {
159             JDomUtils.rewriteElement( "scm", null, project, project.getNamespace() );
160         }
161         else
162         {
163             Element scmRoot = new Element( "scm" );
164             scmRoot.addContent( "\n  " );
165 
166             // Write current values to JDom tree
167             Scm jdomScm = new JDomScm( scmRoot );
168             jdomScm.setConnection( scm.getConnection() );
169             jdomScm.setDeveloperConnection( scm.getDeveloperConnection() );
170             jdomScm.setTag( scm.getTag() );
171             jdomScm.setUrl( scm.getUrl() );
172 
173             project.addContent( "\n  " ).addContent( scmRoot ).addContent( "\n" );
174         }
175     }
176 
177     @Override
178     public Scm getScm()
179     {
180         Element elm = project.getChild( "scm", project.getNamespace() );
181         if ( elm == null )
182         {
183             return null;
184         }
185         else
186         {
187             // this way scm setters change DOM tree immediately
188             return new JDomScm( elm );
189         }
190     }
191 
192     @Override
193     public void setVersion( String version )
194     {
195         Element versionElement = project.getChild( "version", project.getNamespace() );
196 
197         String parentVersion;
198         Element parent = getParentElement();
199         if ( parent != null )
200         {
201             parentVersion = parent.getChildTextTrim( "version", project.getNamespace() );
202         }
203         else
204         {
205             parentVersion = null;
206         }
207 
208         if ( versionElement == null )
209         {
210             if ( !version.equals( parentVersion ) )
211             {
212                 // we will add this after artifactId, since it was missing but different from the inherited version
213                 Element artifactIdElement = project.getChild( "artifactId", project.getNamespace() );
214                 int index = project.indexOf( artifactIdElement );
215 
216                 versionElement = new Element( "version", project.getNamespace() );
217                 versionElement.setText( version );
218                 project.addContent( index + 1, new Text( "\n  " ) );
219                 project.addContent( index + 2, versionElement );
220             }
221         }
222         else
223         {
224             JDomUtils.rewriteValue( versionElement, version );
225         }
226     }
227 }