Skip to content

Commit

Permalink
use EncodedAppenderWriter solely for looking up String encoding state
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari committed Apr 4, 2013
1 parent 32f05e5 commit a5a6a8e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
Expand Up @@ -4,9 +4,9 @@
import java.io.Writer;

public class EncodedAppenderWriter extends Writer {
private EncodedAppender encodedAppender;
private Encoder encoder;
private EncodingStateRegistry encodingStateRegistry;
protected EncodedAppender encodedAppender;
protected Encoder encoder;
protected EncodingStateRegistry encodingStateRegistry;

public EncodedAppenderWriter(EncodedAppender encodedAppender, Encoder encoder, EncodingStateRegistry encodingStateRegistry) {
this.encodedAppender=encodedAppender;
Expand Down
Expand Up @@ -19,7 +19,7 @@ class StreamCharBufferSpec extends Specification {

def setup() {
buffer=new StreamCharBuffer()
out=new GrailsPrintWriter(buffer.writer)
out=new GrailsPrintWriter(buffer.writerForEncoder)

def grailsApplication = Mock(GrailsApplication)
GrailsCodecClass htmlCodecClass = new DefaultGrailsCodecClass(HTMLCodec)
Expand Down Expand Up @@ -59,8 +59,8 @@ class StreamCharBufferSpec extends Specification {
when:
def hello="Hello world & hi".encodeAsHTML()
def buffer2=new StreamCharBuffer()
buffer2.writer << hello
buffer2.writer << "<script>"
buffer2.writerForEncoder << hello
buffer2.writerForEncoder << "<script>"
codecOut << buffer2
then:
buffer.toString() == "Hello world &amp; hi&lt;script&gt;"
Expand All @@ -70,12 +70,12 @@ class StreamCharBufferSpec extends Specification {
when:
def hello="Hello world & hi"
def buffer2=new StreamCharBuffer()
buffer2.writer << hello
buffer2.writer.flush()
buffer2.writerForEncoder << hello
buffer2.writerForEncoder.flush()
def buffer3=new StreamCharBuffer()
def helloEncoded = buffer2.encodeAsHTML().toString()
buffer3.writer << helloEncoded
buffer3.writer << "<script>"
buffer3.writerForEncoder << helloEncoded
buffer3.writerForEncoder << "<script>"
codecOut << buffer3
then:
buffer.toString() == "Hello world &amp; hi&lt;script&gt;"
Expand All @@ -85,12 +85,12 @@ class StreamCharBufferSpec extends Specification {
when:
def hello="Hello world & hi"
def buffer2=new StreamCharBuffer()
def writer = new GrailsPrintWriter(buffer2.writer)
def writer = new GrailsPrintWriter(buffer2.writerForEncoder)
writer << hello
writer.flush()
def buffer3=new StreamCharBuffer()
def helloEncoded = buffer2.encodeAsHTML().encodeAsHTML()
def writer2 = new GrailsPrintWriter(buffer3.writer)
def writer2 = new GrailsPrintWriter(buffer3.writerForEncoder)
writer2 << helloEncoded
writer2 << "<script>"
codecOut << buffer3
Expand All @@ -103,12 +103,12 @@ class StreamCharBufferSpec extends Specification {
when:
def hello="Hello world & hi"
def buffer2=new StreamCharBuffer()
def writer = new GrailsPrintWriter(buffer2.writer)
def writer = new GrailsPrintWriter(buffer2.writerForEncoder)
writer << hello
writer.flush()
def buffer3=new StreamCharBuffer()
def helloEncoded = buffer2.encodeAsRaw().encodeAsHTML()
def writer2 = new GrailsPrintWriter(buffer3.writer)
def writer2 = new GrailsPrintWriter(buffer3.writerForEncoder)
writer2 << helloEncoded
writer2 << "<script>"
codecOut << buffer3
Expand Down
Expand Up @@ -48,6 +48,7 @@
import org.codehaus.groovy.grails.support.encoding.EncodingStateRegistry;
import org.codehaus.groovy.grails.support.encoding.StreamEncodeable;
import org.codehaus.groovy.grails.support.encoding.StreamingEncoder;
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest;

/**
* <p>
Expand Down Expand Up @@ -947,25 +948,12 @@ private final int getNewChunkMinSize() {

@Override
public final void write(final String str) throws IOException {
write(resolveEncodingState(str), str, 0, str.length());
write(null, str, 0, str.length());
}

@Override
public final void write(final String str, final int off, final int len) throws IOException {
EncodingState encodingState=null;
if(off==0 && len==str.length())
encodingState = resolveEncodingState(str);
write(encodingState, str, off, len);
}

private EncodingState resolveEncodingState(String str) {
if(encodingStateRegistry==null) {
encodingStateRegistry=DefaultGrailsCodecClass.getEncodingStateRegistryLookup() != null ? DefaultGrailsCodecClass.getEncodingStateRegistryLookup().lookup() : null;
}
if(encodingStateRegistry != null) {
return encodingStateRegistry.getEncodingStateFor(str);
}
return null;
write(null, str, off, len);
}

public void append(Encoder encoder, EncodingState encodingState, CharSequence str, int off, int len) throws IOException {
Expand Down Expand Up @@ -1050,10 +1038,7 @@ public final Writer append(final CharSequence csq, final int start, final int en
write("null");
}
else {
EncodingState encodingState=null;
if(csq instanceof String && start==0 && end==csq.length())
encodingState = resolveEncodingState((String)csq);
appendCharSequence(encodingState, csq, start, end);
appendCharSequence(null, csq, start, end);
}
return this;
}
Expand Down Expand Up @@ -2085,7 +2070,36 @@ public CharSequence encode(Encoder encoder) {
return encodeToBuffer(encoder);
}

public Writer getWriterForEncoder() {
return getWriterForEncoder(null);
}

public Writer getWriterForEncoder(Encoder encoder) {
return getWriterForEncoder(encoder, DefaultGrailsCodecClass.getEncodingStateRegistryLookup() != null ? DefaultGrailsCodecClass.getEncodingStateRegistryLookup().lookup() : null);
}

public Writer getWriterForEncoder(Encoder encoder, EncodingStateRegistry encodingStateRegistry) {
return new EncodedAppenderWriter((EncodedAppender)getWriter(), encoder, encodingStateRegistry);
return new StreamCharBufferEncodedAppenderWriter(getWriter(), encoder, encodingStateRegistry);
}

private static final class StreamCharBufferEncodedAppenderWriter extends EncodedAppenderWriter implements GrailsWrappedWriter {
private Writer writer;

public StreamCharBufferEncodedAppenderWriter(Writer writer, Encoder encoder, EncodingStateRegistry encodingStateRegistry) {
super((EncodedAppender)writer, encoder, encodingStateRegistry);
this.writer=writer;
}

public boolean isAllowUnwrappingOut() {
return encoder==null;
}

public Writer unwrap() {
return writer;
}

public void markUsed() {

}
}
}

0 comments on commit a5a6a8e

Please sign in to comment.