View Javadoc

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  public class RepositoryMetadataWriter
47  {
48      public static void write( ArchivaRepositoryMetadata metadata, File outputFile )
49          throws RepositoryMetadataException
50      {
51          boolean thrown = false;
52          FileWriter writer = null;
53          try
54          {
55              writer = new FileWriter( outputFile );
56              write( metadata, writer );
57              writer.flush();
58          }
59          catch ( IOException e )
60          {
61              thrown = true;
62              throw new RepositoryMetadataException( "Unable to write metadata file: " + outputFile.getAbsolutePath()
63                  + " - " + e.getMessage(), e );
64          }
65          finally
66          {
67              IOUtils.closeQuietly( writer );
68              if (thrown)
69              {
70                  FileUtils.deleteQuietly(outputFile);
71              }
72          }
73      }
74  
75      public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
76          throws RepositoryMetadataException
77      {
78          Document doc = DocumentHelper.createDocument();
79  
80          Element root = DocumentHelper.createElement( "metadata" );
81          doc.setRootElement( root );
82  
83          addOptionalElementText( root, "groupId", metadata.getGroupId());
84          addOptionalElementText( root, "artifactId", metadata.getArtifactId() );
85          addOptionalElementText( root, "version", metadata.getVersion() );
86  
87          if ( CollectionUtils.isNotEmpty( metadata.getPlugins() ) )
88          {
89              Element plugins = root.addElement( "plugins" );
90              for ( Plugin plugin : (List<Plugin>)metadata.getPlugins() )
91              {
92                  Element p = plugins.addElement( "plugin" );
93                  p.addElement( "prefix" ).setText( plugin.getPrefix() );
94                  p.addElement( "artifactId" ).setText( plugin.getArtifactId() );
95                  addOptionalElementText( p, "name", plugin.getName() );
96              }
97          }
98  
99          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             Element versioning = root.addElement( "versioning" );
105 
106             addOptionalElementText( versioning, "latest", metadata.getLatestVersion() );
107             addOptionalElementText( versioning, "release", metadata.getReleasedVersion() );
108 
109             if ( metadata.getSnapshotVersion() != null )
110             {
111                 Element snapshot = versioning.addElement( "snapshot" );
112                 String bnum = String.valueOf( metadata.getSnapshotVersion().getBuildNumber() );
113                 addOptionalElementText( snapshot, "buildNumber", bnum );
114                 addOptionalElementText( snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp() );
115             }
116             
117             if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) )
118             {
119                 Element versions = versioning.addElement( "versions" );
120                 Iterator<String> it = metadata.getAvailableVersions().iterator();
121                 while ( it.hasNext() )
122                 {
123                     String version = (String) it.next();
124                     versions.addElement( "version" ).setText( version );
125                 }
126             }
127 
128             addOptionalElementText( versioning, "lastUpdated", metadata.getLastUpdated() );
129         }
130 
131         try
132         {
133             XMLWriter.write( doc, writer );
134         }
135         catch ( XMLException e )
136         {
137             throw new RepositoryMetadataException( "Unable to write xml contents to writer: " + e.getMessage(), e );
138         }
139     }
140 
141     private static void addOptionalElementText( Element elem, String elemName, String text )
142     {
143         if ( StringUtils.isBlank( text ) )
144         {
145             return;
146         }
147 
148         elem.addElement( elemName ).setText( text );
149     }
150 }