Skip to content

Commit

Permalink
Readded deprecated parse() method to PageParser, and added getStringC…
Browse files Browse the repository at this point in the history
…ontent() method to buffer fragments so that toString() could safely be used in other contexts
  • Loading branch information
jroper committed Jul 11, 2011
1 parent dab5430 commit 8bbb550
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public static Builder builder() {
}

public static Builder builder(SitemeshBuffer sitemeshBuffer) {
return new Builder((DefaultSitemeshBuffer) sitemeshBuffer);
if (sitemeshBuffer instanceof DefaultSitemeshBuffer) {
return new Builder((DefaultSitemeshBuffer) sitemeshBuffer);
} else {
return new Builder(sitemeshBuffer);
}
}

public static class Builder {
Expand All @@ -105,6 +109,12 @@ private Builder(DefaultSitemeshBuffer buffer) {
this.fragments = new TreeMap<Integer, SitemeshBufferFragment>(buffer.bufferFragments);
}

private Builder(SitemeshBuffer buffer) {
this.buffer = buffer.getCharArray();
this.length = buffer.getBufferLength();
this.fragments = new TreeMap<Integer, SitemeshBufferFragment>();
}

public Builder setBuffer(char[] buffer) {
this.buffer = buffer;
return this;
Expand Down
16 changes: 15 additions & 1 deletion src/java/com/opensymphony/module/sitemesh/PageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,26 @@
* @version $Revision: 1.2 $
*/
public interface PageParser {

/**
* This builds a Page.
* Parse the given buffer into a page object. {@link DefaultSitemeshBuffer} is the appropriate implementation of
* this interface to pass in.
*
* @param buffer The buffer for the page.
* @return The parsed page
* @throws IOException if an error occurs
*/
Page parse(SitemeshBuffer buffer) throws IOException;

/**
* Parse the given buffer into a Page object.
*
* @param buffer The buffer for the page.
* @return The parsed page
* @throws IOException if an error occurs
* @deprecated Use {@link PageParser#parse(SitemeshBuffer)}, to allow performance improvement such as single buffer
* parsing and buffer chaining.
*/
@Deprecated
Page parse(char[] buffer) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public int getTotalLength() {
return total;
}

@Override
public String toString() {
public String getStringContent() {
StringWriter writer = new StringWriter();
try {
writeTo(writer);
Expand All @@ -90,6 +89,19 @@ public String toString() {
return writer.toString();
}

@Override
public String toString()
{
return "SitemeshBufferFragment{" +
// Here we generate our own ID, because if the underlying writer is a CharArrayWriter, we'll end up
// with its entire contents, which we don't really want in this method.
"buffer=" + buffer.getClass().getName() + "@" + Integer.toHexString(hashCode()) +
", start=" + start +
", length=" + length +
", deletions=" + deletions +
'}';
}

public int getStart() {
return start;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected SitemeshBufferFragment.Builder currentBuffer() {
}

protected String getCurrentBufferContent() {
return context.currentBuffer().build().toString();
return context.currentBuffer().build().getStringContent();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public CustomTag(Tag tag) {
public String getContents() {
SitemeshBufferFragment.Builder buffer = SitemeshBufferFragment.builder().setBuffer(new DefaultSitemeshBuffer(new char[]{}));
writeTo(buffer, 0);
return buffer.build().toString();
return buffer.build().getStringContent();
}

public void writeTo(SitemeshBufferFragment.Builder buffer, int position) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.opensymphony.module.sitemesh.multipass;

import com.opensymphony.module.sitemesh.DefaultSitemeshBuffer;
import com.opensymphony.module.sitemesh.PageParser;
import com.opensymphony.module.sitemesh.Page;
import com.opensymphony.module.sitemesh.SitemeshBuffer;
Expand All @@ -21,6 +22,10 @@ public MultipassReplacementPageParser(Page page, HttpServletResponse response) {
this.response = response;
}

public Page parse(char[] buffer) throws IOException {
return parse(new DefaultSitemeshBuffer(buffer));
}

public Page parse(SitemeshBuffer sitemeshBuffer) throws IOException {
SitemeshBufferFragment.Builder builder = SitemeshBufferFragment.builder().setBuffer(sitemeshBuffer);
HTMLProcessor processor = new HTMLProcessor(sitemeshBuffer, builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package com.opensymphony.module.sitemesh.parser;

import com.opensymphony.module.sitemesh.DefaultSitemeshBuffer;
import com.opensymphony.module.sitemesh.Page;
import com.opensymphony.module.sitemesh.PageParser;
import com.opensymphony.module.sitemesh.SitemeshBuffer;
Expand Down Expand Up @@ -85,6 +86,11 @@ public final class FastPageParser implements PageParser
private static final int SLASH_BODY_HASH = 46434897; // "/body".hashCode();
private static final int CONTENT_HASH = 951530617; // "content".hashCode();

public Page parse(char[] buffer) throws IOException
{
return parse(new DefaultSitemeshBuffer(buffer));
}

public Page parse(SitemeshBuffer buffer) throws IOException
{
CharArrayReader reader = new CharArrayReader(buffer.getCharArray(), 0, buffer.getBufferLength());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.opensymphony.module.sitemesh.parser;

import com.opensymphony.module.sitemesh.DefaultSitemeshBuffer;
import com.opensymphony.module.sitemesh.Page;
import com.opensymphony.module.sitemesh.PageParser;
import com.opensymphony.module.sitemesh.SitemeshBuffer;
Expand Down Expand Up @@ -34,6 +35,10 @@
*/
public class HTMLPageParser implements PageParser {

public Page parse(char[] buffer) throws IOException {
return parse(new DefaultSitemeshBuffer(buffer));
}

public Page parse(SitemeshBuffer buffer) throws IOException {
SitemeshBufferFragment.Builder head = SitemeshBufferFragment.builder().setBuffer(buffer).setLength(0);
SitemeshBufferFragment.Builder body = SitemeshBufferFragment.builder().setBuffer(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
public class PartialPageParser implements PageParser
{
public Page parse(char[] buffer) throws IOException {
return parse(new DefaultSitemeshBuffer(buffer));
}

public Page parse(SitemeshBuffer buffer) throws IOException
{
char[] data = buffer.getCharArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public void writeBody(Writer out) throws IOException {
}

public String getHead() {
return head.toString();
return head.getStringContent();
}

public String getBody() {
return body.toString();
return body.getStringContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.opensymphony.module.sitemesh.SitemeshBuffer;
import com.opensymphony.module.sitemesh.SitemeshBufferFragment;
import com.opensymphony.module.sitemesh.html.rules.TagReplaceRule;
import com.opensymphony.module.sitemesh.html.util.StringSitemeshBuffer;

import junit.framework.TestCase;

Expand All @@ -14,7 +15,7 @@ public class HTMLProcessorTest extends TestCase {
private SitemeshBufferFragment.Builder body;

private HTMLProcessor createProcessor(String input) {
SitemeshBuffer buffer = new DefaultSitemeshBuffer(input.toCharArray());
SitemeshBuffer buffer = new StringSitemeshBuffer(input);
body = SitemeshBufferFragment.builder().setBuffer(buffer);
return new HTMLProcessor(buffer, body);
}
Expand Down Expand Up @@ -42,7 +43,7 @@ public void testSupportsConventionalReaderAndWriter() throws IOException {
processor.addRule(new TagReplaceRule("b", "strong"));

processor.process();
assertEquals("<hello><strong id=\"something\">world</strong></hello>", body.build().toString());
assertEquals("<hello><strong id=\"something\">world</strong></hello>", body.build().getStringContent());
}

public void testAllowsRulesToModifyAttributes() throws IOException {
Expand All @@ -61,7 +62,7 @@ public void process(Tag tag) {
});

processor.process();
assertEquals("<hello><a href=\"MODIFY-ME\">world</a></hello>", body.build().toString());
assertEquals("<hello><a href=\"MODIFY-ME\">world</a></hello>", body.build().getStringContent());
}

public void testSupportsChainedFilteringOfTextContent() throws IOException {
Expand All @@ -78,7 +79,7 @@ public String filter(String text) {
});

processor.process();
assertEquals("<HELLo>WoRLD</HELLo>", body.build().toString());
assertEquals("<HELLo>WoRLD</HELLo>", body.build().getStringContent());
}

public void testSupportsTextFiltersForSpecificStates() throws IOException {
Expand All @@ -93,7 +94,7 @@ public String filter(String text) {
});

processor.process();
assertEquals("la la<br> la la <capitalism>LAAAA<BR> LAAAA</capitalism> la la", body.build().toString());
assertEquals("la la<br> la la <capitalism>LAAAA<BR> LAAAA</capitalism> la la", body.build().getStringContent());
}

public void testCanAddAttributesToCustomTag() throws IOException {
Expand All @@ -115,6 +116,6 @@ public void process(Tag tag) {
}
});
htmlProcessor.process();
assertEquals("<h1 class=\"y\">Headline</h1>", body.build().toString());
assertEquals("<h1 class=\"y\">Headline</h1>", body.build().getStringContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void testReplacesTextContentMatchedByRegularExpression() throws IOExcepti
processor.addTextFilter(new RegexReplacementTextFilter("DATE", "1-jan-2009"));

processor.process();
assertEquals("<hello>Today is 1-jan-2009 so hi</hello>", body.build().toString());
assertEquals("<hello>Today is 1-jan-2009 so hi</hello>", body.build().getStringContent());
}

public void testAllowsMatchedGroupToBeUsedInSubsitution() throws IOException {
Expand All @@ -40,7 +40,7 @@ public void testAllowsMatchedGroupToBeUsedInSubsitution() throws IOException {
processor.process();
assertEquals(
"<hello>I think <a href='http://jira.opensymhony.com/browse/SIM-1234'>SIM-1234</a> is the way forward</hello>",
body.build().toString());
body.build().getStringContent());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void testProperties() throws Exception {
String pageKey = pageKeys[i];
String blockValue = props.getProperty(pageKey);
String pageValue = page.getProperty(pageKey);
assertEquals(file.getName(),
assertEquals(file.getName() + ": " + pageKey,
blockValue == null ? null : blockValue.trim(),
pageValue == null ? null : pageValue.trim());
}
Expand Down

0 comments on commit 8bbb550

Please sign in to comment.