Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2011-2013 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 = " ";
 	private static final String SEMICOLON = ":";
 
 	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);
		}
	}
	public SVNCommitInfo setExternals(File repositoryPathList<SVNExternalexternalsString messageString usernameString password) {
		SVNClientManager manager = SVNClientManager.newInstance(nullusernamepassword);
		SVNWCClient client = manager.getWCClient();
		String commitMessage = StringUtils.isBlank(message) ?  : message;
		SVNURL svnUrl = getSvnUrl(repositoryPath.getAbsolutePath());
		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 void markForDeletion(List<Filefiles) {
		try {
			for (File file : files) {
				client.doDelete(filetruefalse);
			}
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
	protected SVNURL[] getSvnUrlArray(List<Filefiles) {
		String singleSlashPrefix = "file:/";
		String doubleSlashPrefix = "file://";
		SVNURL[] array = new SVNURL[files.size()];
		for (int i = 0; i < files.size(); i++) {
			File file = files.get(i);
			String fileUrlString = file.toURI().toString();
			boolean singleSlash = StringUtils.startsWith(fileUrlStringsingleSlashPrefix);
			boolean doubleSlash = StringUtils.startsWith(fileUrlStringdoubleSlashPrefix);
			if (singleSlash && !doubleSlash) {
				fileUrlString = StringUtils.replace(fileUrlStringsingleSlashPrefixdoubleSlashPrefix);
			}
			SVNURL fileUrl = getSvnUrl(fileUrlString);
			array[i] = fileUrl;
		}
		return array;
	}
	public void markForDeletion(File file) {
		markForDeletion(Collections.singletonList(file));
	}
	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);
	}
	public void addFiles (File workingCopyPathString usernameString password) {
		try {
			SVNClientManager manager = SVNClientManager.newInstance(nullusernamepassword);
			SVNWCClient client = manager.getWCClient();
			client.setIgnoreExternals(false);
			client.doAdd(workingCopyPathtruefalsetrue.truetrue);
catch (SVNException e) {
			throw new IllegalStateException(e);
		}
	}
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);
	}

Convert the svn:externals definitions into a List of SVNExternal objects. This method never returns null. If there are no svn:externals definitions an empty List is returned.
	protected List<SVNExternalgetExternals(SVNPropertyData dataFile workingCopyPath) {
		// The property svn:externals is not set on this directory
		if (data == null) {
			return new ArrayList<SVNExternal>();
		}
		// Extract the property value
		SVNPropertyValue value = data.getValue();
		// Convert the value to a String
		String s = SVNPropertyValue.getPropertyAsString(value);
		// Split the string up into lines
		String[] tokens = StringUtils.split(s);
		// Iterate through the lines looking for externals
		List<SVNExternalexternals = new ArrayList<SVNExternal>();
		for (String token : tokens) {
			// Trim whitespace
			token = token.trim();
			// Ignore comments and blank lines
			if (token.startsWith() || StringUtils.isBlank(token)) {
				continue;
			}
			// We've located a non-blank, non-comment line
			// We've already trimmed off leading and trailing whitespace
			// Split up the remaining portion of the string using space as a delimiter
			// The split method skips all spaces in the line (even adjacent spaces)
			// The String[] returned by split() will only contain tokens with non-space characters
			String[] values = StringUtils.split(token);
			// If we don't have exactly 2 non-blank tokens there is trouble
			if (values.length != 2) {
				throw new IllegalStateException("Unparseable svn:externals definition - [" + token + ", " + workingCopyPath + "]");
			}
			// Extract the 2 values we are interested in
			String value1 = values[0];
			String value2 = values[1];
			// Some SVN clients store svn:externals as <module> <location>, others store it as <location> <module>
			String url = getUrl(value1value2);
			String path = getPath(value1value2);
			// Get the file representing the local working copy of the svn:external
			File externalsPath = getExternalWorkingCopyPath(workingCopyPathpath);
			// Store the info we've accumulated into an object
			SVNExternal external = new SVNExternal();
			external.setUrl(url);
			external.setPath(path);
			external.setWorkingCopyPath(externalsPath);
			// Add our object to the list
			externals.add(external);
		}
		// Return the list we've found
		return externals;
	}
	protected boolean isUrl(String s) {
		return s.contains();
	}
	protected String getUrl(String value1String value2) {
		if (isUrl(value1)) {
			return value1;
else {
			return value2;
		}
	}
	protected String getPath(String value1String value2) {
		if (isUrl(value1)) {
			return value2;
else {
			return value1;
		}
	}
	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) {
			try {
				return SVNURL.fromFile(new File (url));
catch (SVNException e1) {
				throw new IllegalStateException(e1);
			}
		}
	}
New to GrepCode? Check out our FAQ X