commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Reid Pinchback <rei...@yahoo.com>
Subject Re: [digester] ref another part of xml
Date Wed, 10 Nov 2004 00:00:31 GMT

I've run into this one before.  It is relatively easy
as long as you are willing to make a simplifying
assumption: a reference can only happen AFTER the real
thing, not before it.  That way you will always
have the real thing recorded for lookup when you
hit the reference.  Things are tougher if you
can't make such an assumption.  In that case you
need a lazy proxy to represent the references,
and after processing all the data you need to
do a second pass to convert the references.

I added a "data" attribute in the example below
that wasn't supplied for the references.  When
the result is printed, you only see data on a
reference if digesting succesfully matched the
reference to the real object.  The output should
be:
  people=Person[sue,x1][Person[bob,x2],
Person[joe,x3][Person[bob,x2]]]

That second "bob,x2" is the result of resolving the
reference.




import java.io.*;
import java.util.*;
import org.apache.commons.digester.*;
import org.xml.sax.*;

public class PersonExample3 {

  static Map repository=new HashMap();
  
  public static class Person {
    String id;
    String data;
    List personList = new ArrayList();
    public void addPerson(Person p){
      personList.add(p);
    }
    public void setId(String id) { this.id=id; }
    public String getId() { return id; }
    public void setData(String data) { this.data=data;
}
    public String getData() { return data; }
    public String toString() { 
      String result="Person["+id+","+data+"]";
      if (personList.size() > 0) { result+=personList;
}
      return result;
    }
  }

  public static class PersonFactory extends
AbstractObjectCreationFactory {
    public Object createObject(Attributes attributes)
throws Exception {
      String id=attributes.getValue("id");
      if (null!=id) {
        Person p=new Person();
        p.id=id;
        p.data=attributes.getValue("data");
        repository.put(id, p);
        return p;
      } 
      String refId=attributes.getValue("ref-id");
      Person p=(Person)repository.get(refId);
      return p;
    }
  }

  public static class Root {
    Person adamOrEve;
    public void addPerson(Person p){
      adamOrEve=p;
    }
  }

  Digester makeConfiguredDigester() {
    Digester digester=new Digester();
    digester.addFactoryCreate("*/person", new
PersonFactory());
    digester.addSetNext("*/person", "addPerson");
    return digester;
  }

  public static void main(String[] args) throws
Exception {
    String input="<root><person id='sue'
data='x1'><person id='bob' data='x2'/><person id='joe'
data='x3'><person
ref-id='bob'/></person></person></root>";
    PersonExample3 ex=new PersonExample3();
    Digester digester=ex.makeConfiguredDigester();
    Root root=new Root();
    digester.push(root);
    digester.parse(new StringReader(input));
    System.out.println("people="+root.adamOrEve);
  }

}


--- Steve Blom <steve_blom2004@yahoo.com> wrote:

> Hi,
> First off, thxs so much for your help Reid! But, yet
> another question :-)  Would it be possible to have a
> reference pointer attribute to grab an object from
> outside that element. Hard to put in words, but
> hopefully you can catch on with the example.
> <person id="Jon">
>  ...
> </person>
> <person id="Sue">
>   <person-ref-id="Jon"/>
>   ...
> </person>
> Now, when when i come across the family-member, I
> want
> to use the person-ref-id to grab the person object
> that is described earlier and set it. Is this
> possible
> with Digester? If so, any restrictions(Jon has to
> come
> before sue in xml). I need to be able to describe a
> person within a person, or have a person-ref that
> points to another person element in the xml. Kinda
> the
> same as
> <person id="Sue">
>   <person id="Jon">
>     ...
>   </person>
> </person>
> Any suggestions on the best way to do this?
> Thanks - Steve
> 
> 
> 		
> __________________________________ 
> Do you Yahoo!? 
> Check out the new Yahoo! Front Page. 
> www.yahoo.com 
>  
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> commons-user-help@jakarta.apache.org
> 
> 



		
__________________________________ 
Do you Yahoo!? 
Check out the new Yahoo! Front Page. 
www.yahoo.com 
 


---------------------------------------------------------------------
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