Author: mir
Date: Wed Jan 13 20:56:29 2010
New Revision: 898939

URL: http://svn.apache.org/viewvc?rev=898939&view=rev
Log:
CLEREZZA-69: implemented replacement self-closing tags with tags with a closing 
tags in xhtml2html filter for IE requests

Added:
    
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/SelfClosing2ClosingTagsByteChannel.java
    
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/Xhtml2HtmlSettingBody.java
      - copied, changed from r898866, 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingBody.java
    
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/SelfClosing2ClosingTagsByteChannelTest.java
Removed:
    
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingBody.java
Modified:
    
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/ResponseStatusInfo.java
    
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/WrappedResponse.java

Modified: 
incubator/clerezza/issues/CLEREZZA-69/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-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/ResponseStatusInfo.java?rev=898939&r1=898938&r2=898939&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/ResponseStatusInfo.java
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/ResponseStatusInfo.java
 Wed Jan 13 20:56:29 2010
@@ -1,20 +1,21 @@
 /*
- *  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.
+ * 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;
 
 /**

Added: 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/SelfClosing2ClosingTagsByteChannel.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/SelfClosing2ClosingTagsByteChannel.java?rev=898939&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/SelfClosing2ClosingTagsByteChannel.java
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/SelfClosing2ClosingTagsByteChannel.java
 Wed Jan 13 20:56:29 2010
@@ -0,0 +1,175 @@
+/*
+ * 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.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.WritableByteChannel;
+import java.util.Arrays;
+
+/**
+ * Changes self-closing tags to tags with closing tags. Self-closing tags that
+ * are allowed in HTML are left unchanged. The allowed self-closing tags are:
+ * area, base, basefont, br, hr, input img, link and meta.
+ * 
+ * @author mir
+ */
+class SelfClosing2ClosingTagsByteChannel implements WritableByteChannel {
+       
+       
+       private final static byte SPACE = " ".getBytes()[0];
+       private final static byte SLASH = "/".getBytes()[0];
+       private final static byte LESS_THAN = "<".getBytes()[0];
+       private final static byte GREATER_THAN = ">".getBytes()[0];
+
+       private static byte[][] allowedTagNamesBytes = {
+               "area".getBytes(),
+               "base".getBytes(),
+               "basefont".getBytes(),
+               "br".getBytes(),
+               "hr".getBytes(),
+               "input".getBytes(),
+               "img".getBytes(),
+               "link".getBytes(),
+               "meta".getBytes()
+       };
+
+       private WritableByteChannel wrappedByteChannel;
+       private ResponseStatusInfo responseStatusInfo;
+       
+       private ByteArrayOutputStream tagNameStream = new 
ByteArrayOutputStream();
+       private OutputStream bytes = new ByteArrayOutputStream();
+
+       private enum Status {SEARCH_TAG, DETERMINE_IF_IS_OPENING_TAG, 
READ_TAG_NAME,
+               SEARCH_SLASH,SEARCH_GREATER_THAN, FOUND}
+       
+       private Status status = Status.SEARCH_TAG;
+       
+       public SelfClosing2ClosingTagsByteChannel(WritableByteChannel 
byteChannel,
+                       ResponseStatusInfo responseStatusInfo) {
+               this.wrappedByteChannel = byteChannel;
+               this.responseStatusInfo = responseStatusInfo;
+               bytes = Channels.newOutputStream(wrappedByteChannel);
+       }       
+
+       @Override
+       public int write(ByteBuffer byteBuffer) throws IOException {
+               if (responseStatusInfo.isHtml()) {
+                       int bytesWritten = byteBuffer.remaining();
+                       while (byteBuffer.remaining() > 0) {
+                               byte b = byteBuffer.get();
+                               switch (status) {
+                                       case SEARCH_TAG:
+                                               if (b == LESS_THAN) {
+                                                       status = 
Status.DETERMINE_IF_IS_OPENING_TAG;
+                                               }
+                                               break;
+
+                                       case DETERMINE_IF_IS_OPENING_TAG:
+                                               if (status == 
Status.DETERMINE_IF_IS_OPENING_TAG) {
+                                                       if (b != SLASH) {
+                                                               status = 
Status.READ_TAG_NAME;
+                                                       } else {
+                                                               status = 
Status.SEARCH_TAG;
+                                                               break;
+                                                       }
+                                               }
+                                       case READ_TAG_NAME:
+                                               if (b == SPACE) {
+                                                       status = 
Status.SEARCH_SLASH;
+                                               } else if (b == GREATER_THAN) {
+                                                       reset();
+                                               } else if (b == SLASH) {
+                                                       status = 
Status.SEARCH_GREATER_THAN;
+                                                       continue;
+                                               } else {
+                                                       tagNameStream.write(b);
+                                               }
+                                               break;
+                                       case SEARCH_SLASH:
+                                               if (b == SLASH) {
+                                                       status = 
Status.SEARCH_GREATER_THAN;
+                                                       continue;
+                                               }
+                                               if (b == GREATER_THAN) {
+                                                       reset();
+                                               }
+                                               break;
+
+                                       case SEARCH_GREATER_THAN:
+                                               if (b == GREATER_THAN) {
+                                                       status = Status.FOUND;
+                                               } else {
+                                                       bytes.write(SLASH); // 
write the slash that we didn't write when we found it
+                                                       status = 
Status.SEARCH_SLASH;
+                                               }
+                                               break;
+                               }
+                               if (status == Status.FOUND) {
+                                       byte[] tagNameBytes = 
tagNameStream.toByteArray();
+                                       if (isAllowedTagName(tagNameBytes)) {
+                                               bytes.write(SLASH);
+                                               bytes.write(GREATER_THAN);
+                                       } else {
+                                               bytes.write(GREATER_THAN);
+                                               bytes.write(LESS_THAN);
+                                               bytes.write(SLASH);
+                                               bytes.write(tagNameBytes);
+                                               bytes.write(GREATER_THAN);      
                                
+                                       }
+                                       reset();
+                               } else {
+                                       bytes.write(b);
+                               }
+                       }
+                       return bytesWritten - byteBuffer.remaining();
+               } else {
+                       return wrappedByteChannel.write(byteBuffer);
+               }
+       }
+
+       private void reset() {
+               tagNameStream.reset();
+               status = Status.SEARCH_TAG;
+       }
+       
+       private boolean isAllowedTagName(byte[] tagNameBytes) {
+               for (int i = 0; i < allowedTagNamesBytes.length; i++) {
+                       byte[] allowedTagNameBytes = allowedTagNamesBytes[i];
+                       if (Arrays.equals(allowedTagNameBytes, tagNameBytes)) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       @Override
+       public boolean isOpen() {
+               return wrappedByteChannel.isOpen();
+       }
+
+       @Override
+       public void close()     throws IOException {
+               wrappedByteChannel.close();
+       }
+}

Modified: 
incubator/clerezza/issues/CLEREZZA-69/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-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/WrappedResponse.java?rev=898939&r1=898938&r2=898939&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/WrappedResponse.java
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/WrappedResponse.java
 Wed Jan 13 20:56:29 2010
@@ -69,7 +69,7 @@
 
        @Override
        public void setBody(MessageBody body) throws HandlerException {
-               super.setBody(new DocTypeSettingBody(body, this));
+               super.setBody(new Xhtml2HtmlSettingBody(body, this));
        }
 
        @Override

Copied: 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/Xhtml2HtmlSettingBody.java
 (from r898866, 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingBody.java)
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/Xhtml2HtmlSettingBody.java?p2=incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/Xhtml2HtmlSettingBody.java&p1=incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingBody.java&r1=898866&r2=898939&rev=898939&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingBody.java
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/Xhtml2HtmlSettingBody.java
 Wed Jan 13 20:56:29 2010
@@ -28,12 +28,12 @@
  *
  * @author tio
  */
-class DocTypeSettingBody extends MessageBody2Write {
+class Xhtml2HtmlSettingBody extends MessageBody2Write {
 
        private WrappedResponse wrappedResponse;
        private MessageBody body;
 
-       public DocTypeSettingBody(MessageBody body, WrappedResponse 
wrappedResponse) {
+       public Xhtml2HtmlSettingBody(MessageBody body, WrappedResponse 
wrappedResponse) {
                this.wrappedResponse = wrappedResponse;
                this.body = body;
        }
@@ -41,7 +41,9 @@
        @Override
        public void writeTo(WritableByteChannel byteChannel)
                        throws IOException {
-               body.writeTo(new DocTypeSettingByteChannel(byteChannel, 
wrappedResponse));
+               body.writeTo(new DocTypeSettingByteChannel(
+                               new SelfClosing2ClosingTagsByteChannel(
+                               byteChannel, wrappedResponse), 
wrappedResponse));
        }
 
 

Added: 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/SelfClosing2ClosingTagsByteChannelTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/SelfClosing2ClosingTagsByteChannelTest.java?rev=898939&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/SelfClosing2ClosingTagsByteChannelTest.java
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-69/org.apache.clerezza.platform.xhtml2html/src/test/java/org/apache/clerezza/platform/xhtml2html/SelfClosing2ClosingTagsByteChannelTest.java
 Wed Jan 13 20:56:29 2010
@@ -0,0 +1,84 @@
+/*
+ * 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.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 SelfClosing2ClosingTagsByteChannelTest  {
+
+       final Charset UTF8 = Charset.forName("UTF-8");
+       @Test
+       public void simpleTest() throws Exception {
+               final String someHtml = "<html>\n" +
+                               "<script/>\n" +
+                               "<test />\n" +
+                               "<foo id=\"bla\" />\n" +
+                               "<area/>\n" +
+                               "<base/>\n" +
+                               "<basefont/>\n" +
+                               "<br/>\n" +
+                               "<hr/>\n" +
+                               "<input/>\n" +
+                               "<img/>\n" +
+                               "<link/>\n" +
+                               "<meta/>\n" +
+                               "<body>\n" +
+                               "hello" +
+                               "</body>\n" +
+                               "</html>";
+               final String expectedHtml = "<html>\n" +
+                               "<script></script>\n" +
+                               "<test ></test>\n" +
+                               "<foo id=\"bla\" ></foo>\n" +
+                               "<area/>\n" +
+                               "<base/>\n" +
+                               "<basefont/>\n" +
+                               "<br/>\n" +
+                               "<hr/>\n" +
+                               "<input/>\n" +
+                               "<img/>\n" +
+                               "<link/>\n" +
+                               "<meta/>\n" +
+                               "<body>\n" +
+                               "hello" +
+                               "</body>\n" +
+                               "</html>";
+               final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               final WritableByteChannel channel = new 
SelfClosing2ClosingTagsByteChannel(Channels.newChannel(baos),
+                               new ResponseStatusInfo() {
+
+                       @Override
+                       public boolean isHtml() {
+                               return true;
+                       }
+
+               });
+               int bytesWritten = 
channel.write(ByteBuffer.wrap(someHtml.getBytes(UTF8)));
+               Assert.assertEquals(someHtml.length(), bytesWritten);
+               final String resultString = new String(baos.toByteArray(), 
UTF8);
+               Assert.assertEquals(expectedHtml, resultString);
+       }
+}


Reply via email to