Author: mir
Date: Mon Aug 30 16:06:31 2010
New Revision: 990840

URL: http://svn.apache.org/viewvc?rev=990840&view=rev
Log:
CLEREZZA-292: added missing classes

Added:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/ThumbnailCondition.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/utils/ConditionalOutputStream.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/utils/StreamCondition.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/test/java/org/apache/clerezza/tools/offline/ThumbnailConditionTest.java

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/ThumbnailCondition.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/ThumbnailCondition.java?rev=990840&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/ThumbnailCondition.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/ThumbnailCondition.java
 Mon Aug 30 16:06:31 2010
@@ -0,0 +1,204 @@
+/*
+ *  Copyright 2010 mir.
+ * 
+ *  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.tools.offline;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import org.apache.clerezza.tools.offline.utils.StreamCondition;
+import 
org.apache.clerezza.platform.content.representations.core.ThumbnailService;
+import org.apache.clerezza.rdf.core.UriRef;
+
+/**
+ *
+ * @author mir
+ */
+public class ThumbnailCondition implements StreamCondition {
+
+       private final static byte[] SRC_BYTES = "src=\"".getBytes();
+       private final static byte[] HREF_BYTES = "href=\"".getBytes();
+       private final static byte QUOTE_BYTE = "\"".getBytes()[0];
+       private final static byte[] THUMBNAIL_SERVICE_BYTES = 
"/thumbnail-service?".getBytes();
+       private ThumbnailService thumbnailService;
+       private boolean isScr = true;
+       private boolean isHref = true;
+       private boolean isSatisfied = false;
+       private byte[] thumbnailBytes = null;
+       private ByteArrayOutputStream cachedQueryParams = new 
ByteArrayOutputStream();
+
+       private enum Phase {CHECK_TAG_ATTRIBUTE, CHECK_THUMBNAIL_SERVICE, 
LOOK_FOR_QUOTE};
+       private Phase currentPhase = Phase.CHECK_TAG_ATTRIBUTE;
+       private int arrayPosition = 0;
+
+       public ThumbnailCondition(ThumbnailService thumbnailService) {
+               this.thumbnailService = thumbnailService;
+       }
+
+       private void reset() {
+               isScr = true;
+               isHref = true;
+               isSatisfied = false;
+               arrayPosition = 0;
+               cachedQueryParams.reset();
+               thumbnailBytes = null;
+               currentPhase = Phase.CHECK_TAG_ATTRIBUTE;
+       }
+
+       @Override
+       public boolean feed(int b) {
+               if (isSatisfied) {
+                       reset();
+               }
+               boolean result = false;
+               if (currentPhase.equals(Phase.CHECK_TAG_ATTRIBUTE)) {
+                       result = checkTagAttribute(b);
+               } else if (currentPhase.equals(Phase.CHECK_THUMBNAIL_SERVICE)) {
+                       result = checkThumbnailUri(b);
+               } else if (currentPhase.equals(Phase.LOOK_FOR_QUOTE)) {
+                       result = lookForQuote(b);
+               }
+               return result;
+       }
+
+       private boolean checkTagAttribute(int b) {
+               if (isScr) {
+                       if (SRC_BYTES[arrayPosition] != b) {
+                               isScr = false;
+                       } else if (SRC_BYTES.length == arrayPosition + 1) {
+                               currentPhase = Phase.CHECK_THUMBNAIL_SERVICE;
+                               arrayPosition = 0;
+                               return true;
+                       }
+               }
+               if (isHref) {
+                       if (HREF_BYTES[arrayPosition] != b) {
+                               isHref = false;
+                       } else if (HREF_BYTES.length == arrayPosition + 1) {
+                               currentPhase = Phase.CHECK_THUMBNAIL_SERVICE;
+                               arrayPosition = 0;
+                               return true;
+                       }
+               }
+               if (!isHref && !isScr) {
+                       reset();
+                       return false;
+               }
+               arrayPosition++;
+               return true;
+       }
+
+       private boolean checkThumbnailUri(int b) {
+               if (arrayPosition == 16) {
+               }
+               if (THUMBNAIL_SERVICE_BYTES[arrayPosition] != b) {
+                       reset();
+                       return false;
+               } else if (THUMBNAIL_SERVICE_BYTES.length == arrayPosition + 1) 
{
+                       currentPhase = Phase.LOOK_FOR_QUOTE;
+               }
+               arrayPosition++;
+               return true;
+       }
+
+       private boolean lookForQuote(int b) {
+               if (b == QUOTE_BYTE) {
+                       prepareBytes();
+                       isSatisfied = true;
+                       return false;
+               } else {
+                       cachedQueryParams.write(b);
+               }
+               return true;
+       }
+
+       private void prepareBytes() {
+               ByteArrayOutputStream bous = new ByteArrayOutputStream();
+               try {
+                       if (isHref) {
+                               bous.write(HREF_BYTES);
+                       } else {
+                               bous.write(SRC_BYTES);
+                       }
+                       bous.write(getThumbnailUri());
+                       bous.write(QUOTE_BYTE);
+                       thumbnailBytes = bous.toByteArray();
+               } catch (IOException ex) {
+                       throw new RuntimeException(ex);
+               }
+       }
+
+       private byte[] getThumbnailUri() {
+               ThumbnailServiceParams params = parseThumbnailServiceParams();
+               UriRef thumbnailUri = 
thumbnailService.getThumbnailUri(params.getUri(),
+                               params.getWidth(), params.getHeight());
+               return thumbnailUri.getUnicodeString().getBytes();
+       }
+
+       @Override
+       public boolean isSatisfied() {
+               return isSatisfied;
+       }
+
+       @Override
+       public byte[] getBytes() {
+               return thumbnailBytes;
+       }
+
+       private ThumbnailServiceParams parseThumbnailServiceParams() {
+               Integer width = null, height = null;
+               UriRef uri = null;
+               String queryParams = cachedQueryParams.toString();
+               queryParams = queryParams.replace("&", "&");
+               String[] nameValues = queryParams.split("&");
+               for (String nameValue : nameValues) {
+                       String[] nameValuePair = nameValue.split("=");
+                       if (nameValuePair.length == 2) {
+                               String name = nameValuePair[0];
+                               if (name.equals("uri")) {
+                                       uri = new UriRef(nameValuePair[1]);
+                               } else if (name.equals("width")) {
+                                       width = 
Integer.valueOf(nameValuePair[1]);
+                               } else if (name.equals("height")) {
+                                       height = 
Integer.valueOf(nameValuePair[1]);
+                               }
+                       }
+               }
+               return new ThumbnailServiceParams(width, height, uri);
+       }
+
+       private class ThumbnailServiceParams {
+                private Integer width, height;
+                private UriRef uri;
+
+               public ThumbnailServiceParams(Integer width, Integer height, 
UriRef uri) {
+                       this.width = width;
+                       this.height = height;
+                       this.uri = uri;
+               }
+
+               public Integer getHeight() {
+                       return height;
+               }
+
+               public UriRef getUri() {
+                       return uri;
+               }
+
+               public Integer getWidth() {
+                       return width;
+               }
+       }
+}

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/utils/ConditionalOutputStream.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/utils/ConditionalOutputStream.java?rev=990840&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/utils/ConditionalOutputStream.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/utils/ConditionalOutputStream.java
 Mon Aug 30 16:06:31 2010
