commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Luke Shannon" <lshan...@futurebrand.com>
Subject Re: Need a little help
Date Tue, 01 Feb 2005 18:05:47 GMT
Continuing with my issue I have simplified things.

I have done away with the Version.class and decided to build my collection
of Item maps within the class that calls the digester methods (a full
description of my code can be seen below). My problem now is using the
addSetNext method to call a local method.

public TreeMap digest() throws IOException, SAXException {
  digester = new Digester();
  digester.setValidating(false);
  digester.addObjectCreate("DATA/VERSION/ITEM", Item.class);
  digester.addSetProperties("DATA/VERSION/ITEM", "name", "name");
  digester.addCallMethod("DATA/VERSION/ITEM", "setContents", 0);
    //declaration for addItem directly below this in the same class
  digester.addSetNext("DATA/VERSION/ITEM", "addItem");
  return fieldsValues;
 }

 public void addItem(Item in) {
     Trace.ENTER("Inserting " + in.getValues().toString() + " in to the main
map");
     fieldsValues.putAll(in.getValues());
 }

The weird thing is addItem doesn't get called and there is no error
reguarding it either (I would have thought one of the two would happen).

Also the name property is not getting set either. I have tried:

digester.addSetProperties("DATA/VERSION/ITEM", "NAME", "name");
digester.addSetProperties("DATA/VERSION/ITEM", "Name", "name");
digester.addSetProperties("DATA/VERSION/ITEM", "name", "name");

Just doesn't get called. Not sure why.

Any advice would be appreciated.

Luke

Schema:

 <VERSION>
   <ITEM NAME="category" TYPE="text">Category 4</ITEM>
   <ITEM NAME="provider" TYPE="text">Provider</ITEM>
   <ITEM NAME="progress_ref" TYPE="text">11008</ITEM>
   <ITEM NAME="name" TYPE="text">Name</ITEM>
   <ITEM NAME="desc" TYPE="text">Description</ITEM>
   <ITEM NAME="poster" TYPE="text">Company Who Posted</ITEM>
   <ITEM NAME="sort" TYPE="text">8</ITEM>
   <ITEM DIR="x.ppt" HEIGHT="-1" NAME="kcfileupload" SIZE="9728"
STYPE="file"
 TYPE="upload" WIDTH="-1">x.ppt</ITEM>
 </VERSION>

Item Class:

import java.util.TreeMap;

/**
 * @author lshannon
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Item {
 private String contents = new String();
 private String name = new String();

 public Item() { }

 /**
  * @param content The content to set.
  */
 public void setName(String name) {
     Trace.ENTER("Setting the name property to " + name);
  this.name = name;
 }

 public void setContents (String _contents) {
     this.contents = _contents;
 }

 public TreeMap getValues () {
     TreeMap map = new TreeMap();
     map.put(name, contents);
     return map;
 }
}




----- Original Message ----- 
From: "Luke Shannon" <lshannon@futurebrand.com>
To: "Jakarta Commons Users List" <commons-user@jakarta.apache.org>; "Kishore
Senji" <ksenji@gmail.com>
Sent: Tuesday, February 01, 2005 10:58 AM
Subject: Re: Need a little help


