In this example we demonstrate how you can implement recursive fetching properties using a single request to the repository server. This technique is also discussed in Recursively fetching properties from a repository (low-level API).
1 /*
2 * ====================================================================
3 * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
4 *
5 * This software is licensed as described in the file COPYING, which
6 * you should have received as part of this distribution. The terms
7 * are also available at http://svnkit.com/license.html.
8 * If newer versions of this license are posted there, you may use a
9 * newer version instead, at your option.
10 * ====================================================================
11 */
12 package org.tmatesoft.svn.examples.repository;
13
14 import java.io.OutputStream;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.LinkedList;
18 import java.util.Map;
19
20 import org.tmatesoft.svn.core.SVNCommitInfo;
21 import org.tmatesoft.svn.core.SVNDepth;
22 import org.tmatesoft.svn.core.SVNException;
23 import org.tmatesoft.svn.core.SVNProperty;
24 import org.tmatesoft.svn.core.SVNPropertyValue;
25 import org.tmatesoft.svn.core.SVNURL;
26 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
27 import org.tmatesoft.svn.core.io.ISVNEditor;
28 import org.tmatesoft.svn.core.io.ISVNReporter;
29 import org.tmatesoft.svn.core.io.ISVNReporterBaton;
30 import org.tmatesoft.svn.core.io.SVNRepository;
31 import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
32 import org.tmatesoft.svn.core.io.diff.SVNDiffWindow;
33
34
35 /**
36 * @version 1.2.0
37 * @author TMate Software Ltd.
38 */
39 public class FetchPropertiesRecursivelyWithStatus {
40
41 /**
42 * Demonstration of recursive fetching properties from a repository tree using only low-level
43 * SVNKit API. This example fetches properties from the SVNKit's project repository which is publicly
44 * readable. So, you may copy-paste this example and run it as is.
45 */
46 public static void main(String[] args) {
47 //initialize the DAV protocol
48 DAVRepositoryFactory.setup();
49
50 try {
51 //create a repository access object
52 SVNRepository repos = SVNRepositoryFactory.create(SVNURL.parseURIEncoded("https://svn.svnkit.com/repos/svnkit/trunk"));
53
54 //get the current HEAD revision
55 final long rev = repos.getLatestRevision();
56
57 //with this reporter we just say to the repository server - please, send us the entire tree,
58 //we do not have any local data
59 ISVNReporterBaton reporter = new ISVNReporterBaton() {
60 public void report(ISVNReporter reporter) throws SVNException {
61
62 reporter.setPath("", null, rev, SVNDepth.INFINITY,
63 true/*we are empty, take us all like in checkout*/);
64
65 reporter.finishReport();
66
67 }
68 };
69
70 //our editor only stores properties of files and directories
71 PropFetchingEditor editor = new PropFetchingEditor();
72
73 //run an update-like request which never receives any real file deltas
74 repos.status(rev, null, SVNDepth.INFINITY, reporter, editor);
75
76 //now iterate over file and directory properties and print them out to the console
77 Map dirProps = editor.getDirsToProps();
78 for (Iterator dirPathsIter = dirProps.keySet().iterator(); dirPathsIter.hasNext();) {
79 String path = (String) dirPathsIter.next();
80 Map props = (Map) dirProps.get(path);
81 System.out.println("Directory '" + path + "' has the following properties:");
82 for (Iterator propNamesIter = props.keySet().iterator(); propNamesIter.hasNext();) {
83 String propName = (String) propNamesIter.next();
84 SVNPropertyValue propValue = (SVNPropertyValue) props.get(propName);
85 System.out.println(" '" + propName + "' = '" + SVNPropertyValue.getPropertyAsString(propValue) + "'");
86 }
87 System.out.println();
88 }
89
90 Map fileProps = editor.getFilesToProps();
91 for (Iterator filePathsIter = fileProps.keySet().iterator(); filePathsIter.hasNext();) {
92 String path = (String) filePathsIter.next();
93 Map props = (Map) fileProps.get(path);
94 System.out.println("File '" + path + "' has the following properties:");
95 for (Iterator propNamesIter = props.keySet().iterator(); propNamesIter.hasNext();) {
96 String propName = (String) propNamesIter.next();
97 SVNPropertyValue propValue = (SVNPropertyValue) props.get(propName);
98 System.out.println(" '" + propName + "' = '" + SVNPropertyValue.getPropertyAsString(propValue) + "'");
99 }
100 System.out.println();
101 }
102
103 } catch (SVNException svne) {
104 System.out.println(svne.getErrorMessage().getFullMessage());
105 System.exit(1);
106 }
107
108 }
109
110 private static class PropFetchingEditor implements ISVNEditor {
111 private LinkedList myDirectoriesStack = new LinkedList();
112 private Map myDirProps = new HashMap();
113 private Map myFileProps = new HashMap();
114
115 public void abortEdit() throws SVNException {
116 }
117
118 public void absentDir(String path) throws SVNException {
119 }
120
121 public void absentFile(String path) throws SVNException {
122 }
123
124 public void addFile(String path, String copyFromPath, long copyFromRevision) throws SVNException {
125 }
126
127 public SVNCommitInfo closeEdit() throws SVNException {
128 return null;
129 }
130
131 public void closeFile(String path, String textChecksum) throws SVNException {
132 }
133
134 public void deleteEntry(String path, long revision) throws SVNException {
135 }
136
137 public void openFile(String path, long revision) throws SVNException {
138 }
139
140 public void targetRevision(long revision) throws SVNException {
141 }
142
143 public void applyTextDelta(String path, String baseChecksum) throws SVNException {
144 }
145
146 public OutputStream textDeltaChunk(String path, SVNDiffWindow diffWindow) throws SVNException {
147 return null;
148 }
149
150 public void textDeltaEnd(String path) throws SVNException {
151 }
152
153 public void addDir(String path, String copyFromPath, long copyFromRevision) throws SVNException {
154 String absouluteDirPath = "/" + path;
155 myDirectoriesStack.push(absouluteDirPath);
156 }
157
158 public void changeDirProperty(String name, SVNPropertyValue value) throws SVNException {
159 //filter out svn:entry and svn:wc properties since we are interested in regular properties only
160 if (!SVNProperty.isRegularProperty(name)) {
161 return;
162 }
163
164 String currentDirPath = (String) myDirectoriesStack.peek();
165 Map props = (Map) myDirProps.get(currentDirPath);
166 if (props == null) {
167 props = new HashMap();
168 myDirProps.put(currentDirPath, props);
169 }
170 props.put(name, value);
171 }
172
173 public void changeFileProperty(String path, String propertyName, SVNPropertyValue propertyValue) throws SVNException {
174 //filter out svn:entry and svn:wc properties since we are interested in regular properties only
175 if (!SVNProperty.isRegularProperty(propertyName)) {
176 return;
177 }
178
179 String absolutePath = "/" + path;
180 Map props = (Map) myFileProps.get(absolutePath);
181 if (props == null) {
182 props = new HashMap();
183 myFileProps.put(absolutePath, props);
184 }
185 props.put(propertyName, propertyValue);
186 }
187
188 public void closeDir() throws SVNException {
189 myDirectoriesStack.pop();
190 }
191
192 public void openDir(String path, long revision) throws SVNException {
193 String absoluteDirPath = "/" + path;
194 myDirectoriesStack.push(absoluteDirPath);
195 }
196
197 public void openRoot(long revision) throws SVNException {
198 String absoluteDirPath = "/";
199 myDirectoriesStack.push(absoluteDirPath);
200 }
201
202 public Map getDirsToProps() {
203 return myDirProps;
204 }
205
206 public Map getFilesToProps() {
207 return myFileProps;
208 }
209 }
210 }