dataprovider.java

来自「web版的SVN客户端」· Java 代码 · 共 845 行 · 第 1/3 页

JAVA
845
字号
    
    public void testConnection() throws AuthenticationException {
        try {
            this.repository.testConnection();
        } catch (SVNException e) {
            throw new AuthenticationException(e);
        }
    }
    
    public DataChangedElement createDirectory(String url, String name, String comment) throws DataProviderException {
    	try {
            DataChangedElement ret = new DataChangedElement();
            SVNCommitClient commitClient = new SVNCommitClient(this.repository.getAuthenticationManager(), new DefaultSVNOptions());
            SVNCommitInfo commitInfo = null;
            if (isCompoundDir(name)) {
            	commitInfo = this.createCompoundDir(url, name, comment, commitClient);            	          	
            } else {
            	SVNURL absoluteUrl = this.repository.getLocation();
                absoluteUrl = absoluteUrl.appendPath(url, false);
                absoluteUrl = absoluteUrl.appendPath(name, false);                                               
                commitInfo = commitClient.doMkDir(new SVNURL[] {absoluteUrl}, comment);	
            }
            
            ret.setName(name);
            ret.setRevision(commitInfo.getNewRevision());
            ret.setAuthor(commitInfo.getAuthor());
            ret.setDate(commitInfo.getDate());
            ret.setComment(CommentsCache.getInstance(this.id,
            		this.getVerifiedUserName(), this.getVerifiedPassword()).getComment(commitInfo.getNewRevision()));
            return ret;
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e);
        } catch (CommentsCacheException e) {
            throw new DataProviderException(e); 
        }
    }
    
    protected SVNCommitInfo createCompoundDir(String url, String name, String comment, SVNCommitClient client) throws DataProviderException {    	
    	SVNCommitInfo commitInfo = null;
    	File tempDirForImport = this.getTempDirForImport(name);
    	String strDirForImport =  tempDirForImport.getAbsolutePath();
    	try {     	    		
    		SVNURL absoluteUrl = this.repository.getLocation();    	
    		absoluteUrl = absoluteUrl.appendPath(url, false);
    		
    		String tmpPaths[] = null; 
    		int index = name.indexOf("/");
    		if (index != -1) {
    			tmpPaths = name.split("/");
    		} else {
    			tmpPaths = name.split("\\\\");
    		}    		
    		List paths = new ArrayList();
    		for (int i = 0; i < tmpPaths.length; i++) {
    			paths.add(tmpPaths[i]);
    		}
    		
    		while (true) {
    			try {
    				commitInfo = client.doImport(tempDirForImport, absoluteUrl, comment, true);
    				break;
    			} catch (SVNException se) {
    				if (paths.size() > 0) {
    					String prefix = (String) paths.remove(0);
    					absoluteUrl = absoluteUrl.appendPath(prefix, false);
    					tempDirForImport = new File(tempDirForImport.getAbsolutePath(), prefix);
    				}
    			}
    		}
    		
    	}  catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e);
        } finally {
        	if (tempDirForImport != null) {
        		FileUtil.deleteDirectory(new File(strDirForImport)); 
        	}
        } 
        return commitInfo;
    }
    
    protected File getTempDirForImport(String name) {
    	String temporaryDirectory = ConfigurationProvider.getInstance().getTempDirectory();
        String destinationDirectory = temporaryDirectory + "/" + UUID.getUUID();
        File res = new File(destinationDirectory, name);
        res.mkdirs();  
        
        int index = name.indexOf("/");
        if (index == -1) {
        	index = name.indexOf("\\");
        }
        return new File(destinationDirectory);
    }
    
    protected boolean isCompoundDir(String path) {
    	return (path.indexOf("/") != -1 || path.indexOf("\\") != -1) ? true : false;
    }
    
    public DataChangedElement addFile(String url, String path, String comment) throws DataProviderException {
        try {
            DataChangedElement ret = new DataChangedElement();
            SVNCommitClient commitClient = new SVNCommitClient(this.repository.getAuthenticationManager(), new DefaultSVNOptions());
            File file = new File(path);
            String filename = file.getName();
            if (file.exists()) {
                SVNURL absoluteUrl = this.repository.getLocation();
                absoluteUrl = absoluteUrl.appendPath(url, false);
                absoluteUrl = absoluteUrl.appendPath(filename, false);
                SVNCommitInfo commitInfo = commitClient.doImport(file, absoluteUrl, comment, false);
                ret.setName(filename);
                ret.setRevision(commitInfo.getNewRevision());
                ret.setAuthor(commitInfo.getAuthor());
                ret.setDate(commitInfo.getDate());
                ret.setComment(CommentsCache.getInstance(this.id,
                		this.getVerifiedUserName(), this.getVerifiedPassword()).getComment(commitInfo.getNewRevision()));                
            } else {
                throw new DataProviderException(filename + " could not be found"); 
            }
            return ret;
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e);
        } catch (CommentsCacheException e) {
            throw new DataProviderException(e); 
        }                    
    }
    
    public DataChangedElement delete(String url, List elements, String comment) throws DataProviderException {
        try {
            DataChangedElement ret = new DataChangedElement();
            SVNCommitClient commitClient = new SVNCommitClient(this.repository.getAuthenticationManager(), new DefaultSVNOptions());
            SVNURL absoluteUrl = this.repository.getLocation();
            absoluteUrl = absoluteUrl.appendPath(url, false);
            SVNURL[] deletedElements = new SVNURL[elements.size()];
            for (int i = 0; i < elements.size(); i++) {
                deletedElements[i] = absoluteUrl.appendPath((String) elements.get(i), false);
            }
            SVNCommitInfo commitInfo = commitClient.doDelete(deletedElements, comment);
            ret.setName(UrlUtil.getLastPathElement(url));
            ret.setRevision(commitInfo.getNewRevision());
            ret.setAuthor(commitInfo.getAuthor());
            ret.setDate(commitInfo.getDate());
            ret.setComment(CommentsCache.getInstance(this.id,
            		this.getVerifiedUserName(), this.getVerifiedPassword()).getComment(commitInfo.getNewRevision()));                
            return ret;
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e);
        } catch (CommentsCacheException e) {
            throw new DataProviderException(e); 
        }                    
    }
    
    public DataChangedElement commitFile(String url, String path, String comment) throws DataProviderException {
        InputStream fileReader = null;        
        try {
            DataChangedElement ret = new DataChangedElement();            
            File file = new File(path);
            String filename = file.getName();
            if (file.exists()) {
                long size = file.length();
                fileReader = new FileInputStream(file);
                String dirUrl = UrlUtil.getPreviousFullPath(url);
                ISVNEditor editor = repository.getCommitEditor(comment, new WorkspaceMediator());                
                SVNCommitInfo commitInfo = SVNUtils.modifyFile(editor, dirUrl, url, fileReader, size);
                ret.setName(filename);
                ret.setRevision(commitInfo.getNewRevision());
                ret.setAuthor(commitInfo.getAuthor());
                ret.setDate(commitInfo.getDate());
                ret.setComment(CommentsCache.getInstance(this.id,
                		this.getVerifiedUserName(), this.getVerifiedPassword()).getComment(commitInfo.getNewRevision()));
                return ret;
            } else {
                throw new DataProviderException(filename + " could not be found");
            }
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (Exception e) {
            throw new DataProviderException(e);
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (Exception e) {
                }
            }
        }
    }
    
    public List compareDirectoryRevisions(String url, long startRevision, long endRevision) throws DataProviderException {
        try {
            Reporter reporter = new Reporter(startRevision);
            Editor editor = new Editor();
            
            SVNURL repositoryUrl = this.repository.getLocation();
            try {
                this.repository.setLocation(repositoryUrl.appendPath(url, false), false);
                this.repository.status(endRevision, null, true, reporter, editor);
            } finally {
                this.repository.setLocation(repositoryUrl, false);
            }

            List items = editor.getChangedItems();
            for (Iterator i = items.iterator(); i.hasNext();) {
                DataDirectoryCompareItem item = (DataDirectoryCompareItem) i.next();
                if (DataDirectoryCompareItem.OPERATION_DELETE == item.getOperation()) {
                	String entryName = null;
                	if ("".equals(url)) {
                		entryName = item.getPath();
                	} else {
                		entryName = url + "/" + item.getPath();
                	}                	
                	SVNDirEntry entry = this.repository.info(entryName, startRevision);                	
                    item.setDirectory(SVNNodeKind.DIR.equals(entry.getKind()));
                    item.setOldRevision(entry.getRevision());
                    item.setNewRevision(-1);
                }
            }
            return items;
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e);
        }            
    }
    
    public String getFileDifference(String url, long startRevision, long endRevision, String pathToStart, String pathToEnd) throws DataProviderException {
        try {
            ISVNDiffGenerator diffGenerator = new DefaultSVNDiffGenerator();         
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            diffGenerator.setEncoding(ConfigurationProvider.getInstance().getDefaultEncoding());
            diffGenerator.displayFileDiff(url, new File(pathToStart), new File(pathToEnd), Long.toString(startRevision), Long.toString(endRevision), null, null, os);
            return os.toString(ConfigurationProvider.getInstance().getDefaultEncoding());         
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (Exception e) {
            throw new DataProviderException(e);
        }                        
    }
    
    public int checkUrl(String url, long revision) throws DataProviderException {
        try {
            SVNNodeKind result = this.repository.checkPath(url, revision);
            if (SVNNodeKind.NONE.equals(result)) {
                return IDataProvider.NOT_EXIST;
            } else if (SVNNodeKind.DIR.equals(result)) {
                return IDataProvider.DIRECTORY;
            } else if (SVNNodeKind.FILE.equals(result)) {
                return IDataProvider.FILE;
            } else {
                return IDataProvider.UNKNOWN;                
            }
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e);
        }                      
    }
    
    protected String getVerifiedUserName() {
    	if (ConfigurationProvider.getInstance().isMultiRepositoryMode()) {
    		return this.userCredentials.getUsername();
    	} else {
    		return ConfigurationProvider.getInstance().getUsername();
    	}
    }
    
    protected String getVerifiedPassword() {
    	if (ConfigurationProvider.getInstance().isMultiRepositoryMode()) {
    		return this.userCredentials.getPassword();
    	} else {
    		return ConfigurationProvider.getInstance().getPassword();
    	}
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?