net.sf.stax
Interface IdListener


public interface IdListener

The callback interface for those interested in ids.

An IdMapper may have any number of IdListeners registered. As ids become associated with objects during parsing, the IdMapper will call idRegistered(Object, Object) on all listeners interested in the id.

There is no reason to believe that the IdListener will be informed at the earliest possible time that the id is known, but it will always be informed before the StAX stream has been fully processed.

For example, given an xml document

 <?xml version="1.0" ?>
 <foo>
   <bar_ref id="0" />

   <bar id="0" />
   <bar id="1" />

   <bar_ref id="1" />
 <foo>
 
You can handle normal elements (bar) and reference elements (bar_ref) with
 class FooHandler
     extends StAXContentHandlerBase
 {
     private List results = new ArrayList();

     public void startElement(..., StAXDelegationContext dctx)
     {
         if ("bar".equals(qName))
         {
             dctx.delegate(barHandler);
         }
         else if ("bar_ref".equals(qName))
         {
             dctx.delegate(barRefHandler);
         }
     }

     class BarHandler
         extends StAXContentHandlerBase
     {
         private Bar bar;

         public void startTree(StAXContext ctx) { bar = new Bar(); }

         public void startElement(..., StAXDelegationContext dctx)
         {
             if ("bar".equals(qName))
             {
                 // ...
                 IdMapper mapper = dctx.getIdMapper();
                 mapper.registerItemForId(attrs.getValue("id"), bar);
             }
         }

         public void endElement(...)
         {
             if ("bar".equals(qName))
                 results.add(bar);
         }
     }

     class BarRefHandler
         extends StAXContentHandlerBase
     {
         private Bar bar;

         public void startElement(..., StAXDelegationContext dctx)
         {
             if ("bar".equals(qName))
             {
                 final String barRefId = attrs.getValue("id");

                 IdMapper mapper = dctx.getIdMapper();
                 mapper.addIdListener(new IdListener()
                     {
                         public void idRegistered(Object id, Object item)
                         {
                             if ((barRefId.equals(id)) && (item instanceof Bar))
                             {
                                 bar = (Bar) item;
                                 results.add(bar);
                             }
                         }
                     });
             }
         }
     }
 }
 

Version:
$Revision: 1.2 $ $Date: 2006/01/02 20:37:34 $
Author:
Matthew Pocock, Michael Heuer

Method Summary
 void idRegistered(Object id, Object item)
          Notify this listener that the specified id has been associated with the specified item.
 

Method Detail

idRegistered

void idRegistered(Object id,
                  Object item)
                  throws SAXException
Notify this listener that the specified id has been associated with the specified item.

Parameters:
id - the id of the bound object
item - the item that has been registered for this id
Throws:
SAXException - any SAX exception, possibly wrapping another exception


Copyright (c) 2001-2006 held jointly by the individual authors. Licensed under the GNU Lesser General Public License (LGPL).