001    package org.apache.maven.scm.provider.local.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.maven.scm.log.ScmLogger;
023    import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
024    import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Writer;
025    import org.codehaus.plexus.util.FileUtils;
026    import org.codehaus.plexus.util.IOUtil;
027    import org.codehaus.plexus.util.ReaderFactory;
028    import org.codehaus.plexus.util.WriterFactory;
029    import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
030    
031    import java.io.File;
032    import java.io.IOException;
033    import java.io.Reader;
034    import java.io.Writer;
035    import java.util.List;
036    
037    /**
038     * Utils for dealing with LocalScmMetadata
039     *
040     * @author <a href="mailto:arne@degenring.de">Arne Degenring</a>
041     * @version $Id: LocalScmMetadataUtils.java 1057018 2011-01-09 20:12:29Z olamy $
042     */
043    public class LocalScmMetadataUtils
044    {
045        /**
046         * The name of the metadata file
047         */
048        public static final String FILENAME = ".maven-scm-local";
049    
050        protected final ScmLogger logger;
051    
052        public LocalScmMetadataUtils( ScmLogger logger )
053        {
054            this.logger = logger;
055        }
056    
057        /**
058         * Builds LocalScmMetadata based on contents of repository
059         *
060         * @param repository
061         * @return
062         * @throws IOException if any
063         */
064        public LocalScmMetadata buildMetadata( File repository )
065            throws IOException
066        {
067            @SuppressWarnings( "unchecked" )
068            List<String> repoFilenames = FileUtils.getFileNames( repository.getAbsoluteFile(), "**", null, false );
069            LocalScmMetadata metadata = new LocalScmMetadata();
070            metadata.setRepositoryFileNames( repoFilenames );
071            return metadata;
072        }
073    
074        /**
075         * Writes metadata file
076         *
077         * @param destinationDir
078         * @param metadata
079         * @throws IOException if any
080         */
081        public void writeMetadata( File destinationDir, LocalScmMetadata metadata )
082            throws IOException
083        {
084            File metadataFile = new File( destinationDir, FILENAME );
085            metadataFile.createNewFile();
086            Writer writer = WriterFactory.newXmlWriter( metadataFile );
087            try
088            {
089                new LocalScmMetadataXpp3Writer().write( writer, metadata );
090            }
091            finally
092            {
093                IOUtil.close( writer );
094            }
095        }
096    
097        /**
098         * Reads metadata file from given directory.
099         *
100         * @param dir The directory that should contain the metadata file
101         * @return LocalScmMetadata or <tt>null</tt> in case of problems
102         */
103        public LocalScmMetadata readMetadata( File dir )
104        {
105            File metadataFile = new File( dir, FILENAME );
106            if ( !metadataFile.exists() )
107            {
108                return null;
109            }
110            LocalScmMetadata result = null;
111            Reader reader = null;
112            try
113            {
114                reader = ReaderFactory.newXmlReader( metadataFile );
115                result = new LocalScmMetadataXpp3Reader().read( reader );
116            }
117            catch ( XmlPullParserException e )
118            {
119                if ( logger.isWarnEnabled() )
120                {
121                    logger.warn( "Could not interpret .maven-scm-local - ignoring", e );
122                }
123                return null;
124            }
125            catch ( IOException e )
126            {
127                if ( logger.isWarnEnabled() )
128                {
129                    logger.warn( "Could not Read .maven-scm-local - ignoring", e );
130                }
131            }
132            finally
133            {
134                IOUtil.close( reader );
135            }
136            return result;
137        }
138    
139    }