Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Enhanced the Ant Task to now support a sitemeshfileset node.

Signed-off-by: joewalnes <joe@truemesh.com>
  • Loading branch information...
commit 66721c97b0b0168cbb57a2087dd998951adebe0a 1 parent 615ba89
@rburton rburton authored joewalnes committed
Showing with 244 additions and 30 deletions.
  1. +51 −0 sitemesh/src/main/java/org/sitemesh/ant/SiteMeshFileSet.java
  2. +132 −29 sitemesh/src/main/java/org/sitemesh/ant/SiteMeshTask.java
  3. +1 −1  sitemesh/src/test/java/org/sitemesh/ant/SiteMeshTaskTestSuite.java
  4. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/expected/sitemeshfileset/page1.html
  5. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/expected/sitemeshfileset/page2.html
  6. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/expected/sitemeshfilesetWithDecorator/page1.html
  7. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/expected/sitemeshfilesetWithDecorator/page2.html
  8. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfileset/WEB-INF/sitemesh3.xml
  9. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfileset/decorators/decorator.html
  10. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfileset/page1.html
  11. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfileset/page2.html
  12. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfilesetWithDecorator/decorators/decorator.html
  13. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfilesetWithDecorator/page1.html
  14. +3 −0  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfilesetWithDecorator/page2.html
  15. +27 −0 sitemesh/src/test/java/org/sitemesh/ant/testproject/test-build.xml
