1 package net.sf.statsvn.util.svnkit;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import net.sf.statsvn.output.SvnConfigurationOptions;
9 import net.sf.statsvn.util.ISvnProcessor;
10 import net.sf.statsvn.util.SvnPropgetUtils;
11
12 import org.tmatesoft.svn.core.SVNDepth;
13 import org.tmatesoft.svn.core.SVNException;
14 import org.tmatesoft.svn.core.SVNProperty;
15 import org.tmatesoft.svn.core.SVNURL;
16 import org.tmatesoft.svn.core.wc.ISVNPropertyHandler;
17 import org.tmatesoft.svn.core.wc.SVNClientManager;
18 import org.tmatesoft.svn.core.wc.SVNPropertyData;
19 import org.tmatesoft.svn.core.wc.SVNRevision;
20
21 /**
22 *
23 * Uses svnkit to do svn propget.
24 * @author jkealey, yogesh
25 *
26 */
27 public class SvnKitPropget extends SvnPropgetUtils {
28
29 protected class SvnKitPropertyHandler implements ISVNPropertyHandler {
30
31 protected List binaryFiles;
32 SvnKitPropget propgetUtils;
33
34 public SvnKitPropertyHandler(SvnKitPropget propgetUtils, List binaryFiles) {
35 this.binaryFiles = binaryFiles;
36 this.propgetUtils = propgetUtils;
37 }
38
39 protected List getBinaryFiles() {
40 return binaryFiles;
41 }
42
43 protected SvnKitPropget getPropgetUtils() {
44 return propgetUtils;
45 }
46
47 public void handleProperty(File file, SVNPropertyData data) throws SVNException {
48 if (isBinary(data)) {
49 String relativePath = file.getAbsoluteFile().getAbsolutePath().substring(
50 getPropgetUtils().getCheckoutDirectory().getAbsoluteFile().getAbsolutePath().length()+1);
51 binaryFiles.add(relativePath.replace(File.separatorChar, '/'));
52 }
53 }
54
55 public void handleProperty(long revision, SVNPropertyData data) throws SVNException {
56
57 }
58
59 public void handleProperty(SVNURL url, SVNPropertyData data) throws SVNException {
60 if (getPropgetUtils().isBinary(data)) {
61 String path = getPropgetUtils().getProcessor().getInfoProcessor().urlToRelativePath(url.toString());
62
63 binaryFiles.add(path.replace(File.separatorChar, '/'));
64 }
65 }
66
67 }
68
69 public SvnKitPropget(ISvnProcessor processor) {
70 super(processor);
71 }
72
73 public List getBinaryFiles() {
74 if (binaryFiles == null) {
75
76 binaryFiles = new ArrayList();
77 try {
78
79 getManager().getWCClient().doGetProperty(getCheckoutDirectory(), SVNProperty.MIME_TYPE, SVNRevision.WORKING, SVNRevision.WORKING,
80 SVNDepth.INFINITY, new SvnKitPropertyHandler(this, binaryFiles), null);
81 } catch (SVNException e) {
82 try {
83 handleSvnException(e);
84 } catch (IOException ex) {
85 }
86 }
87 }
88
89 return binaryFiles;
90 }
91
92 public File getCheckoutDirectory() {
93 return getSvnKitProcessor().getCheckoutDirectory();
94 }
95
96 public SVNClientManager getManager() {
97 return getSvnKitProcessor().getManager();
98 }
99
100 public SvnKitProcessor getSvnKitProcessor() {
101 return (SvnKitProcessor) getProcessor();
102 }
103
104 protected void handleSvnException(SVNException ex) throws IOException {
105 String msg = "svn propget " + ex.getMessage();
106 SvnConfigurationOptions.getTaskLogger().error(msg);
107 throw new IOException(msg);
108 }
109
110 protected boolean isBinary(SVNPropertyData data) {
111 return data != null && (data.getValue().toString().equals("application/octet-stream") || data.getValue().toString().indexOf("text/") < 0);
112 }
113
114 public boolean isBinaryFile(final String revision, final String filename) {
115 try {
116
117 SVNPropertyData data = getManager().getWCClient().doGetProperty(new File(filename), SVNProperty.MIME_TYPE, SVNRevision.parse(revision),
118 SVNRevision.parse(revision));
119 return isBinary(data);
120 } catch (SVNException e) {
121 try {
122 handleSvnException(e);
123 } catch (IOException ex) {
124 }
125 return false;
126 }
127 }
128 }
129