Return-Path: Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 99319 invoked from network); 11 Feb 2003 00:57:44 -0000 Received: from mail8.kc.rr.com (HELO mail8.mn.rr.com) (24.94.162.176) by daedalus.apache.org with SMTP; 11 Feb 2003 00:57:44 -0000 Received: from knobboy.mn.rr.com ([65.30.229.163]) by mail8.mn.rr.com with Microsoft SMTPSVC(5.5.1877.757.75); Mon, 10 Feb 2003 18:56:39 -0600 Message-Id: <5.2.0.9.0.20030210185622.00a73cd8@pop-server.mn.rr.com> X-Sender: pgarvie@pop-server.mn.rr.com X-Mailer: QUALCOMM Windows Eudora Version 5.2.0.9 Date: Mon, 10 Feb 2003 18:57:50 -0600 To: "Jakarta Commons Users List" From: pgarvie Subject: Re: [Digester]How do you populate a map? In-Reply-To: <6143B7F7EAA5D411B51E0090278878F6CF1CE7@sacexc01.ti.transam erica.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N Embarrassed? Awww man forget it. Your example was well worth it and then some. Base on it, here's one way: public void testLibraryBook() throws SAXException, IOException { Digester digester = new Digester(); ClassLoader classLoader = PersistenceHandlerFactory.class.getClassLoader(); URL resource = classLoader.getResource("library.xml"); assertNotNull("Resource should not be null!!", resource); digester.addObjectCreate("Library", HashMap.class); digester.addCallMethod("Library/Book", "put", 2); digester.addRule("Library/Book", new StringObjectCreateRule(String.class, "title")); digester.addCallParam("Library/Book", 0, true); digester.addRule("Library/Book", new LibraryBookCreateRule(LibraryBook.class, "title")); digester.addCallParam("Library/Book", 1, true); HashMap map = (HashMap)digester.parse(resource.toString()); assertNotNull("Hash map should not be null!!", map); assertEquals("Based on the xml file, map should have size of 6", 6, map.size()); } public class LibraryBookCreateRule extends ObjectCreateRule { private String key; public LibraryBookCreateRule(Class aClass, String key) { super(aClass); this.key = key; } public void begin(String s, String s1, Attributes attributes) throws Exception { LibraryBook book = new LibraryBook(attributes.getValue(key)); super.getDigester().push(book); } } public class LibraryBook { private String title; public LibraryBook(String title) { this.title = title; } public String getTitle() { return title; } } Using library.xml: At 03:04 PM 2/10/2003 -0800, you wrote: >I'm really embarrassed after posting a HowTo on populating a map, but I've >found that it's not as clear as I thought it was. I know how to do it with >the following example: > > > > > > > >But how to do it with this? > > > > > > > > > >CallMethodRule is only invoked at the end of , so you can't call it >twice, once for each key, value pair. >The situation I have that I actually need to solve looks more like this: > > > > > > >I would want Library to be a HashMap with titles for keys and Book objects >for values. You can't use a CallMethodRule on Book because the put() method >belongs to Library, not Book. > >Help? > >BTW, I realize you can do this by creating proxy objects that call put() for >you. That's inelegant IMHO and I was hoping that Digester 1.4 had solved >this issue. > >K.C.