-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Apurv Kiri edited this page Jul 10, 2014
·
11 revisions
Welcome to the BakeParser wiki!
- BakeParser is simple small layer above SAX Parser
- It directly calls your object's setter methods for specified tag names for its contents or attributes
####Building Request
BakeParserRequestBuilder builder = BakeParser.newRequestBuilder("parent");
- Two methods for adding request
builder.addRequest("parent>child",methodsObject, "startTagMethod", "contentMethod", "parametersMethod", "endTagMethod");
builder.addRequest("parent>child",methodsObject, "childObjectGetterMethods", "startTagMethod", "contentMethod", "parametersMethod", "endTagMethod");
-
tagName- follow tag hierarchy asparent>child>child. Parser will not parse if any tag is missed in hierarchy -
methodsObject- Object of which the setter methods will be called -
childObjectGetterMethods- If setter methods are of any sub object's ofmethodsObject, so this method will be used to get sub object and then specified method will be called on that sub object. Explained later in example. -
startTagMethod- called when specified tag is started (in SAX parser), method must not have any parameters, and if using wildcard, oneStringparameter for Tag name. -
contentMethod- called when text content is parsed, method must have only oneStringparameter for Text content in normal case and twoStringparameters for Tag name and Text content for wild card. -
parametersMethod- called when parameters/attributes are available, method must have twoStringparameters for Key and Value. -
endTagMethod- called when tag end is parsed ( ins SAX parser ), same asstartTagMerhod, no parameters in general and oneStringparameter for Tag name if using wildcard.
#####Example 1 : Simple Text Content
- XML Sample
<parent>
<child>Hello World</child>
</parent>
- In
BakeParserRequestBuilderconstructor specify top most tag from which you want to parse
BakeParserRequestBuilder builder = parser.newRequestBuilder("parent");
- Class for given XML
....
Parent parent = new Parent();
....
class Parent
{
public void setChild(String arg)
{
System.out.println(arg);
}
}
- Calling
setChild()for<child>node
builder.addRequest("parent>child", parent, null, "setChild", null, null);
#####Example 2 : Tag parameters/attributes
- XML Sample
<parent version="1">
<child type="zero">Hello</child>
<child type="one">World</child>
</parent>
- Class for given XML
....
Parent parent = new Parent();
....
public class Parent
{
private String version;
private LinkedList<Child> childs = new LinkedList<Child>();
private Child currentChild;
public void setAttr(String key,String value)
{
if(key.equals("version"))
{
this.version = value;
}
}
/*
* Methods for BakeParser
*/
public Child getCurrentChild()
{
if(currentChild==null) currentChild = new Child();
return currentChild;
}
public void addChild()
{
childs.add(currentChild);
currentChild = null;
}
/*
* Child class
*/
public class Child
{
private String text,type;
public void setText(String text)
{
this.text = text;
}
public void setTag(String key,String value)
{
if(key.equals("type")
{
this.type = value;
}
}
}
}
- Adding request
builder.addRequest("parent", parent, null, null, "setAttr", null);
builder.addRequest("parent>child",parent,"getCurrentChild",null,"setText","setTag","<addChild");
you see the difference ? yes <addChild, because addChild method is in parent class, one < is added, for two level add << for more add more <<<<< !
builder.addRequest("parent>child",parent,"getChild>getGrandChild",null,"setText",null,null);
builder.addRequest("parent>child",parent,"getChild",null,"setText","Attr1>method1|Attr2>method2|*>generalMethod",null);
-
method1andmethod2will be called forAttr1="val"andAttr2="val". In this case,method1andmethod2must have only oneStringparameter for value. -
*>methodName - called for any other attributes, where
methodNamemust have twoStringparameters
- In case your attribute's name and method's name have same text in name, such as
attrandsetattr(case sensitive)
builder.addRequest("parent>child",parent,"getChild",null,"setText","attr1>set*|attr2>set*|*>generalMethod",null);
- This will call
setattr1andsetattr2for given attributes
builder.addRequest("parent>*",parent,null,null,"setText",null,null);
- In this case the
setText()will have twoStringparameters for Tag name and Text content
builder.addRequest("parent>child","get*","start*","set*",null,"end*");
- this will replace * with Tag name
childand callgetchild,startchild,setchild,endchild - It is not supported for Attributes
- Some times it is needed to invoke multiple methods, so in that case
builder.addRequest("parent>child",parent,"getChild,invokeMe","startTag,invokeMe,invokeMeToo","setText,invokeMeWithStringParameter",null,"endTag,invokeMeAtEnd");
- As shown above, in case of
childObjectGetterMethod, the return object of very first method is used to call further methods, - To call multiple methods simply separate them with
, - You can still use
<to call parent's methods. - Untested / Unimplemented for attributes
- Also supports method hierarchy for getters
builder.addRequest("parent>child",parent,"getChild>getGrandChild>getGrandGrandChild,invokeMe","startTag,invokeMe,invokeMeToo","setText,invokeMeWithStringParameter",null,"endTag,invokeMeAtEnd");