@@ -0,0 +1,61 @@
+/*
+ *  Copyright 2010 mir.
+ * 
+ *  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.tools.offline.utils;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * The conditional output stream wraps an <code>OutputStream</code>. All bytes
+ * written to the condition output stream are tested by the 
<code>StreamCondition</code>
+ * given to the constructor of the conditional output stream. The stream 
condition
+ * can modify the byte stream if it is satisfied.
+ *
+ * @author mir
+ */
+public class ConditionalOutputStream extends OutputStream {
+
+       private OutputStream out;
+       private StreamCondition condition;
+       private ByteArrayOutputStream cachedBytes = new ByteArrayOutputStream();
+
+       public ConditionalOutputStream(OutputStream out, StreamCondition 
condition) {
+               this.out = out;
+               this.condition = condition;
+       }
+
+       @Override
+       public void write(int b) throws IOException {           
+               if (condition.feed(b)) {
+                       cachedBytes.write(b);
+               } else {
+                       if (condition.isSatisfied()) {
+                               out.write(condition.getBytes());
+                               cachedBytes.reset();
+                       } else {
+                               if (cachedBytes.size() > 0) {
+                                       out.write(cachedBytes.toByteArray());
+                                       cachedBytes.reset();                    
                
+                               }
+                               out.write(b);
+                       }
+               }
+       }
+
+}

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/utils/StreamCondition.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/utils/StreamCondition.java?rev=990840&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/utils/StreamCondition.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/utils/StreamCondition.java
 Mon Aug 30 16:06:31 2010
