[Smax-commit] SF.net SVN: smax: [31] trunk/smax
Status: Alpha
Brought to you by:
dbrosius
|
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.
|