I wanted to develop a small parser application. I cannot use built in java parser or other xml parsers due to some reasons. I have developed a small program to parse. The program which I have written does not look good. Now wanted to rewrite the program in such a way that I should be able to apply rules programatically. For example If I want to add a new rule to the application, I know with this code is not possible.
How to design such a code?
package main;
public class Client {
public static void main(String[] args) {
String tag = "<h><e><m></m></e></h>";
TreeParent root = null;
root = parse(tag);
print(root);
}
public static void print(TreeParent root) {
System.out.println(root.getName() + " has " + root.getChildren().length + " children");
TreeObject[] children = root.getChildren();
for (TreeObject treeObject : children) {
if (treeObject instanceof TreeParent) {
TreeParent parent = (TreeParent) treeObject;
print(parent);
}
}
}
public static TreeParent parse(String line) {
TreeParent root = new TreeParent("");
if (line != null) {
StringBuilder sb = new StringBuilder();
TreeParent previsouNode = null;
boolean startNewTag = false;
for (int i = 0; i < line.length(); i++) {
char chars = line.charAt(i);
if (chars == '<') {
// this flag is for grouping purpose.This flag will be false
// when closing tag is found
startNewTag = true;
}
if (startNewTag) {
sb.append(chars);
if (chars == '>') {
String currentNodeName = sb.toString();
// check for closing the tag
String generatedEndTagname = getGeneratedEndTagname(currentNodeName);
// check whether the tag has been closed or not
if (!currentNodeName.equals(generatedEndTagname)) {
// the new node is being added to the previous or
// parent node.
if (previsouNode != null) {
// here if parent node and child is the same
// node name, throws excepetion.
if (currentNodeName.equals(previsouNode.getName())) {
throw new RuntimeException("Parse exception!!");
}
TreeParent treeObject = new TreeParent(currentNodeName);
previsouNode.addChild(treeObject);
previsouNode = treeObject;
} else {
root = new TreeParent(currentNodeName);
previsouNode = root;
}
startNewTag = false;
clear(sb);
} else {// if the tag has been closed, previous node is
// treated as,current nodes,parent will be
// the previous node.
previsouNode = previsouNode.getParent();
clear(sb);
}
}
}
}
}
return root;
}
private static void clear(StringBuilder sb) {
sb.delete(0, sb.length());
}
public static String getGeneratedEndTagname(String tagName) {
if (tagName != null && !tagName.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < tagName.length(); i++) {
if (i == 1 && !(tagName.charAt(i) == '/')) {
sb.append('/');
}
sb.append(tagName.charAt(i));
}
return sb.toString();
}
return "";
}
}
package main;
public class TreeObject {
private String name;
private TreeParent parent;
public TreeObject(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setParent(TreeParent parent) {
this.parent = parent;
}
public TreeParent getParent() {
return parent;
}
public String toString() {
return getName();
}
}
package main;
import java.util.ArrayList;
public class TreeParent extends TreeObject {
private ArrayList<TreeObject> children;
public TreeParent(String name) {
super(name);
children = new ArrayList<TreeObject>();
}
public void addChild(TreeObject child) {
children.add(child);
child.setParent(this);
}
public void removeChild(TreeObject child) {
children.remove(child);
child.setParent(null);
}
public TreeObject[] getChildren() {
return (TreeObject[]) children.toArray(new TreeObject[children.size()]);
}
public boolean hasChildren() {
return children.size() > 0;
}
}
Aucun commentaire:
Enregistrer un commentaire