Author: mir
Date: Tue Mar 16 13:05:41 2010
New Revision: 923719
URL: http://svn.apache.org/viewvc?rev=923719&view=rev
Log:
CLEREZZA-163: DOCTYPE is now not added if there is no <html>-tag
Modified:
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeFilteringByteChannel.java
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java
Modified:
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeFilteringByteChannel.java
URL:
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeFilteringByteChannel.java?rev=923719&r1=923718&r2=923719&view=diff
==============================================================================
---
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeFilteringByteChannel.java
(original)
+++
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeFilteringByteChannel.java
Tue Mar 16 13:05:41 2010
@@ -32,6 +32,7 @@ class DocTypeFilteringByteChannel implem
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[] HTML_TAG_BYTES = "<html".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];
@@ -44,6 +45,8 @@ class DocTypeFilteringByteChannel implem
private ResponseStatusInfo wrappedResponse;
private boolean isXmlDeclaration = true;
private boolean isNotADoctypeDef = false;
+ private boolean hasHtmlTag = false;
+ private boolean lookingForHtmlTag = true;
public DocTypeFilteringByteChannel(WritableByteChannel byteChannel,
ResponseStatusInfo wrappedResponse) {
@@ -64,30 +67,41 @@ class DocTypeFilteringByteChannel implem
}
if (arrayPosition == (DOCTYPE_TAG_BYTES.length
- 1) &&
DOCTYPE_TAG_BYTES[arrayPosition] == b) {
-
writeToWrappedChannel(cachedBytes.toByteArray());
- wrappedByteChannel.write(byteBuffer);
- doctypeWritten = true;
+
WriteEverthingAndSetDoctypeWrittenToTrue(byteBuffer);
break;
}
if (arrayPosition < XML_DECLARATION_BYTES.length
&&
XML_DECLARATION_BYTES[arrayPosition] != b) {
isXmlDeclaration = false;
}
+
+ if (lookingForHtmlTag) {
+ if (arrayPosition <
HTML_TAG_BYTES.length
+ &&
HTML_TAG_BYTES[arrayPosition] != b) {
+ lookingForHtmlTag = false;
+ }
+ if (arrayPosition >=
HTML_TAG_BYTES.length) {
+ hasHtmlTag = true;
+ }
+ }
+
if (arrayPosition >=
XML_DECLARATION_BYTES.length && isXmlDeclaration) {
if (b == GREATER_THAN) {
arrayPosition = 0;
isNotADoctypeDef = false;
+ lookingForHtmlTag = true;
cachedBytes.reset(); // dump
XML Declaration
- }
+ }
continue;
}
if (DOCTYPE_TAG_BYTES[arrayPosition] != b ||
isNotADoctypeDef) {
isNotADoctypeDef = true;
- if (!isXmlDeclaration) {
+ if (!isXmlDeclaration && hasHtmlTag) {
writeToWrappedChannel(DOCTYPE_DEF_BYTES);
-
writeToWrappedChannel(cachedBytes.toByteArray());
-
wrappedByteChannel.write(byteBuffer);
- doctypeWritten = true;
+
WriteEverthingAndSetDoctypeWrittenToTrue(byteBuffer);
+ break;
+ } else if (!isXmlDeclaration &&
!hasHtmlTag && !lookingForHtmlTag){
+
WriteEverthingAndSetDoctypeWrittenToTrue(byteBuffer);
break;
}
}
@@ -99,6 +113,12 @@ class DocTypeFilteringByteChannel implem
}
}
+ private void WriteEverthingAndSetDoctypeWrittenToTrue(ByteBuffer
byteBuffer) throws IOException {
+ writeToWrappedChannel(cachedBytes.toByteArray());
+ wrappedByteChannel.write(byteBuffer);
+ doctypeWritten = true;
+ }
+
@Override
public boolean isOpen() {
return wrappedByteChannel.isOpen();
Modified:
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java
URL:
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java?rev=923719&r1=923718&r2=923719&view=diff
==============================================================================
---
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java
(original)
+++
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java
Tue Mar 16 13:05:41 2010
@@ -38,18 +38,11 @@ public class DocTypeSettingByteChannelTe
"</body>\n" +
"</html>";
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final WritableByteChannel channel = new
DocTypeFilteringByteChannel(Channels.newChannel(baos),
- new ResponseStatusInfo() {
-
- @Override
- public boolean convertXhtml2Html() {
- return true;
- }
-
- });
+ final WritableByteChannel channel =
createDocTypeFilteringByteChannel(baos);
channel.write(ByteBuffer.wrap(someHtml.getBytes(UTF8)));
final String resultString = new String(baos.toByteArray(),
UTF8);
Assert.assertTrue(resultString.startsWith("<!DOCTYPE"));
+
}
/**
@@ -64,18 +57,10 @@ public class DocTypeSettingByteChannelTe
"</body>\n" +
"</html>";
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final WritableByteChannel channel = new
DocTypeFilteringByteChannel(Channels.newChannel(baos),
- new ResponseStatusInfo() {
-
- @Override
- public boolean convertXhtml2Html() {
- return true;
- }
- });
+ final WritableByteChannel channel =
createDocTypeFilteringByteChannel(baos);
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(someHtml.length(), bytesWritten);
Assert.assertTrue(resultString.contains("<!DOCTYPE"));
/* The test fails iff the ?xml is at another position than 0,
not
@@ -95,14 +80,7 @@ public class DocTypeSettingByteChannelTe
"</body>\n" +
"</html>";
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final WritableByteChannel channel = new
DocTypeFilteringByteChannel(Channels.newChannel(baos),
- new ResponseStatusInfo() {
-
- @Override
- public boolean convertXhtml2Html() {
- return true;
- }
- });
+ final WritableByteChannel channel =
createDocTypeFilteringByteChannel(baos);
channel.write(ByteBuffer.wrap(someHtml.getBytes(UTF8)));
final String resultString = new String(baos.toByteArray(),
UTF8);
Assert.assertTrue(resultString.contains("<!DOCTYPE"));
@@ -124,15 +102,7 @@ public class DocTypeSettingByteChannelTe
"</body>\n" +
"</html>";
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final WritableByteChannel channel = new
DocTypeFilteringByteChannel(Channels.newChannel(baos),
- new ResponseStatusInfo() {
-
- @Override
- public boolean convertXhtml2Html() {
- return true;
- }
-
- });
+ final WritableByteChannel channel =
createDocTypeFilteringByteChannel(baos);
channel.write(ByteBuffer.wrap(someHtml.getBytes(UTF8)));
final String resultString = new String(baos.toByteArray(),
UTF8);
Assert.assertTrue(resultString.startsWith("<!DOCTYPE
something"));
@@ -148,15 +118,7 @@ public class DocTypeSettingByteChannelTe
"</body>\n" +
"</html>";
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final WritableByteChannel channel = new
DocTypeFilteringByteChannel(Channels.newChannel(baos),
- new ResponseStatusInfo() {
-
- @Override
- public boolean convertXhtml2Html() {
- return true;
- }
-
- });
+ final WritableByteChannel channel =
createDocTypeFilteringByteChannel(baos);
final byte[] bytes = someHtml.getBytes(UTF8);
for (int i = 0; i < bytes.length; i++) {
ByteBuffer buf = ByteBuffer.allocate(1);
@@ -169,4 +131,44 @@ public class DocTypeSettingByteChannelTe
Assert.assertFalse(resultString.substring(8).contains("<!DOCTYPE"));
}
+ @Test
+ public void notAddDoctypeIfHtmlNotComplete() throws Exception {
+ final String someHtmlSnippet = "<body>\n" +
+ "hello" +
+ "</body>\n";
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final WritableByteChannel channel =
createDocTypeFilteringByteChannel(baos);
+ channel.write(ByteBuffer.wrap(someHtmlSnippet.getBytes(UTF8)));
+ final String resultString = new String(baos.toByteArray(),
UTF8);
+ Assert.assertEquals(someHtmlSnippet, resultString);
+ }
+
+ @Test
+ public void RemoveXmlDeclarationAndDotnotAddDoctypeIfHtmlNotComplete()
throws Exception {
+ final String someHtmlSnippet = "<?xml version=\"1.0\"
encoding=\"UTF-8\" ?>\n\n\n\n\n<body>\n" +
+ "hello" +
+ "</body>\n";
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final WritableByteChannel channel =
createDocTypeFilteringByteChannel(baos);
+ channel.write(ByteBuffer.wrap(someHtmlSnippet.getBytes(UTF8)));
+ final String resultString = new String(baos.toByteArray(),
UTF8);
+ /* 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"));
+ }
+ Assert.assertFalse(resultString.contains("<!DOCTYPE"));
+ }
+
+ private WritableByteChannel
createDocTypeFilteringByteChannel(ByteArrayOutputStream baos) {
+ return new
DocTypeFilteringByteChannel(Channels.newChannel(baos),
+ new ResponseStatusInfo() {
+
+ @Override
+ public boolean convertXhtml2Html() {
+ return true;
+ }
+ });
+ }
+
}