Author: reto
Date: Wed Jan 6 15:37:27 2010
New Revision: 896485
URL: http://svn.apache.org/viewvc?rev=896485&view=rev
Log:
CLEREZZA-39: first review suggestions implemented
Added:
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/ResponseStatusInfo.java
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/
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/pom.xml
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/main/java/org/apache/clerezza/platform/xhtml2html/WrappedResponse.java
Modified:
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/pom.xml?rev=896485&r1=896484&r2=896485&view=diff
==============================================================================
---
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/pom.xml
(original)
+++
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/pom.xml
Wed Jan 6 15:37:27 2010
@@ -1,4 +1,5 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?><project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>org.apache.clerezza.parent</artifactId>
@@ -11,7 +12,8 @@
<version>0.5-incubating-SNAPSHOT</version>
<name>Clerezza - Filter for browsers that do not support XHTML</name>
<description>Provides a wrhapi filter that changes the content-type of
the
- response from application/xhtml+xml to text/html</description>
+ response from application/xhtml+xml to text/html
+ </description>
<dependencies>
<dependency>
<groupId>org.wymiwyg</groupId>
@@ -25,5 +27,10 @@
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
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=896485&r1=896484&r2=896485&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
Wed Jan 6 15:37:27 2010
@@ -39,10 +39,10 @@
private boolean doctypeWritten = false;
private int doctypePosition = 0;
private ByteArrayOutputStream readBytes = new ByteArrayOutputStream();
- private WrappedResponse wrappedResponse;
+ private ResponseStatusInfo wrappedResponse;
public DocTypeSettingByteChannel(WritableByteChannel byteChannel,
- WrappedResponse wrappedResponse) {
+ ResponseStatusInfo wrappedResponse) {
this.wrappedByteChannel = byteChannel;
this.wrappedResponse = wrappedResponse;
}
@@ -51,7 +51,7 @@
public int write(ByteBuffer byteBuffer) throws IOException {
if (!doctypeWritten && wrappedResponse.isHtml()) {
int writtenBytes = byteBuffer.remaining();
- for (int i = 0; i < byteBuffer.remaining(); i++) {
+ while (byteBuffer.remaining() > 0) {
byte b = byteBuffer.get();
readBytes.write(b);
if (doctypePosition == 0 && b == SPACE) {
@@ -94,7 +94,9 @@
}
private void writeToWrappedChannel(byte[] byteArray) throws IOException
{
- OutputStream out = Channels.newOutputStream(wrappedByteChannel);
- out.write(byteArray);
+ ByteBuffer buf = ByteBuffer.wrap(byteArray);
+ while (buf.remaining() > 0) {
+ wrappedByteChannel.write(buf);
+ }
}
}
Added:
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/ResponseStatusInfo.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/ResponseStatusInfo.java?rev=896485&view=auto
==============================================================================
---
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/ResponseStatusInfo.java
(added)
+++
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/ResponseStatusInfo.java
Wed Jan 6 15:37:27 2010
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2010 reto.
+ *
+ * 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.
+ * under the License.
+ */
+
+package org.apache.clerezza.platform.xhtml2html;
+
+/**
+ *
+ * @author reto
+ */
+interface ResponseStatusInfo {
+
+ /**
+ *
+ * @return true if the response is known to be (x)html, false otherwise
+ */
+ boolean isHtml();
+
+}
Modified:
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/WrappedResponse.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/WrappedResponse.java?rev=896485&r1=896484&r2=896485&view=diff
==============================================================================
---
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/WrappedResponse.java
(original)
+++
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/WrappedResponse.java
Wed Jan 6 15:37:27 2010
@@ -28,7 +28,7 @@
*
* @author rbn
*/
-class WrappedResponse extends ResponseWrapper {
+class WrappedResponse extends ResponseWrapper implements ResponseStatusInfo {
private String XHTML_TYPE = "application/xhtml+xml";
private String HTML_TYPE = "text/html";
private boolean isHtml = false;
@@ -64,7 +64,8 @@
super.setBody(new DocTypeSettingBody(body, this));
}
- boolean isHtml() {
+ @Override
+ public boolean isHtml() {
return isHtml;
}
}
Added:
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=896485&view=auto
==============================================================================
---
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java
(added)
+++
incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannelTest.java
Wed Jan 6 15:37:27 2010
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.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;
+import java.nio.charset.Charset;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DocTypeSettingByteChannelTest {
+
+ final Charset UTF8 = Charset.forName("UTF-8");
+ @Test
+ public void simpleTest() throws Exception {
+ final String someHtml = "<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;
+ }
+
+ });
+ channel.write(ByteBuffer.wrap(someHtml.getBytes(UTF8)));
+ final String resultString = new String(baos.toByteArray(),
UTF8);
+ Assert.assertTrue(resultString.startsWith("<!DOCTYPE"));
+ }
+
+ /**
+ * 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"
?>" +
+ "<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;
+ }
+
+ });
+ channel.write(ByteBuffer.wrap(someHtml.getBytes(UTF8)));
+ final String resultString = new String(baos.toByteArray(),
UTF8);
+ 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")) {
+ Assert.assertTrue(resultString.startsWith("<?xml"));
+ }
+ }
+
+ @Test
+ public void notAddedTwiceTest() throws Exception {
+ final String someHtml = "<!DOCTYPE something>\n" +
+ "<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;
+ }
+
+ });
+ channel.write(ByteBuffer.wrap(someHtml.getBytes(UTF8)));
+ final String resultString = new String(baos.toByteArray(),
UTF8);
+ Assert.assertTrue(resultString.startsWith("<!DOCTYPE
something"));
+
Assert.assertFalse(resultString.substring(8).contains("<!DOCTYPE"));
+ }
+
+ @Test
+ public void notAddedTwiceSplittedTest() throws Exception {
+ final String someHtml = "<!DOCTYPE something>\n" +
+ "<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;
+ }
+
+ });
+ final byte[] bytes = someHtml.getBytes(UTF8);
+ for (int i = 0; i < bytes.length; i++) {
+ ByteBuffer buf = ByteBuffer.allocate(1);
+ buf.put(bytes[i]);
+ buf.rewind();
+ channel.write(buf);
+ }
+ final String resultString = new String(baos.toByteArray(),
UTF8);
+ Assert.assertTrue(resultString.startsWith("<!DOCTYPE
something"));
+
Assert.assertFalse(resultString.substring(8).contains("<!DOCTYPE"));
+ }
+
+}