Author: simoneg
Date: Fri Jul 10 02:21:28 2009
New Revision: 792785

URL: http://svn.apache.org/viewvc?rev=792785&view=rev
Log:
LABS-373 : Prelimiray support for XML url rewriting

Added:
    
labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/XMLRewritingTest.java
Modified:
    
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/URLRewritingStream.java

Modified: 
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/URLRewritingStream.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/URLRewritingStream.java?rev=792785&r1=792784&r2=792785&view=diff
==============================================================================
--- 
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/URLRewritingStream.java
 (original)
+++ 
labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/URLRewritingStream.java
 Fri Jul 10 02:21:28 2009
@@ -24,6 +24,22 @@
 /**
  * Filter stream that finds and replaces URLs depending on the context of the 
writer.
  * 
+ * This stream works in the following way :
+ * <ul>
+ *   <li>Data passes inside the stream</li>
+ *   <li>Data is checked for the "insider" char</li>
+ *   <li>When the insider char is found, data is checked for one of the 
keys</li>
+ *   <li>If a key is found, then it starts buffering data (up to 30 bytes by 
default)</li>
+ *   <li>Data is buffered until end of the buffer is reached or the "outsider" 
char is met</li>
+ *   <li>Buffered data is then sent for analysis, and eventually it is 
prefixed so that URL becomes absolute</li>
+ * </ul>
+ * 
+ * So, a simple HTML case is having insider="&lt;", outside="&gt;" and 
keys="href=,src=".
+ * 
+ * This is done to grant high performances. The insider/outsider system 
permits to most of the data
+ * to pass thru with only a single char check (is it insider?). While 
"inside", a fast
+ * system is used to check if we are encountering a key, checking all the keys 
in parallel. 
+ * 
  *
  * @author Simone Gianni <[email protected]>
  */
@@ -108,6 +124,17 @@
                        inside = true;
                } else if (b == outsider && inside) {
                        inside = false;
+                       // Does an additional check to permit keys ending with 
outsider, especially for XML elements
+                       if (!buffering) {
+                               buffering = checkKeys(b);
+                               if (buffering) {
+                                       super.write(b);                         
+                                       inject();
+                                       buffering = false;
+                                       matched = -1;
+                                       return;                                 
                        
+                               }
+                       }
                }
                if (!buffering && !inside) {
                        super.write(b);                         
@@ -221,7 +248,9 @@
         * @return true if one key has been detected passing in the stream.
         */
        private final boolean checkKeys(int b) {
+               // Becomes true if we are matching at least one key
                boolean onematch = false;
+               // Becomes true is we matched one key entirely
                boolean onecomplete = false;
                for (int i = 0; i < keys.length; i++) {
                        if (intbuffpos < keys[i].length() && 
keys[i].charAt(intbuffpos) == b) {

Added: 
labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/XMLRewritingTest.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/XMLRewritingTest.java?rev=792785&view=auto
==============================================================================
--- 
labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/XMLRewritingTest.java
 (added)
+++ 
labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/XMLRewritingTest.java
 Fri Jul 10 02:21:28 2009
@@ -0,0 +1,54 @@
+/*
+ * 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.magma.website.utils;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayOutputStream;
+
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+...@runwith(Theories.class)
+public class XMLRewritingTest {
+       @DataPoint
+       public static final String[] 
+       SET1 = {"<elem>ciao</elem>", 
"<elem>/context/current/place/ciao</elem>"},
+       SET2 = {"<elem url=\"ciao\">test</elem>", "<elem 
url=\"/context/current/place/ciao\">test</elem>"};
+               
+       @Theory
+       public void rewriteTest(String[] set) throws Exception {
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               URLRewritingStream urs = new URLRewritingStream(baos, 
"/context/", "http://localhost";);
+               urs.keys = new String[] { "<elem>" , "url=" };
+               urs.setBaseUrl("current/place/");
+               urs.setTemplatePrefix("template/dummy/");
+               urs.write(set[0].getBytes());
+               String res = baos.toString();
+               try {
+                       assertEquals(set[1], res);
+               } catch (AssertionError e) {
+                       System.out.println(set[1] + " - " + res);
+                       throw e;
+               }
+       }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to