svnkit查询、检测、获取文档功能样例

package com.service.statistics;

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

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.internal.wc.admin.SVNEntry;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;


/**查询svn目录  
* @Title SvnTest.java  
* @Description please descript this file
* @author <a href= "mailto:" style="color:##E0E;">bin</a>
* @date 2016年6月29日 下午2:39:13 
* @version V1.0   
*/
@Exclude
public class SvnService {
	private static final Logger logger = LoggerFactory.getLogger(SvnService.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 SvnService(String userName,String passwd) {
		init(userName, passwd);
	}
	
	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);
	}
	
	public SvnService(String userName,String passwd,String url){
		this(userName,passwd);
		this.url=url;
	}
	
	/**获取文档内容
	 * @param url
	 * @return
	 */
	public String checkoutFileToString(String url){//"", -1, null
		SVNRepository repository = createRepository(url);
		try {
			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;
		} catch (SVNException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	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 = "你的svn路径";
		SvnService svn = new SvnService("账号", "密码");
		String xml = svn.checkoutFileToString(url);
		
	}
}

需要相关包:svnkit、dom4j

实现查询路径是否存在

实现获取文件内容

实现获取子目录











  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值