From 421690761d70f5886450f19d23a8cc6e0c63ce5e Mon Sep 17 00:00:00 2001 From: aheritier Date: Sun, 5 Jun 2005 22:45:11 +0000 Subject: [PATCH] Add our own utilities to manage SCM connections. This is needed because maven 1.1 model does not provide them. git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@180160 13f79535-47bb-0310-9956-ffa450edef68 --- xdoc/plugin.jelly | 2 + .../org/apache/maven/xdoc/util/ScmUtil.java | 216 ++++++++++++++++++ .../plugin-resources/templates/cvs-usage.xml | 18 +- 3 files changed, 227 insertions(+), 9 deletions(-) create mode 100644 xdoc/src/main/org/apache/maven/xdoc/util/ScmUtil.java diff --git a/xdoc/plugin.jelly b/xdoc/plugin.jelly index 86c31b8d..8c74d3e1 100644 --- a/xdoc/plugin.jelly +++ b/xdoc/plugin.jelly @@ -651,6 +651,8 @@ ${dependencyDescriber.build(pom)} + + Jason van Zyl + * @author Arnaud Heritier + * @version $Id$ + */ +public final class ScmUtil +{ + + /** + * Get the SCM type + * + * @param scmConnection the scm connection to analyse. + * @return the scm type : cvs, svn , ... + */ + public String getScmType(final String scmConnection) + { + if (isValid(scmConnection)) + { + return splitSCMConnection(scmConnection)[1]; + } + return null; + } + + /** + * Get cvs root. + * + * @param scmConnection the scm connection to analyse. + * @return CVS root. + */ + public String getCvsRoot(String scmConnection) + { + if (isValid(scmConnection)) + { + return getCvsRoot(scmConnection, ""); + } + + return null; + } + + /** + * Get cvs server. Used in xdocs/src/plugin-resources/templates/cvs-usage.xml + * + * @param conn six token connection string + * @return CVS module. + */ + public String getCvsServer(String conn) + { + String[] tokens = splitSCMConnection(conn); + if (!tokens[1].equals("cvs")) + { + return ""; + } + + if (tokens[3].indexOf('@') >= 0) + { + return tokens[3].substring(tokens[3].indexOf('@') + 1); + } + else + { + return tokens[3]; + } + } + + /** + * Get cvs root. Used in xdocs/src/plugin-resources/templates/cvs-usage.xml + * + * @param conn six token connection string + * @param username username override if non-empty. + * @return CVS root. + */ + public String getCvsRoot(String conn, String username) + { + String[] tokens = splitSCMConnection(conn); + + if (!tokens[1].equals("cvs")) + { + return ""; + } + + if (tokens[3].indexOf('@') >= 0) + { + if (username.length() == 0) + { + username = tokens[3].substring(0, tokens[3].indexOf('@')); + } + tokens[3] = username + "@" + tokens[3].substring(tokens[3].indexOf('@') + 1); + } + String result = ":" + tokens[2] + ":" + tokens[3] + ":" + tokens[4]; + return result; + } + + /** + * Get cvs module. Used in xdocs/src/plugin-resources/templates/cvs-usage.xml + * + * @param conn six token connection string + * @return CVS module. + */ + public String getCvsModule(String conn) + { + if (isValid(conn)) + { + + String[] tokens = splitSCMConnection(conn); + if (!tokens[1].equals("cvs")) + { + return ""; + } + return tokens[5]; + } + + return null; + } + + /** + * Splits an SCM string into parts + * + * @param connection + * @return + */ + public String[] splitSCMConnection(String connection) + { + if (connection == null) + { + throw new NullPointerException("repository connection is null"); + } + + if (connection.length() < 4) + { + throw new IllegalArgumentException("repository connection is too short"); + } + + if (!connection.startsWith("scm")) + { + throw new IllegalArgumentException("repository connection must start with scm[delim]"); + } + + String delimiter = "" + connection.charAt(3); + + EnhancedStringTokenizer tok = new EnhancedStringTokenizer(connection, delimiter); + + String[] tokens = tokenizerToArray(tok); + + // for a valid repository, it should be scm: at least + if (tokens.length >= 1 && tokens[1].equals("cvs")) + { + if (tokens.length != 6) + { + throw new IllegalArgumentException( + "cvs repository connection string doesn't contain six tokens"); + } + } + return tokens; + } + + /** + * Converts a tokenizer to an array of strings FIXME: This should be in a string util class + * + * @param tok + * @return String[] + */ + public String[] tokenizerToArray(EnhancedStringTokenizer tok) + { + List l = new ArrayList(); + while (tok.hasMoreTokens()) + { + l.add(tok.nextToken()); + } + return (String[]) l.toArray(new String[l.size()]); + } + + /** + * Simple check for a value in the POM. Due to the Jelly swizzling fields that aren't set come + * out as empty strings. This will not be required when the new lazy evaluation mechanism is put + * in place. + * + * @param value POM value to test. + * @return Is the value valid. + */ + protected boolean isValid(String value) + { + if (value != null && !value.trim().equals("")) + { + return true; + } + + return false; + } +} diff --git a/xdoc/src/plugin-resources/templates/cvs-usage.xml b/xdoc/src/plugin-resources/templates/cvs-usage.xml index f9b288be..200b7d39 100644 --- a/xdoc/src/plugin-resources/templates/cvs-usage.xml +++ b/xdoc/src/plugin-resources/templates/cvs-usage.xml @@ -47,11 +47,11 @@ #end #if ($repository.connection && $repository.connection != '') - #set ($connscm = $repository.getScmType()) + #set ($connscm = $scmUtil.getScmType($repository.connection)) #if ($connscm == 'cvs') - #set ($conn = $repository.getCvsRoot($repository.connection, '')) - #set ($module = $repository.getCvsModule($repository.connection)) + #set ($conn = $scmUtil.getCvsRoot($repository.connection, '')) + #set ($module = $scmUtil.getCvsModule($repository.connection))
@@ -93,12 +93,12 @@ #end #if ($repository.developerConnection && $repository.developerConnection != '') - #set ($connscm = $repository.getScmType()) + #set ($connscm = $scmUtil.getScmType($repository.developerConnection)) #if ($connscm == 'cvs') - #set ($conn = $repository.getCvsRoot($repository.developerConnection, 'username')) - #set ($module = $repository.getCvsModule($repository.developerConnection)) - #set ($server = $repository.getCvsServer($repository.developerConnection)) + #set ($conn = $scmUtil.getCvsRoot($repository.developerConnection, 'username')) + #set ($module = $scmUtil.getCvsModule($repository.developerConnection)) + #set ($server = $scmUtil.getCvsServer($repository.developerConnection))
@@ -147,8 +147,8 @@ #end #end - #if ($repository.url && $repository.url != '') - #set ($connscm = $repository.getScmType()) + #if ($repository.url && $repository.url != '' && $repository.connection && $repository.connection != '') + #set ($connscm = $scmUtil.getScmType($repository.connection)) #if ($connscm == 'cvs')