001    package 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    
022    import org.apache.archiva.model.ArchivaRepositoryMetadata;
023    import org.apache.archiva.model.Plugin;
024    import org.apache.archiva.xml.XMLException;
025    import org.apache.archiva.xml.XMLWriter;
026    import org.apache.commons.collections.CollectionUtils;
027    import org.apache.commons.io.FileUtils;
028    import org.apache.commons.io.IOUtils;
029    import org.apache.commons.lang.StringUtils;
030    import org.dom4j.Document;
031    import org.dom4j.DocumentHelper;
032    import org.dom4j.Element;
033    
034    import java.io.File;
035    import java.io.FileWriter;
036    import java.io.IOException;
037    import java.io.Writer;
038    import java.util.Iterator;
039    
040    /**
041     * RepositoryMetadataWriter
042     *
043     *
044     */
045    public class RepositoryMetadataWriter
046    {
047        public static void write( ArchivaRepositoryMetadata metadata, File outputFile )
048            throws RepositoryMetadataException
049        {
050            boolean thrown = false;
051            FileWriter writer = null;
052            try
053            {
054                writer = new FileWriter( outputFile );
055                write( metadata, writer );
056                writer.flush();
057            }
058            catch ( IOException e )
059            {
060                thrown = true;
061                throw new RepositoryMetadataException(
062                    "Unable to write metadata file: " + outputFile.getAbsolutePath() + " - " + e.getMessage(), e );
063            }
064            finally
065            {
066                IOUtils.closeQuietly( writer );
067                if ( thrown )
068                {
069                    FileUtils.deleteQuietly( outputFile );
070                }
071            }
072        }
073    
074        public static void write( ArchivaRepositoryMetadata metadata, Writer writer )
075            throws RepositoryMetadataException
076        {
077            Document doc = DocumentHelper.createDocument();
078    
079            Element root = DocumentHelper.createElement( "metadata" );
080            doc.setRootElement( root );
081    
082            addOptionalElementText( root, "groupId", metadata.getGroupId() );
083            addOptionalElementText( root, "artifactId", metadata.getArtifactId() );
084            addOptionalElementText( root, "version", metadata.getVersion() );
085    
086            if ( CollectionUtils.isNotEmpty( metadata.getPlugins() ) )
087            {
088                Element plugins = root.addElement( "plugins" );
089                for ( Plugin plugin : metadata.getPlugins() )
090                {
091                    Element p = plugins.addElement( "plugin" );
092                    p.addElement( "prefix" ).setText( plugin.getPrefix() );
093                    p.addElement( "artifactId" ).setText( plugin.getArtifactId() );
094                    addOptionalElementText( p, "name", plugin.getName() );
095                }
096            }
097    
098            if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) || StringUtils.isNotBlank(
099                metadata.getReleasedVersion() ) || StringUtils.isNotBlank( metadata.getLatestVersion() )
100                || StringUtils.isNotBlank( metadata.getLastUpdated() ) || ( metadata.getSnapshotVersion() != null ) )
101            {
102                Element versioning = root.addElement( "versioning" );
103    
104                addOptionalElementText( versioning, "latest", metadata.getLatestVersion() );
105                addOptionalElementText( versioning, "release", metadata.getReleasedVersion() );
106    
107                if ( metadata.getSnapshotVersion() != null )
108                {
109                    Element snapshot = versioning.addElement( "snapshot" );
110                    String bnum = String.valueOf( metadata.getSnapshotVersion().getBuildNumber() );
111                    addOptionalElementText( snapshot, "buildNumber", bnum );
112                    addOptionalElementText( snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp() );
113                }
114    
115                if ( CollectionUtils.isNotEmpty( metadata.getAvailableVersions() ) )
116                {
117                    Element versions = versioning.addElement( "versions" );
118                    Iterator<String> it = metadata.getAvailableVersions().iterator();
119                    while ( it.hasNext() )
120                    {
121                        String version = it.next();
122                        versions.addElement( "version" ).setText( version );
123                    }
124                }
125    
126                addOptionalElementText( versioning, "lastUpdated", metadata.getLastUpdated() );
127            }
128    
129            try
130            {
131                XMLWriter.write( doc, writer );
132            }
133            catch ( XMLException e )
134            {
135                throw new RepositoryMetadataException( "Unable to write xml contents to writer: " + e.getMessage(), e );
136            }
137        }
138    
139        private static void addOptionalElementText( Element elem, String elemName, String text )
140        {
141            if ( StringUtils.isBlank( text ) )
142            {
143                return;
144            }
145    
146            elem.addElement( elemName ).setText( text );
147        }
148    }