use of org.apache.archiva.xml.XMLReader in project archiva by apache.
the class MavenMetadataReader method read.
/**
* Read and return the {@link org.apache.archiva.model.ArchivaRepositoryMetadata} object from the provided xml file.
*
* @param metadataFile the maven-metadata.xml file to read.
* @return the archiva repository metadata object that represents the provided file contents.
* @throws XMLException
*/
public static ArchivaRepositoryMetadata read(Path metadataFile) throws XMLException {
XMLReader xml = new XMLReader("metadata", metadataFile);
// invoke this to remove namespaces, see MRM-1136
xml.removeNamespaces();
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
metadata.setGroupId(xml.getElementText("//metadata/groupId"));
metadata.setArtifactId(xml.getElementText("//metadata/artifactId"));
metadata.setVersion(xml.getElementText("//metadata/version"));
Date modTime;
try {
modTime = new Date(Files.getLastModifiedTime(metadataFile).toMillis());
} catch (IOException e) {
modTime = new Date();
log.error("Could not read modification time of {}", metadataFile);
}
metadata.setFileLastModified(modTime);
try {
metadata.setFileSize(Files.size(metadataFile));
} catch (IOException e) {
metadata.setFileSize(0);
log.error("Could not read file size of {}", metadataFile);
}
metadata.setLastUpdated(xml.getElementText("//metadata/versioning/lastUpdated"));
metadata.setLatestVersion(xml.getElementText("//metadata/versioning/latest"));
metadata.setReleasedVersion(xml.getElementText("//metadata/versioning/release"));
metadata.setAvailableVersions(xml.getElementListText("//metadata/versioning/versions/version"));
Element snapshotElem = xml.getElement("//metadata/versioning/snapshot");
if (snapshotElem != null) {
SnapshotVersion snapshot = new SnapshotVersion();
snapshot.setTimestamp(snapshotElem.elementTextTrim("timestamp"));
String tmp = snapshotElem.elementTextTrim("buildNumber");
if (NumberUtils.isNumber(tmp)) {
snapshot.setBuildNumber(NumberUtils.toInt(tmp));
}
metadata.setSnapshotVersion(snapshot);
}
for (Element plugin : xml.getElementList("//metadata/plugins/plugin")) {
Plugin p = new Plugin();
p.setPrefix(plugin.elementTextTrim("prefix"));
p.setArtifactId(plugin.elementTextTrim("artifactId"));
p.setName(plugin.elementTextTrim("name"));
metadata.addPlugin(p);
}
return metadata;
}
Aggregations