1 package org.apache.archiva.repository.metadata;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.common.utils.FileUtils;
23 import org.apache.archiva.model.ArchivaRepositoryMetadata;
24 import org.apache.archiva.model.Plugin;
25 import org.apache.archiva.xml.XMLException;
26 import org.apache.archiva.xml.XMLWriter;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.dom4j.Document;
30 import org.dom4j.DocumentHelper;
31 import org.dom4j.Element;
33 import java.io.FileWriter;
34 import java.io.IOException;
35 import java.io.Writer;
36 import java.nio.file.Path;
37 import java.util.Collections;
38 import java.util.Comparator;
39 import java.util.Iterator;
40 import java.util.List;
43 * RepositoryMetadataWriter
45 public class RepositoryMetadataWriter
47 public static void write( ArchivaRepositoryMetadata metadata, Path outputFile )
48 throws RepositoryMetadataException
50 boolean thrown = false;
51 try (FileWriter writer = new FileWriter( outputFile.toFile() ))
53 write( metadata, writer );
56 catch ( IOException e )
59 throw new RepositoryMetadataException(
60 "Unable to write metadata file: " + outputFile.toAbsolutePath() + " - " + e.getMessage(), e );
66 FileUtils.deleteQuietly( outputFile );
71 public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
72 throws RepositoryMetadataException
74 Document doc = DocumentHelper.createDocument();
76 Element root = DocumentHelper.createElement( "metadata" );
77 doc.setRootElement( root );
79 addOptionalElementText( root, "groupId", metadata.getGroupId() );
80 addOptionalElementText( root, "artifactId", metadata.getArtifactId() );
81 addOptionalElementText( root, "version", metadata.getVersion() );
83 if ( CollectionUtils.isNotEmpty( metadata.getPlugins() ) )
85 Element plugins = root.addElement( "plugins" );
87 List<Plugin> pluginList = metadata.getPlugins();
88 Collections.sort( pluginList, PluginComparator.INSTANCE );
90 for ( Plugin plugin : metadata.getPlugins() )
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() );
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 ) )
105 Element versioning = root.addElement( "versioning" );
107 addOptionalElementText( versioning, "latest", metadata.getLatestVersion() );
108 addOptionalElementText( versioning, "release", metadata.getReleasedVersion() );
110 if ( metadata.getSnapshotVersion() != null )
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() );
118 if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) )
120 Element versions = versioning.addElement( "versions" );
121 Iterator<String> it = metadata.getAvailableVersions().iterator();
122 while ( it.hasNext() )
124 String version = it.next();
125 versions.addElement( "version" ).setText( version );
129 addOptionalElementText( versioning, "lastUpdated", metadata.getLastUpdated() );
134 XMLWriter.write( doc, writer );
136 catch ( XMLException e )
138 throw new RepositoryMetadataException( "Unable to write xml contents to writer: " + e.getMessage(), e );
142 private static void addOptionalElementText( Element elem, String elemName, String text )
144 if ( StringUtils.isBlank( text ) )
149 elem.addElement( elemName ).setText( text );
152 private static class PluginComparator
153 implements Comparator<Plugin>
155 private static final PluginComparator INSTANCE = new PluginComparator();
158 public int compare( Plugin plugin, Plugin plugin2 )
160 if ( plugin.getPrefix() != null && plugin2.getPrefix() != null )
162 return plugin.getPrefix().compareTo( plugin2.getPrefix() );
164 if ( plugin.getName() != null && plugin2.getName() != null )
166 return plugin.getName().compareTo( plugin2.getName() );
168 // we assume artifactId is not null which sounds good :-)
169 return plugin.getArtifactId().compareTo( plugin2.getArtifactId() );