You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jb...@apache.org on 2007/03/05 16:01:53 UTC

svn commit: r514684 - in /incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket: markup/parser/XmlPullParser.java protocol/http/WicketFilter.java util/io/XmlReader.java

Author: jbq
Date: Mon Mar  5 07:01:52 2007
New Revision: 514684

URL: http://svn.apache.org/viewvc?view=rev&rev=514684
Log:
WICKET-315 Parsing web.xml for filter mapping is too slow

* Rewrite getFilterPath(String, InputStream) using XmlPullParser (around 200
  times faster than with the DOM API)
* Add XmlPullParser.parse(InputStream)

Modified:
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/parser/XmlPullParser.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/XmlReader.java

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/parser/XmlPullParser.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/parser/XmlPullParser.java?view=diff&rev=514684&r1=514683&r2=514684
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/parser/XmlPullParser.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/markup/parser/XmlPullParser.java Mon Mar  5 07:01:52 2007
@@ -18,6 +18,7 @@
 
 import java.io.BufferedInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.text.ParseException;
 
 import wicket.markup.MarkupElement;
@@ -370,7 +371,7 @@
 	 * @param resource
 	 *            The resource to read and parse
 	 * @param encoding
-	 *            The default character encoding of the input
+	 *            Default character encoding to use when not specified in XML declaration, specify null to use JVM default
 	 * @throws IOException
 	 * @throws ResourceStreamNotFoundException
 	 */
@@ -386,6 +387,31 @@
 		finally
 		{
 			resource.close();
+			if(this.xmlReader != null) this.xmlReader.close();
+		}
+	}
+
+	/**
+	 * Reads and parses markup from an input stream
+	 * 
+	 * @param in
+	 *            The input stream to read and parse
+	 * @throws IOException
+	 * @throws ResourceStreamNotFoundException
+	 */
+	public void parse(final InputStream in) throws IOException,
+			ResourceStreamNotFoundException
+	{
+		try
+		{
+			// When XML declaration does not specify encoding, it defaults to UTF-8
+			this.xmlReader = new XmlReader(
+					new BufferedInputStream(in, 4000), "UTF-8");
+			this.input = new FullyBufferedReader(this.xmlReader);
+		}
+		finally
+		{
+			in.close();
 			if(this.xmlReader != null) this.xmlReader.close();
 		}
 	}

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java?view=diff&rev=514684&r1=514683&r2=514684
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketFilter.java Mon Mar  5 07:01:52 2007
@@ -43,6 +43,8 @@
 import wicket.Resource;
 import wicket.Session;
 import wicket.WicketRuntimeException;
+import wicket.markup.parser.XmlPullParser;
+import wicket.markup.parser.XmlTag;
 import wicket.protocol.http.request.WebRequestCodingStrategy;
 import wicket.session.ISessionStore;
 import wicket.settings.IRequestCycleSettings;
@@ -356,10 +358,10 @@
 					try
 					{
 						filterPath = getFilterPath(filterConfig.getFilterName(), is);
-						is.close();
 					}
 					catch (Exception e)
 					{
+						log.debug("Error parsing web.xml", e);
 						// Swallow IOException or SecurityException or similar, and log.info below.
 					}
 				}
@@ -419,43 +421,35 @@
 		try
 		{
 			ArrayList urlPatterns = new ArrayList();
-			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
-			is.close();
-			NodeList filterMappings = doc.getElementsByTagName("filter-mapping");
-			for (int i = 0; i < filterMappings.getLength(); i++)
-			{
-				Node filterMapping = filterMappings.item(i);
-				NodeList mappingElements = filterMapping.getChildNodes();
-
-				// We might have filter-name and url-pattern in the
-				// wrong order, even though it's a DTD-violation -
-				// many containers don't use strict parsing, so we
-				// have to deal with this.
-				String urlPattern = null;
-				boolean add = false;
-				for (int j = 0; j < mappingElements.getLength(); j++)
-				{
-					Node mappingElement = mappingElements.item(j);
-					if (mappingElement.getNodeType() != Node.ELEMENT_NODE)
-					{
-						continue;
-					}
-					if (mappingElement.getNodeName().equals("filter-name")
-							&& mappingElement.getFirstChild().getNodeValue().equals(filterName))
-					{
-						add = true;
-					}
-					if (mappingElement.getNodeName().equals("url-pattern"))
-					{
-						urlPattern = mappingElement.getFirstChild().getNodeValue();
+			XmlPullParser parser = new XmlPullParser();
+			parser.parse(is);
+			
+			while (true) {
+				XmlTag elem;
+				do {
+					elem = (XmlTag)parser.nextTag();
+				} while (elem != null && (! (elem.getName().equals("filter-mapping") && elem.isOpen())));
+				
+				if (elem == null)
+					break;
+	
+				String encounteredFilterName = null, urlPattern = null;
+	
+				do {
+					elem = (XmlTag)parser.nextTag();
+					if (elem.isOpen()) {
+						parser.setPositionMarker();
+					} else if (elem.isClose() && elem.getName().equals("filter-name")) {
+						encounteredFilterName = parser.getInputFromPositionMarker(elem.getPos()).toString();
+					} else if (elem.isClose() && elem.getName().equals("url-pattern")) {
+						urlPattern = parser.getInputFromPositionMarker(elem.getPos()).toString();
 					}
-				}
-				if (add)
-				{
-					add = false;
+				} while (urlPattern == null || encounteredFilterName == null);
+				
+				if (filterName.equals(encounteredFilterName))
 					urlPatterns.add(urlPattern);
-				}
 			}
+
 			// By the time we get here, we have a list of urlPatterns we match
 			// this filter against.
 			// In all likelihood, we will only have one. If we have none, we

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/XmlReader.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/XmlReader.java?view=diff&rev=514684&r1=514683&r2=514684
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/XmlReader.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/XmlReader.java Mon Mar  5 07:01:52 2007
@@ -60,7 +60,7 @@
 	 * @param inputStream
 	 *            The InputStream to read the xml data from
 	 * @param defaultEncoding
-	 *            Apply 'null' for JVM default
+	 *            Default character encoding to use when not specified in XML declaration, specify null to use JVM default
 	 * @throws IOException
 	 *             In case something went wrong while reading the data
 	 */
@@ -84,7 +84,7 @@
 	/**
 	 * Return the encoding used while reading the markup file.
 	 * 
-	 * @return if null, than JVM default
+	 * @return if null, then JVM default
 	 */
 	public String getEncoding()
 	{