001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.examples.repository;
013:
014: import java.io.ByteArrayOutputStream;
015: import java.io.IOException;
016: import java.util.HashMap;
017: import java.util.Iterator;
018: import java.util.Map;
019:
020: import org.tmatesoft.svn.core.SVNException;
021: import org.tmatesoft.svn.core.SVNNodeKind;
022: import org.tmatesoft.svn.core.SVNProperty;
023: import org.tmatesoft.svn.core.SVNURL;
024: import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
025: import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
026: import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
027: import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
028: import org.tmatesoft.svn.core.io.SVNRepository;
029: import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
030: import org.tmatesoft.svn.core.wc.SVNWCUtil;
031:
032: /*
033: * This example shows how to fetch a file and its properties from the repository
034: * at the latest (HEAD) revision . If the file is a text (either it has no
035: * svn:mime-type property at all or if has and the property value is text/-like)
036: * its contents as well as properties will be displayed in the console,
037: * otherwise - only properties.
038: * As an example here's a part of one of the
039: * program layouts (for the default url and file path used in the program):
040: *
041: * File property: svn:entry:revision=2802
042: * File property: svn:entry:checksum=435f2f0d33d12907ddb6dfd611825ec9
043: * File property: svn:wc:ra_dav:version-url=/repos/svnkit/!svn/ver/2795/trunk/www/license.html
044: * File property: svn:entry:last-author=alex
045: * File property: svn:entry:committed-date=2006-11-13T21:34:27.908657Z
046: * File property: svn:entry:committed-rev=2795
047: * File contents:
048: *
049: * <html>
050: * <head>
051: * <link rel="shortcut icon" href="img/favicon.ico"/>
052: * <title>SVNKit :: License</title>
053: * </head>
054: * <body>
055: * <h1>The TMate Open Source License.</h1>
056: * <pre>
057: * ......................................
058: * ---------------------------------------------
059: * Repository latest revision: 2802
060: */
061: public class DisplayFile {
062: /*
063: * args parameter is used to obtain a repository location URL, user's
064: * account name & password to authenticate him to the server, the file path
065: * in the rpository (the file path should be relative to the the
066: * path/to/repository part of the repository location URL).
067: */
068: public static void main(String[] args) {
069: /*
070: * Default values:
071: */
072: String url = "http://svn.svnkit.com/repos/svnkit/trunk";
073: String name = "anonymous";
074: String password = "anonymous";
075: String filePath = "www/license.html";
076: /*
077: * Initializes the library (it must be done before ever using the
078: * library itself)
079: */
080: setupLibrary();
081:
082: if (args != null) {
083: /*
084: * Obtains a repository location URL
085: */
086: url = (args.length >= 1) ? args[0] : url;
087: /*
088: * Obtains a file path
089: */
090: filePath = (args.length >= 2) ? args[1] : filePath;
091: /*
092: * Obtains an account name (will be used to authenticate the user to
093: * the server)
094: */
095: name = (args.length >= 3) ? args[2] : name;
096: /*
097: * Obtains a password
098: */
099: password = (args.length >= 4) ? args[3] : password;
100: }
101: SVNRepository repository = null;
102: try {
103: /*
104: * Creates an instance of SVNRepository to work with the repository.
105: * All user's requests to the repository are relative to the
106: * repository location used to create this SVNRepository.
107: * SVNURL is a wrapper for URL strings that refer to repository locations.
108: */
109: repository = SVNRepositoryFactory.create(SVNURL
110: .parseURIEncoded(url));
111: } catch (SVNException svne) {
112: /*
113: * Perhaps a malformed URL is the cause of this exception
114: */
115: System.err
116: .println("error while creating an SVNRepository for the location '"
117: + url + "': " + svne.getMessage());
118: System.exit(1);
119: }
120:
121: /*
122: * User's authentication information (name/password) is provided via an
123: * ISVNAuthenticationManager instance. SVNWCUtil creates a default
124: * authentication manager given user's name and password.
125: *
126: * Default authentication manager first attempts to use provided user name
127: * and password and then falls back to the credentials stored in the
128: * default Subversion credentials storage that is located in Subversion
129: * configuration area. If you'd like to use provided user name and password
130: * only you may use BasicAuthenticationManager class instead of default
131: * authentication manager:
132: *
133: * authManager = new BasicAuthenticationsManager(userName, userPassword);
134: *
135: * You may also skip this point - anonymous access will be used.
136: */
137: ISVNAuthenticationManager authManager = SVNWCUtil
138: .createDefaultAuthenticationManager(name, password);
139: repository.setAuthenticationManager(authManager);
140:
141: /*
142: * This Map will be used to get the file properties. Each Map key is a
143: * property name and the value associated with the key is the property
144: * value.
145: */
146: Map fileProperties = new HashMap();
147: ByteArrayOutputStream baos = new ByteArrayOutputStream();
148:
149: try {
150: /*
151: * Checks up if the specified path really corresponds to a file. If
152: * doesn't the program exits. SVNNodeKind is that one who says what is
153: * located at a path in a revision. -1 means the latest revision.
154: */
155: SVNNodeKind nodeKind = repository.checkPath(filePath, -1);
156:
157: if (nodeKind == SVNNodeKind.NONE) {
158: System.err.println("There is no entry at '" + url
159: + "'.");
160: System.exit(1);
161: } else if (nodeKind == SVNNodeKind.DIR) {
162: System.err
163: .println("The entry at '"
164: + url
165: + "' is a directory while a file was expected.");
166: System.exit(1);
167: }
168: /*
169: * Gets the contents and properties of the file located at filePath
170: * in the repository at the latest revision (which is meant by a
171: * negative revision number).
172: */
173: repository.getFile(filePath, -1, fileProperties, baos);
174:
175: } catch (SVNException svne) {
176: System.err
177: .println("error while fetching the file contents and properties: "
178: + svne.getMessage());
179: System.exit(1);
180: }
181:
182: /*
183: * Here the SVNProperty class is used to get the value of the
184: * svn:mime-type property (if any). SVNProperty is used to facilitate
185: * the work with versioned properties.
186: */
187: String mimeType = (String) fileProperties
188: .get(SVNProperty.MIME_TYPE);
189:
190: /*
191: * SVNProperty.isTextMimeType(..) method checks up the value of the mime-type
192: * file property and says if the file is a text (true) or not (false).
193: */
194: boolean isTextType = SVNProperty.isTextMimeType(mimeType);
195:
196: Iterator iterator = fileProperties.keySet().iterator();
197: /*
198: * Displays file properties.
199: */
200: while (iterator.hasNext()) {
201: String propertyName = (String) iterator.next();
202: String propertyValue = (String) fileProperties
203: .get(propertyName);
204: System.out.println("File property: " + propertyName + "="
205: + propertyValue);
206: }
207: /*
208: * Displays the file contents in the console if the file is a text.
209: */
210: if (isTextType) {
211: System.out.println("File contents:");
212: System.out.println();
213: try {
214: baos.writeTo(System.out);
215: } catch (IOException ioe) {
216: ioe.printStackTrace();
217: }
218: } else {
219: System.out
220: .println("File contents can not be displayed in the console since the mime-type property says that it's not a kind of a text file.");
221: }
222: /*
223: * Gets the latest revision number of the repository
224: */
225: long latestRevision = -1;
226: try {
227: latestRevision = repository.getLatestRevision();
228: } catch (SVNException svne) {
229: System.err
230: .println("error while fetching the latest repository revision: "
231: + svne.getMessage());
232: System.exit(1);
233: }
234: System.out.println("");
235: System.out
236: .println("---------------------------------------------");
237: System.out.println("Repository latest revision: "
238: + latestRevision);
239: System.exit(0);
240: }
241:
242: /*
243: * Initializes the library to work with a repository via
244: * different protocols.
245: */
246: private static void setupLibrary() {
247: /*
248: * For using over http:// and https://
249: */
250: DAVRepositoryFactory.setup();
251: /*
252: * For using over svn:// and svn+xxx://
253: */
254: SVNRepositoryFactoryImpl.setup();
255:
256: /*
257: * For using over file:///
258: */
259: FSRepositoryFactory.setup();
260: }
261: }
|