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