Update of /cvsroot/xdoclet/xdoclet2/src/java/xdoclet
In directory sc8-pr-cvs1:/tmp/cvs-serv5480/src/java/xdoclet
Modified Files:
Plugin.java
Added Files:
FilenameSubstitution.java
Log Message:
Applied XDTWO-37: Filename substitution proposal
--- NEW FILE: FilenameSubstitution.java ---
/*
* Copyright (c) 2001, 2002 The XDoclet team
* All rights reserved.
*/
package xdoclet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Uses JDK 1.4 java.util.regex to perform file name substitutions. Prefixes and
* suffixes are a comma separated list of expressions to remove from the file
* name.
*
* For example:
* <pre>
* prefix = null
* suffix = Bean
* MyBean => My
*
* prefix = Remote
* suffix = null
* RemoteConnector => Connector
*
* prefix = Remote
* suffix = Bean
* RemoteConnectorBean => Connector
* </pre>
*
* @author <a href="mailto:letiemble at users.sourceforge.net">Laurent Etiemble</a>
* @created 2 juin 2003
* @version $Revision: 1.1 $
*/
public class FilenameSubstitution {
/** Regexp Pattern */
private Pattern _pattern = null;
/** Empty pattern */
private String _prefixList = "()";
/** Empty pattern */
private String _suffixList = "()";
/**
* Gets the substituted file name.
*
* @param fileName The filename to process
* @return Substituted file name or unmodified class name
* if no prefixes and/or suffixes were matched.
*/
public String getSubstitutedFilename(String fileName) {
// Implementation trick :
// ======================
// If the prefix list or the suffix list are not filled with their
// setters, we use their default values : an empty pattern "()".
// This trick allows to have a constant match group number.
// That's why the regexp pattern is always compiled with a prefix
// and a suffix lists.
//
// To remember :
// =============
// If the match group number is not constant, we have to store which
// group to return. Is there an ELEGANT solution to this problem ?
// Check if fileName is OK
if (fileName == null) {
throw new IllegalArgumentException("fileName cannot be null");
}
// Compiles pattern on demand
if (_pattern == null) {
// Use a non greedy pattern for the middle
String pattern = "^" + _prefixList + "(.+?)" + _suffixList + "$";
_pattern = Pattern.compile(pattern);
}
// Has Compilation failed ?
if (_pattern == null) {
throw new IllegalStateException("pattern is not compiled (null)");
}
Matcher matcher = _pattern.matcher(fileName);
assert matcher != null : "matcher is null";
String substituted = fileName;
// It always match.
if (!matcher.matches()) {
throw new IllegalStateException("Pattern must always match");
}
// The number of match group must be constant and equals to 3.
if (matcher.groupCount() != 3) {
throw new IllegalStateException("Match group count must be 3");
}
// The groups are :
// - 0 : the entire expression
// - 1 : the prefixes match group (may be empty)
// - 2 : what we are interested in : the substitute file name
// - 3 : the suffixes match group (may be empty)
substituted = matcher.group(2);
return fileName.equals(substituted) ? null : substituted;
}
/**
* Sets the list of prefixes to be removed from class name.
* The list is comma separated and can be made of regexps.
*
* @param prefixList A comma separated list of prefixes
*/
public void setPrefixes(String prefixList) {
if (prefixList == null) {
throw new IllegalArgumentException("prefixList cannot be null");
}
// Replace comma by pipe, to make a OR selection
// We use a greedy pattern to make sure that a prefix is match
_prefixList = "(" + prefixList.replace(',', '|') + ")?";
}
/**
* Sets the list of suffixes to be removed from class name.
* The list is comma separated and can be made of regexps.
*
* @param suffixList A comma separated list of suffixes
*/
public void setSuffixes(String suffixList) {
if (suffixList == null) {
throw new IllegalArgumentException("suffixList cannot be null");
}
// Replace comma by pipe, to make a OR selection
// We use a greedy pattern to make sure that a suffix is match
_suffixList = "(" + suffixList.replace(',', '|') + ")?";
}
}
Index: Plugin.java
===================================================================
RCS file: /cvsroot/xdoclet/xdoclet2/src/java/xdoclet/Plugin.java,v
retrieving revision 1.30
retrieving revision 1.31
diff -C2 -r1.30 -r1.31
*** Plugin.java 27 May 2003 11:16:18 -0000 1.30
--- Plugin.java 15 Jun 2003 21:46:56 -0000 1.31
***************
*** 54,57 ****
--- 54,67 ----
* </ul>
*
+ *
+ * <p>Note :</p>
+ *
+ * <ul>
+ * <li>If the Plugin contains Package Substitution(s), they will be applied
during
+ * the generation to find a package substitute</li>
+ * <li>If the Plugin contains FileName Substitution(s), they will be applied
during
+ * the generation to find a filename substitute</li>
+ * </ul>
+ *
* @see PluginFactory
* @see XDoclet
***************
*** 283,286 ****
--- 293,321 ----
/**
+ * Creates a new Filename Substitution
+ *
+ * @bean.method shortDescription="Add a new Filename Substitution"
+ * displayName="New Filename Substitution"
+ */
+ public final FilenameSubstitution createFilenameSubstitution() {
+ LogFactory.getLog(Plugin.class).info(this + ".createFileNameSubstitution");
+
+ FilenameSubstitution filenameSubstitution = new FilenameSubstitution();
+
+ add(filenameSubstitution);
+
+ return filenameSubstitution ;
+ }
+
+ private Collection getFilenameSubstitutions() {
+ return CollectionUtils.select(this,
+ new Predicate() {
+ public boolean evaluate(Object o) {
+ return o instanceof FilenameSubstitution;
+ }
+ });
+ }
+
+ /**
* Generates the content.
*
***************
*** 410,413 ****
--- 445,476 ----
}
+ private final String getSubstitutedFilename(Object object)
+ throws XDocletException {
+ String fileName = null;
+ String originalFileName =
getMetadataProvider().getFilenameSubstitutionValue(object);
+
+ for (Iterator i = getFilenameSubstitutions().iterator(); i.hasNext();) {
+ FilenameSubstitution filenameSubstitution = (FilenameSubstitution)
i.next();
+
+ // Check that we don't have a conflict. We must verify that there is not
more than one match for from.
+ String candidate =
filenameSubstitution.getSubstitutedFilename(originalFileName);
+
+ if ((fileName != null) && (candidate != null)) {
+ throw new XDocletException("Ambiguous filename substitution for "
+ + getMetadataProvider().getFilenameSubstitutionValue(object) +
". Both " + fileName + " or "
+ + candidate + " are possible substitution results.");
+ }
+
+ fileName = candidate;
+ }
+
+ // If fileName is null (no match), use the originalFileName
+ if (fileName == null) {
+ fileName = originalFileName;
+ }
+
+ return fileName;
+ }
+
/**
* Gets all accepted objects. Subclasses can control what's accepted
***************
*** 488,492 ****
public final File getDestinationFileForOne(Object object)
throws XDocletException {
! String fileNameSubstitutionValue =
getMetadataProvider().getFilenameSubstitutionValue(object);
String packageName = getSubstitutedPackageName(object);
--- 551,555 ----
public final File getDestinationFileForOne(Object object)
throws XDocletException {
! String fileNameSubstitutionValue = getSubstitutedFilename(object);
String packageName = getSubstitutedPackageName(object);
-------------------------------------------------------
This SF.NET email is sponsored by: eBay
Great deals on office technology -- on eBay now! Click here:
http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
_______________________________________________
xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel