This project has retired. For details please refer to its
Attic page.
MavenMetadataReader xref
1 package org.apache.archiva.maven2.metadata;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
41
42
43 public class MavenMetadataReader
44 {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
80
81
82
83
84
85 public static ArchivaRepositoryMetadata read( Path metadataFile )
86 throws XMLException {
87
88 XMLReaderLReader.html#XMLReader">XMLReader xml = new XMLReader( "metadata", metadataFile );
89
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 }