SVNKit getFileFromSVN
/*
* ====================================================================
* Copyright (c) 2004-2011 TMate Software Ltd. All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://svnkit.com/license.html
* If newer versions of this license are posted there, you may use a
* newer version instead, at your option.
* ====================================================================
*/
package org.tmatesoft.svn.examples.repository; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator; import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.SVNProperty;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil; /*
* This example shows how to fetch a file and its properties from the repository
* at the latest (HEAD) revision . If the file is a text (either it has no
* svn:mime-type property at all or if has and the property value is text/-like)
* its contents as well as properties will be displayed in the console,
* otherwise - only properties.
* As an example here's a part of one of the
* program layouts (for the default url and file path used in the program):
*
* File property: svn:entry:revision=2802
* File property: svn:entry:checksum=435f2f0d33d12907ddb6dfd611825ec9
* File property: svn:wc:ra_dav:version-url=/repos/svnkit/!svn/ver/2795/trunk/www/license.html
* File property: svn:entry:last-author=alex
* File property: svn:entry:committed-date=2006-11-13T21:34:27.908657Z
* File property: svn:entry:committed-rev=2795
* File contents:
*
* <html>
* <head>
* <link rel="shortcut icon" href="img/favicon.ico"/>
* <title>SVNKit :: License</title>
* </head>
* <body>
* <h1>The TMate Open Source License.</h1>
* <pre>
* ......................................
* ---------------------------------------------
* Repository latest revision: 2802
*/
public class DisplayFile {
/*
* args parameter is used to obtain a repository location URL, user's
* account name & password to authenticate him to the server, the file path
* in the rpository (the file path should be relative to the the
* path/to/repository part of the repository location URL).
*/
public static void main(String[] args) {
/*
* Default values:
*/
String url = "http://svn.svnkit.com/repos/svnkit/trunk";
String name = "anonymous";
String password = "anonymous";
String filePath = "www/license.html";
/*
* Initializes the library (it must be done before ever using the
* library itself)
*/
setupLibrary(); if (args != null) {
/*
* Obtains a repository location URL
*/
url = (args.length >= 1) ? args[0] : url;
/*
* Obtains a file path
*/
filePath = (args.length >= 2) ? args[1] : filePath;
/*
* Obtains an account name (will be used to authenticate the user to
* the server)
*/
name = (args.length >= 3) ? args[2] : name;
/*
* Obtains a password
*/
password = (args.length >= 4) ? args[3] : password;
}
SVNRepository repository = null;
try {
/*
* Creates an instance of SVNRepository to work with the repository.
* All user's requests to the repository are relative to the
* repository location used to create this SVNRepository.
* SVNURL is a wrapper for URL strings that refer to repository locations.
*/
repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
} catch (SVNException svne) {
/*
* Perhaps a malformed URL is the cause of this exception
*/
System.err
.println("error while creating an SVNRepository for the location '"
+ url + "': " + svne.getMessage());
System.exit(1);
} /*
* User's authentication information (name/password) is provided via an
* ISVNAuthenticationManager instance. SVNWCUtil creates a default
* authentication manager given user's name and password.
*
* Default authentication manager first attempts to use provided user name
* and password and then falls back to the credentials stored in the
* default Subversion credentials storage that is located in Subversion
* configuration area. If you'd like to use provided user name and password
* only you may use BasicAuthenticationManager class instead of default
* authentication manager:
*
* authManager = new BasicAuthenticationsManager(userName, userPassword);
*
* You may also skip this point - anonymous access will be used.
*/
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
repository.setAuthenticationManager(authManager); /*
* This Map will be used to get the file properties. Each Map key is a
* property name and the value associated with the key is the property
* value.
*/
SVNProperties fileProperties = new SVNProperties();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {
/*
* Checks up if the specified path really corresponds to a file. If
* doesn't the program exits. SVNNodeKind is that one who says what is
* located at a path in a revision. -1 means the latest revision.
*/
SVNNodeKind nodeKind = repository.checkPath(filePath, -1); if (nodeKind == SVNNodeKind.NONE) {
System.err.println("There is no entry at '" + url + "'.");
System.exit(1);
} else if (nodeKind == SVNNodeKind.DIR) {
System.err.println("The entry at '" + url
+ "' is a directory while a file was expected.");
System.exit(1);
}
/*
* Gets the contents and properties of the file located at filePath
* in the repository at the latest revision (which is meant by a
* negative revision number).
*/
repository.getFile(filePath, -1, fileProperties, baos); } catch (SVNException svne) {
System.err.println("error while fetching the file contents and properties: " + svne.getMessage());
System.exit(1);
} /*
* Here the SVNProperty class is used to get the value of the
* svn:mime-type property (if any). SVNProperty is used to facilitate
* the work with versioned properties.
*/
String mimeType = fileProperties.getStringValue(SVNProperty.MIME_TYPE); /*
* SVNProperty.isTextMimeType(..) method checks up the value of the mime-type
* file property and says if the file is a text (true) or not (false).
*/
boolean isTextType = SVNProperty.isTextMimeType(mimeType); Iterator iterator = fileProperties.nameSet().iterator();
/*
* Displays file properties.
*/
while (iterator.hasNext()) {
String propertyName = (String) iterator.next();
String propertyValue = fileProperties.getStringValue(propertyName);
System.out.println("File property: " + propertyName + "="
+ propertyValue);
}
/*
* Displays the file contents in the console if the file is a text.
*/
if (isTextType) {
System.out.println("File contents:");
System.out.println();
try {
baos.writeTo(System.out);
} catch (IOException ioe) {
ioe.printStackTrace();
}
} else {
System.out
.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.");
}
/*
* Gets the latest revision number of the repository
*/
long latestRevision = -1;
try {
latestRevision = repository.getLatestRevision();
} catch (SVNException svne) {
System.err.println("error while fetching the latest repository revision: " + svne.getMessage());
System.exit(1);
}
System.out.println("");
System.out.println("---------------------------------------------");
System.out.println("Repository latest revision: " + latestRevision);
System.exit(0);
} /*
* Initializes the library to work with a repository via
* different protocols.
*/
private static void setupLibrary() {
/*
* For using over http:// and https://
*/
DAVRepositoryFactory.setup();
/*
* For using over svn:// and svn+xxx://
*/
SVNRepositoryFactoryImpl.setup(); /*
* For using over file:///
*/
FSRepositoryFactory.setup();
}
}
http://wiki.svnkit.com/Printing_Out_File_Contents
http://svn.svnkit.com/repos/svnkit/tags/1.3.5/doc/examples/src/org/tmatesoft/svn/examples/repository/DisplayFile.java
SVNKit getFileFromSVN的更多相关文章
- 使用svnkit 的相关实例及相关问题汇总
SVNKIT操作SVN版本库的完整例子 http://www.cnblogs.com/wangjiyuan/p/svnkitwanchenglizi.html#!comments 2.SVNClien ...
- SVNKit支持SSH连接
SVNKit这个开源工具,用于Java语言访问SVN库,咋看的时候很方便,其实坑特别多.我在这里只想跟大家说一句,如果你还没有用过,请不要在生产环境使用这个东西了,兼容性问题搞死你(替换方案是直接用s ...
- SVNKIT的low api应用之修改库中文件内容(File modification)
最近在做一个仓库管理系统,架构在svn之上.要求每一项操作要记录在log文件中,弄了很久起初感觉无法向库中的文本文件添加东西,就是修改库中的文本文件.于是采用了一个很笨的办法: 现将库中的log ...
- svnkit添加节点
package com.repositoryclient.svnoptions; import org.tmatesoft.svn.core.SVNException; import org.tmat ...
- 使用SVNkit删除版本库的文件
源网址:http://wiki.svnkit.com/Committing_To_A_Repository Editing Operation: commiting to a repository T ...
- SVNKIT一段代码的分析
打印SVNkit版本库中的结构: 函数如下: 调用方法如下: listEntries(repository, ""); System.out.println("XXXXX ...
- Java svnkit check update commit
import java.io.File; import org.apache.log4j.Logger;import org.tmatesoft.svn.core.SVNCommitInfo;impo ...
- SVNKit学习——使用低级别的API(ISVNEditor接口)直接操作Repository的目录和文件(五)
本文是参考官方文档的实现,官方wiki:https://wiki.svnkit.com/Committing_To_A_Repository 本文核心使用的是ISVNEditor这个接口直接对Re ...
- SVNKit学习——Setting Up A Subversion Repository 创建仓库(三)
所谓Setting Up A Subversion Repository,就是在Subversion所在的服务器上创建一个仓库,说白了就是在磁盘上建一个特殊的目录,这里我以windows举例. 1.使 ...
随机推荐
- ViewPageAsImage
var ViewPageAsImage = function(target, label) { var setting = { min_height: 4, min_width: 4 ...
- 在WINDOWS下 三步快速配置 eclipse c++ 环境
所需软件 1.Eclipse IDE for C/C++ Developers http://www.eclipse.org/downloads/packages/eclipse-ide-cc-dev ...
- The type MultipartEntity is deprecated
在HttpCient4.3之前上传文件主要使用MultipartEntity这个类,但如今这个类已经不在推荐使用了(过时了).随之替代它的类是MultipartEntityBuilder.关于Mult ...
- Swift - .plist文件数据的读取和存储
每次在Xcode中新建一个iOS项目后,都会自己产生一个.plist文件,里面记录项目的一些配置信息.我们也可以自己创建.plist文件来进行数据的存储和读取. .plist文件其实就是一个XML格式 ...
- 常用字符串string
字符串 特性: 不可变性 当一个字符串赋新值时,老值依旧在(靠GC来回收) 可以看做是char类型的只读数组 eg:string str="abcd"; str[0]---> ...
- UVA 10317 - Equating Equations (背包)
Problem F Equating Equations Input: standard input Output: standard output Time Limit: 6 seconds Mem ...
- jquery遍历筛选数组的几种方法和遍历解析json对象
jquery grep()筛选遍历数组 $().ready( function(){ var array = [1,2,3,4,5,6,7,8,9]; var fil ...
- js打印
js打印,其实是打印当前页面的内容,是调用 系统的js方法,来弹出 打印设置窗口,用法很简单. window.print()就行,有的考虑到 浏览器兼容性问题,会用到document.execComm ...
- 基于visual Studio2013解决C语言竞赛题之1017次数
题目 解决代码及点评 /* 功能:有人说在400, 401, 402, ...499这些数中4这个数字共出现112次,请编程序判定这 种说法是否正确.若正确请打印出'YE ...
- Swift - final关键字的介绍,以及使用场景
final关键字在大多数的编程语言中都存在,表示不允许对其修饰的内容进行继承或者重新操作.Swift中,final关键字可以在class.func和var前修饰. 通常大家都认为使用final可以更好 ...