Thread: [Smax-commit] SF.net SVN: smax: [23] trunk/smax
Status: Alpha
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2008-01-19 05:59:09
|
Revision: 23
http://smax.svn.sourceforge.net/smax/?rev=23&view=rev
Author: dbrosius
Date: 2008-01-18 21:59:06 -0800 (Fri, 18 Jan 2008)
Log Message:
-----------
Initial checkin of skeleton
Added Paths:
-----------
trunk/smax/src/
trunk/smax/src/com/
trunk/smax/src/com/mebigfatguy/
trunk/smax/src/com/mebigfatguy/smax/
trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java
trunk/smax/src/com/mebigfatguy/smax/SmaxParserFactory.java
trunk/smax/src/javax/
trunk/smax/src/javax/xml/
trunk/smax/src/javax/xml/parsers/
trunk/smax/src/javax/xml/parsers/smax/
trunk/smax/src/javax/xml/parsers/smax/SMAXParserFactory.java
trunk/smax/src/org/
trunk/smax/src/org/xml/
trunk/smax/src/org/xml/smax/
trunk/smax/src/org/xml/smax/ContentHandler.java
trunk/smax/src/org/xml/smax/DefaultHandler.java
trunk/smax/src/org/xml/smax/InputSource.java
trunk/smax/src/org/xml/smax/SMAXException.java
trunk/smax/src/org/xml/smax/SMAXParser.java
Added: trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java
===================================================================
--- trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java (rev 0)
+++ trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java 2008-01-19 05:59:06 UTC (rev 23)
@@ -0,0 +1,35 @@
+/*
+ * smax - The Simple Mutated API for XML
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package com.mebigfatguy.smax;
+
+import java.io.IOException;
+
+import org.xml.smax.ContentHandler;
+import org.xml.smax.InputSource;
+import org.xml.smax.SMAXException;
+import org.xml.smax.SMAXParser;
+
+public class SmaxParser implements SMAXParser {
+
+ public void setContentHandler(ContentHandler handler) {
+ }
+
+ public void parse(InputSource input) throws IOException, IllegalArgumentException, SMAXException {
+ }
+}
Added: trunk/smax/src/com/mebigfatguy/smax/SmaxParserFactory.java
===================================================================
--- trunk/smax/src/com/mebigfatguy/smax/SmaxParserFactory.java (rev 0)
+++ trunk/smax/src/com/mebigfatguy/smax/SmaxParserFactory.java 2008-01-19 05:59:06 UTC (rev 23)
@@ -0,0 +1,32 @@
+/*
+ * smax - The Simple Mutated API for XML
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package com.mebigfatguy.smax;
+
+import javax.xml.parsers.smax.SMAXParserFactory;
+
+import org.xml.smax.SMAXParser;
+
+public class SmaxParserFactory extends SMAXParserFactory {
+
+ @Override
+ public SMAXParser newSMAXParser() {
+ return null;
+ }
+
+}
Added: trunk/smax/src/javax/xml/parsers/smax/SMAXParserFactory.java
===================================================================
--- trunk/smax/src/javax/xml/parsers/smax/SMAXParserFactory.java (rev 0)
+++ trunk/smax/src/javax/xml/parsers/smax/SMAXParserFactory.java 2008-01-19 05:59:06 UTC (rev 23)
@@ -0,0 +1,48 @@
+/*
+ * smax - The Simple Mutated API for XML
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package javax.xml.parsers.smax;
+
+import javax.xml.parsers.FactoryConfigurationError;
+
+import org.xml.smax.SMAXParser;
+
+import com.mebigfatguy.smax.SmaxParserFactory;
+
+public abstract class SMAXParserFactory {
+ public static final String FACTORY_PROPERTY = "javax.xml.parsers.SMAXParserFactory";
+
+ public static SMAXParserFactory newInstance() throws FactoryConfigurationError {
+ try
+ {
+ String factoryClsName = System.getProperty(FACTORY_PROPERTY);
+ if (factoryClsName != null) {
+ Class<?> cls = Class.forName(factoryClsName);
+ return (SMAXParserFactory)cls.newInstance();
+ }
+ return new SmaxParserFactory();
+ }
+ catch (Exception e) {
+ FactoryConfigurationError fce = new FactoryConfigurationError("Failed initializing SMAXParserFactory");
+ fce.initCause(e);
+ throw fce;
+ }
+ }
+
+ public abstract SMAXParser newSMAXParser();
+}
Added: trunk/smax/src/org/xml/smax/ContentHandler.java
===================================================================
--- trunk/smax/src/org/xml/smax/ContentHandler.java (rev 0)
+++ trunk/smax/src/org/xml/smax/ContentHandler.java 2008-01-19 05:59:06 UTC (rev 23)
@@ -0,0 +1,35 @@
+/*
+ * smax - The Simple Mutated API for XML
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.xml.smax;
+
+public interface ContentHandler {
+ public enum NextParserAction { ProcessChildren, ProcessChildElements, ProcessChildTextNodes, ProcessAttributes, SkipChildElements, SkipChildTextNodes, SkipAttributes, SkipChildren, Terminate };
+
+ NextParserAction startDocument();
+
+ void endDocument();
+
+ NextParserAction startElement(String uri, String localName, String qName);
+
+ void endElement(String uri, String localName, String qName);
+
+ void attribute(String uri, String localName, String qName, String value);
+
+ void textNode(String text);
+}
Added: trunk/smax/src/org/xml/smax/DefaultHandler.java
===================================================================
--- trunk/smax/src/org/xml/smax/DefaultHandler.java (rev 0)
+++ trunk/smax/src/org/xml/smax/DefaultHandler.java 2008-01-19 05:59:06 UTC (rev 23)
@@ -0,0 +1,42 @@
+/*
+ * smax - The Simple Mutated API for XML
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.xml.smax;
+
+public class DefaultHandler implements ContentHandler {
+
+ public NextParserAction startDocument() {
+ return NextParserAction.ProcessChildren;
+ }
+
+ public void endDocument() {
+ }
+
+ public NextParserAction startElement(String uri, String localName, String name) {
+ return NextParserAction.ProcessChildren;
+ }
+
+ public void endElement(String uri, String localName, String name) {
+ }
+
+ public void attribute(String uri, String localName, String qName, String value) {
+ }
+
+ public void textNode(String text) {
+ }
+}
Added: trunk/smax/src/org/xml/smax/InputSource.java
===================================================================
--- trunk/smax/src/org/xml/smax/InputSource.java (rev 0)
+++ trunk/smax/src/org/xml/smax/InputSource.java 2008-01-19 05:59:06 UTC (rev 23)
@@ -0,0 +1,23 @@
+/*
+ * smax - The Simple Mutated API for XML
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.xml.smax;
+
+public class InputSource {
+
+}
Added: trunk/smax/src/org/xml/smax/SMAXException.java
===================================================================
--- trunk/smax/src/org/xml/smax/SMAXException.java (rev 0)
+++ trunk/smax/src/org/xml/smax/SMAXException.java 2008-01-19 05:59:06 UTC (rev 23)
@@ -0,0 +1,36 @@
+/*
+ * smax - The Simple Mutated API for XML
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.xml.smax;
+
+public class SMAXException extends Exception {
+
+ private static final long serialVersionUID = -6902556051191638977L;
+
+ public SMAXException() {
+ super();
+ }
+
+ public SMAXException(String message) {
+ super(message);
+ }
+
+ public SMAXException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}
Added: trunk/smax/src/org/xml/smax/SMAXParser.java
===================================================================
--- trunk/smax/src/org/xml/smax/SMAXParser.java (rev 0)
+++ trunk/smax/src/org/xml/smax/SMAXParser.java 2008-01-19 05:59:06 UTC (rev 23)
@@ -0,0 +1,28 @@
+/*
+ * smax - The Simple Mutated API for XML
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.xml.smax;
+
+import java.io.IOException;
+
+
+public interface SMAXParser {
+ void setContentHandler(ContentHandler handler);
+
+ void parse(InputSource input) throws IOException, IllegalArgumentException, SMAXException;
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <dbr...@us...> - 2008-01-24 05:59:59
|
Revision: 31
http://smax.svn.sourceforge.net/smax/?rev=31&view=rev
Author: dbrosius
Date: 2008-01-23 21:59:54 -0800 (Wed, 23 Jan 2008)
Log Message:
-----------
initial simple parser test
Modified Paths:
--------------
trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java
trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java
Added Paths:
-----------
trunk/smax/test/
trunk/smax/test/com/
trunk/smax/test/com/mebigfatguy/
trunk/smax/test/com/mebigfatguy/smax/
trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java
Modified: trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java
===================================================================
--- trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java 2008-01-24 04:26:51 UTC (rev 30)
+++ trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java 2008-01-24 05:59:54 UTC (rev 31)
@@ -21,10 +21,10 @@
public enum SmaxParseState {
UNKNOWN,
LTSYM,
- LEADINGSLASH,
TRAILINGEMPTYSLASH,
TRAILINGSLASH,
STARTTAGNAME,
+ AFTERSTARTTAG,
ENDTAGNAME,
ATTRIBUTENAME,
EQUALS,
Modified: trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java
===================================================================
--- trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java 2008-01-24 04:26:51 UTC (rev 30)
+++ trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java 2008-01-24 05:59:54 UTC (rev 31)
@@ -58,7 +58,7 @@
handler.endDocument();
}
- private void parseReader(Reader r) throws IOException, SMAXException {
+ protected void parseReader(Reader r) throws IOException, SMAXException {
SmaxToken t = getNextToken(r);
while (t.getTokenType() != SmaxTokenType.Eof) {
@@ -66,7 +66,7 @@
}
}
- private SmaxToken getNextToken(Reader r) throws IOException, SMAXException {
+ protected SmaxToken getNextToken(Reader r) throws IOException, SMAXException {
StringBuilder sb = new StringBuilder();
int i = r.read();
while (i != -1) {
@@ -84,14 +84,21 @@
if (state == SmaxParseState.UNKNOWN) {
if (c == '<') {
state = SmaxParseState.LTSYM;
+ } else {
+ state = SmaxParseState.PCDATA;
+ sb.setLength(0);
+ sb.append(c);
}
-
} else if (state == SmaxParseState.LTSYM) {
- if (c == '/')
- state = SmaxParseState.LEADINGSLASH;
- else
+ if (c == '/') {
+ state = SmaxParseState.ENDTAGNAME;
+ sb.setLength(0);
+ }
+ else {
state = SmaxParseState.STARTTAGNAME;
- } else if (state == SmaxParseState.LEADINGSLASH) {
+ sb.setLength(0);
+ sb.append(c);
+ }
} else if (state == SmaxParseState.TRAILINGEMPTYSLASH) {
if (c == '>') {
return new SmaxToken(SmaxTokenType.EmptyTag, sb.toString());
@@ -111,8 +118,21 @@
} else if (c == '>') {
state = SmaxParseState.UNKNOWN;
return new SmaxToken(SmaxTokenType.StartTag, sb.toString());
+ } else if (Character.isWhitespace(c)) {
+ state = SmaxParseState.AFTERSTARTTAG;
}
} else if (state == SmaxParseState.ENDTAGNAME) {
+ if (((c >= 'a') && (c <= 'z'))
+ || ((c >= 'A') && (c <= 'Z'))
+ || ((c >= '0') && (c <= '9'))
+ || (c == ':')
+ || (c == '_')
+ || (c == '-')) {
+ sb.append(c);
+ } else if (c == '>') {
+ state = SmaxParseState.UNKNOWN;
+ return new SmaxToken(SmaxTokenType.EndTag, sb.toString());
+ }
} else if (state == SmaxParseState.ATTRIBUTENAME) {
} else if (state == SmaxParseState.ATTRIBUTEVALUE) {
} else {
Added: trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java
===================================================================
--- trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java (rev 0)
+++ trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java 2008-01-24 05:59:54 UTC (rev 31)
@@ -0,0 +1,42 @@
+/*
+ * smax - The Simple Mutated API for XML
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package com.mebigfatguy.smax;
+
+import java.io.BufferedReader;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+
+public class SmaxParserTest {
+ @Test
+ public void testOneElement() throws Exception {
+ SmaxParser parser = new SmaxParser();
+ Reader r = new BufferedReader(new StringReader("<test></test>"));
+ SmaxToken t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.StartTag, t.getTokenType());
+ Assert.assertEquals("test", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.EndTag, t.getTokenType());
+ Assert.assertEquals("test", t.getTokenValue());
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <dbr...@us...> - 2008-01-24 06:53:08
|
Revision: 33
http://smax.svn.sourceforge.net/smax/?rev=33&view=rev
Author: dbrosius
Date: 2008-01-23 22:53:04 -0800 (Wed, 23 Jan 2008)
Log Message:
-----------
add preliminary test for parsing attributes
Modified Paths:
--------------
trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java
trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java
trunk/smax/src/com/mebigfatguy/smax/SmaxToken.java
trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java
Modified: trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java
===================================================================
--- trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java 2008-01-24 06:11:50 UTC (rev 32)
+++ trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java 2008-01-24 06:53:04 UTC (rev 33)
@@ -27,7 +27,9 @@
AFTERSTARTTAG,
ENDTAGNAME,
ATTRIBUTENAME,
+ AFTERATTRIBUTENAME,
EQUALS,
+ AFTEREQUALS,
ATTRIBUTEVALUE,
PCDATA,
PROCESSINGINSTRUCTION,
Modified: trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java
===================================================================
--- trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java 2008-01-24 06:11:50 UTC (rev 32)
+++ trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java 2008-01-24 06:53:04 UTC (rev 33)
@@ -35,6 +35,7 @@
private boolean lastWasCR = false;
private int row = 1;
private int column = -1;
+ private char matchingQuote;
public void setContentHandler(ContentHandler cHandler) {
@@ -69,6 +70,7 @@
protected SmaxToken getNextToken(Reader r) throws IOException, SMAXException {
StringBuilder sb = new StringBuilder();
int i = r.read();
+
while (i != -1) {
char c = (char)i;
column++;
@@ -81,62 +83,138 @@
lastWasCR = false;
}
- if (state == SmaxParseState.UNKNOWN) {
- if (c == '<') {
- state = SmaxParseState.LTSYM;
- } else {
- state = SmaxParseState.PCDATA;
- sb.setLength(0);
- sb.append(c);
- }
- } else if (state == SmaxParseState.LTSYM) {
- if (c == '/') {
- state = SmaxParseState.ENDTAGNAME;
- sb.setLength(0);
- }
- else {
- state = SmaxParseState.STARTTAGNAME;
- sb.setLength(0);
- sb.append(c);
- }
- } else if (state == SmaxParseState.TRAILINGEMPTYSLASH) {
- if (c == '>') {
- return new SmaxToken(SmaxTokenType.EmptyTag, sb.toString());
- } else {
- throw new SMAXException("Invalid character parsed: " + c, row, column);
- }
- } else if (state == SmaxParseState.STARTTAGNAME) {
- if (((c >= 'a') && (c <= 'z'))
- || ((c >= 'A') && (c <= 'Z'))
- || ((c >= '0') && (c <= '9'))
- || (c == ':')
- || (c == '_')
- || (c == '-')) {
- sb.append(c);
- } else if (c == '/') {
- state = SmaxParseState.TRAILINGEMPTYSLASH;
- } else if (c == '>') {
- state = SmaxParseState.UNKNOWN;
- return new SmaxToken(SmaxTokenType.StartTag, sb.toString());
- } else if (Character.isWhitespace(c)) {
- state = SmaxParseState.AFTERSTARTTAG;
- }
- } else if (state == SmaxParseState.ENDTAGNAME) {
- if (((c >= 'a') && (c <= 'z'))
- || ((c >= 'A') && (c <= 'Z'))
- || ((c >= '0') && (c <= '9'))
- || (c == ':')
- || (c == '_')
- || (c == '-')) {
+ switch (state) {
+ case UNKNOWN:
+ if (c == '<') {
+ state = SmaxParseState.LTSYM;
+ } else {
+ state = SmaxParseState.PCDATA;
+ sb.setLength(0);
+ sb.append(c);
+ }
+ break;
+
+ case LTSYM:
+ if (c == '/') {
+ state = SmaxParseState.ENDTAGNAME;
+ sb.setLength(0);
+ }
+ else {
+ state = SmaxParseState.STARTTAGNAME;
+ sb.setLength(0);
+ sb.append(c);
+ }
+ break;
+
+ case TRAILINGEMPTYSLASH:
+ if (c == '>') {
+ return new SmaxToken(SmaxTokenType.EmptyTag, sb.toString());
+ } else {
+ return new SmaxToken(SmaxTokenType.Error, String.valueOf(c));
+ }
+ //break;
+
+ case STARTTAGNAME:
+ if (Character.isJavaIdentifierPart(c)
+ || (c == ':')
+ || (c == '-')) {
+ sb.append(c);
+ } else if (c == '/') {
+ state = SmaxParseState.TRAILINGEMPTYSLASH;
+ } else if (c == '>') {
+ state = SmaxParseState.UNKNOWN;
+ return new SmaxToken(SmaxTokenType.StartTag, sb.toString());
+ } else if (Character.isWhitespace(c)) {
+ state = SmaxParseState.AFTERSTARTTAG;
+ return new SmaxToken(SmaxTokenType.StartTag, sb.toString());
+ }
+ break;
+
+ case ENDTAGNAME:
+ if (Character.isJavaIdentifierPart(c)
+ || (c == ':')
+ || (c == '-')) {
+ sb.append(c);
+ } else if (c == '>') {
+ state = SmaxParseState.UNKNOWN;
+ return new SmaxToken(SmaxTokenType.EndTag, sb.toString());
+ }
+ break;
+
+ case ATTRIBUTENAME:
+ if (Character.isJavaIdentifierPart(c)) {
+ sb.append(c);
+ } else if (Character.isWhitespace(c)) {
+ state = SmaxParseState.AFTERATTRIBUTENAME;
+ return new SmaxToken(SmaxTokenType.AttributeName, sb.toString());
+ } else if (c == '=') {
+ state = SmaxParseState.EQUALS;
+ return new SmaxToken(SmaxTokenType.AttributeName, sb.toString());
+ }
+ break;
+
+ case EQUALS:
+ if (!Character.isWhitespace(c)) {
+ if ((c == '"') || (c == '\'')) {
+ matchingQuote = c;
+ state = SmaxParseState.ATTRIBUTEVALUE;
+ sb.setLength(0);
+ } else {
+ return new SmaxToken(SmaxTokenType.Error, String.valueOf(c));
+ }
+ }
+ return new SmaxToken(SmaxTokenType.Equals, "=");
+ //break;
+
+ case AFTERATTRIBUTENAME:
+ if (!Character.isWhitespace(c)) {
+ if (c == '=') {
+ state = SmaxParseState.AFTEREQUALS;
+ sb.setLength(0);
+ return new SmaxToken(SmaxTokenType.Equals, "=");
+ } else {
+ return new SmaxToken(SmaxTokenType.Error, String.valueOf(c));
+ }
+ }
+ break;
+
+ case AFTEREQUALS:
+ if (!Character.isWhitespace(c)) {
+ if ((c == '"') || (c == '\'')) {
+ matchingQuote = c;
+ state = SmaxParseState.ATTRIBUTEVALUE;
+ sb.setLength(0);
+ } else {
+ return new SmaxToken(SmaxTokenType.Error, String.valueOf(c));
+ }
+ }
+ break;
+
+ case ATTRIBUTEVALUE:
+ if (c == matchingQuote) {
+ state = SmaxParseState.AFTERSTARTTAG;
+ return new SmaxToken(SmaxTokenType.AttributeValue, sb.toString());
+ } else {
+ sb.append(c);
+ }
+ break;
+
+ case AFTERSTARTTAG:
+ if (!Character.isWhitespace(c)) {
+ if (Character.isJavaIdentifierStart(c)) {
+ state = SmaxParseState.ATTRIBUTENAME;
+ sb.setLength(0);
sb.append(c);
- } else if (c == '>') {
- state = SmaxParseState.UNKNOWN;
- return new SmaxToken(SmaxTokenType.EndTag, sb.toString());
- }
- } else if (state == SmaxParseState.ATTRIBUTENAME) {
- } else if (state == SmaxParseState.ATTRIBUTEVALUE) {
- } else {
- return new SmaxToken(SmaxTokenType.Error, String.valueOf(c));
+ } else {
+ return new SmaxToken(SmaxTokenType.Error, String.valueOf(c));
+ }
+
+ }
+ break;
+
+ default:
+ return new SmaxToken(SmaxTokenType.Error, String.valueOf(c));
+ //break;
}
i = r.read();
Modified: trunk/smax/src/com/mebigfatguy/smax/SmaxToken.java
===================================================================
--- trunk/smax/src/com/mebigfatguy/smax/SmaxToken.java 2008-01-24 06:11:50 UTC (rev 32)
+++ trunk/smax/src/com/mebigfatguy/smax/SmaxToken.java 2008-01-24 06:53:04 UTC (rev 33)
@@ -34,4 +34,9 @@
public String getTokenValue() {
return data;
}
+
+ @Override
+ public String toString() {
+ return ttype + "[" + data + "]";
+ }
}
Modified: trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java
===================================================================
--- trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java 2008-01-24 06:11:50 UTC (rev 32)
+++ trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java 2008-01-24 06:53:04 UTC (rev 33)
@@ -31,13 +31,13 @@
@Test
public void testOneElement() throws Exception {
SmaxParser parser = new SmaxParser();
- Reader r = new BufferedReader(new StringReader("<test></test>"));
+ Reader r = new BufferedReader(new StringReader("<test1></test1>"));
SmaxToken t = parser.getNextToken(r);
Assert.assertEquals(SmaxTokenType.StartTag, t.getTokenType());
- Assert.assertEquals("test", t.getTokenValue());
+ Assert.assertEquals("test1", t.getTokenValue());
t = parser.getNextToken(r);
Assert.assertEquals(SmaxTokenType.EndTag, t.getTokenType());
- Assert.assertEquals("test", t.getTokenValue());
+ Assert.assertEquals("test1", t.getTokenValue());
}
@Test
@@ -48,4 +48,32 @@
Assert.assertEquals(SmaxTokenType.EmptyTag, t.getTokenType());
Assert.assertEquals("test", t.getTokenValue());
}
+
+ @Test
+ public void testAttributes() throws Exception {
+ SmaxParser parser = new SmaxParser();
+ Reader r = new BufferedReader(new StringReader("<test one='1' two = \"2\" ></test>"));
+ SmaxToken t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.StartTag, t.getTokenType());
+ Assert.assertEquals("test", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.AttributeName, t.getTokenType());
+ Assert.assertEquals("one", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.Equals, t.getTokenType());
+ Assert.assertEquals("=", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.AttributeValue, t.getTokenType());
+ Assert.assertEquals("1", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.AttributeName, t.getTokenType());
+ Assert.assertEquals("two", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.Equals, t.getTokenType());
+ Assert.assertEquals("=", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.AttributeValue, t.getTokenType());
+ Assert.assertEquals("2", t.getTokenValue());
+
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <dbr...@us...> - 2008-01-27 18:20:40
|
Revision: 35
http://smax.svn.sourceforge.net/smax/?rev=35&view=rev
Author: dbrosius
Date: 2008-01-27 10:20:30 -0800 (Sun, 27 Jan 2008)
Log Message:
-----------
implement PCData parsing
Modified Paths:
--------------
trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java
trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java
Modified: trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java
===================================================================
--- trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java 2008-01-25 04:45:37 UTC (rev 34)
+++ trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java 2008-01-27 18:20:30 UTC (rev 35)
@@ -94,6 +94,15 @@
}
break;
+ case PCDATA:
+ if (c == '<') {
+ state = SmaxParseState.LTSYM;
+ return new SmaxToken(SmaxTokenType.PCData, sb.toString());
+ } else {
+ sb.append(c);
+ }
+ break;
+
case LTSYM:
if (c == '/') {
state = SmaxParseState.ENDTAGNAME;
Modified: trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java
===================================================================
--- trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java 2008-01-25 04:45:37 UTC (rev 34)
+++ trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java 2008-01-27 18:20:30 UTC (rev 35)
@@ -74,6 +74,20 @@
t = parser.getNextToken(r);
Assert.assertEquals(SmaxTokenType.AttributeValue, t.getTokenType());
Assert.assertEquals("2", t.getTokenValue());
-
}
+
+ @Test
+ public void testPCData() throws Exception {
+ SmaxParser parser = new SmaxParser();
+ Reader r = new BufferedReader(new StringReader("<test>Sample Data</test>"));
+ SmaxToken t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.StartTag, t.getTokenType());
+ Assert.assertEquals("test", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.PCData, t.getTokenType());
+ Assert.assertEquals("Sample Data", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.EndTag, t.getTokenType());
+ Assert.assertEquals("test", t.getTokenValue());
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <dbr...@us...> - 2008-01-27 18:53:01
|
Revision: 36
http://smax.svn.sourceforge.net/smax/?rev=36&view=rev
Author: dbrosius
Date: 2008-01-27 10:52:28 -0800 (Sun, 27 Jan 2008)
Log Message:
-----------
add support for parsing comments
Modified Paths:
--------------
trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java
trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java
trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java
Modified: trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java
===================================================================
--- trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java 2008-01-27 18:20:30 UTC (rev 35)
+++ trunk/smax/src/com/mebigfatguy/smax/SmaxParseState.java 2008-01-27 18:52:28 UTC (rev 36)
@@ -33,5 +33,8 @@
ATTRIBUTEVALUE,
PCDATA,
PROCESSINGINSTRUCTION,
- COMMENT
+ COMMENT,
+ BANG,
+ STARTDASH,
+ ENDDASH
}
Modified: trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java
===================================================================
--- trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java 2008-01-27 18:20:30 UTC (rev 35)
+++ trunk/smax/src/com/mebigfatguy/smax/SmaxParser.java 2008-01-27 18:52:28 UTC (rev 36)
@@ -36,6 +36,7 @@
private int row = 1;
private int column = -1;
private char matchingQuote;
+ private int dashCount;
public void setContentHandler(ContentHandler cHandler) {
@@ -107,6 +108,9 @@
if (c == '/') {
state = SmaxParseState.ENDTAGNAME;
sb.setLength(0);
+ } else if (c == '!') {
+ state = SmaxParseState.BANG;
+ sb.setLength(0);
}
else {
state = SmaxParseState.STARTTAGNAME;
@@ -115,6 +119,22 @@
}
break;
+ case BANG:
+ if (c == '-') {
+ state = SmaxParseState.STARTDASH;
+ } else {
+ return new SmaxToken(SmaxTokenType.Error, String.valueOf(c));
+ }
+ break;
+
+ case STARTDASH:
+ if (c == '-') {
+ state = SmaxParseState.COMMENT;
+ } else {
+ return new SmaxToken(SmaxTokenType.Error, String.valueOf(c));
+ }
+ break;
+
case TRAILINGEMPTYSLASH:
if (c == '>') {
return new SmaxToken(SmaxTokenType.EmptyTag, sb.toString());
@@ -221,6 +241,34 @@
}
break;
+ case COMMENT:
+ if (c == '-') {
+ state = SmaxParseState.ENDDASH;
+ dashCount++;
+ } else {
+ sb.append(c);
+ }
+ break;
+
+ case ENDDASH:
+ if (c == '-')
+ dashCount++;
+ else if (c == '>') {
+ if (dashCount >= 2) {
+ state = SmaxParseState.UNKNOWN;
+ return new SmaxToken(SmaxTokenType.Comment, sb.toString());
+ } else {
+ dashCount = 0;
+ sb.append("->");
+ state = SmaxParseState.COMMENT;
+ }
+ } else {
+ dashCount = 0;
+ sb.append("-").append(c);
+ state = SmaxParseState.COMMENT;
+ }
+ break;
+
default:
return new SmaxToken(SmaxTokenType.Error, String.valueOf(c));
//break;
Modified: trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java
===================================================================
--- trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java 2008-01-27 18:20:30 UTC (rev 35)
+++ trunk/smax/test/com/mebigfatguy/smax/SmaxParserTest.java 2008-01-27 18:52:28 UTC (rev 36)
@@ -90,4 +90,19 @@
Assert.assertEquals(SmaxTokenType.EndTag, t.getTokenType());
Assert.assertEquals("test", t.getTokenValue());
}
+
+ @Test
+ public void testComment() throws Exception {
+ SmaxParser parser = new SmaxParser();
+ Reader r = new BufferedReader(new StringReader("<test><!--This is a test--></test>"));
+ SmaxToken t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.StartTag, t.getTokenType());
+ Assert.assertEquals("test", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.Comment, t.getTokenType());
+ Assert.assertEquals("This is a test", t.getTokenValue());
+ t = parser.getNextToken(r);
+ Assert.assertEquals(SmaxTokenType.EndTag, t.getTokenType());
+ Assert.assertEquals("test", t.getTokenValue());
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|