/*
* ====================================================================
* 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&nbsp;::&nbsp;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的更多相关文章

  1. 使用svnkit 的相关实例及相关问题汇总

    SVNKIT操作SVN版本库的完整例子 http://www.cnblogs.com/wangjiyuan/p/svnkitwanchenglizi.html#!comments 2.SVNClien ...

  2. SVNKit支持SSH连接

    SVNKit这个开源工具,用于Java语言访问SVN库,咋看的时候很方便,其实坑特别多.我在这里只想跟大家说一句,如果你还没有用过,请不要在生产环境使用这个东西了,兼容性问题搞死你(替换方案是直接用s ...

  3. SVNKIT的low api应用之修改库中文件内容(File modification)

    最近在做一个仓库管理系统,架构在svn之上.要求每一项操作要记录在log文件中,弄了很久起初感觉无法向库中的文本文件添加东西,就是修改库中的文本文件.于是采用了一个很笨的办法:    现将库中的log ...

  4. svnkit添加节点

    package com.repositoryclient.svnoptions; import org.tmatesoft.svn.core.SVNException; import org.tmat ...

  5. 使用SVNkit删除版本库的文件

    源网址:http://wiki.svnkit.com/Committing_To_A_Repository Editing Operation: commiting to a repository T ...

  6. SVNKIT一段代码的分析

    打印SVNkit版本库中的结构: 函数如下: 调用方法如下: listEntries(repository, ""); System.out.println("XXXXX ...

  7. Java svnkit check update commit

    import java.io.File; import org.apache.log4j.Logger;import org.tmatesoft.svn.core.SVNCommitInfo;impo ...

  8. SVNKit学习——使用低级别的API(ISVNEditor接口)直接操作Repository的目录和文件(五)

      本文是参考官方文档的实现,官方wiki:https://wiki.svnkit.com/Committing_To_A_Repository 本文核心使用的是ISVNEditor这个接口直接对Re ...

  9. SVNKit学习——Setting Up A Subversion Repository 创建仓库(三)

    所谓Setting Up A Subversion Repository,就是在Subversion所在的服务器上创建一个仓库,说白了就是在磁盘上建一个特殊的目录,这里我以windows举例. 1.使 ...

随机推荐

  1. [WPF源代码]QQ空间相册下载工具

    放一个WPF源代码,源代码地址 http://download.csdn.net/detail/witch_soya/6195987 代码没多少技术含量,就是用WPF做的一个QQ空间相册下载工具,效果 ...

  2. CSharp Algorithm - Replace multiplication operator with a method

    /* Author: Jiangong SUN */ How to replace multiplication operation with a method? For example, you h ...

  3. HDU2571:命运(DP)

    Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了! 可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个 ...

  4. Java基础02 方法与数据成员

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在Java基础01 从HelloWorld到面向对象,我们初步了解了对象(obje ...

  5. AspNet MVC4 教学-22:Asp.Net MVC4 Partial View 技术高速应用Demo

    A.创建Basic类型的MVC项目. B.Model文件夹下,创建文件: LoginModel.cs: using System; using System.Collections.Generic; ...

  6. golang各版本的变化

    https://golang.org/doc/https://golang.org/doc/go1.6https://golang.org/doc/go1.5https://golang.org/do ...

  7. 动静结合学内核:linux idle进程和init进程浅析

    刘柳 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 + titer1@qq.com 退休的贵族进程 ...

  8. GDI 总结三: CImage类使用

    前言          CImage类是基于GDI+的.可是这里为什么要讲归于GDI? 主要是基于这种考虑: 在GDI+环境中,我们能够直接使用GDI+ ,没多少必要再使用CImage类 可是,假设再 ...

  9. 内省(二)之BeanUtils工具类

    上一篇内省(Introspector)讲到的是采用JavaAPI中的类来操作bean及其属性,而Apache也开源了第三方框架来简化和丰富了对bean属性的操作,这个框架就是BeanUtils. 使用 ...

  10. QNX系统-关于delay函数与sleep函数的区别

    QNX是类unix系统.在c语言编程过程中,往往会用到delay或者sleep延时函数.两者之间在使用上有一定的区别!!! delay()是循环等待,该进程还在运行,占用处理器. sleep()不同, ...