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.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.io.IOUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.dom4j.Document;
31 import org.dom4j.DocumentHelper;
32 import org.dom4j.Element;
35 import java.io.FileWriter;
36 import java.io.IOException;
37 import java.io.Writer;
38 import java.util.Iterator;
39 import java.util.List;
42 * RepositoryMetadataWriter
46 public class RepositoryMetadataWriter
48 public static void write( ArchivaRepositoryMetadata metadata, File outputFile )
49 throws RepositoryMetadataException
51 boolean thrown = false;
52 FileWriter writer = null;
55 writer = new FileWriter( outputFile );
56 write( metadata, writer );
59 catch ( IOException e )
62 throw new RepositoryMetadataException(
63 "Unable to write metadata file: " + outputFile.getAbsolutePath() + " - " + e.getMessage(), e );
67 IOUtils.closeQuietly( writer );
70 FileUtils.deleteQuietly( outputFile );
75 public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
76 throws RepositoryMetadataException
78 Document doc = DocumentHelper.createDocument();
80 Element root = DocumentHelper.createElement( "metadata" );
81 doc.setRootElement( root );
83 addOptionalElementText( root, "groupId", metadata.getGroupId() );
84 addOptionalElementText( root, "artifactId", metadata.getArtifactId() );
85 addOptionalElementText( root, "version", metadata.getVersion() );
87 if ( CollectionUtils.isNotEmpty( metadata.getPlugins() ) )
89 Element plugins = root.addElement( "plugins" );
90 for ( Plugin plugin : (List<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() ) || StringUtils.isNotBlank(
100 metadata.getReleasedVersion() ) || StringUtils.isNotBlank( metadata.getLatestVersion() )
101 || StringUtils.isNotBlank( metadata.getLastUpdated() ) || ( metadata.getSnapshotVersion() != null ) )
103 Element versioning = root.addElement( "versioning" );
105 addOptionalElementText( versioning, "latest", metadata.getLatestVersion() );
106 addOptionalElementText( versioning, "release", metadata.getReleasedVersion() );
108 if ( metadata.getSnapshotVersion() != null )
110 Element snapshot = versioning.addElement( "snapshot" );
111 String bnum = String.valueOf( metadata.getSnapshotVersion().getBuildNumber() );
112 addOptionalElementText( snapshot, "buildNumber", bnum );
113 addOptionalElementText( snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp() );
116 if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) )
118 Element versions = versioning.addElement( "versions" );
119 Iterator<String> it = metadata.getAvailableVersions().iterator();
120 while ( it.hasNext() )
122 String version = (String) it.next();
123 versions.addElement( "version" ).setText( version );
127 addOptionalElementText( versioning, "lastUpdated", metadata.getLastUpdated() );
132 XMLWriter.write( doc, writer );
134 catch ( XMLException e )
136 throw new RepositoryMetadataException( "Unable to write xml contents to writer: " + e.getMessage(), e );
140 private static void addOptionalElementText( Element elem, String elemName, String text )
142 if ( StringUtils.isBlank( text ) )
147 elem.addElement( elemName ).setText( text );