dataprovider.java

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

JAVA
845
字号

            for (Iterator i = logEntries.iterator(); i.hasNext();) {
                SVNLogEntry logEntry = (SVNLogEntry) i.next();
                DataRevision revision = new DataRevision();
                revision.setRevision(logEntry.getRevision());
                revision.setAuthor(logEntry.getAuthor());
                revision.setDate(logEntry.getDate());
                revision.setComment(CommentsCache.getInstance(this.id,
                		this.getVerifiedUserName(), this.getVerifiedPassword()).getComment(logEntry.getRevision()));
                ret.add(revision);
            }
            
            return ret;
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);            
        } catch (SVNException e) {
            throw new DataProviderException(e);
        } catch (CommentsCacheException e) {
            throw new DataProviderException(e); 
        }                
    }
    
    
    public void setRelativeLocation(String id, String reposName) {
    	this.reposLocation = this.getLocation(id, reposName);
    }
    
    public String getLocation(String url, long pegRevision, long revision) throws DataProviderException {
        try {
            Collection locations = this.repository.getLocations(url, (Collection) null, pegRevision, new long[] {revision});
            if (locations.size() == 1) {
                String location = ((SVNLocationEntry) locations.iterator().next()).getPath();
                if (location.startsWith("/")) {
                    if (location.length() > 1) {
                        location = location.substring(1, location.length());
                    } else {
                        location = "";
                    }
                }             
                return this.normalizeLocation(location);
            } else {
                throw new DataProviderException("Unable to find location in " + revision + " revision of " + url + " in revision " + pegRevision);
            }
        } catch (SVNException e) {
            throw new DataProviderException(e);
        }
    }
    
    
    protected String normalizeLocation(String location) {
    	int index = location.indexOf(this.reposLocation);
        if (index == 0) {
        	if (location.equals(this.reposLocation)) {
        		location = "";
        	} else {
        		location = location.substring(this.reposLocation.length() + 1);
        	}
        }
        return location;
    }
    
    protected String getLocation(String id, String repositoryName) {
    	String res = null;
    	if (ConfigurationProvider.getInstance().isMultiRepositoryMode()) {
    		res = repositoryName;
    		res = this.formatLocation(res);
    	} else {
    		String url = ConfigurationProvider.getInstance().getRepositoryUrl();
    		if (!url.equals(id)) {
    			res = url.substring(id.length() + 1);
    		} else {
    			res = id;
    		}
    	}
    	return res;
    }
    
    protected String formatLocation(String location) {
		String res = null;
    	int index = location.indexOf("/");
    	if (index == -1) {
    		res = location;
    	} else {
    		res = location.substring(index + 1);
    	}    		
    	return res;
    }

    
    public DataRepositoryElement getInfo(String url, long revision) throws DataProviderException {
        try {
            DataRepositoryElement ret;
            SVNDirEntry entry = this.repository.info(url, revision);
            if (entry == null) {
                return null;
            } else {
                if (SVNNodeKind.DIR == entry.getKind()) {
                    ret = new DataDirectoryElement();
                } else {
                    ret = new DataFileElement();
                    ret.setSize(entry.getSize());                
                }
                ret.setName(entry.getName());
                ret.setRevision(entry.getRevision());
                ret.setAuthor(entry.getAuthor());
                ret.setDate(entry.getDate());
                ret.setComment(CommentsCache.getInstance(id, 
                		this.getVerifiedUserName(), this.getVerifiedPassword()).getComment(entry.getRevision()));
                return ret;
            }
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e); 
        } catch (CommentsCacheException e) {
            throw new DataProviderException(e); 
        }        
    }

    public DataFileElement getFile(String url, long revision) throws DataProviderException {
        try {
            DataFileElement file = (DataFileElement) this.getInfo(url, revision);
            Map fileProperties = new HashMap();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            this.repository.getFile(url, revision, fileProperties, outputStream);
            file.setContent(outputStream.toByteArray());
            
            String mimeType = (String) fileProperties.get(SVNProperty.MIME_TYPE);
            if (mimeType == null) {
                mimeType = SVNFileUtil.detectMimeType(new ByteArrayInputStream(file.getContent()));
            }
            
            if (DataProvider.MIME_TYPE_BINARY.equalsIgnoreCase(mimeType)) {
                file.setBinary(true);
            } else {
                file.setBinary(false);
            }
            
            return file;
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e); 
        } catch (IOException ex) {
            throw new DataProviderException(ex);
        }
    }
    
    public DataFile getFileData(String url, long revision) throws DataProviderException {
        try {
            DataFile file = new DataFile();
            Map fileProperties = new HashMap();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            this.repository.getFile(url, revision, fileProperties, outputStream);
            file.setContent(outputStream.toByteArray());
            
            String mimeType = (String) fileProperties.get(SVNProperty.MIME_TYPE);
            if (mimeType == null) {
                mimeType = SVNFileUtil.detectMimeType(new ByteArrayInputStream(file.getContent()));
            }
            
            if (DataProvider.MIME_TYPE_BINARY.equalsIgnoreCase(mimeType)) {
                file.setBinary(true);
            } else {
                file.setBinary(false);
            }
            
            return file;
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e); 
        } catch (IOException ex) {
            throw new DataProviderException(ex);
        }
    }

    public List getAnnotation(String url, long revision) throws DataProviderException {
        try {
            final List ret = new ArrayList();
            File tempDirectory = new File(ConfigurationProvider.getInstance().getTempDirectory());
            tempDirectory.mkdirs();
            SVNAnnotationGenerator generator = new SVNAnnotationGenerator(url, tempDirectory, 1, new ISVNEventHandler() {
                public void handleEvent(SVNEvent event, double progress) {
                }
                public void checkCancelled() throws SVNCancelException {
                }
            });

            this.repository.getFileRevisions(url, 1, revision, generator);
            generator.reportAnnotations(new ISVNAnnotateHandler() {
                public void handleLine(Date date, long revision, String author, String line) {
                    DataAnnotationElement annotationElement = new DataAnnotationElement(revision, date, author, line); 
                    ret.add(annotationElement);
                }
            }, null);
            
            return ret;
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e); 
        }
    }
    
    public DataRevision getRevisionInfo(long revision) throws DataProviderException {
        try {
            final DataRevision ret = new DataRevision();
            ret.setRevision(revision);
            this.repository.log(new String[] {""}, revision, revision, true, false, 1,
                    new ISVNLogEntryHandler() {
                        public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
                            try {
                                ret.setAuthor(logEntry.getAuthor());
                                ret.setDate(logEntry.getDate());
                                ret.setComment(CommentsCache.getInstance(id,
                                		getVerifiedUserName(), getVerifiedPassword()).getComment(logEntry.getRevision()));
                                
                                Map changes = logEntry.getChangedPaths();
                                for (Iterator i = changes.entrySet().iterator(); i.hasNext();) {
                                    Map.Entry entry = (Map.Entry) i.next();
                                    SVNLogEntryPath value = (SVNLogEntryPath) entry.getValue();                                                                        
                                    String copyPath = value.getCopyPath();
                                    long copyRevision = value.getCopyRevision();
                                    
                                    String path = value.getPath();
                                    if (path.startsWith("/") && (path.length() > 1)) {
                                        path = path.substring(1);
                                    }
                                    path = normalizeLocation(path);
                                    if (copyPath != null) {
                                    	copyPath = normalizeLocation(copyPath);
                                    }
                                    char type = value.getType();
                                    if ('A' == type) {
                                        ret.addChangedElement(DataRevision.TYPE_ADDED, path, copyPath, copyRevision);
                                    } else if ('D' == type) {
                                        ret.addChangedElement(DataRevision.TYPE_DELETED, path, copyPath, copyRevision);                                        
                                    } else if ('M' == type) {
                                        ret.addChangedElement(DataRevision.TYPE_MODIFIED, path, copyPath, copyRevision);                                        
                                    } else {
                                        ret.addChangedElement(DataRevision.TYPE_REPLACED, path, copyPath, copyRevision);                                        
                                    }
                                }
                            } catch (Exception e) {
                                throw new SVNException(SVNErrorMessage.UNKNOWN_ERROR_MESSAGE, e);
                            }
                        }
                    });
            return ret;
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e); 
        }
    }
    
    /**
     * Cheap detection of file type - only be mime type property. This property must be set
     * by SVN client
     * @param url
     * @param revision
     * @return
     * @throws DataProviderException
     */
    public boolean isBinaryFile(String url, long revision) throws DataProviderException {
        try {
            Map fileProperties = new HashMap();
            this.repository.getFile(url, revision, fileProperties, null);
            String mimeType = (String) fileProperties.get(SVNProperty.MIME_TYPE);
            
            if (DataProvider.MIME_TYPE_BINARY.equalsIgnoreCase(mimeType)) {
                return true;
            } else {
                return false;
            }        
        } catch (SVNAuthenticationException ae) {
            throw new AuthenticationException(ae);
        } catch (SVNException e) {
            throw new DataProviderException(e);
        }
    }

⌨️ 快捷键说明

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