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.ReaderFactory;
031import org.codehaus.plexus.util.WriterFactory;
032import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * Utils for dealing with LocalScmMetadata
038 *
039 * @author <a href="mailto:arne@degenring.de">Arne Degenring</a>
040 *
041 */
042public class LocalScmMetadataUtils {
043    /**
044     * The name of the metadata file
045     */
046    public static final String FILENAME = ".maven-scm-local";
047
048    private static final Logger LOGGER = LoggerFactory.getLogger(LocalScmMetadataUtils.class);
049
050    /**
051     * Builds LocalScmMetadata based on contents of repository
052     *
053     * @param repository
054     * @return TODO
055     * @throws IOException if any
056     */
057    public LocalScmMetadata buildMetadata(File repository) throws IOException {
058        @SuppressWarnings("unchecked")
059        List<String> repoFilenames = FileUtils.getFileNames(repository.getAbsoluteFile(), "**", null, false);
060        LocalScmMetadata metadata = new LocalScmMetadata();
061        metadata.setRepositoryFileNames(repoFilenames);
062        return metadata;
063    }
064
065    /**
066     * Writes metadata file
067     *
068     * @param destinationDir
069     * @param metadata
070     * @throws IOException if any
071     */
072    public void writeMetadata(File destinationDir, LocalScmMetadata metadata) throws IOException {
073        File metadataFile = new File(destinationDir, FILENAME);
074        metadataFile.createNewFile();
075
076        try (Writer writer = WriterFactory.newXmlWriter(metadataFile)) {
077            new LocalScmMetadataXpp3Writer().write(writer, metadata);
078        }
079    }
080
081    /**
082     * Reads metadata file from given directory.
083     *
084     * @param dir The directory that should contain the metadata file
085     * @return LocalScmMetadata or <code>null</code> in case of problems
086     */
087    public LocalScmMetadata readMetadata(File dir) {
088        File metadataFile = new File(dir, FILENAME);
089        if (!metadataFile.exists()) {
090            return null;
091        }
092
093        try (Reader reader = ReaderFactory.newXmlReader(metadataFile)) {
094            LocalScmMetadata result = new LocalScmMetadataXpp3Reader().read(reader);
095            return result;
096        } catch (XmlPullParserException e) {
097            if (LOGGER.isWarnEnabled()) {
098                LOGGER.warn("Could not interpret .maven-scm-local - ignoring", e);
099            }
100            return null;
101        } catch (IOException e) {
102            if (LOGGER.isWarnEnabled()) {
103                LOGGER.warn("Could not read .maven-scm-local - ignoring", e);
104            }
105            return null;
106        }
107    }
108}