Coverage Report - org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader
 
Classes in this File Line Coverage Branch Coverage Complexity
RepositoryMetadataReader
0%
0/32
0%
0/6
0
 
 1  
 package org.apache.maven.archiva.repository.metadata;
 2  
 
 3  
 /*
 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
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 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
 19  
  * under the License.
 20  
  */
 21  
 
 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;
 29  
 
 30  
 import java.io.File;
 31  
 import java.util.Date;
 32  
 
 33  
 /**
 34  
  * RepositoryMetadataReader - read maven-metadata.xml files.
 35  
  *
 36  
  * @version $Id: RepositoryMetadataReader.java 756559 2009-03-20 16:05:05Z oching $
 37  
  */
 38  0
 public class RepositoryMetadataReader
 39  
 {
 40  
     /**
 41  
      * Read and return the {@link ArchivaRepositoryMetadata} object from the provided xml file.
 42  
      * 
 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
 46  
      */
 47  
     public static ArchivaRepositoryMetadata read( File metadataFile )
 48  
         throws RepositoryMetadataException
 49  
     {
 50  
         try
 51  
         {
 52  0
             XMLReader xml = new XMLReader( "metadata", metadataFile );
 53  
             // invoke this to remove namespaces, see MRM-1136
 54  0
             xml.removeNamespaces();
 55  
             
 56  0
             ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
 57  
 
 58  0
             metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
 59  0
             metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
 60  0
             metadata.setVersion( xml.getElementText( "//metadata/version" ) );
 61  0
             metadata.setFileLastModified( new Date( metadataFile.lastModified() ) );
 62  0
             metadata.setFileSize( metadataFile.length() );
 63  0
             metadata.setWhenIndexed( null );
 64  
 
 65  0
             metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
 66  0
             metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
 67  0
             metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
 68  0
             metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
 69  
 
 70  0
             Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
 71  0
             if ( snapshotElem != null )
 72  
             {
 73  0
                 SnapshotVersion snapshot = new SnapshotVersion();
 74  0
                 snapshot.setTimestamp( snapshotElem.elementTextTrim( "timestamp" ) );
 75  0
                 String tmp = snapshotElem.elementTextTrim( "buildNumber" );
 76  0
                 if( NumberUtils.isNumber( tmp ))
 77  
                 {
 78  0
                     snapshot.setBuildNumber( NumberUtils.toInt( tmp ) );
 79  
                 }
 80  0
                 metadata.setSnapshotVersion( snapshot );
 81  
             }
 82  
 
 83  0
             for ( Element plugin : xml.getElementList( "//metadata/plugins/plugin" ) )
 84  
             {
 85  0
                 Plugin p = new Plugin();
 86  0
                 p.setPrefix( plugin.elementTextTrim( "prefix" ) );
 87  0
                 p.setArtifactId( plugin.elementTextTrim( "artifactId" ) );
 88  0
                 p.setName( plugin.elementTextTrim( "name" ) );
 89  0
                 metadata.addPlugin( p );
 90  0
             }
 91  
 
 92  0
             return metadata;
 93  
         }
 94  0
         catch ( XMLException e )
 95  
         {
 96  0
             throw new RepositoryMetadataException( e.getMessage(), e );
 97  
         }
 98  
     }
 99  
 }