1 | |
package net.sf.statsvn.util; |
2 | |
|
3 | |
import java.io.FileInputStream; |
4 | |
import java.io.IOException; |
5 | |
import java.io.InputStream; |
6 | |
import java.io.InputStreamReader; |
7 | |
import java.util.ArrayList; |
8 | |
import java.util.List; |
9 | |
|
10 | |
import net.sf.statcvs.util.LookaheadReader; |
11 | |
import net.sf.statsvn.output.SvnConfigurationOptions; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public class SvnPropgetUtils implements ISvnPropgetProcessor { |
21 | |
|
22 | |
protected List binaryFiles; |
23 | |
|
24 | |
protected ISvnProcessor processor; |
25 | |
|
26 | 0 | |
27 | 0 | |
28 | |
|
29 | 1 | public SvnPropgetUtils(ISvnProcessor processor) { |
30 | 1 | this.processor = processor; |
31 | 1 | } |
32 | |
|
33 | |
protected ISvnProcessor getProcessor() { |
34 | 0 | return processor; |
35 | 0 | } |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
protected synchronized ProcessUtils getFileMimeTypes() { |
43 | 0 | return getFileMimeTypes(null, null); |
44 | |
} |
45 | |
|
46 | |
|
47 | |
|
48 | 0 | |
49 | 0 | |
50 | 0 | |
51 | |
|
52 | |
|
53 | 0 | |
54 | 0 | |
55 | |
protected synchronized ProcessUtils getFileMimeTypes(final String revision, final String filename) { |
56 | 0 | String svnPropgetCommand = "svn propget svn:mime-type"; |
57 | 0 | if (revision != null && revision.length() > 0) { |
58 | 0 | svnPropgetCommand += " -r " + revision; |
59 | |
} |
60 | 0 | |
61 | 0 | if (filename != null && filename.length() > 0) { |
62 | 0 | svnPropgetCommand += " " + StringUtils.replace(" ", "%20", getProcessor().getInfoProcessor().relativePathToUrl(filename)); |
63 | 0 | |
64 | 0 | if (revision != null && revision.length() > 0) { |
65 | 0 | svnPropgetCommand += "@" + revision; |
66 | 0 | } |
67 | 0 | } else { |
68 | 0 | svnPropgetCommand += " -R "; |
69 | 0 | } |
70 | |
|
71 | 0 | svnPropgetCommand += SvnCommandHelper.getAuthString(); |
72 | |
|
73 | |
try { |
74 | 0 | return ProcessUtils.call(svnPropgetCommand); |
75 | 0 | } catch (final Exception e) { |
76 | 0 | SvnConfigurationOptions.getTaskLogger().info(e.toString()); |
77 | 0 | return null; |
78 | |
} |
79 | 157122 | } |
80 | 0 | |
81 | |
|
82 | 0 | |
83 | 0 | |
84 | 0 | public List getBinaryFiles() { |
85 | 5418 | if (binaryFiles == null) { |
86 | 0 | ProcessUtils pUtils = null; |
87 | 0 | try { |
88 | 0 | pUtils = getFileMimeTypes(); |
89 | 0 | loadBinaryFiles(pUtils); |
90 | 0 | } finally { |
91 | 0 | if (pUtils != null) { |
92 | 0 | try { |
93 | 0 | pUtils.close(); |
94 | 0 | } catch (final IOException e) { |
95 | 157122 | SvnConfigurationOptions.getTaskLogger().info(e.toString()); |
96 | 0 | } |
97 | |
} |
98 | |
} |
99 | |
} |
100 | |
|
101 | 5418 | return binaryFiles; |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | 29 | |
108 | 29 | |
109 | |
|
110 | 4263 | |
111 | 4234 | |
112 | 4234 | public void loadBinaryFiles(final String path) throws IOException |
113 | 4234 | { |
114 | 4235 | final InputStream stream = new FileInputStream(path); |
115 | 1 | final ProcessUtils pUtils = new ProcessUtils(); |
116 | 3796 | try { |
117 | 30 | pUtils.setInputStream(stream); |
118 | 1 | loadBinaryFiles(pUtils); |
119 | |
} finally { |
120 | 1 | pUtils.close(); |
121 | 1 | } |
122 | 30 | } |
123 | 29 | |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
protected void loadBinaryFiles(final ProcessUtils pUtils) { |
131 | 1 | binaryFiles = new ArrayList(); |
132 | 1 | final LookaheadReader mimeReader = new LookaheadReader(new InputStreamReader(pUtils.getInputStream())); |
133 | |
try { |
134 | 147 | while (mimeReader.hasNextLine()) { |
135 | 146 | mimeReader.nextLine(); |
136 | 146 | final String file = getBinaryFilename(mimeReader.getCurrentLine(), false); |
137 | 146 | if (file != null) { |
138 | 146 | binaryFiles.add(file); |
139 | 0 | } |
140 | 146 | } |
141 | 1 | if (pUtils.hasErrorOccured()) { |
142 | 0 | throw new IOException(pUtils.getErrorMessage()); |
143 | 0 | } |
144 | 0 | } catch (final IOException e) { |
145 | 0 | SvnConfigurationOptions.getTaskLogger().info(e.getMessage()); |
146 | 1 | } |
147 | 1 | } |
148 | 0 | |
149 | 0 | |
150 | 0 | |
151 | 0 | |
152 | |
public boolean isBinaryFile(final String revision, final String filename) { |
153 | 0 | ProcessUtils pUtils = null; |
154 | 0 | try { |
155 | 0 | pUtils = getFileMimeTypes(revision, filename); |
156 | 0 | final LookaheadReader mimeReader = new LookaheadReader(new InputStreamReader(pUtils.getInputStream())); |
157 | 0 | while (mimeReader.hasNextLine()) { |
158 | 0 | mimeReader.nextLine(); |
159 | 0 | final String file = getBinaryFilename(mimeReader.getCurrentLine(), true); |
160 | 0 | if (file != null && file.equals(filename)) { |
161 | 0 | return true; |
162 | |
} |
163 | 0 | } |
164 | 0 | } catch (final IOException e) { |
165 | 0 | SvnConfigurationOptions.getTaskLogger().info(e.toString()); |
166 | |
} finally { |
167 | 0 | if (pUtils != null) { |
168 | |
try { |
169 | 0 | pUtils.close(); |
170 | 0 | } catch (final IOException e) { |
171 | 0 | SvnConfigurationOptions.getTaskLogger().info(e.toString()); |
172 | 0 | } |
173 | |
} |
174 | |
} |
175 | |
|
176 | 0 | return false; |
177 | |
} |
178 | |
|
179 | 4234 | |
180 | |
|
181 | |
|
182 | |
|
183 | 4234 | |
184 | |
|
185 | 4234 | |
186 | |
|
187 | 4234 | |
188 | 4234 | |
189 | 0 | |
190 | |
|
191 | 4234 | |
192 | |
|
193 | |
protected String getBinaryFilename(final String currentLine, final boolean removeRoot) { |
194 | 0 | |
195 | 146 | String line = removeRoot ? currentLine : currentLine.replace('\\', '/'); |
196 | |
|
197 | |
|
198 | |
|
199 | 146 | final String octetStream = " - application/octet-stream"; |
200 | |
|
201 | 146 | if (line.endsWith(octetStream) || line.lastIndexOf(" - text/") < 0 && line.lastIndexOf(" - text/") == line.lastIndexOf(" - ") |
202 | |
&& line.lastIndexOf(" - ") >= 0) { |
203 | 146 | line = line.substring(0, line.lastIndexOf(" - ")); |
204 | 146 | if (removeRoot) { |
205 | 0 | line = getProcessor().getInfoProcessor().urlToRelativePath(line); |
206 | |
} |
207 | 146 | return line; |
208 | |
} |
209 | |
|
210 | 0 | return null; |
211 | |
} |
212 | |
|
213 | |
} |