From: <tri...@us...> - 2007-07-04 05:35:54
|
Revision: 3 http://staticwiki.svn.sourceforge.net/staticwiki/?rev=3&view=rev Author: triathlon98 Date: 2007-07-03 22:35:50 -0700 (Tue, 03 Jul 2007) Log Message: ----------- Initial code, contains a wiki renderer Modified Paths: -------------- trunk/pom.xml Added Paths: ----------- trunk/staticwiki-renderer/ trunk/staticwiki-renderer/pom.xml trunk/staticwiki-renderer/src/ trunk/staticwiki-renderer/src/main/ trunk/staticwiki-renderer/src/main/java/ trunk/staticwiki-renderer/src/main/java/org/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ConfigReader.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultTemplateLoader.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultWikiContext.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultWikiRenderer.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/MarkupSplitter.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ResourceHandler.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ResourceHandlerAdapter.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/TemplateLoader.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiContext.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiException.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiFactory.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiRenderer.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiRendererAdapter.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultMacroLoader.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultMacroSplitter.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultWikiMacro.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/MacroLoader.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/MacroPart.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/MacroPartRenderer.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/MacroSplitter.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/WikiMacro.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/WikiSubRenderer.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/WikiSubRendererAdapter.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/HeaderLineHandler.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/LineRenderer.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/LineSplitter.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/ListLineHandler.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/ParagraphLineHandler.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/QuoteLineHandler.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/TableLineHandler.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/WikiLineContext.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/WikiLineHandler.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/line/WikiLineHandlerContainer.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/list/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/simple/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/simple/EscapeUnescapeRenderer.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/simple/ImageRenderer.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/simple/LinkRenderer.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/simple/SurroundMarkupRenderer.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/renderer/table/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/util/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/util/StringSplitter.java trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/util/io/ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/util/io/TextBuilderWriter.java trunk/staticwiki-renderer/src/main/macro/ trunk/staticwiki-renderer/src/main/macro/org/ trunk/staticwiki-renderer/src/main/macro/org/staticwiki/ trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/ trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/WikiRenderPipeline.txt trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/macro/ trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/macro/ERROR.properties trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/macro/WIKI.properties trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/macro/nowiki.properties trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/macro/quote.properties trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/template/ trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/template/ERROR.vm trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/template/WIKI.vm trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/template/nowiki.vm trunk/staticwiki-renderer/src/main/macro/org/staticwiki/wiki/template/quote.vm trunk/staticwiki-renderer/src/site/ trunk/staticwiki-renderer/src/site/wiki/ trunk/staticwiki-renderer/src/site/wiki/architecture.wiki trunk/staticwiki-renderer/src/test/ trunk/staticwiki-renderer/src/test/java/ trunk/staticwiki-renderer/src/test/java/org/ trunk/staticwiki-renderer/src/test/java/org/staticwiki/ trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/ trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/ConfigReaderTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/DefaultMacroLoaderTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/DefaultMacroSplitterTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/DefaultResourceHandlerTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/DefaultTemplateLoaderTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/MarkupSplitterTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/SimpleWikiTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/WikiFactoryTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/renderer/ trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/renderer/line/ trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/renderer/line/LineSplitterTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/renderer/simple/ trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/renderer/simple/EscapeUnescapeRendererTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/renderer/simple/ImageRendererTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/renderer/simple/LinkRendererTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/renderer/simple/SurroundMarkupRendererTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/util/ trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/util/StringSplitterTest.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/util/io/ trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/util/io/TextBuilderWriterTest.java trunk/staticwiki-renderer/src/test/macro/ trunk/staticwiki-renderer/src/test/macro/org/ trunk/staticwiki-renderer/src/test/macro/org/staticwiki/ trunk/staticwiki-renderer/src/test/macro/org/staticwiki/wiki/ trunk/staticwiki-renderer/src/test/macro/org/staticwiki/wiki/macro/ trunk/staticwiki-renderer/src/test/macro/org/staticwiki/wiki/macro/nobody.properties trunk/staticwiki-renderer/src/test/macro/org/staticwiki/wiki/macro/test.properties trunk/staticwiki-renderer/src/test/macro/org/staticwiki/wiki/macro/withbody.properties trunk/staticwiki-renderer/src/test/macro/org/staticwiki/wiki/template/ trunk/staticwiki-renderer/src/test/macro/org/staticwiki/wiki/template/empty.vm trunk/staticwiki-renderer/src/test/macro/org/staticwiki/wiki/template/test.vm trunk/staticwiki.mm Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2007-06-27 07:17:09 UTC (rev 2) +++ trunk/pom.xml 2007-07-04 05:35:50 UTC (rev 3) @@ -13,7 +13,6 @@ staticwiki is an tool to generate a static wiki site. </description> <modules> - <module>staticwiki-util</module> <module>staticwiki-renderer</module> </modules> <licenses> @@ -106,11 +105,6 @@ </dependency> <dependency> <groupId>org.staticwiki</groupId> - <artifactId>staticwiki-util</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.staticwiki</groupId> <artifactId>staticwiki-renderer</artifactId> <version>${project.version}</version> </dependency> Added: trunk/staticwiki-renderer/pom.xml =================================================================== --- trunk/staticwiki-renderer/pom.xml (rev 0) +++ trunk/staticwiki-renderer/pom.xml 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,56 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <parent> + <groupId>org.staticwiki</groupId> + <artifactId>staticwiki</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <groupId>org.staticwiki</groupId> + <artifactId>staticwiki-renderer</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + <name>staticwiki-renderer</name> + <description> + General wiki rendering code, the defaults render wiki content with markup similar to Confluence. + However, the system can be configured at will to render different wiki styles. + </description> + <build> + <resources> + <resource> + <directory>src/main/resources</directory> + </resource> + <resource> + <directory>src/main/macro</directory> + </resource> + </resources> + <testResources> + <testResource> + <directory>src/test/resources</directory> + </testResource> + <testResource> + <directory>src/test/macro</directory> + </testResource> + </testResources> + </build> + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>velocity</groupId> + <artifactId>velocity-dep</artifactId> + </dependency> + <dependency> + <groupId>org.javolution</groupId> + <artifactId>javolution</artifactId> + </dependency> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <scope>provided</scope> + </dependency> + </dependencies> +</project> Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ConfigReader.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ConfigReader.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ConfigReader.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,110 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +import org.staticwiki.wiki.renderer.WikiSubRenderer; +import org.staticwiki.wiki.renderer.line.WikiLineHandlerContainer; +import org.staticwiki.wiki.renderer.line.WikiLineHandler; + +import java.io.Reader; +import java.io.BufferedReader; +import java.lang.reflect.Constructor; + +/** + * Read the configuration settings for the renderer, specifically how to build the render pipeline. + * It basically expects a reader with a classname on each line. + * If the class needs a parameter, then this can be passed after a "|" seperator (one string parameter only, passed to the constructor). + * If the class is a WikiLineHandler, then it is added in the last preceding WikiLineHandlerContainer. + * All classes are expected to be either WikiSubRenderer or WikiLineHandler instances. + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public class ConfigReader +{ + public WikiSubRenderer buildRenderPipeline( Reader sreader ) + throws WikiException + { + BufferedReader reader = new BufferedReader( sreader ); + WikiSubRenderer root = null; + WikiSubRenderer previous = null; + WikiLineHandlerContainer handlerContainer = null; + + try + { + String line, param, className; + int sepPos; + Class clazz; + Object instance; + Constructor constructor; + while ( ( line = reader.readLine() ) != null ) + { + sepPos = line.indexOf( '|' ); + if ( sepPos == -1 ) + { + className = line; + param = null; + } + else + { + className = line.substring( 0, sepPos ); + param = line.substring( sepPos + 1 ); + } + clazz = Thread.currentThread().getContextClassLoader().loadClass( className ); + if ( param == null ) + { + constructor = clazz.getConstructor(); + instance = constructor.newInstance(); + } + else + { + constructor = clazz.getConstructor( String.class ); + instance = constructor.newInstance( param ); + } + if ( instance instanceof WikiSubRenderer ) + { + WikiSubRenderer curr = (WikiSubRenderer) instance; + if ( root == null ) + { + root = curr; + previous = curr; + } + previous.setWikiSubRenderer( curr ); + if ( curr instanceof WikiLineHandlerContainer ) handlerContainer = (WikiLineHandlerContainer) curr; + previous = curr; + } + else if ( instance instanceof WikiLineHandler ) + { + if ( handlerContainer == null ) + { + throw new WikiException( "WikiLineHandler before first WikiLineHandlerContainer" ); + } + handlerContainer.addLineHandler( (WikiLineHandler) instance ); + } + else + { + throw new WikiException( "instance of unknown type " + instance + ", " + className + + " should be a WikiSubRenderer or WikiLineHandler" ); + } + } + return root; + } + catch ( WikiException we ) + { + throw we; + } + catch ( Exception ex ) + { + throw new WikiException( ex ); + } + } +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultTemplateLoader.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultTemplateLoader.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultTemplateLoader.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,58 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +import javolution.lang.TextBuilder; + +import java.io.*; + +/** + * Default template loader which loads the templates from the classpath. + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public class DefaultTemplateLoader + implements TemplateLoader +{ + public CharSequence getTemplate( String templateName ) + throws WikiException + { + String name = "org/staticwiki/wiki/template/" + templateName + ".vm"; + InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream( name ); + return slurp( stream ); + } + + public String slurp( InputStream stream ) + throws WikiException + { + if ( stream == null ) return ""; + try + { + BufferedReader br = new BufferedReader( new InputStreamReader( stream, "UTF-8" ) ); + TextBuilder sb = TextBuilder.newInstance(); + String line = null; + + while ( ( line = br.readLine() ) != null ) + { + sb.append( line + "\n" ); + } + + br.close(); + return sb.toString(); + } + catch ( Exception ex ) + { + throw new WikiException( ex ); + } + } +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultWikiContext.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultWikiContext.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultWikiContext.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,167 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +import org.staticwiki.wiki.util.io.TextBuilderWriter; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; +import org.staticwiki.wiki.macro.*; +import org.staticwiki.wiki.renderer.WikiSubRenderer; +import org.staticwiki.wiki.renderer.simple.EscapeUnescapeRenderer; +import javolution.lang.TextBuilder; + +import java.io.Writer; +import java.io.IOException; +import java.util.Map; +import java.util.HashMap; +import java.util.Stack; + +/** + * Default implementation for a WikiContext + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public class DefaultWikiContext + implements WikiContext +{ + private VelocityContext velocityContext; + private TemplateLoader templateLoader; + private MacroLoader macroLoader; + private WikiSubRenderer renderPipeline; + private ResourceHandler resourceHandler; + private Map<String, Object> renderMemory = new HashMap<String, Object>(); + private Stack<Map<String, Object>> renderMemoryStack = new Stack<Map<String, Object>>(); + + public DefaultWikiContext() + { + velocityContext = new VelocityContext(); + try + { + Velocity.init(); + } + catch ( Exception ex ) + { + throw new RuntimeException( ex ); + } + templateLoader = new DefaultTemplateLoader(); + macroLoader = new DefaultMacroLoader(); + } + + public VelocityContext getVelocityContext() + { + return velocityContext; + } + + public void setVelocityContext( VelocityContext velocityContext ) + { + this.velocityContext = velocityContext; + } + + public TemplateLoader getTemplateLoader() + { + return templateLoader; + } + + public void setTemplateLoader( TemplateLoader templateLoader ) + { + this.templateLoader = templateLoader; + } + + public MacroLoader getMacroLoader() + { + return macroLoader; + } + + public void setMacroLoader( MacroLoader macroLoader ) + { + this.macroLoader = macroLoader; + } + + public MacroSplitter getMacroSplitter( CharSequence input ) + { + return new DefaultMacroSplitter( this, input ); + } + + public String render( MacroPart input ) + throws WikiException + { + TextBuilder buffer = TextBuilder.newInstance(); + if ( input.canContainMacros() ) + { + MacroPartRenderer renderer = new MacroPartRenderer(); + Writer writer = new TextBuilderWriter( buffer ); + push(); + // split in further macros and render those + for ( MacroPart part : getMacroSplitter( input.getBody() ) ) + { + try + { + renderer.render( this, part, writer ); + } + catch ( IOException ioe ) + { + throw new WikiException( ioe ); + } + } + pop( buffer ); + } + else + { + if ( renderPipeline == null ) + { + renderPipeline = new EscapeUnescapeRenderer(); + } + renderPipeline.setContext( this ); + renderPipeline.render( buffer, input.getBody() ); + } + return buffer.toString(); + } + + public void setRenderPipeline( WikiSubRenderer renderPipeline ) + { + this.renderPipeline = renderPipeline; + } + + public ResourceHandler getResourceHandler() + { + return resourceHandler; + } + + public void setResourceHandler( ResourceHandler resourceHandler ) + { + this.resourceHandler = resourceHandler; + } + + public Object getRenderMemory( String key ) + { + return renderMemory.get( key ); + } + + public void setRenderMemory( String key, Object value ) + { + renderMemory.put( key, value ); + } + + public void push() + { + if ( renderPipeline != null ) renderPipeline.clear( this ); + renderMemoryStack.push( renderMemory ); + renderMemory = new HashMap<String, Object>(); + } + + public void pop( TextBuilder output ) + { + renderMemory = renderMemoryStack.pop(); + if ( renderPipeline != null ) renderPipeline.flush( this, output ); + } +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultWikiRenderer.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultWikiRenderer.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/DefaultWikiRenderer.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,71 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +import org.staticwiki.wiki.macro.MacroPart; +import org.staticwiki.wiki.macro.MacroPartRenderer; +import org.staticwiki.wiki.macro.WikiMacro; + +import java.io.Writer; +import java.io.IOException; + +/** + * Default wiki renderer. + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public class DefaultWikiRenderer + extends WikiRendererAdapter +{ + private WikiContext context; + + public DefaultWikiRenderer( WikiContext renderContext ) + { + context = renderContext; + } + + public void render( CharSequence input, Writer writer ) + throws WikiException, IOException + { + MacroPart macroPart; + MacroPartRenderer renderer = new MacroPartRenderer(); + WikiMacro macro; + + // render the pre-wiki template + macro = context.getMacroLoader().load( "WIKI-PREFIX" ); + renderPrePost( renderer, macro, writer ); + + // render the actual wiki content + macroPart = new MacroPart(); + macroPart.setMacro( context.getMacroLoader().load( "WIKI" ) ); + macroPart.setBody( input ); + renderer.render( context, macroPart, writer ); + + // render the post-wiki template + macro = context.getMacroLoader().load( "WIKI-SUFFIX" ); + renderPrePost( renderer, macro, writer ); + } + + private void renderPrePost( MacroPartRenderer renderer, WikiMacro macro, Writer writer ) + throws WikiException, IOException + { + if ( macro != null ) + { + MacroPart macroPart = new MacroPart(); + macroPart.setMacro( macro ); + macroPart.setBody( "" ); + macroPart.setCanContainMacros( false ); + renderer.render( context, macroPart, writer ); + } + } +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/MarkupSplitter.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/MarkupSplitter.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/MarkupSplitter.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,90 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * Splits a string, providing 2 characters like '!' and '!', '[' and ']' as separators + * + * @author Florin + */ +public class MarkupSplitter + implements Iterator<CharSequence>, Iterable<CharSequence> +{ + CharSequence data; + CharSequence markup; + int curPos; + int nextPos; + char markupBegin, markupEnd; + boolean inside; + + public MarkupSplitter( CharSequence input, char markupBegin, char markupEnd ) + { + data = input; + this.markupBegin = markupBegin; + this.markupEnd = markupEnd; + } + + public boolean hasNext() + { + markup = null; + + nextPos = curPos; + char terminator=(inside)?markupEnd:markupBegin; + + while ( nextPos < data.length() ) + { + char c = data.charAt( nextPos ); + if ( c == terminator ) + { + break; + } + nextPos++; + } + if ( nextPos<data.length() || !inside ) + { + markup = data.subSequence( curPos, nextPos ); + nextPos++; + return true; + } + return false; + } + + public boolean isNextInsideMarkup() + { + return inside; + } + + public CharSequence next() + { + if ( markup != null ) + { + curPos = nextPos; + inside=!inside; + return markup; + } + throw new NoSuchElementException(); + } + + public void remove() + { + throw new UnsupportedOperationException(); + } + + public Iterator<CharSequence> iterator() + { + return this; + } +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ResourceHandler.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ResourceHandler.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ResourceHandler.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,67 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +/** + * Allow checking whether pages exist or not, and determines how links to pages and/or media resources + * need to be rendered. + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public interface ResourceHandler +{ + public static enum ResourceType + { + TYPE_IMAGE, TYPE_OTHER + } + + /** + * Determine the resource type. + * + * @param resource resource location + * @return type + */ + public abstract ResourceType getResourceType( String resource ); + + /** + * Check whether the given resource is valid. + * + * @param resource resource location + * @return true when exists + */ + public abstract boolean resourceExists( String resource ); + + /** + * Get the URL for the given resource. + * + * @param resource resource location + * @return URL as string + */ + public abstract String getResourceURL( String resource ); + + /** + * Check whether the given link is valid. + * + * @param link resource location + * @return true when exists + */ + public abstract boolean linkExists( String link ); + + /** + * Get the URL for the given link. + * + * @param link link location + * @return URL as string + */ + public abstract String getLinkURL( String link ); +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ResourceHandlerAdapter.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ResourceHandlerAdapter.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/ResourceHandlerAdapter.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,60 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +/** + * A simple ResourceHandler which cannot find any resources (allows only external resources). + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + * @author <a href="mailto:fl...@pa...">Florin</a> + */ +public class ResourceHandlerAdapter + implements ResourceHandler +{ + private static String[] imageExtensions = { ".JPG", ".GIF", ".PNG", ".ICO", ".BMP", ".TIF", ".JPEG", ".TIFF" }; + + public ResourceType getResourceType( String resource ) + { + String resolvedURL = getResourceURL( resource ); + if ( resolvedURL == null ) return ResourceType.TYPE_OTHER; + String urlStr = resolvedURL.toUpperCase(); + for ( String ext : imageExtensions ) + { + if ( urlStr.endsWith( ext ) ) return ResourceType.TYPE_IMAGE; + } + return ResourceType.TYPE_OTHER; + } + + public boolean resourceExists( String resource ) + { + return getResourceURL( resource ) != null; + } + + public String getResourceURL( String resource ) + { + if ( resource.toLowerCase().startsWith( "http://" ) ) return resource; + return null; + } + + public boolean linkExists( String link ) + { + return getLinkURL( link ) != null; + } + + public String getLinkURL( String link ) + { + if ( link.toLowerCase().startsWith( "http://" ) ) return link; + return null; + } + +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/TemplateLoader.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/TemplateLoader.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/TemplateLoader.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,31 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +/** + * Template loader, determines how the velocity templates are obtained. + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public interface TemplateLoader +{ + /** + * Get the template (as string). + * + * @param templateName template name + * @return template string + * @throws WikiException oops + */ + CharSequence getTemplate( String templateName ) + throws WikiException; +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiContext.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiContext.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiContext.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,92 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +import org.apache.velocity.VelocityContext; +import org.staticwiki.wiki.macro.MacroLoader; +import org.staticwiki.wiki.macro.MacroPart; +import javolution.lang.TextBuilder; + +/** + * Context which is used for wiki rendering + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public interface WikiContext +{ + /** + * Get the Velocity context which is to be used for rendering. + * + * @return velocity context + */ + VelocityContext getVelocityContext(); + + /** + * Get the TemplateLoader instance which loads template instances based on template name. + * + * @return template loader + */ + TemplateLoader getTemplateLoader(); + + /** + * Get the MacroLoader instance which loads macros based on the macro name. + * + * @return macro loader + */ + MacroLoader getMacroLoader(); + + /** + * Get the resource handler, which is used to check/build resource and page links. + * + * @return resource handler + */ + ResourceHandler getResourceHandler(); + + /** + * Render given MacroPart to a String, using wiki markup. + * + * @param input stuff to render + * @return rendered stuff + * @throws WikiException wiki "render" problems + */ + String render( MacroPart input ) + throws WikiException; + + /** + * Get a value from the render memory based on the given key. + * + * @param key key for data + * @return data for key + */ + Object getRenderMemory( String key ); + + /** + * Set a value in the render memory (key, value pair). + * + * @param key key for data + * @param value value for key + */ + void setRenderMemory( String key, Object value ); + + /** + * Push the rendermemory on a stack and start one a new level with a cleared renderMemory. + */ + void push(); + + /** + * Flush the current renderMemory and pop it off the stack (making the previous valid again). + * + * @param output buffer in which to flush the output + */ + void pop( TextBuilder output ); +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiException.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiException.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiException.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,41 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +/** + * In case of problems during wiki rendering + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public class WikiException + extends Exception +{ + public WikiException() + { + } + + public WikiException( String string ) + { + super( string ); + } + + public WikiException( String string, Throwable throwable ) + { + super( string, throwable ); + } + + public WikiException( Throwable throwable ) + { + super( throwable ); + } +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiFactory.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiFactory.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiFactory.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,74 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +import org.staticwiki.wiki.renderer.WikiSubRenderer; + +import java.io.InputStream; +import java.io.Reader; +import java.io.InputStreamReader; + +/** + * Factory class to get a wiki renderer + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public class WikiFactory +{ + public static WikiRenderer getWikiRenderer() + throws WikiException + { + return getWikiRenderer( "org/staticwiki/wiki/WikiRenderPipeline.txt", new ResourceHandlerAdapter() ); + } + + public static WikiRenderer getWikiRenderer( ResourceHandler resourceHandler ) + throws WikiException + { + return getWikiRenderer( "org/staticwiki/wiki/WikiRenderPipeline.txt", resourceHandler ); + } + + public static WikiRenderer getWikiRenderer( String pipelineResource ) + throws WikiException + { + return getWikiRenderer( pipelineResource, new ResourceHandlerAdapter() ); + } + + public static WikiRenderer getWikiRenderer( String pipelineResource, ResourceHandler resourceHandler ) + throws WikiException + { + DefaultWikiContext context = new DefaultWikiContext(); + context.setRenderPipeline( getRenderPipeline( pipelineResource ) ); + context.setResourceHandler( resourceHandler ); + return getWikiRenderer( context ); + } + + public static WikiRenderer getWikiRenderer( WikiContext context ) + { + return new DefaultWikiRenderer( context ); + } + + private static WikiSubRenderer getRenderPipeline( String pipelineResource ) + throws WikiException + { + InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream( pipelineResource ); + if ( stream == null ) + { + throw new WikiException( "Render pipeline description " + pipelineResource + " not found on classpath." ); + } + Reader reader = new InputStreamReader( stream ); + ConfigReader cr = new ConfigReader(); + return cr.buildRenderPipeline( reader ); + } + + +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiRenderer.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiRenderer.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiRenderer.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,65 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +import java.io.Reader; +import java.io.Writer; +import java.io.IOException; + +/** + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public interface WikiRenderer +{ + /** + * Get the rendered ourtput as string (with input given as string). + * + * @param input wiki string + * @param writer writer for output + * @throws WikiException wiki render problem + * @throws IOException read/write problem + */ + void render( CharSequence input, Writer writer ) + throws WikiException, IOException; + + /** + * Get the rendered output as CHarSequence (with input given as CharSequence). + * + * @param input wiki string + * @return rendered output + * @throws WikiException wiki render problem + */ + CharSequence render( CharSequence input ) + throws WikiException; + + /** + * Get the rendered output as string (with input given as string). + * + * @param input wiki string + * @return rendered output + * @throws WikiException wiki render problem + */ + String render( String input ) + throws WikiException; + + /** + * Render a wiki string, passed in the reader into the given writer. + * + * @param reader reader with wiki string + * @param writer writer for output + * @throws WikiException wiki render problem + * @throws IOException read/write problem + */ + void render( Reader reader, Writer writer ) + throws WikiException, IOException; +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiRendererAdapter.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiRendererAdapter.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/WikiRendererAdapter.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,61 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki; + +import javolution.lang.TextBuilder; +import javolution.lang.Text; + +import java.io.Reader; +import java.io.Writer; +import java.io.StringWriter; +import java.io.IOException; + +/** + * Renderer which can be extended to avoid duplicating convenience methods. + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public abstract class WikiRendererAdapter + implements WikiRenderer +{ + public String render( String input ) + throws WikiException + { + return render( Text.valueOf( input ) ).toString(); + } + + public CharSequence render( CharSequence input ) + throws WikiException + { + StringWriter writer = new StringWriter(); + try + { + render( input, writer ); + writer.close(); + } + catch ( IOException ioe ) + { + // should not happen as this is in-memory + throw new WikiException( ioe ); + } + return writer.getBuffer(); + } + + public void render( Reader reader, Writer writer ) + throws WikiException, IOException + { + TextBuilder tb = TextBuilder.newInstance(); + + render( tb, writer ); + } +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultMacroLoader.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultMacroLoader.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultMacroLoader.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,48 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki.macro; + +import org.staticwiki.wiki.WikiException; + +import java.util.Properties; +import java.io.InputStream; +import java.io.IOException; + +/** + * Macro loader which gets the macro definitions from the classpath (org/staticwiki/wiki/macro/xxx.properties + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + */ +public class DefaultMacroLoader + implements MacroLoader +{ + public WikiMacro load( CharSequence macroName ) + throws WikiException + { + Properties prop = new Properties(); + String name = "org/staticwiki/wiki/macro/" + macroName + ".properties"; + InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream( name ); + if ( stream == null ) return null; + try + { + prop.load( stream ); + } + catch ( IOException ioe ) + { + throw new WikiException( ioe ); + } + boolean hasBody = "true".equals( prop.get( "hasBody" ) ); + String template = (String) prop.get( "template" ); + return new DefaultWikiMacro( macroName.toString(), hasBody, template ); + } +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultMacroSplitter.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultMacroSplitter.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultMacroSplitter.java 2007-07-04 05:35:50 UTC (rev 3) @@ -0,0 +1,264 @@ +/** + * This file is part of the staticwiki project. + * + * The contents of this file are subject to the GNU Lesser General Public License Version 2.1 or later (the "LGPL") + * (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.fsf.org/licenses/lgpl.html + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF + * ANY KIND, either express or implied. See the License for the specific language governing rights and + * limitations under the License. + */ + +package org.staticwiki.wiki.macro; + +import org.staticwiki.wiki.WikiContext; +import org.staticwiki.wiki.WikiException; + +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.HashMap; +import java.util.Map; + +import javolution.lang.Text; + +/** + * Split input according to Radeox/Confluence syntax. + * For example : "{panel:type=info,color=red}blabla{panel}" + * + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> + * @version $Revision $ + */ +public class DefaultMacroSplitter + implements MacroSplitter +{ + private static final char START = '{'; + private static final char END = '}'; + private static final char STARTPARAM = ':'; + private static final char BETWEENPARAM = ','; + private static final char NAMEVALUEPARAM = '='; + private static final char ESCAPE = '\\'; + + private WikiContext context; + private Text data; + + // when scanning for MacroPart pieces, the system searches for a preamble (before the next macro) and + // the actual following macro part + private int startPos, endPos; // start and end of current (scanned) part (inc decl) + private int macroStart; // start of macro (if<endPos) (inc decl) + private int endStartPos, startEndPos; // start and end of current macro body (excl decl) + + private boolean gotNext; // is the next position known + private WikiMacro macro; // macro or null if not inside macro + private Map<String, String> params = new HashMap<String, String>(); + + public DefaultMacroSplitter( WikiContext wc, CharSequence input ) + { + context = wc; + data = Text.valueOf( input ); + } + + /** + * reset internal variables for calculating next part + */ + private void resetForNext() + { + gotNext = false; + params = new HashMap<String, String>(); + macro = null; + startPos = endPos; + } + + /** + * Indicate whether the character at the given index is escaped. + * The character is escaped is preceded by an odd number of esscape characters + * + * @param index index of character to test + * @return escaped status + */ + private boolean isEscaped( int index ) + { + int count = 0; + while ( index > 0 && ESCAPE == data.charAt( --index ) ) count++; + return ( count & 1 ) == 1; + } + + private void calcPreambleAndOrMacro() + { + // scan for macro start + macroStart = startPos; + while ( !gotNext ) + { + // search start of macro definition + while ( !gotNext && macroStart != -1 ) + { + macroStart = data.indexOf( START, macroStart ); + if ( macroStart != -1 ) + { + if ( macroStart >= startPos && !isEscaped( macroStart ) ) gotNext = true; + macroStart++; // prevent loops for escaped START, is corrected again later + } + } + if ( macroStart == -1 ) + { + setLastPart(); + } + else + { + // search end of macro definition + endStartPos = data.indexOf( END, macroStart ); + if ( endStartPos == -1 ) + { + setLastPart(); + return; + } + // and trackback to start of macro again + macroStart = data.lastIndexOf( START, endStartPos ); + processMacroParameters(); + if ( macro == null ) + { + markAsError(); + } + else + { + if ( macro.hasBody() ) + { + Text endTag = Text.valueOf( START ).plus( macro.getName() ).plus( END ); + startEndPos = data.indexOf( endTag, macroStart+1 ); + if ( startEndPos != -1 ) + { + endPos = startEndPos + endTag.length(); + } + else + { + markAsError(); + } + } + else + { + startEndPos = endStartPos; + endPos = endStartPos; + } + } + } + } + } + + private WikiMacro loadMacro( CharSequence name ) + { + try + { + return context.getMacroLoader().load( name ); + } + catch ( WikiException we ) + { + return null; + } + } + + private void processMacroParameters() + { + Text def = data.subtext( macroStart + 1, endStartPos ); + Text name; + endStartPos++; // endStartPos should be start of body instead of end of declaration + int sepPos = def.indexOf( STARTPARAM, 0 ); + if ( sepPos != -1 ) + { + name = def.subtext( 0, sepPos ); + def = def.subtext( sepPos + 1 ); + int paramCount = 1; + for ( sepPos = def.indexOf( BETWEENPARAM, 0 ); sepPos != -1; paramCount++ ) + { + setParam( def.subtext( 0, sepPos ), paramCount ); + def = def.subtext( sepPos + 1 ); + sepPos = def.indexOf( BETWEENPARAM, 0 ); + } + setParam( def, paramCount ); + } + else + { + name = def; + } + macro = loadMacro( name ); + } + + private void setParam( Text param, int index ) + { + int pos = param.indexOf( NAMEVALUEPARAM, 0 ); + if ( pos == -1 ) + { + params.put( Integer.toString( index ), param.toString() ); + } + else + { + params.put( param.subtext( 0, pos ).toString(), param.subtext( pos + 1 ).toString() ); + } + } + + private void markAsError() + { + macro = loadMacro( "ERROR" ); + if ( macro == null ) + { + // oops, no error macro, just ignore + macroStart = endStartPos; + gotNext = false; + } + else + { + endPos = endStartPos; + startEndPos = endStartPos; + endStartPos = macroStart; + gotNext = true; + } + } + + private void setLastPart() + { + endPos = data.length(); + macroStart = endPos; + gotNext = true; + } + + public boolean hasNext() + { + if ( gotNext ) return true; + if ( startPos == endPos && startPos >= data.length() ) return false; + calcPreambleAndOrMacro(); + return true; + } + + public MacroPart next() + { + if ( !hasNext() ) throw new NoSuchElementException(); + MacroPart res = new MacroPart(); + if ( macroStart > startPos ) + { + // return preamble (not inside macro) + res.setBody( Text.valueOf( data, startPos, macroStart ) ); + res.setCanContainMacros( false ); + startPos = macroStart; + if ( startPos == endPos ) resetForNext(); + } + else + { + // return macro + res.setBody( Text.valueOf( data, endStartPos, startEndPos ) ); + res.setCanContainMacros( true ); + res.setMacro( macro ); + res.setParameters( params ); + resetForNext(); + } + return res; + } + + public void remove() + { + throw new UnsupportedOperationException(); + } + + public Iterator<MacroPart> iterator() + { + return this; + } +} Added: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultWikiMacro.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/macro/DefaultWikiMacro.java (rev 0) +++ trunk/staticwiki-renderer/src/main/java/org/sta... [truncated message content] |