001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.local.metadata;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.Reader;
024import java.io.Writer;
025import java.util.List;
026
027import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
028import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Writer;
029import org.codehaus.plexus.util.FileUtils;
030import org.codehaus.plexus.util.IOUtil;
031import org.codehaus.plexus.util.ReaderFactory;
032import org.codehaus.plexus.util.WriterFactory;
033import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * Utils for dealing with LocalScmMetadata
039 *
040 * @author <a href="mailto:arne@degenring.de">Arne Degenring</a>
041 *
042 */
043public class LocalScmMetadataUtils {
044    /**
045     * The name of the metadata file
046     */
047    public static final String FILENAME = ".maven-scm-local";
048
049    private static final Logger LOGGER = LoggerFactory.getLogger(LocalScmMetadataUtils.class);
050
051    /**
052     * Builds LocalScmMetadata based on contents of repository
053     *
054     * @param repository
055     * @return TODO
056     * @throws IOException if any
057     */
058    public LocalScmMetadata buildMetadata(File repository) throws IOException {
059        @SuppressWarnings("unchecked")
060        List<String> repoFilenames = FileUtils.getFileNames(repository.getAbsoluteFile(), "**", null, false);
061        LocalScmMetadata metadata = new LocalScmMetadata();
062        metadata.setRepositoryFileNames(repoFilenames);
063        return metadata;
064    }
065
066    /**
067     * Writes metadata file
068     *
069     * @param destinationDir
070     * @param metadata
071     * @throws IOException if any
072     */
073    public void writeMetadata(File destinationDir, LocalScmMetadata metadata) throws IOException {
074        File metadataFile = new File(destinationDir, FILENAME);
075        metadataFile.createNewFile();
076        Writer writer = WriterFactory.newXmlWriter(metadataFile);
077        try {
078            new LocalScmMetadataXpp3Writer().write(writer, metadata);
079        } finally {
080            IOUtil.close(writer);
081        }
082    }
083
084    /**
085     * Reads metadata file from given directory.
086     *
087     * @param dir The directory that should contain the metadata file
088     * @return LocalScmMetadata or <code>null</code> in case of problems
089     */
090    public LocalScmMetadata readMetadata(File dir) {
091        File metadataFile = new File(dir, FILENAME);
092        if (!metadataFile.exists()) {
093            return null;
094        }
095        LocalScmMetadata result = null;
096        Reader reader = null;
097        try {
098            reader = ReaderFactory.newXmlReader(metadataFile);
099            result = new LocalScmMetadataXpp3Reader().read(reader);
100        } catch (XmlPullParserException e) {
101            if (LOGGER.isWarnEnabled()) {
102                LOGGER.warn("Could not interpret .maven-scm-local - ignoring", e);
103            }
104            return null;
105        } catch (IOException e) {
106            if (LOGGER.isWarnEnabled()) {
107                LOGGER.warn("Could not Read .maven-scm-local - ignoring", e);
108            }
109        } finally {
110            IOUtil.close(reader);
111        }
112        return result;
113    }
114}