1 package org.apache.maven.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.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;
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;
41 * RepositoryMetadataWriter
43 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46 public class RepositoryMetadataWriter
48 public static void write( ArchivaRepositoryMetadata metadata, File outputFile )
49 throws RepositoryMetadataException
51 FileWriter writer = null;
54 writer = new FileWriter( outputFile );
55 write( metadata, writer );
58 catch ( IOException e )
60 throw new RepositoryMetadataException( "Unable to write metadata file: " + outputFile.getAbsolutePath()
61 + " - " + e.getMessage(), e );
65 IOUtils.closeQuietly( writer );
69 public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
70 throws RepositoryMetadataException
72 Document doc = DocumentHelper.createDocument();
74 Element root = DocumentHelper.createElement( "metadata" );
75 doc.setRootElement( root );
77 root.addElement( "groupId" ).setText( metadata.getGroupId() );
78 addOptionalElementText( root, "artifactId", metadata.getArtifactId() );
79 addOptionalElementText( root, "version", metadata.getVersion() );
81 if ( CollectionUtils.isNotEmpty( metadata.getPlugins() ) )
83 Element plugins = root.addElement( "plugins" );
84 for ( Plugin plugin : (List<Plugin>)metadata.getPlugins() )
86 Element p = plugins.addElement( "plugin" );
87 p.addElement( "prefix" ).setText( plugin.getPrefix() );
88 p.addElement( "artifactId" ).setText( plugin.getArtifactId() );
89 addOptionalElementText( p, "name", plugin.getName() );
93 if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() )
94 || StringUtils.isNotBlank( metadata.getReleasedVersion() )
95 || StringUtils.isNotBlank( metadata.getLatestVersion() )
96 || StringUtils.isNotBlank( metadata.getLastUpdated() ) || ( metadata.getSnapshotVersion() != null ) )
98 Element versioning = root.addElement( "versioning" );
100 addOptionalElementText( versioning, "latest", metadata.getLatestVersion() );
101 addOptionalElementText( versioning, "release", metadata.getReleasedVersion() );
103 if ( metadata.getSnapshotVersion() != null )
105 Element snapshot = versioning.addElement( "snapshot" );
106 String bnum = String.valueOf( metadata.getSnapshotVersion().getBuildNumber() );
107 addOptionalElementText( snapshot, "buildNumber", bnum );
108 addOptionalElementText( snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp() );
111 if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) )
113 Element versions = versioning.addElement( "versions" );
114 Iterator<String> it = metadata.getAvailableVersions().iterator();
115 while ( it.hasNext() )
117 String version = (String) it.next();
118 versions.addElement( "version" ).setText( version );
122 addOptionalElementText( versioning, "lastUpdated", metadata.getLastUpdated() );
127 XMLWriter.write( doc, writer );
129 catch ( XMLException e )
131 throw new RepositoryMetadataException( "Unable to write xml contents to writer: " + e.getMessage(), e );
135 private static void addOptionalElementText( Element elem, String elemName, String text )
137 if ( StringUtils.isBlank( text ) )
142 elem.addElement( elemName ).setText( text );