package com.leadbank.oprPlatform.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class SvnUtil{

private Logger logger = LoggerFactory.getLogger(SvnUtil.class);

// @Inject(instance = PropertyUtil.class)
// private PropertyUtil propertyUtil;

static {
DAVRepositoryFactory.setup();
}

private SVNClientManager manager;
private SVNUpdateClient updateClient;
private String url;
private String userName;
private String passwd;

public SvnUtil(String userName, String passwd) {
init(userName, passwd);
}

public SvnUtil(String userName, String passwd, String url){
this(userName,passwd);
this.url=url;
}

private void init(String userName,String passwd){
DefaultSVNOptions options = new DefaultSVNOptions();
manager = SVNClientManager.newInstance(options);
manager = SVNClientManager.newInstance(options,userName,passwd);
updateClient = manager.getUpdateClient();
updateClient.setIgnoreExternals(false);
}

/**获取文档内容
* @param url
* @return
*/
public String checkoutFileToString(String url) throws SVNException {//"", -1, null
SVNRepository repository = createRepository(url);
SVNDirEntry entry = repository.getDir("", -1, false, null);
int size = (int)entry.getSize();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(size);
SVNProperties properties = new SVNProperties();
repository.getFile("", -1, properties, outputStream);
String doc = new String(outputStream.toByteArray(), Charset.forName("utf-8"));
return doc;
}

public boolean toParantFolder(){
if(url!=null){
StringBuffer sb = new StringBuffer(url);
if(url.endsWith("/")){
sb.deleteCharAt(sb.length()-1);
}
int index = sb.lastIndexOf("/");
url=sb.substring(0, index);
return true;
}
return false;
}

/**进入子目录
* @param folder
* @return
*/
public boolean toChildFolder(String folder){
if(url!=null){
StringBuffer sb = new StringBuffer(url);
boolean a = url.endsWith("/");
boolean b = folder.startsWith("/");
if(a^b){
sb.append(folder);
}else if(a&b){
sb.deleteCharAt(sb.length()-1);
sb.append(folder);
}else{
sb.append('/').append(folder);
}
if(checkPath(sb.toString())==1){
this.url=sb.toString();
return true;
}
}
return false;
}

/**获取当前目录下的子目录和文件
* @return
* @throws SVNException
*/
public List<SVNDirEntry> listFolder() throws SVNException {
return listFolder(url);
}

/**列出指定SVN 地址目录下的子目录
* @param url
* @return
* @throws SVNException
*/
public List<SVNDirEntry> listFolder(String url){
if(checkPath(url)==1){

SVNRepository repository = createRepository(url);
try {
Collection<SVNDirEntry> list = repository.getDir("", -1, null, (List<SVNDirEntry>)null);
List<SVNDirEntry> dirs = new ArrayList<SVNDirEntry>(list.size());
dirs.addAll(list);
return dirs;
} catch (SVNException e) {
logger.error("listFolder error",e);
}

}
return null;
}

private SVNRepository createRepository(String url){

try {
return manager.createRepository(SVNURL.parseURIEncoded(url), true);
} catch (SVNException e) {
logger.error("createRepository error",e);
}
return null;
}

/**检查路径是否存在
* @param url
* @return 1:存在 0:不存在 -1:出错
*/
public int checkPath(String url){
SVNRepository repository = createRepository(url);
SVNNodeKind nodeKind;
try {
nodeKind = repository.checkPath("", -1);
boolean result = nodeKind == SVNNodeKind.NONE ? false : true;
if(result) return 1;
} catch (SVNException e) {
logger.error("checkPath error",e);
return -1;
}
return 0;
}

public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public static void main(String[] args) throws SVNException {
String url = "http://10.1.1.135/svn/cloudPlatform/oprPlatform/trunk/oprPlatform-web/src/main/webapp/WEB-INF/web.xml";
SvnUtil svn = new SvnUtil("xxx", "xxx");
String xml = svn.checkoutFileToString(url);
System.out.print(xml);
}
}

