This project has retired. For details please refer to its Attic page.
MavenMetadataReader xref
View Javadoc
1   package org.apache.archiva.maven2.metadata;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with 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,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.model.ArchivaRepositoryMetadata;
22  import org.apache.archiva.model.Plugin;
23  import org.apache.archiva.model.SnapshotVersion;
24  import org.apache.archiva.xml.XMLException;
25  import org.apache.archiva.xml.XMLReader;
26  import org.apache.commons.lang.math.NumberUtils;
27  import org.dom4j.Element;
28  
29  import java.io.File;
30  import java.util.Date;
31  
32  /**
33   * @author Olivier Lamy
34   * @since 1.4-M3
35   */
36  public class MavenMetadataReader
37  {
38      /*
39      <?xml version="1.0" encoding="UTF-8"?>
40      <metadata modelVersion="1.1.0">
41        <groupId>org.apache.archiva</groupId>
42        <artifactId>archiva</artifactId>
43        <version>1.4-M3-SNAPSHOT</version>
44        <versioning>
45          <snapshot>
46            <timestamp>20120310.230917</timestamp>
47            <buildNumber>2</buildNumber>
48          </snapshot>
49          <lastUpdated>20120310230917</lastUpdated>
50          <snapshotVersions>
51            <snapshotVersion>
52              <extension>pom</extension>
53              <value>1.4-M3-20120310.230917-2</value>
54              <updated>20120310230917</updated>
55            </snapshotVersion>
56          </snapshotVersions>
57        </versioning>
58      </metadata>
59      */
60  
61      /**
62       * Read and return the {@link org.apache.archiva.model.ArchivaRepositoryMetadata} object from the provided xml file.
63       *
64       * @param metadataFile the maven-metadata.xml file to read.
65       * @return the archiva repository metadata object that represents the provided file contents.
66       * @throws XMLException
67       */
68      public static ArchivaRepositoryMetadata read( File metadataFile )
69          throws XMLException
70      {
71  
72          XMLReader xml = new XMLReader( "metadata", metadataFile );
73          // invoke this to remove namespaces, see MRM-1136
74          xml.removeNamespaces();
75  
76          ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
77  
78          metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
79          metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
80          metadata.setVersion( xml.getElementText( "//metadata/version" ) );
81          metadata.setFileLastModified( new Date( metadataFile.lastModified() ) );
82          metadata.setFileSize( metadataFile.length() );
83  
84          metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
85          metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
86          metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
87          metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
88  
89          Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
90          if ( snapshotElem != null )
91          {
92              SnapshotVersion snapshot = new SnapshotVersion();
93              snapshot.setTimestamp( snapshotElem.elementTextTrim( "timestamp" ) );
94              String tmp = snapshotElem.elementTextTrim( "buildNumber" );
95              if ( NumberUtils.isNumber( tmp ) )
96              {
97                  snapshot.setBuildNumber( NumberUtils.toInt( tmp ) );
98              }
99              metadata.setSnapshotVersion( snapshot );
100         }
101 
102         for ( Element plugin : xml.getElementList( "//metadata/plugins/plugin" ) )
103         {
104             Plugin p = new Plugin();
105             p.setPrefix( plugin.elementTextTrim( "prefix" ) );
106             p.setArtifactId( plugin.elementTextTrim( "artifactId" ) );
107             p.setName( plugin.elementTextTrim( "name" ) );
108             metadata.addPlugin( p );
109         }
110 
111         return metadata;
112 
113     }
114 }