Author: ivaynberg
Date: Sat Sep 17 18:22:28 2011
New Revision: 1172037

URL: http://svn.apache.org/viewvc?rev=1172037&view=rev
Log:

Issue: WICKET-3773

Added:
    
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/io/FullyBufferedReaderTest.java
   (with props)
Modified:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/parser/XmlPullParser.java
    
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/io/FullyBufferedReader.java

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/parser/XmlPullParser.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/parser/XmlPullParser.java?rev=1172037&r1=1172036&r2=1172037&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/parser/XmlPullParser.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/parser/XmlPullParser.java
 Sat Sep 17 18:22:28 2011
@@ -199,7 +199,18 @@ public final class XmlPullParser impleme
                input.countLinesTo(openBracketIndex);
 
                // Get index of closing tag and advance past the tag
-               int closeBracketIndex = input.find('>', openBracketIndex + 1);
+               int closeBracketIndex = -1;
+
+               if (openBracketIndex != -1 && openBracketIndex < input.size() - 
1)
+               {
+                       char nextChar = input.charAt(openBracketIndex + 1);
+
+                       if ((nextChar == '!') || (nextChar == '?'))
+                               closeBracketIndex = input.find('>', 
openBracketIndex);
+                       else
+                               closeBracketIndex = input.findOutOfQuotes('>', 
openBracketIndex);
+               }
+
                if (closeBracketIndex == -1)
                {
                        throw new ParseException("No matching close bracket at" 
+ getLineAndColumnText(),

Modified: 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/io/FullyBufferedReader.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/io/FullyBufferedReader.java?rev=1172037&r1=1172036&r2=1172037&view=diff
==============================================================================
--- 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/io/FullyBufferedReader.java
 (original)
+++ 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/io/FullyBufferedReader.java
 Sat Sep 17 18:22:28 2011
@@ -18,6 +18,7 @@ package org.apache.wicket.util.io;
 
 import java.io.IOException;
 import java.io.Reader;
+import java.text.ParseException;
 
 /**
  * This is not a reader like e.g. FileReader. It rather reads the whole data 
until the end from a
@@ -218,6 +219,77 @@ public final class FullyBufferedReader
        }
 
        /**
+        * Find a char starting at the position provided. The char must not be 
inside a quoted string
+        * (single or double)
+        * 
+        * @param ch
+        *            The char to search for
+        * @param startPos
+        *            The index to start at
+        * @return -1 if not found
+        */
+       public int findOutOfQuotes(final char ch, int startPos) throws 
ParseException
+       {
+               return findOutOfQuotes(ch, startPos, (char)0);
+       }
+
+       /**
+        * Find a char starting at the position provided. The char must not be 
inside a quoted string
+        * (single or double)
+        * 
+        * @param ch
+        *            The char to search for
+        * @param startPos
+        *            The index to start at
+        * @param quotationChar
+        *            The current quotation char. Must be ' or ", otherwise 
will be ignored.
+        * @param insideQuotations
+        *            Indicates if we are inside quotes or not.
+        * @return -1 if not found
+        */
+       public int findOutOfQuotes(final char ch, int startPos, char 
quotationChar)
+               throws ParseException
+       {
+               int closeBracketIndex = find(ch, startPos + 1);
+               char nextChar = closeBracketIndex == -1 ? nextChar = (char)0 : 
input.charAt(startPos + 1);
+
+               if (closeBracketIndex != -1)
+               {
+                       CharSequence tagCode = getSubstring(startPos, 
closeBracketIndex + 1);
+
+                       for (int i = 0; i < tagCode.length(); i++)
+                       {
+                               char currentChar = tagCode.charAt(i);
+                               char previousTag = tagCode.charAt(i > 0 ? i - 1 
: 0);
+
+                               if (quotationChar == 0 && (currentChar == '\'' 
|| currentChar == '\"'))
+                               {// I'm entering inside a quoted string. Set 
quotationChar
+                                       quotationChar = currentChar;
+                                       countLinesTo(startPos + i);
+                               }
+                               else if (currentChar == quotationChar && 
previousTag != '\\')
+                               { // I'm out of quotes, reset quotationChar
+                                       quotationChar = 0;
+                               }
+                               // I've found character but I'm inside quotes
+                               if (currentChar == ch && quotationChar != 0)
+                                       return findOutOfQuotes(ch, 
closeBracketIndex + 1, quotationChar);
+                       }
+               }
+
+               return closeBracketIndex;
+       }
+
+       /**
+        * 
+        * @return line and column number
+        */
+       private String getLineAndColumnText()
+       {
+               return " (line " + getLineNumber() + ", column " + 
getColumnNumber() + ")";
+       }
+
+       /**
         * Position the reader at the index provided. Could be anywhere within 
the data
         * 
         * @param pos

Added: 
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/io/FullyBufferedReaderTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/io/FullyBufferedReaderTest.java?rev=1172037&view=auto
==============================================================================
--- 
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/io/FullyBufferedReaderTest.java
 (added)
+++ 
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/io/FullyBufferedReaderTest.java
 Sat Sep 17 18:22:28 2011
@@ -0,0 +1,60 @@
+/*
+ * 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.wicket.util.io;
+
+import java.text.ParseException;
+
+import junit.framework.TestCase;
+
+import org.junit.Test;
+
+
+public class FullyBufferedReaderTest extends TestCase
+{
+
+       @Test
+       public void testNestedQuotes() throws ParseException
+       {
+               // testTag is <a href='b \'" > a' theAtr="at'r'\"r">
+               String testTag = "<a href='b \\'\" > a' theAtr=\"at'r'\\\"r\">";
+               FullyBufferedReader fullyBufferedReader = new 
FullyBufferedReader(testTag);
+
+               // System.out.println(fullyBufferedReader);
+               int position = fullyBufferedReader.findOutOfQuotes('>', 0);
+
+               // have you found a close bracket?
+               assertEquals('>', testTag.charAt(position));
+               // close bracket must be at the end of the string
+               assertEquals(testTag.length(), position + 1);
+       }
+
+       @Test
+       public void testQuotedEsclamationQuotationMark() throws ParseException
+       {
+               // testTag is <a href='b " >!! a<??!!' theAtr=">">
+               String testTag = "<a href='b \" >!! a<??!!' theAtr=\">\">";
+               FullyBufferedReader fullyBufferedReader = new 
FullyBufferedReader(testTag);
+
+               // System.out.println(fullyBufferedReader);
+               int position = fullyBufferedReader.findOutOfQuotes('>', 0);
+
+               // have you found a close bracket?
+               assertEquals('>', testTag.charAt(position));
+               // close bracket must be at the end of the string
+               assertEquals(testTag.length(), position + 1);
+       }
+}
\ No newline at end of file

Propchange: 
wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/io/FullyBufferedReaderTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to