This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.repository.metadata;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.model.ArchivaRepositoryMetadata;
023import org.apache.archiva.model.Plugin;
024import org.apache.archiva.xml.XMLException;
025import org.apache.archiva.xml.XMLWriter;
026import org.apache.commons.collections.CollectionUtils;
027import org.apache.commons.io.FileUtils;
028import org.apache.commons.lang.StringUtils;
029import org.dom4j.Document;
030import org.dom4j.DocumentHelper;
031import org.dom4j.Element;
032
033import java.io.File;
034import java.io.FileWriter;
035import java.io.IOException;
036import java.io.Writer;
037import java.util.Collections;
038import java.util.Comparator;
039import java.util.Iterator;
040import java.util.List;
041
042/**
043 * RepositoryMetadataWriter
044 */
045public class RepositoryMetadataWriter
046{
047    public static void write( ArchivaRepositoryMetadata metadata, File outputFile )
048        throws RepositoryMetadataException
049    {
050        boolean thrown = false;
051        try (FileWriter writer = new FileWriter( outputFile ))
052        {
053            write( metadata, writer );
054            writer.flush();
055        }
056        catch ( IOException e )
057        {
058            thrown = true;
059            throw new RepositoryMetadataException(
060                "Unable to write metadata file: " + outputFile.getAbsolutePath() + " - " + e.getMessage(), e );
061        }
062        finally
063        {
064            if ( thrown )
065            {
066                FileUtils.deleteQuietly( outputFile );
067            }
068        }
069    }
070
071    public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
072        throws RepositoryMetadataException
073    {
074        Document doc = DocumentHelper.createDocument();
075
076        Element root = DocumentHelper.createElement( "metadata" );
077        doc.setRootElement( root );
078
079        addOptionalElementText( root, "groupId", metadata.getGroupId() );
080        addOptionalElementText( root, "artifactId", metadata.getArtifactId() );
081        addOptionalElementText( root, "version", metadata.getVersion() );
082
083        if ( CollectionUtils.isNotEmpty( metadata.getPlugins() ) )
084        {
085            Element plugins = root.addElement( "plugins" );
086
087            List<Plugin> pluginList = metadata.getPlugins();
088            Collections.sort( pluginList, PluginComparator.INSTANCE );
089
090            for ( Plugin plugin : metadata.getPlugins() )
091            {
092                Element p = plugins.addElement( "plugin" );
093                p.addElement( "prefix" ).setText( plugin.getPrefix() );
094                p.addElement( "artifactId" ).setText( plugin.getArtifactId() );
095                addOptionalElementText( p, "name", plugin.getName() );
096            }
097        }
098
099        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}