1 package org.apache.maven.archiva.repository.metadata;
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
22 import org.apache.commons.lang.math.NumberUtils;
23 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
24 import org.apache.maven.archiva.model.Plugin;
25 import org.apache.maven.archiva.model.SnapshotVersion;
26 import org.apache.maven.archiva.xml.XMLException;
27 import org.apache.maven.archiva.xml.XMLReader;
28 import org.dom4j.Element;
31 import java.util.Date;
34 * RepositoryMetadataReader - read maven-metadata.xml files.
38 public class RepositoryMetadataReader
41 * Read and return the {@link ArchivaRepositoryMetadata} object from the provided xml file.
43 * @param metadataFile the maven-metadata.xml file to read.
44 * @return the archiva repository metadata object that represents the provided file contents.
45 * @throws RepositoryMetadataException
47 public static ArchivaRepositoryMetadata read( File metadataFile )
48 throws RepositoryMetadataException
52 XMLReader xml = new XMLReader( "metadata", metadataFile );
54 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
56 metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
57 metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
58 metadata.setVersion( xml.getElementText( "//metadata/version" ) );
59 metadata.setFileLastModified( new Date( metadataFile.lastModified() ) );
60 metadata.setFileSize( metadataFile.length() );
61 metadata.setWhenIndexed( null );
63 metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
64 metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
65 metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
66 metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
68 Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
69 if ( snapshotElem != null )
71 SnapshotVersion snapshot = new SnapshotVersion();
72 snapshot.setTimestamp( snapshotElem.elementTextTrim( "timestamp" ) );
73 String tmp = snapshotElem.elementTextTrim( "buildNumber" );
74 if( NumberUtils.isNumber( tmp ))
76 snapshot.setBuildNumber( NumberUtils.toInt( tmp ) );
78 metadata.setSnapshotVersion( snapshot );
81 for ( Element plugin : xml.getElementList( "//metadata/plugins/plugin" ) )
83 Plugin p = new Plugin();
84 p.setPrefix( plugin.elementTextTrim( "prefix" ) );
85 p.setArtifactId( plugin.elementTextTrim( "artifactId" ) );
86 p.setName( plugin.elementTextTrim( "name" ) );
87 metadata.addPlugin( p );
92 catch ( XMLException e )
94 throw new RepositoryMetadataException( e.getMessage(), e );