@@ -0,0 +1,52 @@
+/*
+ *  Copyright 2010 mir.
+ * 
+ *  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.tools.offline.utils;
+
+/**
+ *
+ * @author mir
+ */
+public interface StreamCondition {
+
+       /**
+        * This method is called by the ConditionalOutputStream. The conditional
+        * output stream feeds the condition with bytes as long as the 
feed()-method
+        * returns true. If false is returned, then the condition is either 
satified
+        * or unsatisfied. This can be determined by calling isSatisfied() of 
this
+        * condition.
+        * After returning false, the condition can be fed again.
+        *
+        * @param b
+        * @return
+        */
+       public boolean feed(int b);
+
+       /**
+        * Returns true if the condition is satisfied, false otherwise.
+        * @return
+        */
+       public boolean isSatisfied();
+
+       /**
+        * The ConditionOutputStream will call this method if the condition is
+        * satisfied. The returned bytes will be written into its underlying 
outputstream
+        * instead of the bytes that were fed to the condition.
+        * @return
+        */
+       public byte[] getBytes();
+}

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/test/java/org/apache/clerezza/tools/offline/ThumbnailConditionTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/test/java/org/apache/clerezza/tools/offline/ThumbnailConditionTest.java?rev=990840&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/test/java/org/apache/clerezza/tools/offline/ThumbnailConditionTest.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.tools.offline/src/test/java/org/apache/clerezza/tools/offline/ThumbnailConditionTest.java
 Mon Aug 30 16:06:31 2010
@@ -0,0 +1,52 @@
+/*
+ *  Copyright 2010 mir.
+ * 
+ *  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.tools.offline;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import 
org.apache.clerezza.platform.content.representations.core.ThumbnailService;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.tools.offline.utils.ConditionalOutputStream;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ * @author mir
+ */
+public class ThumbnailConditionTest {
+       
+       private static final byte[] input = "<img 
href=\"/thumbnail-service?uri=http://localhost:8080/html_export/digital-assets/2010/08/30/770a7f14-74a7-4036-8341-f9e50e944e06&amp;width=700&height=300\";
 />".getBytes();
+       
+       @Test
+       public void thumbnailConditionTest() throws IOException {
+               ByteArrayOutputStream bous = new ByteArrayOutputStream();
+               OutputStream out = new ConditionalOutputStream(bous,
+                               new ThumbnailCondition(new ThumbnailService() {
+
+                       @Override
+                       public UriRef getThumbnailUri(UriRef infoBitUri, 
Integer width, Integer height) {
+                               return new UriRef("http://example.com/test";);
+                       }                               
+               }));
+               
+               out.write(input);
+               Assert.assertEquals("<img href=\"http://example.com/test\"; />", 
bous.toString());
+       }
+}


Reply via email to