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.repository.storage.StorageAsset;
25  import org.apache.archiva.xml.XMLException;
26  import org.apache.archiva.xml.XMLReader;
27  import org.apache.archiva.xml.XmlUtil;
28  import org.apache.commons.lang3.math.NumberUtils;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.w3c.dom.Element;
32  import org.w3c.dom.Node;
33  
34  import java.io.IOException;
35  import java.nio.file.Files;
36  import java.nio.file.Path;
37  import java.util.Date;
38  
39  /**
40   * @author Olivier Lamy
41   * @since 1.4-M3
42   */
43  public class MavenMetadataReader
44  {
45      /*
46      <?xml version="1.0" encoding="UTF-8"?>
47      <metadata modelVersion="1.1.0">
48        <groupId>org.apache.archiva</groupId>
49        <artifactId>archiva</artifactId>
50        <version>1.4-M3-SNAPSHOT</version>
51        <versioning>
52          <snapshot>
53            <timestamp>20120310.230917</timestamp>
54            <buildNumber>2</buildNumber>
55          </snapshot>
56          <lastUpdated>20120310230917</lastUpdated>
57          <snapshotVersions>
58            <snapshotVersion>
59              <extension>pom</extension>
60              <value>1.4-M3-20120310.230917-2</value>
61              <updated>20120310230917</updated>
62            </snapshotVersion>
63          </snapshotVersions>
64        </versioning>
65      </metadata>
66      */
67  
68      private static final Logger log = LoggerFactory.getLogger( MavenMetadataReader.class );
69  
70      public static ArchivaRepositoryMetadata read(StorageAsset metadataFile) throws XMLException, IOException {
71          if (metadataFile.isFileBased()) {
72              return read(metadataFile.getFilePath());
73          } else {
74              throw new IOException("StorageAsset is not file based");
75          }
76      }
77  
78      /**
79       * Read and return the {@link org.apache.archiva.model.ArchivaRepositoryMetadata} object from the provided xml file.
80       *
81       * @param metadataFile the maven-metadata.xml file to read.
82       * @return the archiva repository metadata object that represents the provided file contents.
83       * @throws XMLException
84       */
85      public static ArchivaRepositoryMetadata read( Path metadataFile )
86              throws XMLException {
87  
88          XMLReaderLReader.html#XMLReader">XMLReader xml = new XMLReader( "metadata", metadataFile );
89          // invoke this to remove namespaces, see MRM-1136
90          xml.removeNamespaces();
91  
92          ArchivaRepositoryMetadatadata.html#ArchivaRepositoryMetadata">ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
93  
94          metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
95          metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
96          metadata.setVersion( xml.getElementText( "//metadata/version" ) );
97          Date modTime;
98          try {
99              modTime = new Date(Files.getLastModifiedTime(metadataFile).toMillis());
100         } catch (IOException e) {
101             modTime = new Date();
102             log.error("Could not read modification time of {}", metadataFile);
103         }
104         metadata.setFileLastModified( modTime );
105         try {
106             metadata.setFileSize(Files.size(metadataFile));
107         } catch (IOException e) {
108             metadata.setFileSize( 0 );
109             log.error("Could not read file size of {}", metadataFile);
110         }
111         metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
112         metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
113         metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
114         metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
115 
116         Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
117         if ( snapshotElem != null )
118         {
119             SnapshotVersionsion.html#SnapshotVersion">SnapshotVersion snapshot = new SnapshotVersion();
120             snapshot.setTimestamp(XmlUtil.getChildText(snapshotElem, "timestamp"));
121             String buildNumber = XmlUtil.getChildText(snapshotElem, "buildNumber");
122             if ( NumberUtils.isCreatable( buildNumber ) )
123             {
124                 snapshot.setBuildNumber( NumberUtils.toInt( buildNumber ) );
125             }
126             metadata.setSnapshotVersion( snapshot );
127         }
128 
129         for ( Node node : xml.getElementList( "//metadata/plugins/plugin" ) )
130         {
131             if (node instanceof Element) {
132                 Element plugin = (Element) node;
133                 Pluginodel/Plugin.html#Plugin">Plugin p = new Plugin();
134                 String prefix = plugin.getElementsByTagName("prefix").item(0).getTextContent().trim();
135                 p.setPrefix(prefix);
136                 String artifactId = plugin.getElementsByTagName("artifactId").item(0).getTextContent().trim();
137                 p.setArtifactId(artifactId);
138                 String name = plugin.getElementsByTagName("name").item(0).getTextContent().trim();
139                 p.setName(name);
140                 metadata.addPlugin(p);
141             }
142         }
143 
144         return metadata;
145 
146     }
147 }