Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit (because someone deleted the original project)

  • Loading branch information...
commit b17d8216761112dd1c77f67518d5291cbc642d87 0 parents
@tobrien tobrien authored
6 .gitignore
@@ -0,0 +1,6 @@
+*~
+target
+.classpath
+.settings
+.project
+.versionsBackup
41 pom.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0"?><project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.sonatype.book</groupId>
+ <artifactId>book-sitemesh-plugin</artifactId>
+ <packaging>maven-plugin</packaging>
+ <name>maven-book-sitemesh-plugin Maven Mojo</name>
+ <version>1.1-SNAPSHOT</version>
+ <url>http://maven.apache.org</url>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.velocity</groupId>
+ <artifactId>velocity</artifactId>
+ <version>1.6.1</version>
+ <type>jar</type>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>opensymphony</groupId>
+ <artifactId>sitemesh</artifactId>
+ <version>2.4</version>
+ <type>jar</type>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>1.4</version>
+ </dependency>
+ </dependencies>
+</project>
46 src/main/java/org/sonatype/book/#VelocityMerger.java#
@@ -0,0 +1,46 @@
+package org.sonatype.book;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.Reader;
+import java.io.StringWriter;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+
+public class VelocityMerger {
+
+ private File velocityTemplate;
+
+ public VelocityMerger(File velocityTemplate) {
+ this.velocityTemplate = velocityTemplate;
+ }
+
+ public void mergeFile( File htmlFile, SiteMeshPageExtractor extractor ) throws Exception {
+
+
+ Velocity.setProperty("input.encoding", "UTF-8");
+ Velocity.setProperty("output.encoding", "UTF-8");
+ Velocity.setProperty("response.encoding", "UTF-8");
+ Velocity.init();
+
+ VelocityContext context = new VelocityContext();
+ context.put( "title", extractor.getProperties().get("title") );
+ context.put( "head", extractor.getHead() );
+ context.put( "body", extractor.getBody() );
+ context.
+
+ Reader template = new FileReader( velocityTemplate );
+
+ StringWriter writer = new StringWriter();
+
+ Velocity.evaluate( context, writer, "MERGE", template );
+
+ FileWriter output = new FileWriter( htmlFile );
+ IOUtils.write( writer.toString(), output );
+ output.close();
+ }
+
+}
49 src/main/java/org/sonatype/book/ContentHandler.java
@@ -0,0 +1,49 @@
+package org.sonatype.book;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.util.Collection;
+
+import org.apache.commons.io.DirectoryWalker;
+import org.apache.commons.io.filefilter.IOFileFilter;
+
+public class ContentHandler extends DirectoryWalker {
+
+ private VelocityMerger velocityMerger;
+
+ public ContentHandler(IOFileFilter directoryFilter,
+ IOFileFilter fileFilter, int depthLimit, VelocityMerger vMerger) {
+ super(directoryFilter, fileFilter, depthLimit);
+ this.velocityMerger = vMerger;
+ }
+
+
+
+ @Override
+ protected boolean handleDirectory(File directory, int depth,
+ Collection results) throws IOException {
+ return true;
+ }
+
+
+
+ @Override
+ protected void handleFile(File file, int depth, Collection results)
+ throws IOException {
+ SiteMeshPageExtractor extractor = new SiteMeshPageExtractor();
+ extractor.parse( file );
+
+ try {
+ velocityMerger.mergeFile( file, extractor );
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+
+ public void go(File dir, Collection results) throws IOException {
+ walk( dir, results);
+ }
+}
68 src/main/java/org/sonatype/book/MeshVelocityMojo.java
@@ -0,0 +1,68 @@
+package org.sonatype.book;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Properties;
+
+import org.apache.commons.io.DirectoryWalker;
+import org.apache.commons.io.filefilter.FileFilterUtils;
+import org.apache.commons.io.filefilter.HiddenFileFilter;
+import org.apache.commons.io.filefilter.IOFileFilter;
+
+/**
+ * @goal mesh-velocity
+ *
+ * @phase test
+ */
+public class MeshVelocityMojo extends AbstractMojo {
+ /**
+ * Location of the file.
+ *
+ * @parameter expression="${basedir}/target/site"
+ * @required
+ */
+ private File htmlDirectory;
+
+ /**
+ * Velocity Template
+ *
+ * @parameter
+ * @required
+ */
+ private File velocityTemplate;
+
+ /**
+ * Book Title
+ *
+ * @parameter
+ * @required
+ */
+ private String bookTitle;
+
+ public void execute() throws MojoExecutionException {
+
+ // Create a filter for Non-hidden directories
+ IOFileFilter fooDirFilter = FileFilterUtils
+ .andFileFilter(FileFilterUtils.directoryFileFilter(),
+ HiddenFileFilter.VISIBLE);
+
+ Properties properties = new Properties();
+ properties.put( "bookTitle", bookTitle);
+
+ ContentHandler handler = new ContentHandler(fooDirFilter,
+ FileFilterUtils.suffixFileFilter(".html"), -1,
+ new VelocityMerger(velocityTemplate, properties));
+ try {
+ handler.go(htmlDirectory, new ArrayList());
+ } catch (IOException e) {
+ throw new MojoExecutionException("Problem during directory scan", e);
+ }
+
+ }
+}
154 src/main/java/org/sonatype/book/SiteMeshPageExtractor.java
@@ -0,0 +1,154 @@
+ package org.sonatype.book;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.commons.io.FileUtils;
+
+import com.opensymphony.module.sitemesh.html.BasicRule;
+import com.opensymphony.module.sitemesh.html.HTMLProcessor;
+import com.opensymphony.module.sitemesh.html.Tag;
+import com.opensymphony.module.sitemesh.html.TagRule;
+import com.opensymphony.module.sitemesh.html.TextFilter;
+import com.opensymphony.module.sitemesh.html.rules.BodyTagRule;
+import com.opensymphony.module.sitemesh.html.rules.HeadExtractingRule;
+import com.opensymphony.module.sitemesh.html.rules.MetaTagRule;
+import com.opensymphony.module.sitemesh.html.rules.PageBuilder;
+import com.opensymphony.module.sitemesh.html.rules.TitleExtractingRule;
+import com.opensymphony.module.sitemesh.html.util.CharArray;
+
+/**
+ * PageExtractor which extract page information from an HTML file using the SiteMesh library.
+ *
+ * @author Joe Walnes
+ * @author J&ouml;rg Schaible
+ */
+public class SiteMeshPageExtractor
+{
+ private Properties properties;
+ private String filename;
+ private String head;
+ private String body;
+ private List links;
+ private final TagRule[] rules;
+ private final TextFilter[] filter;
+
+ public SiteMeshPageExtractor()
+ {
+ rules = new TagRule[0];
+ filter = new TextFilter[0];
+ links = new ArrayList();
+ }
+
+ public void parse(File htmlFile) {
+ try {
+ extractContentFromHTML( FileUtils.readFileToString( htmlFile ).toCharArray() );
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void extractContentFromHTML( char[] rawHTML ) throws IOException
+ {
+ // where to dump properties extracted from the page
+ properties = new Properties();
+ PageBuilder pageBuilder = new PageBuilder()
+ {
+ public void addProperty( String key, String value )
+ {
+ properties.setProperty( key, value );
+ }
+ };
+
+ // buffers to hold head and body content
+ CharArray headBuffer = new CharArray( 64 );
+ CharArray bodyBuffer = new CharArray( 4096 );
+
+ // setup rules for html processor
+ HTMLProcessor htmlProcessor = new HTMLProcessor( rawHTML, bodyBuffer );
+ htmlProcessor.addRule( new BodyTagRule( pageBuilder, bodyBuffer ) );
+ htmlProcessor.addRule( new HeadExtractingRule( headBuffer ) );
+ htmlProcessor.addRule( new TitleExtractingRule( pageBuilder ) );
+ htmlProcessor.addRule( new MetaTagRule( pageBuilder ) );
+ htmlProcessor.addRule( new LinkExtractingRule() );
+
+ for ( int i = 0; i < rules.length; i++ )
+ {
+ htmlProcessor.addRule( rules[i] );
+ }
+ for ( int i = 0; i < filter.length; i++ )
+ {
+ htmlProcessor.addTextFilter( filter[i] );
+ }
+
+ // go!
+ htmlProcessor.process();
+ this.head = headBuffer.toString();
+ this.body = bodyBuffer.toString();
+ }
+
+ public static class CannotParsePageException extends RuntimeException
+ {
+ public CannotParsePageException( Throwable cause )
+ {
+ super( cause );
+ }
+ }
+
+ /** Rule for HTMLProcessor that records all <a href=""> links. */
+ private class LinkExtractingRule extends BasicRule
+ {
+
+ public boolean shouldProcess( String tag )
+ {
+ return tag.equalsIgnoreCase( "a" );
+ }
+
+ public void process( Tag tag )
+ {
+ if ( tag.hasAttribute( "href", false ) )
+ {
+ links.add( tag.getAttributeValue( "href", false ) );
+ }
+ tag.writeTo( currentBuffer() );
+ }
+ }
+
+ public Properties getProperties() {
+ return properties;
+ }
+
+ public void setProperties(Properties properties) {
+ this.properties = properties;
+ }
+
+ public String getFilename() {
+ return filename;
+ }
+
+ public void setFilename(String filename) {
+ this.filename = filename;
+ }
+
+ public String getHead() {
+ return head;
+ }
+
+ public void setHead(String head) {
+ this.head = head;
+ }
+
+ public String getBody() {
+ return body;
+ }
+
+ public void setBody(String body) {
+ this.body = body;
+ }
+
+
+
+}
49 src/main/java/org/sonatype/book/VelocityMerger.java
@@ -0,0 +1,49 @@
+package org.sonatype.book;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.util.Properties;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+
+public class VelocityMerger {
+
+ private File velocityTemplate;
+ private Properties properties;
+
+ public VelocityMerger(File velocityTemplate, Properties properties) {
+ this.velocityTemplate = velocityTemplate;
+ this.properties = properties;
+ }
+
+ public void mergeFile( File htmlFile, SiteMeshPageExtractor extractor ) throws Exception {
+ Velocity.setProperty("input.encoding", "UTF-8");
+ Velocity.setProperty("output.encoding", "UTF-8");
+ Velocity.setProperty("response.encoding", "UTF-8");
+ Velocity.init();
+
+ VelocityContext context = new VelocityContext();
+ context.put( "title", extractor.getProperties().get("title") );
+ context.put( "head", extractor.getHead() );
+ context.put( "body", extractor.getBody() );
+ for( Object key : properties.keySet() ) {
+ context.put( (String) key, properties.get(key));
+ }
+
+ Reader template = new FileReader( velocityTemplate );
+
+ StringWriter writer = new StringWriter();
+
+ Velocity.evaluate( context, writer, "MERGE", template );
+
+ FileWriter output = new FileWriter( htmlFile );
+ IOUtils.write( writer.toString(), output );
+ output.close();
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.