Skip to content
Apurv Kiri edited this page Jul 10, 2014 · 11 revisions

Welcome to the BakeParser wiki!

About

  • 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

Working

####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 as parent>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 of methodsObject, 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, one String parameter for Tag name.
  • contentMethod - called when text content is parsed, method must have only one String parameter for Text content in normal case and two String parameters for Tag name and Text content for wild card.
  • parametersMethod - called when parameters/attributes are available, method must have two String parameters for Key and Value.
  • endTagMethod - called when tag end is parsed ( ins SAX parser ), same as startTagMerhod, no parameters in general and one String parameter for Tag name if using wildcard.

#####Example 1 : Simple Text Content

  • XML Sample
<parent>
     <child>Hello World</child>
</parent>
  • In BakeParserRequestBuilder constructor 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 <<<<< !

More examples soon.....

Advance stuff

Support multiple childObjectGetterMethods

builder.addRequest("parent>child",parent,"getChild>getGrandChild",null,"setText",null,null);

Support methods for specific attributes

builder.addRequest("parent>child",parent,"getChild",null,"setText","Attr1>method1|Attr2>method2|*>generalMethod",null);
  • method1 and method2 will be called for Attr1="val" and Attr2="val". In this case, method1 and method2 must have only one String parameter for value.

  • *>methodName - called for any other attributes, where methodName must have two String parameters

Support wildcard for attribute names

  • In case your attribute's name and method's name have same text in name, such as attr and setattr (case sensitive)
builder.addRequest("parent>child",parent,"getChild",null,"setText","attr1>set*|attr2>set*|*>generalMethod",null);
  • This will call setattr1 and setattr2 for given attributes

Support wildcard first level child nodes

builder.addRequest("parent>*",parent,null,null,"setText",null,null);
  • In this case the setText() will have two String parameters for Tag name and Text content

Support wildcard method names for static Tag names

builder.addRequest("parent>child","get*","start*","set*",null,"end*");
  • this will replace * with Tag name child and call getchild, startchild, setchild, endchild
  • It is not supported for Attributes

Support multiple methods invocation

  • 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");