This project has retired. For details please refer to its Attic page.
Source code
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.xml.XMLException;
025import org.apache.archiva.xml.XMLReader;
026import org.apache.commons.lang.math.NumberUtils;
027import org.dom4j.Element;
028
029import java.io.File;
030import java.util.Date;
031
032/**
033 * @author Olivier Lamy
034 * @since 1.4-M3
035 */
036public class MavenMetadataReader
037{
038    /*
039    <?xml version="1.0" encoding="UTF-8"?>
040    <metadata modelVersion="1.1.0">
041      <groupId>org.apache.archiva</groupId>
042      <artifactId>archiva</artifactId>
043      <version>1.4-M3-SNAPSHOT</version>
044      <versioning>
045        <snapshot>
046          <timestamp>20120310.230917</timestamp>
047          <buildNumber>2</buildNumber>
048        </snapshot>
049        <lastUpdated>20120310230917</lastUpdated>
050        <snapshotVersions>
051          <snapshotVersion>
052            <extension>pom</extension>
053            <value>1.4-M3-20120310.230917-2</value>
054            <updated>20120310230917</updated>
055          </snapshotVersion>
056        </snapshotVersions>
057      </versioning>
058    </metadata>
059    */
060
061    /**
062     * Read and return the {@link org.apache.archiva.model.ArchivaRepositoryMetadata} object from the provided xml file.
063     *
064     * @param metadataFile the maven-metadata.xml file to read.
065     * @return the archiva repository metadata object that represents the provided file contents.
066     * @throws XMLException
067     */
068    public static ArchivaRepositoryMetadata read( File metadataFile )
069        throws XMLException
070    {
071
072        XMLReader xml = new XMLReader( "metadata", metadataFile );
073        // invoke this to remove namespaces, see MRM-1136
074        xml.removeNamespaces();
075
076        ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
077
078        metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
079        metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
080        metadata.setVersion( xml.getElementText( "//metadata/version" ) );
081        metadata.setFileLastModified( new Date( metadataFile.lastModified() ) );
082        metadata.setFileSize( metadataFile.length() );
083
084        metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
085        metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
086        metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
087        metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
088
089        Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
090        if ( snapshotElem != null )
091        {
092            SnapshotVersion snapshot = new SnapshotVersion();
093            snapshot.setTimestamp( snapshotElem.elementTextTrim( "timestamp" ) );
094            String tmp = snapshotElem.elementTextTrim( "buildNumber" );
095            if ( NumberUtils.isNumber( tmp ) )
096            {
097                snapshot.setBuildNumber( NumberUtils.toInt( tmp ) );
098            }
099            metadata.setSnapshotVersion( snapshot );
100        }
101
102        for ( Element plugin : xml.getElementList( "//metadata/plugins/plugin" ) )
103        {
104            Plugin p = new Plugin();
105            p.setPrefix( plugin.elementTextTrim( "prefix" ) );
106            p.setArtifactId( plugin.elementTextTrim( "artifactId" ) );
107            p.setName( plugin.elementTextTrim( "name" ) );
108            metadata.addPlugin( p );
109        }
110
111        return metadata;
112
113    }
114}