From: <ath...@us...> - 2014-03-28 10:40:37
|
Revision: 1593 http://sourceforge.net/p/webassembletool/code/1593 Author: athaveau Date: 2014-03-28 10:40:32 +0000 (Fri, 28 Mar 2014) Log Message: ----------- Handle unknown tag with NullObject pattern. Modified Paths: -------------- trunk/esigate-app-aggregated1/src/main/webapp/template.html trunk/esigate-app-aggregated1/src/main/webapp/templatewithvariables.html trunk/esigate-app-aggregator/src/test/resources/template.html trunk/esigate-core/src/main/java/org/esigate/parser/Parser.java trunk/esigate-core/src/main/java/org/esigate/parser/ParserContext.java trunk/esigate-core/src/main/java/org/esigate/parser/ParserContextImpl.java trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParser.java trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParserContext.java trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParserContextImpl.java trunk/esigate-core/src/test/java/org/esigate/esi/BaseElementTest.java trunk/esigate-core/src/test/java/org/esigate/extension/parallelesi/BaseElementTest.java Added Paths: ----------- trunk/esigate-core/src/main/java/org/esigate/parser/UnknownElement.java trunk/esigate-core/src/main/java/org/esigate/parser/UnknownElementType.java trunk/esigate-core/src/main/java/org/esigate/parser/future/UnknownElement.java Modified: trunk/esigate-app-aggregated1/src/main/webapp/template.html =================================================================== --- trunk/esigate-app-aggregated1/src/main/webapp/template.html 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-app-aggregated1/src/main/webapp/template.html 2014-03-28 10:40:32 UTC (rev 1593) @@ -3,6 +3,7 @@ <!--$beginput$content$--> <div style="background-color: yellow"> Some text from aggregated1 +<!--$unknowntag$aggregated2$block.html$myblock$--><!--$unknowntag$--> </div> <!--$endput$--> <!--$endincludetemplate$--> Modified: trunk/esigate-app-aggregated1/src/main/webapp/templatewithvariables.html =================================================================== --- trunk/esigate-app-aggregated1/src/main/webapp/templatewithvariables.html 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-app-aggregated1/src/main/webapp/templatewithvariables.html 2014-03-28 10:40:32 UTC (rev 1593) @@ -3,6 +3,7 @@ <!--$beginput$content$--> <div style="background-color: yellow"> Some text from aggregated1 +<!--$unknowntag$aggregated2$block.html$myblock$--><!--$unknowntag$--> </div> <!--$endput$--> <!--$endincludetemplate$--> Modified: trunk/esigate-app-aggregator/src/test/resources/template.html =================================================================== --- trunk/esigate-app-aggregator/src/test/resources/template.html 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-app-aggregator/src/test/resources/template.html 2014-03-28 10:40:32 UTC (rev 1593) @@ -9,6 +9,7 @@ <div style="background-color: yellow"> Some text from aggregated1 +<!--$unknowntag$aggregated2$block.html$myblock$--><!--$unknowntag$--> </div> </body> Modified: trunk/esigate-core/src/main/java/org/esigate/parser/Parser.java =================================================================== --- trunk/esigate-core/src/main/java/org/esigate/parser/Parser.java 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-core/src/main/java/org/esigate/parser/Parser.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -16,6 +16,8 @@ package org.esigate.parser; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -28,7 +30,7 @@ public class Parser { private static final Logger LOG = LoggerFactory.getLogger(Parser.class); private final Pattern pattern; - private final ElementType[] elementTypes; + private final List<ElementType> elementTypes; private DriverRequest httpRequest; private HttpResponse httpResponse; @@ -42,7 +44,12 @@ */ public Parser(Pattern pattern, ElementType... elementTypes) { this.pattern = pattern; - this.elementTypes = elementTypes; + this.elementTypes = new ArrayList<ElementType>(elementTypes.length + 1); + for (ElementType elementType : elementTypes) { + this.elementTypes.add(elementType); + } + this.elementTypes.add(new UnknownElementType()); + } /** @@ -77,17 +84,12 @@ break; } } - if (type != null) { - Element element = type.newInstance(); - ctx.startElement(type, element, tag); - if (element.isClosed()) { - ctx.endElement(tag); - } - } else { - // if no element matches, we just ignore it and write it to - // the output - ctx.characters(tag); + Element element = type.newInstance(); + ctx.startElement(type, element, tag); + if (element.isClosed()) { + ctx.endElement(tag); } + } } // we reached the end of input Modified: trunk/esigate-core/src/main/java/org/esigate/parser/ParserContext.java =================================================================== --- trunk/esigate-core/src/main/java/org/esigate/parser/ParserContext.java 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-core/src/main/java/org/esigate/parser/ParserContext.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -18,6 +18,8 @@ import org.apache.http.HttpResponse; import org.esigate.impl.DriverRequest; +import java.io.IOException; + /** * The current context used during parsing. * @@ -40,4 +42,8 @@ Element getCurrent(); <T> T findAncestor(Class<T> type); + + /** Writes characters into current writer. */ + void characters(CharSequence cs) throws IOException; + } Modified: trunk/esigate-core/src/main/java/org/esigate/parser/ParserContextImpl.java =================================================================== --- trunk/esigate-core/src/main/java/org/esigate/parser/ParserContextImpl.java 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-core/src/main/java/org/esigate/parser/ParserContextImpl.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -89,7 +89,7 @@ } /** Writes characters into current writer. */ - void characters(CharSequence cs) throws IOException { + public void characters(CharSequence cs) throws IOException { characters(cs, 0, cs.length()); } Added: trunk/esigate-core/src/main/java/org/esigate/parser/UnknownElement.java =================================================================== --- trunk/esigate-core/src/main/java/org/esigate/parser/UnknownElement.java (rev 0) +++ trunk/esigate-core/src/main/java/org/esigate/parser/UnknownElement.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -0,0 +1,52 @@ +/* + * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.esigate.parser; + +import org.esigate.HttpErrorPage; + +import java.io.IOException; + +/** + * NullObject pattern to handle unknown tag + * @author Alexis Thaveau + */ +public class UnknownElement implements Element { + @Override + public void onTagStart(String tag, ParserContext ctx) throws IOException, HttpErrorPage { + //Write content in parent element + ctx.characters(tag); + } + + @Override + public void onTagEnd(String tag, ParserContext ctx) throws IOException, HttpErrorPage { + + } + + @Override + public boolean onError(Exception e, ParserContext ctx) { + return false; + } + + @Override + public void characters(CharSequence csq, int start, int end) throws IOException { + throw new UnsupportedOperationException("characters are appended in onTagStart method"); + } + + @Override + public boolean isClosed() { + return true; + } +} Added: trunk/esigate-core/src/main/java/org/esigate/parser/UnknownElementType.java =================================================================== --- trunk/esigate-core/src/main/java/org/esigate/parser/UnknownElementType.java (rev 0) +++ trunk/esigate-core/src/main/java/org/esigate/parser/UnknownElementType.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -0,0 +1,43 @@ +/* + * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.esigate.parser; + +/** + * NullObject pattern to handle unknown tag type + * + * @author Alexis thaveau + */ +public class UnknownElementType implements ElementType { + /** + * singleton + */ + private static final UnknownElement INSTANCE = new UnknownElement(); + + @Override + public boolean isStartTag(String tag) { + return true; + } + + @Override + public boolean isEndTag(String tag) { + return false; + } + + @Override + public Element newInstance() { + return UnknownElementType.INSTANCE; + } +} Modified: trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParser.java =================================================================== --- trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParser.java 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParser.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -16,7 +16,9 @@ package org.esigate.parser.future; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -24,6 +26,7 @@ import org.apache.http.HttpResponse; import org.esigate.HttpErrorPage; import org.esigate.impl.DriverRequest; +import org.esigate.parser.UnknownElementType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,7 +40,7 @@ public class FutureParser { private static final Logger LOG = LoggerFactory.getLogger(FutureParser.class); private final Pattern pattern; - private final FutureElementType[] elementTypes; + private final List<FutureElementType> elementTypes; private DriverRequest httpRequest; private HttpResponse httpResponse; private Map<String, Object> data = null; @@ -52,7 +55,11 @@ */ public FutureParser(Pattern pattern, FutureElementType... elementTypes) { this.pattern = pattern; - this.elementTypes = elementTypes; + this.elementTypes = new ArrayList<FutureElementType>(elementTypes.length + 1); + for (FutureElementType elementType : elementTypes) { + this.elementTypes.add(elementType); + } + this.elementTypes.add(UnknownElement.TYPE); } /** @@ -87,16 +94,10 @@ break; } } - if (type != null) { - FutureElement element = type.newInstance(); - ctx.startElement(type, element, tag); - if (element.isClosed()) { - ctx.endElement(tag); - } - } else { - // if no element matches, we just ignore it and write it to - // the output - ctx.characters(new CharSequenceFuture(tag)); + FutureElement element = type.newInstance(); + ctx.startElement(type, element, tag); + if (element.isClosed()) { + ctx.endElement(tag); } } } Modified: trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParserContext.java =================================================================== --- trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParserContext.java 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParserContext.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -18,6 +18,9 @@ import org.apache.http.HttpResponse; import org.esigate.impl.DriverRequest; +import java.io.IOException; +import java.util.concurrent.Future; + /** * The current context used during parsing. * <p> @@ -54,4 +57,9 @@ */ Object getData(String key); + + + /** Writes characters into current writer. */ + void characters(Future<CharSequence> csq) throws IOException; + } Modified: trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParserContextImpl.java =================================================================== --- trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParserContextImpl.java 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-core/src/main/java/org/esigate/parser/future/FutureParserContextImpl.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -101,7 +101,7 @@ } /** Writes characters into current writer. */ - void characters(Future<CharSequence> csq) throws IOException { + public void characters(Future<CharSequence> csq) throws IOException { getCurrent().characters(csq); } Added: trunk/esigate-core/src/main/java/org/esigate/parser/future/UnknownElement.java =================================================================== --- trunk/esigate-core/src/main/java/org/esigate/parser/future/UnknownElement.java (rev 0) +++ trunk/esigate-core/src/main/java/org/esigate/parser/future/UnknownElement.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -0,0 +1,82 @@ +/* + * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.esigate.parser.future; + +import org.esigate.HttpErrorPage; +import org.esigate.parser.future.CharSequenceFuture; +import org.esigate.parser.future.FutureElement; +import org.esigate.parser.future.FutureElementType; +import org.esigate.parser.future.FutureParserContext; + +import java.io.IOException; +import java.util.concurrent.Future; + +/** + * NullObject pattern to handle unknown tag + * + * @author Alexis thaveau + */ +public class UnknownElement implements FutureElement { + + public static final FutureElementType TYPE = new FutureElementType() { + + private final UnknownElement instance = new UnknownElement(); + + @Override + public boolean isStartTag(String tag) { + return true; + } + + @Override + public boolean isEndTag(String tag) { + return false; + } + + @Override + public FutureElement newInstance() { + return instance; + } + }; + + @Override + public void onTagStart(String tag, FutureParserContext ctx) throws IOException, HttpErrorPage { + ctx.characters(new CharSequenceFuture(tag)); + } + + @Override + public void onTagEnd(String tag, FutureParserContext ctx) throws IOException, HttpErrorPage { + + } + + @Override + public boolean onError(Exception e, FutureParserContext ctx) { + return false; + } + + @Override + public void characters(Future<CharSequence> csq) throws IOException { + throw new UnsupportedOperationException("characters are appended in onTagStart method"); + } + + @Override + public boolean isClosed() { + return true; + } + + @Override + public FutureElement getParent() { + return null; + } +} Modified: trunk/esigate-core/src/test/java/org/esigate/esi/BaseElementTest.java =================================================================== --- trunk/esigate-core/src/test/java/org/esigate/esi/BaseElementTest.java 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-core/src/test/java/org/esigate/esi/BaseElementTest.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -22,6 +22,8 @@ import org.esigate.parser.Element; import org.esigate.parser.ParserContext; +import java.io.IOException; + public class BaseElementTest extends TestCase { public void testOnTagStart() throws Exception { @@ -97,5 +99,10 @@ return null; } + @Override + public void characters(CharSequence cs) throws IOException { + + } + } } Modified: trunk/esigate-core/src/test/java/org/esigate/extension/parallelesi/BaseElementTest.java =================================================================== --- trunk/esigate-core/src/test/java/org/esigate/extension/parallelesi/BaseElementTest.java 2014-03-28 09:05:40 UTC (rev 1592) +++ trunk/esigate-core/src/test/java/org/esigate/extension/parallelesi/BaseElementTest.java 2014-03-28 10:40:32 UTC (rev 1593) @@ -22,6 +22,9 @@ import org.esigate.parser.future.FutureElement; import org.esigate.parser.future.FutureParserContext; +import java.io.IOException; +import java.util.concurrent.Future; + public class BaseElementTest extends TestCase { public void testOnTagStart() throws Exception { @@ -101,5 +104,10 @@ public Object getData(String key) { return null; } + + @Override + public void characters(Future<CharSequence> csq) throws IOException { + + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |