Author: mir
Date: Thu Jan 7 09:35:31 2010
New Revision: 896819
URL: http://svn.apache.org/viewvc?rev=896819&view=rev
Log:
CLEREZZA-39: removes now XML declaration
Modified:
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannel.java
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java
Modified:
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannel.java
URL:
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannel.java?rev=896819&r1=896818&r2=896819&view=diff
==============================================================================
---
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannel.java
(original)
+++
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannel.java
Thu Jan 7 09:35:31 2010
@@ -21,9 +21,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.OutputStream;
import java.nio.ByteBuffer;
-import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
/**
@@ -34,12 +32,18 @@
private final static byte[] DOCTYPE_DEF_BYTES = "<!DOCTYPE html PUBLIC
\"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> ".getBytes();
private final static byte[] DOCTYPE_TAG_BYTES = "<!DOCTYPE".getBytes();
+ private final static byte[] XML_DECLARATION_BYTES =
"<?xml".getBytes();
+ private final static byte GREATER_THAN = ">".getBytes()[0];
private final static byte SPACE = " ".getBytes()[0];
+ private final static byte NEXTLINE = "\n".getBytes()[0];
+ private final static byte CARRIAGE_RETURN = "\r".getBytes()[0];
private WritableByteChannel wrappedByteChannel;
private boolean doctypeWritten = false;
- private int doctypePosition = 0;
- private ByteArrayOutputStream readBytes = new ByteArrayOutputStream();
+ private int arrayPosition = 0;
+ private ByteArrayOutputStream cachedBytes = new ByteArrayOutputStream();
private ResponseStatusInfo wrappedResponse;
+ private boolean isXmlDeclaration = true;
+ private boolean isNotADoctypeDef = false;
public DocTypeSettingByteChannel(WritableByteChannel byteChannel,
ResponseStatusInfo wrappedResponse) {
@@ -51,29 +55,48 @@
public int write(ByteBuffer byteBuffer) throws IOException {
if (!doctypeWritten && wrappedResponse.isHtml()) {
int writtenBytes = byteBuffer.remaining();
+ System.out.println(writtenBytes);
while (byteBuffer.remaining() > 0) {
byte b = byteBuffer.get();
- readBytes.write(b);
- if (doctypePosition == 0 && b == SPACE) {
+ cachedBytes.write(b);
+ if (arrayPosition == 0 &&
+ (b == SPACE || b == NEXTLINE ||
b == CARRIAGE_RETURN)) {
continue;
- }
- if (doctypePosition ==
(DOCTYPE_TAG_BYTES.length - 1) &&
-
DOCTYPE_TAG_BYTES[doctypePosition] == b) {
-
writeToWrappedChannel(readBytes.toByteArray());
- wrappedByteChannel.write(byteBuffer);
- doctypeWritten = true;
- return writtenBytes;
- }
- if (DOCTYPE_TAG_BYTES[doctypePosition] != b) {
-
writeToWrappedChannel(DOCTYPE_DEF_BYTES);
-
writeToWrappedChannel(readBytes.toByteArray());
- wrappedByteChannel.write(byteBuffer);
+ }
+ if (arrayPosition == (DOCTYPE_TAG_BYTES.length
- 1) &&
+
DOCTYPE_TAG_BYTES[arrayPosition] == b) {
+ byte[] cachedBytesArray =
cachedBytes.toByteArray();
+ writeToWrappedChannel(cachedBytesArray);
+ int rest =
wrappedByteChannel.write(byteBuffer);
doctypeWritten = true;
- return DOCTYPE_DEF_BYTES.length +
writtenBytes;
+ return cachedBytesArray.length + rest;
+ }
+ if (arrayPosition < XML_DECLARATION_BYTES.length
+ &&
XML_DECLARATION_BYTES[arrayPosition] != b) {
+ isXmlDeclaration = false;
+ }
+ if (arrayPosition >=
XML_DECLARATION_BYTES.length && isXmlDeclaration) {
+ if (b == GREATER_THAN) {
+ arrayPosition = 0;
+ isNotADoctypeDef = false;
+ cachedBytes.reset(); // dump
XML Declaration
+ }
+ continue;
+ }
+ if (DOCTYPE_TAG_BYTES[arrayPosition] != b ||
isNotADoctypeDef) {
+ isNotADoctypeDef = true;
+ if (!isXmlDeclaration) {
+
writeToWrappedChannel(DOCTYPE_DEF_BYTES);
+ byte[] cachedBytesArray =
cachedBytes.toByteArray();
+
writeToWrappedChannel(cachedBytesArray);
+ int rest =
wrappedByteChannel.write(byteBuffer);
+ doctypeWritten = true;
+ return DOCTYPE_DEF_BYTES.length
+ cachedBytesArray.length + rest;
+ }
}
- doctypePosition++;
+ arrayPosition++;
}
- return writtenBytes;
+ return 0;
} else {
return wrappedByteChannel.write(byteBuffer);
}
@@ -88,7 +111,7 @@
public void close()
throws IOException {
if (!doctypeWritten) {
- writeToWrappedChannel(readBytes.toByteArray());
+ writeToWrappedChannel(cachedBytes.toByteArray());
}
wrappedByteChannel.close();
}
Modified:
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java
URL:
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java?rev=896819&r1=896818&r2=896819&view=diff
==============================================================================
---
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java
(original)
+++
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java
Thu Jan 7 09:35:31 2010
@@ -20,7 +20,6 @@
package org.apache.clerezza.platform.xhtml2html;
import java.io.ByteArrayOutputStream;
-import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
@@ -57,8 +56,40 @@
* XML declaration allowed only at the start of the document
*/
@Test
- public void simpleTest() throws Exception {
- final String someHtml = "<?xml version="1.0" encoding="UTF-8"
?>" +
+ public void removeXmlDeclarationTest() throws Exception {
+ final String someHtml = "<?xml version=\"1.0\"
encoding=\"UTF-8\" ?>" +
+ "<html>\n" +
+ "<body>\n" +
+ "hello" +
+ "</body>\n" +
+ "</html>";
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final WritableByteChannel channel = new
DocTypeSettingByteChannel(Channels.newChannel(baos),
+ new ResponseStatusInfo() {
+
+ @Override
+ public boolean isHtml() {
+ return true;
+ }
+
+ });
+ int bytesWritten =
channel.write(ByteBuffer.wrap(someHtml.substring(0, 20).getBytes(UTF8)));
+ bytesWritten +=
channel.write(ByteBuffer.wrap(someHtml.substring(20).getBytes(UTF8)));
+ final String resultString = new String(baos.toByteArray(),
UTF8);
+ System.out.println(resultString);
+ Assert.assertEquals(resultString.length(), bytesWritten);
+ Assert.assertTrue(resultString.contains("<!DOCTYPE"));
+ /* The test fails iff the ?xml is at another position than 0,
not
+ * if its removed*/
+ if (resultString.contains("<?xml version=\"1.0\"
encoding=\"UTF-8\" ?>")) {
+ Assert.assertTrue(resultString.startsWith("<?xml"));
+ }
+ }
+
+ @Test
+ public void removeXmlDeclarationAndNotAddedTwiceTest() throws Exception
{
+ final String someHtml = "<?xml version=\"1.0\"
encoding=\"UTF-8\" ?>\n" +
+ "<!DOCTYPE something>\n" +
"<html>\n" +
"<body>\n" +
"hello" +
@@ -79,9 +110,11 @@
Assert.assertTrue(resultString.contains("<!DOCTYPE"));
/* The test fails iff the ?xml is at another position than 0,
not
* if its removed*/
- if (resultString.contains("<?xml")) {
+ if (resultString.contains("<?xml version=\"1.0\"
encoding=\"UTF-8\" ?>")) {
Assert.assertTrue(resultString.startsWith("<?xml"));
}
+ Assert.assertTrue(resultString.startsWith("\n<!DOCTYPE
something"));
+
Assert.assertFalse(resultString.substring(8).contains("<!DOCTYPE"));
}
@Test