1 package org.apache.maven.scm.provider.local.metadata;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.log.ScmLogger;
23 import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
24 import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Writer;
25 import org.codehaus.plexus.util.FileUtils;
26 import org.codehaus.plexus.util.IOUtil;
27 import org.codehaus.plexus.util.ReaderFactory;
28 import org.codehaus.plexus.util.WriterFactory;
29 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30
31 import java.io.File;
32 import java.io.IOException;
33 import java.io.Reader;
34 import java.io.Writer;
35 import java.util.List;
36
37
38
39
40
41
42
43 public class LocalScmMetadataUtils
44 {
45
46
47
48 public static final String FILENAME = ".maven-scm-local";
49
50 protected final ScmLogger logger;
51
52 public LocalScmMetadataUtils( ScmLogger logger )
53 {
54 this.logger = logger;
55 }
56
57
58
59
60
61
62
63
64 public LocalScmMetadata buildMetadata( File repository )
65 throws IOException
66 {
67 @SuppressWarnings( "unchecked" )
68 List<String> repoFilenames = FileUtils.getFileNames( repository.getAbsoluteFile(), "**", null, false );
69 LocalScmMetadata metadata = new LocalScmMetadata();
70 metadata.setRepositoryFileNames( repoFilenames );
71 return metadata;
72 }
73
74
75
76
77
78
79
80
81 public void writeMetadata( File destinationDir, LocalScmMetadata metadata )
82 throws IOException
83 {
84 File metadataFile = new File( destinationDir, FILENAME );
85 metadataFile.createNewFile();
86 Writer writer = WriterFactory.newXmlWriter( metadataFile );
87 try
88 {
89 new LocalScmMetadataXpp3Writer().write( writer, metadata );
90 }
91 finally
92 {
93 IOUtil.close( writer );
94 }
95 }
96
97
98
99
100
101
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 }