diff --git a/src/java/org/jivesoftware/multiplexer/net/XMLLightweightParser.java b/src/java/org/jivesoftware/multiplexer/net/XMLLightweightParser.java index c500186..ad018f7 100644 --- a/src/java/org/jivesoftware/multiplexer/net/XMLLightweightParser.java +++ b/src/java/org/jivesoftware/multiplexer/net/XMLLightweightParser.java @@ -54,6 +54,10 @@ protected static final int INSIDE_PARAM_VALUE = 7; // Status used when you are inside a cdata section protected static final int INSIDE_CDATA = 8; + // Status used when you are outside a tag/reading text + protected static final int OUTSIDE = 9; + + final String[] sstatus = {"INIT", "", "HEAD", "INSIDE", "PRETAIL", "TAIL", "VERIFY", "INSIDE_PARAM", "INSIDE_CDATA", "OUTSIDE"}; // Current robot status @@ -133,6 +137,7 @@ head.setLength(0); insideRootTag = false; insideChildrenTag = false; + depth = 0; } /* @@ -154,7 +159,6 @@ // Robot. char ch; for (int i = 0; i < readByte; i++) { - //ch = rawByteBuffer[ i ]; ch = buf[i]; if (status == XMLLightweightParser.TAIL) { // Looking for the close tag @@ -199,6 +203,7 @@ } else if (status == XMLLightweightParser.VERIFY_CLOSE_TAG) { if (ch == '>') { depth--; + status = XMLLightweightParser.OUTSIDE; if (depth < 1) { // Found a tag in the form int end = buffer.length() - readByte + (i + 1); @@ -206,7 +211,7 @@ // Add message to the list foundMsg(msg); startLastMsg = end; - } + } } else if (ch == '<') { status = XMLLightweightParser.PRETAIL; insideChildrenTag = true; @@ -222,7 +227,7 @@ if (ch == XMLLightweightParser.CDATA_END[cdataOffset]) { cdataOffset++; if (cdataOffset == XMLLightweightParser.CDATA_END.length) { - status = XMLLightweightParser.INSIDE; + status = XMLLightweightParser.OUTSIDE; cdataOffset = 0; } } else { @@ -238,10 +243,12 @@ } } else { cdataOffset = 0; + status = XMLLightweightParser.INSIDE; } if (ch == '"') { status = XMLLightweightParser.INSIDE_PARAM_VALUE; } else if (ch == '>') { + status = XMLLightweightParser.OUTSIDE; if (insideRootTag && ("stream:stream>".equals(head.toString()) || ("?xml>".equals(head.toString())) || ("flash:stream>".equals(head.toString())))) { // Found closing stream:stream @@ -255,17 +262,17 @@ startLastMsg = end; } insideRootTag = false; - } else if (ch == '<') { - status = XMLLightweightParser.PRETAIL; - insideChildrenTag = true; } else if (ch == '/') { status = XMLLightweightParser.VERIFY_CLOSE_TAG; } } else if (status == XMLLightweightParser.HEAD) { if (ch == ' ' || ch == '>') { - // Append > to head to facility the research of + // Append > to head to allow searching head.append(">"); - status = XMLLightweightParser.INSIDE; + if(ch == '>') + status = XMLLightweightParser.OUTSIDE; + else + status = XMLLightweightParser.INSIDE; insideRootTag = true; insideChildrenTag = false; continue; @@ -284,6 +291,12 @@ else { startLastMsg++; } + } else if (status == XMLLightweightParser.OUTSIDE) { + if (ch == '<') { + status = XMLLightweightParser.PRETAIL; + cdataOffset = 1; + insideChildrenTag = true; + } } } if (head.length() > 0 && diff --git a/test/org/jivesoftware/multiplexer/net/XMLLightweightParserTest.java b/test/org/jivesoftware/multiplexer/net/XMLLightweightParserTest.java index b22276e..6531ee0 100644 --- a/test/org/jivesoftware/multiplexer/net/XMLLightweightParserTest.java +++ b/test/org/jivesoftware/multiplexer/net/XMLLightweightParserTest.java @@ -268,6 +268,35 @@ assertEquals(msg1, doc.asXML()); } + public void testWeirdoContent() throws Exception { + final String[] testStanzas = + { + "", + "", + "", + ">here<<<<< /> />]]>]]>", + " this \" is / a test /> test /> ", + "this is a comment", + "", + " text ", + " ", + "12\"" , + " /> /> " , + }; + String testMsg = ""; + for(String s : testStanzas) { + testMsg += s; + } + ByteBuffer mybuffer = ByteBuffer.wrap(testMsg.getBytes()); + parser.read(mybuffer); + + String[] msgs = parser.getMsgs(); + for(int i = 0; i < testStanzas.length; i++) { + assertTrue(i < msgs.length); + assertEquals(testStanzas[i], msgs[i]); + } + } + protected void setUp() throws Exception { super.setUp(); // Create parser