Coverage Report - org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
RepositoryMetadataWriter
0%
0/53
0%
0/24
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.collections.CollectionUtils;
 23  
 import org.apache.commons.io.IOUtils;
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
 26  
 import org.apache.maven.archiva.model.Plugin;
 27  
 import org.apache.maven.archiva.xml.XMLException;
 28  
 import org.apache.maven.archiva.xml.XMLWriter;
 29  
 import org.dom4j.Document;
 30  
 import org.dom4j.DocumentHelper;
 31  
 import org.dom4j.Element;
 32  
 
 33  
 import java.io.File;
 34  
 import java.io.FileWriter;
 35  
 import java.io.IOException;
 36  
 import java.io.Writer;
 37  
 import java.util.Iterator;
 38  
 import java.util.List;
 39  
 import org.apache.commons.io.FileUtils;
 40  
 
 41  
 /**
 42  
  * RepositoryMetadataWriter 
 43  
  *
 44  
  * @version $Id: RepositoryMetadataWriter.java 718864 2008-11-19 06:33:35Z brett $
 45  
  */
 46  0
 public class RepositoryMetadataWriter
 47  
 {
 48  
     public static void write( ArchivaRepositoryMetadata metadata, File outputFile )
 49  
         throws RepositoryMetadataException
 50  
     {
 51  0
         boolean thrown = false;
 52  0
         FileWriter writer = null;
 53  
         try
 54  
         {
 55  0
             writer = new FileWriter( outputFile );
 56  0
             write( metadata, writer );
 57  0
             writer.flush();
 58  
         }
 59  0
         catch ( IOException e )
 60  
         {
 61  0
             thrown = true;
 62  0
             throw new RepositoryMetadataException( "Unable to write metadata file: " + outputFile.getAbsolutePath()
 63  
                 + " - " + e.getMessage(), e );
 64  
         }
 65  
         finally
 66  
         {
 67  0
             IOUtils.closeQuietly( writer );
 68  0
             if (thrown)
 69  
             {
 70  0
                 FileUtils.deleteQuietly(outputFile);
 71  
             }
 72  
         }
 73  0
     }
 74  
 
 75  
     public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
 76  
         throws RepositoryMetadataException
 77  
     {
 78  0
         Document doc = DocumentHelper.createDocument();
 79  
 
 80  0
         Element root = DocumentHelper.createElement( "metadata" );
 81  0
         doc.setRootElement( root );
 82  
 
 83  0
         addOptionalElementText( root, "groupId", metadata.getGroupId());
 84  0
         addOptionalElementText( root, "artifactId", metadata.getArtifactId() );
 85  0
         addOptionalElementText( root, "version", metadata.getVersion() );
 86  
 
 87  0
         if ( CollectionUtils.isNotEmpty( metadata.getPlugins() ) )
 88  
         {
 89  0
             Element plugins = root.addElement( "plugins" );
 90  0
             for ( Plugin plugin : (List<Plugin>)metadata.getPlugins() )
 91  
             {
 92  0
                 Element p = plugins.addElement( "plugin" );
 93  0
                 p.addElement( "prefix" ).setText( plugin.getPrefix() );
 94  0
                 p.addElement( "artifactId" ).setText( plugin.getArtifactId() );
 95  0
                 addOptionalElementText( p, "name", plugin.getName() );
 96  0
             }
 97  
         }
 98  
 
 99  0
         if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() )
 100  
             || StringUtils.isNotBlank( metadata.getReleasedVersion() )
 101  
             || StringUtils.isNotBlank( metadata.getLatestVersion() )
 102  
             || StringUtils.isNotBlank( metadata.getLastUpdated() ) || ( metadata.getSnapshotVersion() != null ) )
 103  
         {
 104  0
             Element versioning = root.addElement( "versioning" );
 105  
 
 106  0
             addOptionalElementText( versioning, "latest", metadata.getLatestVersion() );
 107  0
             addOptionalElementText( versioning, "release", metadata.getReleasedVersion() );
 108  
 
 109  0
             if ( metadata.getSnapshotVersion() != null )
 110  
             {
 111  0
                 Element snapshot = versioning.addElement( "snapshot" );
 112  0
                 String bnum = String.valueOf( metadata.getSnapshotVersion().getBuildNumber() );
 113  0
                 addOptionalElementText( snapshot, "buildNumber", bnum );
 114  0
                 addOptionalElementText( snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp() );
 115  
             }
 116  
             
 117  0
             if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) )
 118  
             {
 119  0
                 Element versions = versioning.addElement( "versions" );
 120  0
                 Iterator<String> it = metadata.getAvailableVersions().iterator();
 121  0
                 while ( it.hasNext() )
 122  
                 {
 123  0
                     String version = (String) it.next();
 124  0
                     versions.addElement( "version" ).setText( version );
 125  0
                 }
 126  
             }
 127  
 
 128  0
             addOptionalElementText( versioning, "lastUpdated", metadata.getLastUpdated() );
 129  
         }
 130  
 
 131  
         try
 132  
         {
 133  0
             XMLWriter.write( doc, writer );
 134  
         }
 135  0
         catch ( XMLException e )
 136  
         {
 137  0
             throw new RepositoryMetadataException( "Unable to write xml contents to writer: " + e.getMessage(), e );
 138  0
         }
 139  0
     }
 140  
 
 141  
     private static void addOptionalElementText( Element elem, String elemName, String text )
 142  
     {
 143  0
         if ( StringUtils.isBlank( text ) )
 144  
         {
 145  0
             return;
 146  
         }
 147  
 
 148  0
         elem.addElement( elemName ).setText( text );
 149  0
     }
 150  
 }