001package org.apache.archiva.maven2.metadata; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.archiva.model.ArchivaRepositoryMetadata; 022import org.apache.archiva.model.Plugin; 023import org.apache.archiva.model.SnapshotVersion; 024import org.apache.archiva.repository.storage.StorageAsset; 025import org.apache.archiva.xml.XMLException; 026import org.apache.archiva.xml.XMLReader; 027import org.apache.archiva.xml.XmlUtil; 028import org.apache.commons.lang3.math.NumberUtils; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031import org.w3c.dom.Element; 032import org.w3c.dom.Node; 033 034import java.io.IOException; 035import java.nio.file.Files; 036import java.nio.file.Path; 037import java.util.Date; 038 039/** 040 * @author Olivier Lamy 041 * @since 1.4-M3 042 */ 043public class MavenMetadataReader 044{ 045 /* 046 <?xml version="1.0" encoding="UTF-8"?> 047 <metadata modelVersion="1.1.0"> 048 <groupId>org.apache.archiva</groupId> 049 <artifactId>archiva</artifactId> 050 <version>1.4-M3-SNAPSHOT</version> 051 <versioning> 052 <snapshot> 053 <timestamp>20120310.230917</timestamp> 054 <buildNumber>2</buildNumber> 055 </snapshot> 056 <lastUpdated>20120310230917</lastUpdated> 057 <snapshotVersions> 058 <snapshotVersion> 059 <extension>pom</extension> 060 <value>1.4-M3-20120310.230917-2</value> 061 <updated>20120310230917</updated> 062 </snapshotVersion> 063 </snapshotVersions> 064 </versioning> 065 </metadata> 066 */ 067 068 private static final Logger log = LoggerFactory.getLogger( MavenMetadataReader.class ); 069 070 public static ArchivaRepositoryMetadata read(StorageAsset metadataFile) throws XMLException, IOException { 071 if (metadataFile.isFileBased()) { 072 return read(metadataFile.getFilePath()); 073 } else { 074 throw new IOException("StorageAsset is not file based"); 075 } 076 } 077 078 /** 079 * Read and return the {@link org.apache.archiva.model.ArchivaRepositoryMetadata} object from the provided xml file. 080 * 081 * @param metadataFile the maven-metadata.xml file to read. 082 * @return the archiva repository metadata object that represents the provided file contents. 083 * @throws XMLException 084 */ 085 public static ArchivaRepositoryMetadata read( Path metadataFile ) 086 throws XMLException { 087 088 XMLReader xml = new XMLReader( "metadata", metadataFile ); 089 // invoke this to remove namespaces, see MRM-1136 090 xml.removeNamespaces(); 091 092 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata(); 093 094 metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) ); 095 metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) ); 096 metadata.setVersion( xml.getElementText( "//metadata/version" ) ); 097 Date modTime; 098 try { 099 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 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 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}