You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by mi...@apache.org on 2010/01/05 16:42:30 UTC

svn commit: r896100 - in /incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html: DocTypeSettingBody.java DocTypeSettingByteChannel.java

Author: mir
Date: Tue Jan  5 15:42:30 2010
New Revision: 896100

URL: http://svn.apache.org/viewvc?rev=896100&view=rev
Log:
CLEREZZA-39: forgot to add classes

Added:
    incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingBody.java
    incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannel.java

Added: incubator/clerezza/issues/CLEREZZA-39/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-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingBody.java?rev=896100&view=auto
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingBody.java (added)
+++ incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingBody.java Tue Jan  5 15:42:30 2010
@@ -0,0 +1,48 @@
+/*
+ * 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.IOException;
+import java.nio.channels.WritableByteChannel;
+import org.wymiwyg.wrhapi.MessageBody;
+import org.wymiwyg.wrhapi.util.MessageBody2Write;
+
+/**
+ *
+ * @author tio
+ */
+class DocTypeSettingBody extends MessageBody2Write {
+
+	private WrappedResponse wrappedResponse;
+	private MessageBody body;
+
+	public DocTypeSettingBody(MessageBody body, WrappedResponse wrappedResponse) {
+		this.wrappedResponse = wrappedResponse;
+		this.body = body;
+	}
+
+	@Override
+	public void writeTo(WritableByteChannel byteChannel)
+			throws IOException {
+		body.writeTo(new DocTypeSettingByteChannel(byteChannel, wrappedResponse));
+	}
+
+
+}

Added: 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=896100&view=auto
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannel.java (added)
+++ incubator/clerezza/issues/CLEREZZA-39/org.apache.clerezza.platform.xhtml2html/src/main/java/org/apache/clerezza/platform/xhtml2html/DocTypeSettingByteChannel.java Tue Jan  5 15:42:30 2010
@@ -0,0 +1,100 @@
+/*
+ * 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;
+
+/**
+ *
+ * @author mir
+ */
+class DocTypeSettingByteChannel implements WritableByteChannel {
+	
+	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 SPACE = " ".getBytes()[0];
+	private WritableByteChannel wrappedByteChannel;
+	private boolean doctypeWritten = false;
+	private int doctypePosition = 0;
+	private ByteArrayOutputStream readBytes = new ByteArrayOutputStream();
+	private WrappedResponse wrappedResponse;
+	
+	public DocTypeSettingByteChannel(WritableByteChannel byteChannel, 
+			WrappedResponse wrappedResponse) {
+		this.wrappedByteChannel = byteChannel;
+		this.wrappedResponse = wrappedResponse;
+	}	
+
+	@Override
+	public int write(ByteBuffer byteBuffer) throws IOException {
+		if (!doctypeWritten && wrappedResponse.isHtml()) {
+			int writtenBytes = byteBuffer.remaining();
+			for (int i = 0; i < byteBuffer.remaining(); i++) {
+				byte b = byteBuffer.get();
+				readBytes.write(b);
+				if (doctypePosition == 0 && b == SPACE) {
+					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);
+					doctypeWritten = true;
+					return DOCTYPE_DEF_BYTES.length + writtenBytes;
+				}
+				doctypePosition++;
+			}
+			return writtenBytes;
+		} else {
+			return wrappedByteChannel.write(byteBuffer);
+		}
+	}
+
+	@Override
+	public boolean isOpen() {
+		return wrappedByteChannel.isOpen();
+	}
+
+	@Override
+	public void close()
+			throws IOException {
+		if (!doctypeWritten) {
+			writeToWrappedChannel(readBytes.toByteArray());
+		}
+		wrappedByteChannel.close();
+	}
+
+	private void writeToWrappedChannel(byte[] byteArray) throws IOException {
+		OutputStream out = Channels.newOutputStream(wrappedByteChannel);
+		out.write(byteArray);
+	}
+}