You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2009/07/10 04:56:53 UTC

svn commit: r792791 - in /labs/magma/trunk/foundation-website/src: main/java/org/apache/magma/website/utils/ test/java/org/apache/magma/website/utils/

Author: simoneg
Date: Fri Jul 10 02:56:53 2009
New Revision: 792791

URL: http://svn.apache.org/viewvc?rev=792791&view=rev
Log:
LABS-373 : Support for simple "*" wildcard match in URLRewritingStream keys, to better support XML rewriting.

Modified:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/URLRewritingStream.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/URLRewritingStreamTest.java
    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
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/URLRewritingStream.java?rev=792791&r1=792790&r2=792791&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:56:53 2009
@@ -19,6 +19,7 @@
 import java.io.FilterOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Arrays;
 import java.util.Stack;
 
 /**
@@ -52,6 +53,11 @@
 	protected String[] keys = { "href=", "src=", "action=", "data=" };
 	
 	/**
+	 * Last element of the key matched, or 0 if no match.
+	 */
+	protected int[] keysMatch = new int[4];
+	
+	/**
 	 * Chars that enables or disables parsing
 	 */
 	protected char insider = '<', outsider = '>';
@@ -131,7 +137,7 @@
 					super.write(b);				
 					inject();
 					buffering = false;
-					matched = -1;
+					cleanChecks();
 					return;								
 				}
 			}
@@ -151,14 +157,14 @@
 					inject();
 					inside = false;
 					buffering = false;
-					matched = -1;
+					cleanChecks();
 				} else {
 					intbuffpos++;
 				}
 			} else {
 				inject();
 				buffering = false;
-				matched = -1;
+				cleanChecks();
 			}
 		}
 		if (!buffering) {
@@ -237,41 +243,57 @@
 			super.write(segment.getBytes());
 		}
 	}
+	
+	private void cleanChecks() {
+		Arrays.fill(keysMatch, 0);
+		matched = -1;
+	}
 
 	/**
-	 * Checks if the given byte is part of the keys at the right position. In that case,
-	 * b is added to the buffer. If the total length of one key is reached, the buffer is checked
-	 * against it. If the buffer contains the key, the buffer is cleared and this method returns true.
-	 * If it does not, but still matches other keys, the byte is added to the buffer. If it does not
-	 * match any key, the buffer is cleared. 
+	 * Checks if the given byte is part of the keys at the right position. 
+	 * 
+	 * This uses {@link #keysMatch} array to keep track of where the key was matched
+	 * last time. If the key has been fully matched, it returns true, otherwise if the key
+	 * is still matching, it will update keysMatch accordingly. Otherwise, if the key is lost,
+	 * it is returned to 0 and checked again.
+	 * 
 	 * @param b the byte currently in the stream
 	 * @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;
+		// First pass, running keys
 		for (int i = 0; i < keys.length; i++) {
-			if (intbuffpos < keys[i].length() && keys[i].charAt(intbuffpos) == b) {
-				onematch = true;
-				if (intbuffpos + 1 == keys[i].length()) onecomplete = true;
-			}
-		}
-		if (onematch) {
-			intbuff[intbuffpos++] = b;
-			if (onecomplete) {
-				String str = new String(intbuff,0, intbuffpos);
-				for (int i = 0; i < keys.length; i++) {
-					if (str.equals(keys[i])) {
+			int pos = keysMatch[i]; 
+			if (pos > 0) {
+				char acch = keys[i].charAt(pos);
+				// Special case for wildcard
+				boolean wildcard = acch == '*';
+				if (wildcard) {
+					pos++;
+					acch = keys[i].charAt(pos);
+				} 
+				if (acch == b) {
+					pos++;
+					if (pos == keys[i].length()) {
+						keysMatch[i] = 0;
 						matched = i;
-						intbuffpos = 0;
 						return true;
+					} else {
+						keysMatch[i] = pos;
 					}
+				} else {
+					if (!wildcard) keysMatch[i] = 0;
+				}
+			}
+		}		
+		// Second pass, starting or just resetted keys
+		for (int i = 0; i < keys.length; i++) {
+			int pos = keysMatch[i]; 
+			if (pos == 0) {
+				if (b == keys[i].charAt(0)) {
+					keysMatch[i]++;
 				}
 			}
-		} else {
-			intbuffpos = 0;
 		}
 		return false;
 	}

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/URLRewritingStreamTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/URLRewritingStreamTest.java?rev=792791&r1=792790&r2=792791&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/URLRewritingStreamTest.java (original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/URLRewritingStreamTest.java Fri Jul 10 02:56:53 2009
@@ -53,13 +53,13 @@
 		assertFalse(urs.buffering);
 		assertTrue(urs.inside);
 		assertEquals(-1, urs.matched);		
-		assertEquals(3, urs.intbuffpos);
+		assertEquals(3, urs.keysMatch[0]);
 		
 		urs.write("b".getBytes());
 		assertFalse(urs.buffering);
 		assertTrue(urs.inside);
 		assertEquals(-1, urs.matched);
-		assertEquals(0, urs.intbuffpos);
+		assertEquals(0, urs.keysMatch[0]);
 
 		urs.write("and outside>".getBytes());
 		assertFalse(urs.buffering);

Modified: 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=792791&r1=792790&r2=792791&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/XMLRewritingTest.java (original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/utils/XMLRewritingTest.java Fri Jul 10 02:56:53 2009
@@ -33,13 +33,14 @@
 	@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>"};
+	SET2 = {"<elem url=\"ciao\">test</elem>", "<elem url=\"/context/current/place/ciao\">test</elem>"},
+	SET3 = {"<withattrs some='attr' another='attr'>test</withattrs>", "<withattrs some='attr' another='attr'>/context/current/place/test</withattrs>"};
 	 	
 	@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.keys = new String[] { "<elem>" , "url=", "<withattrs *>"};
 		urs.setBaseUrl("current/place/");
 		urs.setTemplatePrefix("template/dummy/");
 		urs.write(set[0].getBytes());



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org