View
51 sitemesh/src/main/java/org/sitemesh/ant/SiteMeshFileSet.java
@@ -0,0 +1,51 @@
+package org.sitemesh.ant;
+
+import org.apache.tools.ant.types.FileSet;
+
+import java.io.File;
+
+/**
+ * A SiteMesh <tt>FileSet</tt> that is aware of what decorator to
+ * apply for the given set of resource files.
+ * @author Richard L. Burton III - SmartCode LLC
+ */
+public class SiteMeshFileSet extends FileSet {
+
+ /** The decorator to use when decorating the set of files. */
+ private String decorator;
+
+ /** An optional destination directory. */
+ private File destdir;
+
+ public SiteMeshFileSet() {
+ }
+
+ public SiteMeshFileSet(FileSet fileSet) {
+ super(fileSet);
+ }
+
+ public String getDecorator() {
+ return decorator;
+ }
+
+ public void setDecorator(String decorator) {
+ this.decorator = decorator;
+ }
+
+ public File getDestdir() {
+ return destdir;
+ }
+
+ public void setDestdir(File destdir) {
+ this.destdir = destdir;
+ }
+
+ public boolean hasDecorator() {
+ return decorator != null;
+ }
+
+ public boolean hasDestdir(){
+ return destdir != null;
+ }
+
+}
View
161 sitemesh/src/main/java/org/sitemesh/ant/SiteMeshTask.java
@@ -13,6 +13,7 @@
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.IOException;
+import java.util.Vector;
/**
* @author Richard L. Burton III - SmartCode LLC
@@ -20,58 +21,153 @@
*/
public class SiteMeshTask extends MatchingTask {
- private File srcDir;
+ /**
+ * The destination directory where the decorated files will be placed.
+ */
private File destDir;
+
+ /**
+ * The sitemesh configuration file.
+ */
private File config;
+ private File srcdir;
+
+ /**
+ * The files to decorate with SiteMesh.
+ */
+ private Vector<FileSet> resources = new Vector<FileSet>();
+
@Override
public void execute() throws BuildException {
verify();
- SiteMeshOffline siteMeshOffline = createSiteMeshOffline();
- FileSet implicitFileSet = getImplicitFileSet();
- implicitFileSet.setDir(srcDir);
- processFileSet(siteMeshOffline, implicitFileSet);
+ Vector<FileSet> implicitFileSet = getImplicitAndExplicitFileSet();
+ processFileSet(implicitFileSet);
}
/**
- * Verify parameters.
+ * Returns the all implicit and explicit FileSets.
+ *
+ * @return The FileSets to process.
*/
- protected void verify() {
- if (srcDir == null) {
- throw new BuildException("srcdir not specified");
+ protected Vector<FileSet> getImplicitAndExplicitFileSet() {
+ Vector<FileSet> vfss = new Vector<FileSet>();
+ FileSet fs = (FileSet) getImplicitFileSet().clone();
+
+ if (srcdir != null) {
+ fs.setDir(srcdir);
+ }
+
+ if (fs.getDir() != null) {
+ vfss.addElement(fs);
+ }
+
+ for (FileSet rc : resources) {
+ log("Adding FileSet to the list of FileSets to be processed " + rc.getDescription());
+ vfss.addElement(rc);
}
+ return vfss;
+ }
+
+ /**
+ * Verify the required parameters.
+ *
+ * @throws BuildException If either the source or destination directories are not set.
+ */
+ protected void verify() throws BuildException {
if (destDir == null) {
throw new BuildException("dest not specified");
}
}
- protected void processFileSet(SiteMeshOffline generator, FileSet fileset) {
- DirectoryScanner directoryScanner = fileset.getDirectoryScanner(getProject());
- directoryScanner.setBasedir(fileset.getDir(getProject()));
- for (String path : directoryScanner.getIncludedFiles()) {
- try {
- System.out.println("path = " + path);
- generator.process(path);
- } catch (IOException ioe) {
- throw new BuildException(ioe);
+ /**
+ * Processes the FileSet to decorate all of the provided files.
+ *
+ * @param filesets The set of files to be processed.
+ * @throws BuildException When there's a problem decorating the files.
+ */
+ protected void processFileSet(Vector<FileSet> filesets) {
+ for (FileSet fileset : filesets) {
+ DirectoryScanner directoryScanner = fileset.getDirectoryScanner(getProject());
+ directoryScanner.setBasedir(fileset.getDir(getProject()));
+ for (String path : directoryScanner.getIncludedFiles()) {
+ SiteMeshOffline siteMeshOffline = createSiteMeshOffline(fileset);
+ try {
+ log("Processing '" + path + "'");
+ siteMeshOffline.process(path);
+ } catch (IOException ioe) {
+ throw new BuildException(ioe);
+ }
}
}
}
- protected SiteMeshOffline createSiteMeshOffline() {
- SiteMeshOfflineBuilder builder = new SiteMeshOfflineBuilder()
- .setSourceDirectory(srcDir)
- .setDestinationDirectory(destDir);
- if (config != null) {
+ /**
+ * Creates The SiteMeshOffline generator for the given FileSet.
+ *
+ * @param fileset The FileSet to be processed.
+ * @return An instance of SiteMeshOffline.
+ */
+ protected SiteMeshOffline createSiteMeshOffline(FileSet fileset) {
+ File destintationDir = destDir;
+
+ SiteMeshOfflineBuilder builder = new SiteMeshOfflineBuilder();
+ if (isSiteMeshFileSet(fileset)) {
+ SiteMeshFileSet sfs = (SiteMeshFileSet) fileset;
+ if (hasDestdir(fileset)) {
+ destintationDir = sfs.getDestdir();
+ }
+
+ if (sfs.hasDecorator()) {
+ String[] includes = fileset.mergeIncludes(getProject());
+ for (String include : includes) {
+ builder.addDecoratorPath(include, sfs.getDecorator());
+ }
+ }
+ }
+
+ builder.setSourceDirectory(fileset.getDir())
+ .setDestinationDirectory(destintationDir);
+
+ if (isSiteMeshFileSetWithoutDecorator(fileset) || config != null) {
new XmlOfflineConfigurator(new ObjectFactory.Default(), parseSiteMeshXmlConfig(config))
.configureOffline(builder);
}
+
+
+
applyCustomConfiguration(builder);
return builder.create();
}
+ /**
+ * A helper method that checks if the given FileSet is an instance of
+ * SiteMesh and no decorator was set on the given FileSet.
+ *
+ * @param fileset The FileSet to inspect.
+ * @return True if the FileSet is an instance of SiteMeshFileSet and no decorator was set,
+ * false otherwise.
+ */
+ protected boolean isSiteMeshFileSetWithoutDecorator(FileSet fileset) {
+ return isSiteMeshFileSet(fileset) && !((SiteMeshFileSet) fileset).hasDecorator();
+ }
+
+ /**
+ * Checks to if the FileSet has a destdir set.
+ *
+ * @param fileset The FileSet to check.
+ * @return true if the destdir is set, false otherwise.
+ */
+ protected boolean hasDestdir(FileSet fileset) {
+ return isSiteMeshFileSet(fileset) && ((SiteMeshFileSet) fileset).hasDestdir();
+ }
+
+ protected boolean isSiteMeshFileSet(FileSet fileset) {
+ return fileset instanceof SiteMeshFileSet;
+ }
+
protected Element parseSiteMeshXmlConfig(File config) throws BuildException {
try {
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(config).getDocumentElement();
@@ -82,23 +178,26 @@ protected Element parseSiteMeshXmlConfig(File config) throws BuildException {
/**
* Override this to apply custom configuration after after the default configuration mechanisms.
+ *
+ * @param builder The SitemeshOfflineBuilder used in creating the SiteMeshOfflineGenerator.
*/
@SuppressWarnings("UnusedDeclaration")
protected void applyCustomConfiguration(SiteMeshOfflineBuilder builder) {
}
/**
- * Source directory containing undecorated content and decorators.
+ * Destination directory where decorated content will be output to.
*/
- public void setSrcdir(File srcDir) {
- this.srcDir = srcDir;
+ public void setDestdir(File destDir) {
+ this.destDir = destDir;
}
+
/**
- * Destination directory where decorated content will be output to.
+ * Source directory containing undecorated content and decorators.
*/
- public void setDestdir(File destDir) {
- this.destDir = destDir;
+ public void setSrcdir(File srcDir) {
+ this.srcdir = srcDir;
}
/**
@@ -108,4 +207,8 @@ public void setConfig(File config) {
this.config = config;
}
+ public void addSiteMeshfileset(SiteMeshFileSet fileset) {
+ resources.add(fileset);
+ }
+
}
View
2  sitemesh/src/test/java/org/sitemesh/ant/SiteMeshTaskTestSuite.java
@@ -35,7 +35,7 @@
public class SiteMeshTaskTestSuite {
public static Test suite() {
- final File baseDir = new File("src/test/java/org/sitemesh/ant/testproject");
+ final File baseDir = new File("sitemesh/src/test/java/org/sitemesh/ant/testproject");
final FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
View
3  sitemesh/src/test/java/org/sitemesh/ant/testproject/expected/sitemeshfileset/page1.html
@@ -0,0 +1,3 @@
+<html>
+ <body>DECORATOR{{{I am a simple page 1}}}</body>
+</html>
View
3  sitemesh/src/test/java/org/sitemesh/ant/testproject/expected/sitemeshfileset/page2.html
@@ -0,0 +1,3 @@
+<html>
+ <body>DECORATOR{{{I am a simple page 2}}}</body>
+</html>
View
3  sitemesh/src/test/java/org/sitemesh/ant/testproject/expected/sitemeshfilesetWithDecorator/page1.html
@@ -0,0 +1,3 @@
+<html>
+ <body>DECORATOR{{{I am a simple page 1}}}</body>
+</html>
View
3  sitemesh/src/test/java/org/sitemesh/ant/testproject/expected/sitemeshfilesetWithDecorator/page2.html
@@ -0,0 +1,3 @@
+<html>
+ <body>DECORATOR{{{I am a simple page 2}}}</body>
+</html>
View
3  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfileset/WEB-INF/sitemesh3.xml
@@ -0,0 +1,3 @@
+<sitemesh>
+ <mapping path="/*" decorator="/decorators/decorator.html"/>
+</sitemesh>
View
3  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfileset/decorators/decorator.html
@@ -0,0 +1,3 @@
+<html>
+ <body>DECORATOR{{{<sitemesh:write property='body'/>}}}</body>
+</html>
View
3  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfileset/page1.html
@@ -0,0 +1,3 @@
+<html>
+ <body>I am a simple page 1</body>
+</html>
View
3  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfileset/page2.html
@@ -0,0 +1,3 @@
+<html>
+ <body>I am a simple page 2</body>
+</html>
View
3  ...esh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfilesetWithDecorator/decorators/decorator.html
@@ -0,0 +1,3 @@
+<html>
+ <body>DECORATOR{{{<sitemesh:write property='body'/>}}}</body>
+</html>
View
3  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfilesetWithDecorator/page1.html
@@ -0,0 +1,3 @@
+<html>
+ <body>I am a simple page 1</body>
+</html>
View
3  sitemesh/src/test/java/org/sitemesh/ant/testproject/in/sitemeshfilesetWithDecorator/page2.html
@@ -0,0 +1,3 @@
+<html>
+ <body>I am a simple page 2</body>
+</html>
View
27 sitemesh/src/test/java/org/sitemesh/ant/testproject/test-build.xml
@@ -12,6 +12,33 @@
destdir="out/simplewebapp1"
includes="**/*.html"
excludes="decorators/*"/>
+
+ </target>
+
+ <target name="sitemeshfileset">
+
+ <sitemesh destdir="out/sitemeshfileset"
+ config="in/sitemeshfileset/WEB-INF/sitemesh3.xml">
+
+ <sitemeshfileset dir="in/sitemeshfileset">
+ <include name="**/*.html"/>
+ <exclude name="decorators/*"/>
+ </sitemeshfileset>
+
+ </sitemesh>
+ </target>
+
+ <target name="sitemeshfilesetWithDecorator">
+
+ <sitemesh destdir="out/sitemeshfilesetWithDecorator">
+
+ <sitemeshfileset dir="in/sitemeshfilesetWithDecorator"
+ decorator="decorators/decorator.html">
+ <include name="**/*.html"/>
+ <exclude name="decorators/*"/>
+ </sitemeshfileset>
+
+ </sitemesh>
</target>
</project>
Please sign in to comment.
Something went wrong with that request. Please try again.