Java代码操作SVN的更多相关文章

  1. Java代码操作HDFS测试类

    1.Java代码操作HDFS需要用到Jar包和Java类 Jar包: hadoop-common-2.6.0.jar和hadoop-hdfs-2.6.0.jar Java类: java.net.URL ...

  2. 使用java代码操作Redis

    1导入pom.xml依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis ...

  3. java代码操作Redis

    1.导入需要的pom依赖 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEn ...

  4. Java代码操作zookeeper

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  5. 分享知识-快乐自己:java代码 操作 solr

    POM 文件: <!-- solr客户端 --> <dependency> <groupId>org.apache.solr</groupId> < ...

  6. 大数据之路week07--day01(HDFS学习,Java代码操作HDFS,将HDFS文件内容存入到Mysql)

    一.HDFS概述 数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管理多台机器上的文件,这就是分布式文件管理系统 ...

  7. java:nginx(java代码操作ftp服务器)

    1.检查是否安装了vsftpd [root@linux01 ~]# rpm -qa|grep vsftpd 2.安装vsftpd [root@linux01 ~]# yum -y install vs ...

  8. Java代码操作HDFS(在/user/root/下面創建目錄)

    1.创建HDFS目录并打成jar包 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache.h ...

  9. kerberos下JAVA代码操作hbase的方式(客户端方式,应用程序方式)

    (一)如果部署JAVA 代码的主机用户获取了kerberos权限情况下 假设主机名是:client su - client 登录主机后 kinit -kt /keytab的路径/client.keyt ...

随机推荐

  1. code force 424 A - Office Keys

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  2. 关于安卓手机的牛逼软件termux使用

    最近在学着用linux,偶尔发现了一款神奇的软件--termux,termux是一款来自国外的终端模拟器,是运行在内部存储上的程序(不在内存卡上),功能比较强大,启动程序之后会进入命令行终端,需要基本 ...

  3. CI脚本异常退出问题定位

    背景 在CI脚本中,使用类似如下脚本进行项目编译的计时,但在执行过程中,有时会出现CI脚本(命名为ci.sh)未完全执行的情况: #!/bin/bash -e sleep_time=$1 start_ ...

  4. Hive安装和部署

    在root的用户下搭建的 构建hive之前必须要先搭建好hadoop才可以. hive定义了一种类似SQL查询语言--HQL 类似SQL ,但是不完全相同 Hive是一个数据仓库,它部署在Hadoop ...

  5. Nginx HTTP 核心模块

    原文链接:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=17238776&id=2982697aio 语法:aio [ ...

  6. Xcode修改个性化注释

    1.首先找到Xcode进入包内容 2.依次进入/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Develo ...

  7. 你有什么理由还不选择阿里云服务器呢--从阿里云发布自研商用关系型数据库POLARDB想到的

    最近几天,阿里云发布自研商用关系型数据库POLARDB的消息可谓是重磅炸弹啊.借用官方宣传的话就是:6倍性能于MySQL并100%兼容/100TB存储容量/2分钟创建只读副本/3分钟创建容灾实例,第三 ...

  8. Java实现邮箱发送验证码

    第一步,导入JAR包,JAR包下载地址[http://pan.baidu.com/s/1kVRvGyF] 正式代码: 首先书写一个工具类: MailUtil import javax.mail.*; ...

  9. Linux系列教程(二十一)——Linux的bash基本功能

    上篇博客我们介绍了什么是shell,以及编写shell脚本的两种执行方式.我们知道在敲命令的时候,有很多快捷键,比如tab键能补全命令,在比如为什么我们直接敲 ll 命令能显示目录的长格式,其实这是b ...

  10. C# WPF动点任意移动气泡画法(解决方案使用到数学勾股定理、正弦定理、向量知识)。

    许久没写博客了,最近在研究WPF下气泡的画法,研发过程还是比较艰辛的(主要是复习了高中的数学知识,MMP全忘光了),这篇博客主要是提供一个思路给大家参考,如果有大神还有更好的解决方案可以不吝您的言论尽 ...