1 package net.sf.statsvn.util.svnkit;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import net.sf.statcvs.input.LogSyntaxException;
7 import net.sf.statsvn.output.SvnConfigurationOptions;
8 import net.sf.statsvn.util.ISvnProcessor;
9 import net.sf.statsvn.util.SvnInfoUtils;
10 import net.sf.statsvn.util.SvnVersionMismatchException;
11
12 import org.tmatesoft.svn.core.SVNDepth;
13 import org.tmatesoft.svn.core.SVNException;
14 import org.tmatesoft.svn.core.wc.SVNClientManager;
15 import org.tmatesoft.svn.core.wc.xml.SVNXMLInfoHandler;
16 import org.xml.sax.Attributes;
17
18 /**
19 *
20 * Performs svn info using svnkit.
21 *
22 * @author jkealey, yogesh
23 *
24 */
25 public class SvnKitInfo extends SvnInfoUtils {
26
27 protected static class SvnKitInfoHandler extends SvnInfoUtils.SvnInfoHandler {
28
29 public SvnKitInfoHandler(SvnInfoUtils infoUtils) {
30 super(infoUtils);
31 }
32
33 protected boolean isRootFolder(Attributes attributes) {
34 String path = attributes.getValue("path");
35
36 return (path.equals(".") || new File(path).equals(((SvnKitInfo) getInfoUtils()).getCheckoutDirectory()))
37 && attributes.getValue("kind").equals("dir");
38 }
39
40 }
41
42 public SvnKitInfo(ISvnProcessor processor) {
43 super(processor);
44 }
45
46 /**
47 * Verifies that the "svn info" command can return the repository root
48 * (info available in svn >= 1.3.0)
49 *
50 * @throws SvnVersionMismatchException
51 * if <tt>svn info</tt> failed to provide a non-empty repository root
52 */
53 public synchronized void checkRepoRootAvailable() throws SvnVersionMismatchException {
54
55 try {
56 loadInfo(true);
57 if (getRootUrl() != null)
58 return;
59 } catch (Exception e) {
60 SvnConfigurationOptions.getTaskLogger().info(e.getMessage());
61 }
62
63 throw new SvnVersionMismatchException(SVN_REPO_ROOT_NOTFOUND);
64 }
65
66 public File getCheckoutDirectory() {
67 return getSvnKitProcessor().getCheckoutDirectory();
68 }
69
70 public SVNClientManager getManager() {
71 return getSvnKitProcessor().getManager();
72 }
73
74 public SvnKitProcessor getSvnKitProcessor() {
75 return (SvnKitProcessor) getProcessor();
76 }
77
78 protected void handleSvnException(SVNException ex) throws IOException {
79 String msg = "svn info " + ex.getMessage();
80 SvnConfigurationOptions.getTaskLogger().error(msg);
81 throw new IOException(msg);
82 }
83
84 /**
85 * Loads the information from svn info if needed.
86 *
87 * @param bRootOnly
88 * load only the root?
89 * @throws LogSyntaxException
90 * if the format of the svn info is invalid
91 * @throws IOException
92 * if we can't read from the response stream.
93 */
94 protected void loadInfo(final boolean bRootOnly) throws LogSyntaxException, IOException {
95 if (isQueryNeeded(true
96 clearCache();
97
98 try {
99 SVNXMLInfoHandler handler = new SVNXMLInfoHandler(new SvnKitInfoHandler(this));
100 handler.setTargetPath(getCheckoutDirectory());
101 getManager().getWCClient().doInfo(getCheckoutDirectory(), null, null, SVNDepth.fromRecurse(!bRootOnly), null,
102 handler);
103 } catch (SVNException e) {
104 handleSvnException(e);
105 }
106 }
107 }
108
109 }