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. sql脚本

    Windows下执行命令 \. d:\book.sql 这里使用了case when 这个小技巧来实现批量更新.//一个字段 UPDATE categories      SET display_or ...

  2. AngularJS学习篇(二十一)

    AngularJS 动画 AngularJS 提供了动画效果,可以配合 CSS 使用. AngularJS 使用动画需要引入 angular-animate.min.js 库. <!doctyp ...

  3. Chartist.js-同时画柱状图和折线图

    最近几天都在研究chartist,因为echarts生成的图是位图,导成PDF的时候不够清晰.而chartist是搜到的免费插件中呼声较高的,基于SVG. 今天主要是想举一些代码例子给大家,介绍下如何 ...

  4. [小程序开发] 微信小程序内嵌网页web-view开发教程

    为了便于开发者灵活配置小程序,微信小程序开放了内嵌网页能力.这意味着小程序的内容不再局限于pages和large,我们可以借助内嵌网页丰富小程序的内容.下面附上详细的开发教程(含视频操作以及注意事项) ...

  5. 前端基于react,后端基于.net core2.0的开发之路(1) 介绍

    文章提纲目录 1.前端基于react,后端基于.net core2.0的开发之路(1) 介绍 2.前端基于react,后端基于.net core2.0的开发之路(2) 开发环境的配置,注意事项,后端数 ...

  6. Linux服务器上安装vsftpd

    1.首先判断你服务器上是否安装了vsftpd rpm -q vsftpd   2.安装vsftpd yum -y install vsftpd   3.重启vsftpd service vsftpd ...

  7. PHP通过Zabbix API获取服务器监控信息

    开源监控系统Zabbix提供了丰富的API,供第三方系统调用. 基本步骤如下: 1.获取合法认证:连接对应Zabbix URL,并提供用户名和密码,HTTP方法为"POST",HT ...

  8. mac环境下支持PHP调试工具xdebug,不需要建项目server

    先让php支持xdebug 方式一: https://xdebug.org/download.php 下载相应的xdebug  可以到http://xdebug.org/wizard.php 把php ...

  9. tornado超时机制

    1.https://blog.lzhaohao.info/archive/build-a-comet-application-using-tornado-and-nginx/ 2.http://qin ...

  10. RocketMQ集群部署配置

    目标,使用2台机器部署RocketMQ多Master多Slave模式,异步复制集群模式. 第一步,修改/etc/hosts文件 192.168.116.115 rocketmq1 192.168.11 ...