de.ispsoft.jaxme
Class ObservedHandlerImpl

java.lang.Object
  extended byde.ispsoft.jaxme.ObservedHandlerImpl
All Implemented Interfaces:
org.xml.sax.ContentHandler, ObservedHandler
Direct Known Subclasses:
JMAtomicContentHandler, JMContentHandler

public abstract class ObservedHandlerImpl
extends java.lang.Object
implements ObservedHandler

A default implementation for an ObservedHandler.

Author:
Jochen Wiedmann

Field Summary
protected  java.lang.String context
           
protected  int level
           
protected  org.xml.sax.Locator locator
           
protected  Observer observer
           
protected  java.util.Collection resultCollection
           
protected  java.lang.Object resultNode
           
 
Constructor Summary
ObservedHandlerImpl()
          Creates a new instance of ObservedHandlerImpl.
 
Method Summary
protected  void addResultNode(java.lang.Object o)
          Sets a result node, the result of calling getResultNode().
 void endDocument()
          Called to terminate the document.
 java.lang.String getContext()
          Returns the context to use in error messages.
 java.lang.String getContext(java.lang.String pName)
          Returns the context to use in error messages, extended by the given element or attribute name.
 Observer getObserver()
          Returns a possible observer.
 java.util.Collection getResultCollection()
          Returns a collection where to add successfully parsed elements, Strings or other JMNode instances.
 java.lang.Object getResultNode()
          This method may be called after a startDocument(), ..., endDocument() sequence.
protected  org.xml.sax.SAXException getSAXException(java.lang.String msg)
          Depending on the presence of a locator, returns a SAXException or a SAXParseException.
protected  org.xml.sax.SAXException getSAXException(java.lang.String msg, java.lang.Exception e)
          Depending on the presence of a locator, returns a SAXException or a SAXParseException.
 void setContext(java.lang.String pContext)
          Sets a context to use in error messages.
 void setDocumentLocator(org.xml.sax.Locator pLocator)
          Sets a document locator.
 void setObserver(Observer pObserver)
          Sets a possible observer.
 void setResultCollection(java.util.Collection pCollection)
          Sets a collection where to add successfully parsed elements, Strings or other JMNode instances.
 void startDocument()
          This method is for resetting the ObservedHandlerImpl.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.xml.sax.ContentHandler
characters, endElement, endPrefixMapping, ignorableWhitespace, processingInstruction, skippedEntity, startElement, startPrefixMapping
 

Field Detail

resultNode

protected java.lang.Object resultNode

resultCollection

protected java.util.Collection resultCollection

observer

protected Observer observer

locator

protected org.xml.sax.Locator locator

context

protected java.lang.String context

level

protected int level
Constructor Detail

ObservedHandlerImpl

public ObservedHandlerImpl()
Creates a new instance of ObservedHandlerImpl.

Method Detail

getObserver

public Observer getObserver()

Returns a possible observer. If the observer is non-null, then it will be notified for any element, String or JMNode that has been read successfully.

Specified by:
getObserver in interface ObservedHandler
See Also:
setObserver(de.ispsoft.jaxme.Observer)

setObserver

public void setObserver(Observer pObserver)

Sets a possible observer. If the observer is non-null, then it will be notified for any element, String or JMNode that has been read successfully. The parsed element can be accessed as follows:

  public void notify(Object o) {
    ObservedHandler handler = (ObservedHandler) o;
    Object o = handler.getResultNode();
    if (o instanceof JMAnyElement) {
      JMAnyElement element = (JMAnyElement) o;
      ... // Do something here
    } else {
      ... // Other possibilities: String, JMPI, JMEntity
    }
  }
 

Specified by:
setObserver in interface ObservedHandler
Parameters:
pObserver - The observer being notified or null to disable notification.
See Also:
getObserver()

getResultNode

public java.lang.Object getResultNode()

This method may be called after a startDocument(), ..., endDocument() sequence. It returns the result object, which may be either of

Specified by:
getResultNode in interface ObservedHandler

setResultCollection

public void setResultCollection(java.util.Collection pCollection)

Sets a collection where to add successfully parsed elements, Strings or other JMNode instances. The collection can be processed if the parsing is done completely.

This collection is relevant only, if you expect to parse multiple documents in a sequence. For example, you might read a SOAP reply where the actual result consists of a single root node and a lot of childs.

If you expect a single result element only, you might prefer using the getResultNode() method.

The collection can be processed as follows:

   for (java.util.Iterator iter = handler.getResultCollection();
        iter.hasNext();  ) {
     Object o = iter.next();
     if (o instanceof JMAnyElement) {
       JMAnyElement element = (JMAnyElement) o;
       ... // Do something here
     } else {
       ... // Other possibilities: String, JMPI or JMEntity
     }
   }
 

Specified by:
setResultCollection in interface ObservedHandler
See Also:
getResultCollection()

getResultCollection

public java.util.Collection getResultCollection()

Returns a collection where to add successfully parsed elements, Strings or other JMNode instances. The collection can be processed if the parsing is done completely.

This collection is relevant only, if you expect to parse multiple documents in a sequence. For example, you might read a SOAP reply where the actual result consists of a single root node and a lot of childs.

If you expect a single result element only, you might prefer using the getResultNode() method.

Specified by:
getResultCollection in interface ObservedHandler
See Also:
setResultCollection(java.util.Collection)

addResultNode

protected void addResultNode(java.lang.Object o)
                      throws org.xml.sax.SAXException

Sets a result node, the result of calling getResultNode(). If the result collection is non-null, then the result node will be added to the collection. If the observer is non-null, then it will be notified.

Throws:
org.xml.sax.SAXException

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator pLocator)

Sets a document locator.

Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler

getSAXException

protected org.xml.sax.SAXException getSAXException(java.lang.String msg)

Depending on the presence of a locator, returns a SAXException or a SAXParseException.


getSAXException

protected org.xml.sax.SAXException getSAXException(java.lang.String msg,
                                                   java.lang.Exception e)

Depending on the presence of a locator, returns a SAXException or a SAXParseException.


startDocument

public void startDocument()
                   throws org.xml.sax.SAXException

This method is for resetting the ObservedHandlerImpl. Concrete subclasses should call this from within their own startDocument() methods.

Specified by:
startDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException

Called to terminate the document. Does nothing.

Specified by:
endDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

setContext

public void setContext(java.lang.String pContext)

Sets a context to use in error messages.

Specified by:
setContext in interface ObservedHandler
Parameters:
pContext - or null for top level elements.

getContext

public java.lang.String getContext()

Returns the context to use in error messages.

Specified by:
getContext in interface ObservedHandler
Returns:
Current context or null for top level elements.

getContext

public java.lang.String getContext(java.lang.String pName)

Returns the context to use in error messages, extended by the given element or attribute name.

Example:

  String context = "/Address/Name";
  setContext(context);
  getContext("FirstName");  // Returns "/Address/Name/FirstName"
 

Specified by:
getContext in interface ObservedHandler