Created
January 8, 2016 06:52
-
-
Save RaasAhsan/8800cc2c674e022381a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xml; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.jantox.xmlparser.Scanner; | |
/* | |
* <xmlNode> ::= "<" ~ <name> ~ ">" ~ <body> ~ "</" ~ <name> ">" | |
* <letter> ::= [A-Za-z] | |
* <name> ::= <letter> | <letter> <name> | |
* <body> ::= <xmlNode> | <xmlNode> <body> | "" | |
*/ | |
// In Scala, a Parser would just be a function S => ParseResult[S, S] | |
// Pass in a string, get back a ParseResult which can be a ParseSuccess[S, S] | |
// ParseSuccess[S, S] returns the unparsed string and the parsed value. | |
public class XmlParser { | |
private Scanner scanner; | |
public XmlParser() { | |
} | |
public XmlElement parse(String xmlString) { | |
this.scanner = new Scanner(xmlString); | |
return parseElement(); | |
} | |
public XmlElement parseElement() { | |
if (!this.scanner.expect("<")) { | |
return null; | |
} | |
String name = this.parseName(); | |
if (!this.scanner.expect(">")) { | |
return null; | |
} | |
List<XmlNode> body = this.parseBody(); | |
if (!this.scanner.expect("<")) { | |
return null; | |
} | |
if (!this.scanner.expect("/")) { | |
return null; | |
} | |
String endName = this.parseName(); | |
if (!this.scanner.expect(">")) { | |
return null; | |
} | |
if (!name.equals(endName)) { | |
return null; | |
} | |
return new XmlElement(name, new ArrayList<XmlAttribute>(), body); | |
} | |
public List<XmlNode> parseBody() { | |
List<XmlNode> body = new ArrayList<XmlNode>(); | |
while (true) { | |
if (!this.scanner.startsWith("</")) { | |
XmlNode node = this.parseElement(); | |
body.add(node); | |
} else { | |
break; | |
} | |
} | |
return body; | |
} | |
public String parseName() { | |
if (this.isAlphabet()) { | |
String letter = this.parseAlphabet(); | |
if (this.isAlphabet()) { // There's another letter, so we possible have a name | |
return letter + this.parseName(); | |
} else { // There is no letter next | |
return letter; | |
} | |
} | |
return null; | |
} | |
public String parseLetter(String t) { | |
if (this.scanner.length() > 0) { | |
if (this.scanner.peek().equals(t)) { | |
return this.scanner.take(); | |
} | |
} | |
return null; | |
} | |
public boolean isLetter(String l) { | |
if (this.scanner.length() > 0) { | |
String token = this.scanner.peek(); | |
if (token.equals(l)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public String parseAlphabet() { | |
if (this.isAlphabet()) { | |
return this.scanner.take(); | |
} | |
return null; | |
} | |
public boolean isAlphabet() { | |
if (this.scanner.length() > 0) { | |
String token = this.scanner.peek(); | |
char charToken = token.charAt(0); | |
if ((charToken >= 'A' && charToken <= 'Z') || (charToken >= 'a' && charToken <= 'z')) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment