1 package org.apache.archiva.metadata.repository.storage.maven2;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
24 import org.apache.commons.lang.math.NumberUtils;
25 import org.apache.maven.archiva.xml.XMLException;
26 import org.apache.maven.archiva.xml.XMLReader;
27 import org.dom4j.Element;
30 * RepositoryMetadataReader - read maven-metadata.xml files.
32 * TODO: we should improve on this, ideally using the Maven standard libraries (which are unfortunately baked into
35 public final class MavenRepositoryMetadataReader
37 private MavenRepositoryMetadataReader()
42 * Read and return the {@link MavenRepositoryMetadata} object from the provided xml file.
44 * @param metadataFile the maven-metadata.xml file to read.
45 * @return the archiva repository metadata object that represents the provided file contents.
46 * @throws org.apache.maven.archiva.xml.XMLException
48 public static MavenRepositoryMetadata read( File metadataFile )
51 XMLReader xml = new XMLReader( "metadata", metadataFile );
52 // invoke this to remove namespaces, see MRM-1136
53 xml.removeNamespaces();
55 MavenRepositoryMetadata metadata = new MavenRepositoryMetadata();
57 metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
58 metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
59 metadata.setVersion( xml.getElementText( "//metadata/version" ) );
61 metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
62 metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
63 metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
64 metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
66 Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
67 if ( snapshotElem != null )
69 MavenRepositoryMetadata.Snapshot snapshot = new MavenRepositoryMetadata.Snapshot();
70 snapshot.setTimestamp( snapshotElem.elementTextTrim( "timestamp" ) );
71 String tmp = snapshotElem.elementTextTrim( "buildNumber" );
72 if ( NumberUtils.isNumber( tmp ) )
74 snapshot.setBuildNumber( NumberUtils.toInt( tmp ) );
76 metadata.setSnapshotVersion( snapshot );
79 for ( Element plugin : xml.getElementList( "//metadata/plugins/plugin" ) )
81 MavenRepositoryMetadata.Plugin p = new MavenRepositoryMetadata.Plugin();
82 p.setPrefix( plugin.elementTextTrim( "prefix" ) );
83 p.setArtifactId( plugin.elementTextTrim( "artifactId" ) );
84 p.setName( plugin.elementTextTrim( "name" ) );
85 metadata.addPlugin( p );