Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2011-2012 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.opensource.org/licenses/ecl2.php Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.kuali.maven.plugins.externals;
 
 import java.io.File;
 import java.util.List;
 
 
 public class SVNUtils {
 
 	private static final Logger LOGGER = LoggerFactory.getLogger(SVNUtils.class);
 	private static final String EMPTY_STRING = "";
 	private static final String EXTERNALS_PROPERTY_NAME = "svn:externals";
 	private static final String EXTERNALS_COMMENT = "#";
 	private static final String DELETE_EXTERNALS_COMMIT_MESSAGE = "Delete externals";
 	private static final String CREATE_EXTERNALS_COMMIT_MESSAGE = "Create externals";
 	private static final String LINEFEED = "\n";
 	private static final String SPACE = " ";
 
 	protected static SVNUtils instance;
 
 	protected SVNUtils() {
 		super();
 	}
 
 	protected void initialize() {
 		SVNRepositoryFactoryImpl.setup();
 		DAVRepositoryFactory.setup();
 		FSRepositoryFactory.setup();
 	}
 
 	public synchronized static SVNUtils getInstance() {
 		if ( == null) {
 			 = new SVNUtils();
 		}
 		return ;
 	}
 
 	public void showExternals(List<SVNExternalexternals) {
 		for (SVNExternal e : externals) {
 			StringBuilder sb = new StringBuilder();
 			sb.append("[" + e.getPath());
 			sb.append(", " + e.getWorkingCopyPath());
 			sb.append(", " + e.getUrl());
 			sb.append("]");
 		}
 	}

Copy src to dst creating parent directories as needed. An exception is thrown if dst already exists.
 
 	public SVNCommitInfo copy(String srcString dst) {
 		return copy(srcnulldstnull);
 	}

Copy src to dst creating parent directories as needed. An exception is thrown if dst already exists.
 
	public SVNCommitInfo copy(String srcString dstString msg) {
		return copy(srcnulldstmsg);
	}

Copy src at the indicated revision to dst creating parent directories as needed. An exception is thrown if dst already exists.
	public SVNCommitInfo copy(String srcLong revisionString dst) {
		return copy(srcrevisiondstnull);
	}

Copy src at the indicated revision to dst creating parent directories as needed. An exception is thrown if dst already exists.
	public SVNCommitInfo copy(String srcLong revisionString dstString msg) {
		Copy copy = new Copy();
		copy.setSource(src);
		copy.setRevision(revision);
		copy.setDestination(dst);
		copy.setMessage(msg);
		return copy(copy);
	}
	public SVNCommitInfo setExternals(String urlList<SVNExternalexternals) {
		return setExternals(urlexternalsnull);
	}
	public SVNCommitInfo setExternals(String urlList<SVNExternalexternalsString message) {
		return setExternals(urlexternalsmessagenullnull);
	}
	public SVNCommitInfo setExternals(String urlList<SVNExternalexternalsString messageString usernameString password) {
		SVNClientManager manager = SVNClientManager.newInstance(nullusernamepassword);
		SVNWCClient client = manager.getWCClient();
		String commitMessage = StringUtils.isBlank(message) ?  : message;
		SVNURL svnUrl = getSvnUrl(url);
		for (SVNExternal external : externals) {
			sb.append(external.getPath() + " " + external.getUrl() + "\n");
		}
		SVNPropertyValue value = SVNPropertyValue.create(sb.toString());
		try {
			return client.doSetProperty(svnUrlvalue.commitMessagenulltruenull);
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
		return deleteExternals(urlnull);
	}
	public SVNCommitInfo deleteExternals(String urlString message) {
		return deleteExternals(urlmessagenullnull);
	}
	public SVNCommitInfo deleteExternals(String urlString messageString usernameString password) {
		SVNClientManager manager = SVNClientManager.newInstance(nullusernamepassword);
		SVNWCClient client = manager.getWCClient();
		String commitMessage = StringUtils.isBlank(message) ?  : message;
		SVNURL svnUrl = getSvnUrl(url);
		try {
			return client.doSetProperty(svnUrlnull.commitMessagenulltruenull);
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
	public SVNCommitInfo copy(Copy copy) {
		SVNClientManager manager = SVNClientManager.newInstance(nullcopy.getUsername(), copy.getPassword());
		SVNCopyClient client = manager.getCopyClient();
		SVNURL dstUrl = getSvnUrl(copy.getDestination());
		SVNURL srcUrl = getSvnUrl(copy.getSource());
		if (copy.getRevision() != null) {
			revision = SVNRevision.create(copy.getRevision());
		}
		String msg = copy.getMessage();
		if (StringUtils.isBlank(msg)) {
			String r = (copy.getRevision() != null) ? "@" + revision : "";
			msg = "Copy " + copy.getSource() + r + " to " + copy.getDestination();
		}
		SVNCopySource svnCopySource = new SVNCopySource(.revisionsrcUrl);
		SVNCopySource[] sources = new SVNCopySource[] { svnCopySource };
		try {
			return client.doCopy(sourcesdstUrlfalsetruetruemsgnull);
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}

Return the Subversion url that corresponds with the local working copy
	public String getUrl(File workingCopyPath) {
		SVNInfo info = getInfo(workingCopyPath);
		return info.getURL().toDecodedString();
	}

Return any svn:externals associated with the given url. Returns an empty list if there are none. Never returns null.
		try {
			SVNURL svnUrl = getSvnUrl(url);
			return getExternals(datanull);
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
	public long checkout(String urlFile dstPathString usernameString password) {
		try {
			SVNClientManager manager = SVNClientManager.newInstance(nullusernamepassword);
			SVNUpdateClient client = manager.getUpdateClient();
			client.setIgnoreExternals(false);
			SVNURL svnUrl = getSvnUrl(url);
			return client.doCheckout(svnUrldstPath...false);
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
	public SVNCommitInfo commit(List<FileworkingCopyPathsString messageString usernameString password) {
		File[] fileArray = workingCopyPaths.toArray(new File[workingCopyPaths.size()]);
		return commit(fileArraymessageusernamepassword);
	}
	public SVNCommitInfo commit(File[] workingCopyPathsString messageString usernameString password) {
		try {
			SVNClientManager manager = SVNClientManager.newInstance(nullusernamepassword);
			SVNCommitClient client = manager.getCommitClient();
			client.setIgnoreExternals(false);
			return client.doCommit(workingCopyPathstruemessagenullnulltruefalse.);
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
	public SVNCommitInfo commit(File workingCopyPathString messageString usernameString password) {
		return commit(new File[] { workingCopyPath }, messageusernamepassword);
	}

Return any svn:externals associated with the working copy. Returns an empty list if there are none. Never returns null.
	public List<SVNExternalgetExternals(File workingCopyPath) {
		try {
			return getExternals(dataworkingCopyPath);
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}

Return the revision of the last commit for the working copy.
	public long getLastRevision(File workingCopyPath) {
		SVNInfo info = getInfo(workingCopyPath);
	}

Return the revision of the last commit for the given url
	public long getLastRevision(String url) {
		SVNRepository repository = getRepository(url);
		return getLastRevision(repository);
	}
	protected List<SVNExternalgetExternals(SVNPropertyData dataFile workingCopyPath) {
		if (data == null) {
			return new ArrayList<SVNExternal>();
		}
		SVNPropertyValue value = data.getValue();
		String s = SVNPropertyValue.getPropertyAsString(value);
		String[] tokens = StringUtils.split(s);
		List<SVNExternalexternals = new ArrayList<SVNExternal>();
		for (String token : tokens) {
			token = token.trim();
				continue;
			}
			if (StringUtils.isBlank(token)) {
				continue;
			}
			String[] values = StringUtils.split(token);
			String path = values[0];
			String url = values[1];
			File externalsPath = getExternalWorkingCopyPath(workingCopyPathpath);
			SVNExternal external = new SVNExternal();
			external.setUrl(url);
			external.setPath(path);
			external.setWorkingCopyPath(externalsPath);
			externals.add(external);
		}
		return externals;
	}
	protected File getExternalWorkingCopyPath(File workingCopyPathString path) {
		if (workingCopyPath == null) {
			return null;
else {
			return new File(workingCopyPath.getAbsolutePath() + . + path);
		}
	}
	protected SVNWCClient getSVNWCClient() {
		return new SVNWCClient(authMgrnull);
	}
	protected SVNInfo getInfo(File workingCopyPath) {
		try {
			SVNRevision revision = SVNRevision.create(-1);
			return client.doInfo(workingCopyPathrevision);
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
	protected long getLastRevision(SVNRepository repository) {
		try {
			SVNDirEntry entry = repository.info(, -1);
			return entry.getRevision();
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
	protected SVNRepository getRepository(String url) {
		return getRepository(urlnullnull);
	}
	protected SVNRepository getRepository(String urlString usernameString password) {
		try {
			SVNURL svnUrl = getSvnUrl(url);
			SVNRepository repository = SVNRepositoryFactory.create(svnUrlnull);
			if (!StringUtils.isBlank(username) && !StringUtils.isBlank(password)) {
				ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(usernamepassword);
				repository.setAuthenticationManager(authManager);
			}
			return repository;
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
	@SuppressWarnings("deprecation")
	protected SVNURL getSvnUrl(String url) {
		try {
			return SVNURL.parseURIDecoded(url);
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
New to GrepCode? Check out our FAQ X