CVS 2002/css-validator/org/w3c/css/util

Update of /sources/public/2002/css-validator/org/w3c/css/util
In directory roscoe:/tmp/cvs-serv12316/util

Modified Files:
	Util.java 
Log Message:
Added support to stop recursive checking (might do it better later)

--- /sources/public/2002/css-validator/org/w3c/css/util/Util.java	2012/08/04 21:17:07	1.8
+++ /sources/public/2002/css-validator/org/w3c/css/util/Util.java	2014/01/27 15:04:41	1.9
@@ -1,5 +1,5 @@
 //
-// $Id: Util.java,v 1.8 2012/08/04 21:17:07 ylafon Exp $
+// $Id: Util.java,v 1.9 2014/01/27 15:04:41 ylafon Exp $
 // From Philippe Le Hegaret (Philippe.Le_Hegaret@sophia.inria.fr)
 //
 // (c) COPYRIGHT MIT and INRIA, 1997.
@@ -39,10 +39,16 @@
 package org.w3c.css.util;
 
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 /**
  * This class holds various utility methods.
  *
- * @version $Revision: 1.8 $
+ * @version $Revision: 1.9 $
  */
 
 public final class Util {
@@ -54,6 +60,50 @@
 	private Util() {
 	}
 
+	private static Pattern uri_param = Pattern.compile("[&?][^&?=]+=([^&?]+)");
+
+	//unravel uris contained in querystrings by url decoding and then looking for all sub URIs
+	// like http://www.example.com/foo?bar=http%3A%2F%2fwww.example.org%2Ftoto
+	// ->  http://www.example.com/foo?bar=http%3A%2F%2fwww.example.org%2Ftoto
+	// and http://www.example.org/toto
+	private static ArrayList<String> parseURIs(String uri) {
+		ArrayList<String> uris = new ArrayList<String>();
+		int pos = 0;
+		uris.add(uri);
+		// we avoid recursion by putting stuff on our plate at the right place for processing
+		while (pos < uris.size()) {
+			String u = uris.get(pos++);
+			Matcher m = uri_param.matcher(u);
+			while (m.find()) {
+				try {
+					String compound = URLDecoder.decode(m.group(1), "UTF-8");
+					if (compound.contains("://")) {
+						uris.add(compound);
+					}
+				} catch (UnsupportedEncodingException e) {
+					// it is supported
+				}
+			}
+		}
+		return uris;
+	}
+
+	public static boolean checkURI(String uri) {
+		ArrayList<String> uris = parseURIs(uri);
+		// now let's check if there is a recursive call
+		for (String u: uris) {
+			int qm = u.indexOf('?');
+			if (qm != -1) {
+				u = u.substring(0, qm);
+			}
+			u.toLowerCase();
+			// TODO: use a list of forbidden URIs
+			if (u.startsWith("http://jigsaw.w3.org/css-validator/validator")) {
+				return false;
+			}
+		}
+		return true;
+	}
 
 	// Methods
 

Received on Monday, 27 January 2014 15:04:44 UTC