Skip to content

Commit

Permalink
if not using timers, just iterate instead of recursing
Browse files Browse the repository at this point in the history
  • Loading branch information
client9 committed May 5, 2010
1 parent 93d9619 commit 4f8cc28
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 41 deletions.
11 changes: 8 additions & 3 deletions htmlparser/gwt2/build.sh
Expand Up @@ -3,19 +3,22 @@
#
# Autodownload Parser
# from mozilla HG.
# TODO: specific specific releases
# TODO: use specific releases
#
PARSER=htmlparser

#
# Use mozilla trunk
#
echo "HG update from http://hg.mozilla.org/projects/htmlparser"
if [ ! -d "${PARSER}" ]; then
hg clone http://hg.mozilla.org/projects/htmlparser
else
(cd "${PARSER}"; hg pull; hg update )
fi
echo "----"

echo "Installing GWT"

#
# Autodownload GWT
Expand All @@ -28,15 +31,17 @@ if [ ! -d "${GWT}" ]; then
wget "http://google-web-toolkit.googlecode.com/files/${GWT_ZIP}"
unzip ${GWT_ZIP}
fi
echo "----"

echo "Starting GWT compile..."
CP="./src:./${PARSER}/src:./${PARSER}/super:./${GWT}/gwt-user.jar:./${GWT}/gwt-dev.jar"

# Compile a new GWT linker. Very simple to Single Script Linker but
# removes all the "bootstrap" and client (real browser) onScriptLoad
# events, etc.
echo "Javac our linker"
javac -cp ${CP} src/com/envjs/gwt/linker/ServerSingleScriptLinker.java

echo "Starting GWT..."
java \
-Xmx256M \
-cp "${CP}" \
Expand All @@ -46,7 +51,7 @@ java \
nu.validator.htmlparser.HtmlParser;

# -draftCompile \

echo "----"
echo "COPY to envjs src tree"
cp war/nu.validator.htmlparser.HtmlParser/nu.validator.htmlparser.HtmlParser.nocache.js ../../src/parser/htmlparser.js

Expand Down
99 changes: 61 additions & 38 deletions htmlparser/gwt2/src/nu/validator/htmlparser/gwt/HtmlParser.java
Expand Up @@ -144,16 +144,73 @@ private void tokenize(String source, final boolean useSetTimeouts, String contex
pump(useSetTimeouts);
}

/**
* pump pushes tokens out to the parser (I believe).
*
* The default version of this (same file/directory but in the
* htmlparser/src-gwt directory, works file. However this has
* been hacked for the "useSetTimeout" flag which corresponses to
* the doc.async flag (with async --> use SetTimeouts).
*
* The async version parses a token then calls SetTimeout with 1ms,
* to be called again inorder to get another token. This lets other
* tasks in the timer run (perhaps networking) or pre-empty the parser.
*
* The Sync version just repeatably calls pumpcore
*/
private void pump(boolean useSetTimeouts) throws SAXException {


if (pumpcore()) {
return;
}

if(useSetTimeouts){
// schedule
Timer timer = new Timer() {

@Override public void run() {
try {
pump(true);
} catch (SAXException e) {
ending = true;
if (errorHandler != null) {
try {
errorHandler.fatalError(new SAXParseException(
e.getMessage(), null, null, -1, -1, e));
} catch (SAXException e1) {
}
}
}
}

};
timer.schedule(1);
}else{
try {
while (!pumpcore()) { }
} catch (SAXException e) {
ending = true;
if (errorHandler != null) {
try {
errorHandler.fatalError(new SAXParseException(
e.getMessage(), null, null, -1, -1, e));
} catch (SAXException e1) {
}
}
}
}
}

private boolean pumpcore() throws SAXException {

if (ending) {
tokenizer.end();
domTreeBuilder.getDocument(); // drops the internal reference
parseEndListener.parseComplete();
// Don't schedule timeout
return;
}
return true;
}


int docWriteLen = documentWriteBuffer.length();
if (docWriteLen > 0) {
Expand Down Expand Up @@ -194,42 +251,8 @@ private void pump(boolean useSetTimeouts) throws SAXException {
continue;
}
}
return false;

if(useSetTimeouts){
// schedule
Timer timer = new Timer() {

@Override public void run() {
try {
pump(true);
} catch (SAXException e) {
ending = true;
if (errorHandler != null) {
try {
errorHandler.fatalError(new SAXParseException(
e.getMessage(), null, null, -1, -1, e));
} catch (SAXException e1) {
}
}
}
}

};
timer.schedule(1);
}else{
try {
pump(false);
} catch (SAXException e) {
ending = true;
if (errorHandler != null) {
try {
errorHandler.fatalError(new SAXParseException(
e.getMessage(), null, null, -1, -1, e));
} catch (SAXException e1) {
}
}
}
}
}

private void push(UTF16Buffer buffer) {
Expand Down

0 comments on commit 4f8cc28

Please sign in to comment.