> Thank you Kishore;
>
> Your suggestions make sense and work.
>
> Now that things are working a little better a flaw has occurred to me. And
I
> am not married to this approach in anyway. If someone sees less complex
way
> of achieving my goal please let me know.
>
> To review, the end result was to get a TreeMap containing all the name
> attributes as keys and the contents of the item tags as values from a
> directory of XML files containing the below schema.
>
> <VERSION>
>   <ITEM NAME="category" TYPE="text">Category 4</ITEM>
>   <ITEM NAME="provider" TYPE="text">Provider</ITEM>
>   <ITEM NAME="progress_ref" TYPE="text">11008</ITEM>
>   <ITEM NAME="name" TYPE="text">Name</ITEM>
>   <ITEM NAME="desc" TYPE="text">Description</ITEM>
>   <ITEM NAME="poster" TYPE="text">Company Who Posted</ITEM>
>   <ITEM NAME="sort" TYPE="text">8</ITEM>
>   <ITEM DIR="x.ppt" HEIGHT="-1" NAME="kcfileupload" SIZE="9728"
STYPE="file"
> TYPE="upload" WIDTH="-1">x.ppt</ITEM>
> </VERSION>
>
> My plan was to have an Item class create a TreeMap for each item tag (key
=
> @NAME, value = contents of the tag).
>
> At the close of each Item tag the TreeMap would be added to a TreeMap in
the
> Version class, which by the time the parser reaches the closing version
tag
> would contain entries for each of the item tags (it would get called for
> each item tag).
>
> Here are the methods below:
>
> At the close of each item tag I need to call the method below in the
Version
> class. In the example above this method would be called 8 times resulting
in
> fieldsValues contain 8 key/value pairings.
>
>  /**
>   * @param items The iTEMS to set.
>   */
>  public void setItems(TreeMap map) {
>   fieldsValues.putAll(map);
>  }
> }
>
> This is the method in the Item class that supplies the argument for the
> setITEMS above. For each item class it would be called once.
>
> /**
>   * @return Returns the iTEMS.
>   */
>  public TreeMap getMap() {
>   return fieldsValues;
>
>  }
>
> Here is my parsing method. After set
> digester.addCallMethod("DATA/VERSION/ITEM", "setContents", 0); I need to
> call the setItems method in version with the getMap method of Item
providing
> the argument. I took a stab at it below, this doesn't even compile, but I
> hope it illustrates what I mean. How can I do this? Is there a better way?
>
> public Version digest() throws IOException, SAXException {
>   digester = new Digester();
>   digester.setValidating(false);
>   digester.addObjectCreate("DATA/VERSION", Version.class);
>   digester.addObjectCreate("DATA/VERSION/ITEM", Item.class);
>   digester.addSetProperties("DATA/VERSION/ITEM", "Name", "name");
>   digester.addCallMethod("DATA/VERSION/ITEM", "setContents", 0);
> //how can i call the method of the version class with the item pattern,
> using an item method for the arguement?
>   digester.addCallMethod("DATA/VERSION/ITEM", "Version.setItems",
"getMap");
>     return (Version)digester.parse(parseMe);
>  }
>
> Thanks,
>
> Luke
>
>
> ----- Original Message ----- 
> From: "Kishore Senji" <ksenji@gmail.com>
> To: "Jakarta Commons Users List" <commons-user@jakarta.apache.org>
> Sent: Monday, January 31, 2005 7:18 PM
> Subject: Re: Need a little help
>
>
> > >
> > > public Version digest() throws IOException, SAXException {
> > >  digester = new Digester();
> > >  digester.setValidating(false);
> > >  digester.addObjectCreate("DATA/VERSION", Version.class);
> > >  digester.addObjectCreate("DATA/VERSION/ITEM", Item.class);
> > >  digester.addSetProperties("DATA/VERSION/ITEM", "name", "name");
> >
> > The above line should be
> > digester.addSetProperties("DATA/VERSION/ITEM", "NAME", "name"); Since
> > the NAME attribute is in capitals, where as the "name" property on
> > Item is in small letters
> >
> > >  digester.addCallMethod("DATA/VERSION/ITEM", "setContents");
> >
> > Digester is not able to find setContents because the above tells
> > digester to look for a method called setContents which takes no
> > arguments. But the setContents on Item takes a String. Since you are
> > looking to get the body content and set it to the contents of the
> > Item, use
> >
> > digester.addCallMethod("DATA/VERSION/ITEM", "setContents", 0);
> >
> > You might also want to add the Item to the Version's map I believe.
> > Since I don't see that call here, I would add it here
> >
> > digester.addSetNext("DATA/VERSION/ITEM", "addItem");
> >
> > Where "addItem" method will live on the Version class which takes an
> > Item as an argument and add it to the TreeMap with the "name" of the
> > "Item" as the key
> >
> > >  return (Version)digester.parse(parseMe);
> > > }
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message