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.xml.XMLException;
27 import org.apache.maven.archiva.xml.XMLWriter;
28 import org.dom4j.Document;
29 import org.dom4j.DocumentHelper;
30 import org.dom4j.Element;
33 import java.io.FileWriter;
34 import java.io.IOException;
35 import java.io.Writer;
36 import java.util.Iterator;
39 * RepositoryMetadataWriter
41 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
44 public class RepositoryMetadataWriter
46 public static void write( ArchivaRepositoryMetadata metadata, File outputFile )
47 throws RepositoryMetadataException
49 FileWriter writer = null;
52 writer = new FileWriter( outputFile );
53 write( metadata, writer );
56 catch ( IOException e )
58 throw new RepositoryMetadataException( "Unable to write metadata file: " + outputFile.getAbsolutePath()
59 + " - " + e.getMessage(), e );
63 IOUtils.closeQuietly( writer );
67 public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
68 throws RepositoryMetadataException
70 Document doc = DocumentHelper.createDocument();
72 Element root = DocumentHelper.createElement( "metadata" );
73 doc.setRootElement( root );
75 root.addElement( "groupId" ).setText( metadata.getGroupId() );
76 root.addElement( "artifactId" ).setText( metadata.getArtifactId() );
77 addOptionalElementText( root, "version", metadata.getVersion() );
79 if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() )
80 || StringUtils.isNotBlank( metadata.getReleasedVersion() )
81 || StringUtils.isNotBlank( metadata.getLatestVersion() )
82 || StringUtils.isNotBlank( metadata.getLastUpdated() ) || ( metadata.getSnapshotVersion() != null ) )
84 Element versioning = root.addElement( "versioning" );
86 addOptionalElementText( versioning, "latest", metadata.getLatestVersion() );
87 addOptionalElementText( versioning, "release", metadata.getReleasedVersion() );
89 if ( metadata.getSnapshotVersion() != null )
91 Element snapshot = versioning.addElement( "snapshot" );
92 String bnum = String.valueOf( metadata.getSnapshotVersion().getBuildNumber() );
93 addOptionalElementText( snapshot, "buildNumber", bnum );
94 addOptionalElementText( snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp() );
97 if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) )
99 Element versions = versioning.addElement( "versions" );
100 Iterator<String> it = metadata.getAvailableVersions().iterator();
101 while ( it.hasNext() )
103 String version = (String) it.next();
104 versions.addElement( "version" ).setText( version );
108 addOptionalElementText( versioning, "lastUpdated", metadata.getLastUpdated() );
113 XMLWriter.write( doc, writer );
115 catch ( XMLException e )
117 throw new RepositoryMetadataException( "Unable to write xml contents to writer: " + e.getMessage(), e );
121 private static void addOptionalElementText( Element elem, String elemName, String text )
123 if ( StringUtils.isBlank( text ) )
128 elem.addElement( elemName ).setText( text );