This project has retired. For details please refer to its Attic page.
RepositoryMetadataWriter xref
View Javadoc
1   package org.apache.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.archiva.model.ArchivaRepositoryMetadata;
23  import org.apache.archiva.model.Plugin;
24  import org.apache.archiva.xml.XMLException;
25  import org.apache.archiva.xml.XMLWriter;
26  import org.apache.commons.collections.CollectionUtils;
27  import org.apache.commons.io.FileUtils;
28  import org.apache.commons.lang.StringUtils;
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.Collections;
38  import java.util.Comparator;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * RepositoryMetadataWriter
44   */
45  public class RepositoryMetadataWriter
46  {
47      public static void write( ArchivaRepositoryMetadata metadata, File outputFile )
48          throws RepositoryMetadataException
49      {
50          boolean thrown = false;
51          try (FileWriter writer = new FileWriter( outputFile ))
52          {
53              write( metadata, writer );
54              writer.flush();
55          }
56          catch ( IOException e )
57          {
58              thrown = true;
59              throw new RepositoryMetadataException(
60                  "Unable to write metadata file: " + outputFile.getAbsolutePath() + " - " + e.getMessage(), e );
61          }
62          finally
63          {
64              if ( thrown )
65              {
66                  FileUtils.deleteQuietly( outputFile );
67              }
68          }
69      }
70  
71      public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
72          throws RepositoryMetadataException
73      {
74          Document doc = DocumentHelper.createDocument();
75  
76          Element root = DocumentHelper.createElement( "metadata" );
77          doc.setRootElement( root );
78  
79          addOptionalElementText( root, "groupId", metadata.getGroupId() );
80          addOptionalElementText( root, "artifactId", metadata.getArtifactId() );
81          addOptionalElementText( root, "version", metadata.getVersion() );
82  
83          if ( CollectionUtils.isNotEmpty( metadata.getPlugins() ) )
84          {
85              Element plugins = root.addElement( "plugins" );
86  
87              List<Plugin> pluginList = metadata.getPlugins();
88              Collections.sort( pluginList, PluginComparator.INSTANCE );
89  
90              for ( Plugin 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() ) //
103             || ( metadata.getSnapshotVersion() != null ) )
104         {
105             Element versioning = root.addElement( "versioning" );
106 
107             addOptionalElementText( versioning, "latest", metadata.getLatestVersion() );
108             addOptionalElementText( versioning, "release", metadata.getReleasedVersion() );
109 
110             if ( metadata.getSnapshotVersion() != null )
111             {
112                 Element snapshot = versioning.addElement( "snapshot" );
113                 String bnum = String.valueOf( metadata.getSnapshotVersion().getBuildNumber() );
114                 addOptionalElementText( snapshot, "buildNumber", bnum );
115                 addOptionalElementText( snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp() );
116             }
117 
118             if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) )
119             {
120                 Element versions = versioning.addElement( "versions" );
121                 Iterator<String> it = metadata.getAvailableVersions().iterator();
122                 while ( it.hasNext() )
123                 {
124                     String version = it.next();
125                     versions.addElement( "version" ).setText( version );
126                 }
127             }
128 
129             addOptionalElementText( versioning, "lastUpdated", metadata.getLastUpdated() );
130         }
131 
132         try
133         {
134             XMLWriter.write( doc, writer );
135         }
136         catch ( XMLException e )
137         {
138             throw new RepositoryMetadataException( "Unable to write xml contents to writer: " + e.getMessage(), e );
139         }
140     }
141 
142     private static void addOptionalElementText( Element elem, String elemName, String text )
143     {
144         if ( StringUtils.isBlank( text ) )
145         {
146             return;
147         }
148 
149         elem.addElement( elemName ).setText( text );
150     }
151 
152     private static class PluginComparator
153         implements Comparator<Plugin>
154     {
155         private static final PluginComparator INSTANCE = new PluginComparator();
156 
157         @Override
158         public int compare( Plugin plugin, Plugin plugin2 )
159         {
160             if ( plugin.getPrefix() != null && plugin2.getPrefix() != null )
161             {
162                 return plugin.getPrefix().compareTo( plugin2.getPrefix() );
163             }
164             if ( plugin.getName() != null && plugin2.getName() != null )
165             {
166                 return plugin.getName().compareTo( plugin2.getName() );
167             }
168             // we assume artifactId is not null which sounds good :-)
169             return plugin.getArtifactId().compareTo( plugin2.getArtifactId() );
170         }
171     }
172 }