Skip to content

Commit

Permalink
replaced character stack with string buffer, fixed characters populat…
Browse files Browse the repository at this point in the history
…ion mechanism
  • Loading branch information
andrew-aladev committed Sep 28, 2017
1 parent 7afdf12 commit ac7060c
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions ext/java/nokogiri/internals/NokogiriHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Expand All @@ -38,7 +38,6 @@
import static nokogiri.internals.NokogiriHelpers.stringOrNil;

import java.util.LinkedList;
import java.util.Stack;

import nokogiri.XmlSyntaxError;

Expand All @@ -57,12 +56,12 @@

/**
* A handler for SAX parsing.
*
*
* @author sergio
* @author Yoko Harada <yokolet@gmail.com>
*/
public class NokogiriHandler extends DefaultHandler2 implements XmlDeclHandler {
Stack<StringBuffer> characterStack;
StringBuffer charactersBuffer;
private final Ruby ruby;
private final RubyClass attrClass;
private final IRubyObject object;
Expand Down Expand Up @@ -100,7 +99,7 @@ public void setDocumentLocator(Locator locator) {
@Override
public void startDocument() throws SAXException {
call("start_document");
characterStack = new Stack();
charactersBuffer = new StringBuffer();
}

@Override
Expand All @@ -112,13 +111,7 @@ public void xmlDecl(String version, String encoding, String standalone) {

@Override
public void endDocument() throws SAXException {
StringBuffer sb;
if (!characterStack.empty()) {
for (int i=0; i<characterStack.size(); i++) {
sb = characterStack.get(i);
call("characters", ruby.newString(sb.toString()));
}
}
populateCharacters();
call("end_document");
}

Expand Down Expand Up @@ -172,7 +165,7 @@ public void startElement(String uri, String localName, String qName, Attributes
args[1] = stringOrNil(ruby, pre);
args[2] = stringOrNil(ruby, u);
}
}
}
if (args == null) {
args = new IRubyObject[4];
args[0] = stringOrNil(ruby, ln);
Expand All @@ -187,34 +180,34 @@ public void startElement(String uri, String localName, String qName, Attributes
}

if (localName == null || localName.equals("")) localName = getLocalPart(qName);
populateCharacters();
call("start_element_namespace",
stringOrNil(ruby, localName),
rubyAttr,
stringOrNil(ruby, getPrefix(qName)),
stringOrNil(ruby, uri),
rubyNSAttr);
characterStack.push(new StringBuffer());
}

private static String[] emptyAttrs =
{"checked", "compact", "declare", "defer", "disabled", "ismap", "multiple",
{"checked", "compact", "declare", "defer", "disabled", "ismap", "multiple",
"noresize", "nohref", "noshade", "nowrap", "readonly", "selected"};

private boolean isEmptyAttr(String name) {
for (String emptyAttr : emptyAttrs) {
if (emptyAttr.equals(name)) return true;
}
return false;
}

public Integer getLine() {
return locator.getLineNumber();
}

public Integer getColumn() {
return locator.getColumnNumber() - 1;
}

private boolean isFromFragmentHandler() {
if (object != null && object instanceof RubyObject) {
RubyObject rubyObj = (RubyObject)object;
Expand All @@ -231,8 +224,7 @@ private boolean isFromFragmentHandler() {

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
StringBuffer sb = characterStack.pop();
call("characters", ruby.newString(sb.toString()));
populateCharacters();
call("end_element_namespace",
stringOrNil(ruby, localName),
stringOrNil(ruby, getPrefix(qName)),
Expand All @@ -241,24 +233,24 @@ public void endElement(String uri, String localName, String qName) throws SAXExc

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
StringBuffer sb = characterStack.peek();
sb.append(ch, start, length);
charactersBuffer.append(new String(ch, start, length));
}

@Override
public void comment(char[] ch, int start, int length) throws SAXException {
populateCharacters();
call("comment", ruby.newString(new String(ch, start, length)));
}

@Override
public void startCDATA() throws SAXException {
characterStack.push(new StringBuffer());
populateCharacters();
}

@Override
public void endCDATA() throws SAXException {
StringBuffer sb = characterStack.pop();
call("cdata_block", ruby.newString(sb.toString()));
call("cdata_block", ruby.newString(charactersBuffer.toString()));
charactersBuffer.setLength(0);
}

@Override
Expand Down Expand Up @@ -337,4 +329,10 @@ private IRubyObject document(ThreadContext context) {
return context.getRuntime().getNil();
}

protected void populateCharacters() {
if (charactersBuffer.length() > 0) {
call("characters", ruby.newString(charactersBuffer.toString()));
charactersBuffer.setLength(0);
}
}
}

0 comments on commit ac7060c

Please sign in to comment.