SASAX API - 1.5

jp.ne.dti.lares.foozy.sasax
Class AbstractElement

java.lang.Object
  |
  +--jp.ne.dti.lares.foozy.sasax.AbstractElement
All Implemented Interfaces:
Element
Direct Known Subclasses:
CompositeElement, DormantElement, ElementChoice, ElementRepetition, ElementSet, ElementWrapper, IgnoreElement, ValueElement

public abstract class AbstractElement
extends java.lang.Object
implements Element

Abstract implementation of Element interface.

This provides basic functionalities for Element implementations.

This is designed for "simple content"/"complex content" models of XML, not for "mixed content" model of it even though this may be also good for the later unexpectedly.

What you should do to define class(es) derived from this correctly are not only concretization of methods shown below:

but also invocation of below methods at appropriate places.

Author:
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>(code/doc)

Constructor Summary
AbstractElement(Element parent)
          Constructor.
 
Method Summary
abstract  boolean accepts(ParseContext context, java.lang.String uri, java.lang.String lName)
          Examine whether specified name is acceptable or not.
 void addCleanup(java.lang.Runnable cleanup)
          Add custom clean up procedure as Runnable.
 void addNotification(Notification notification)
          Add custom notification procedure.
 Element characters(ParseContext context, char[] chars, int offset, int length)
          Receive notification of character data.
 void clear()
          Reset state to re-use.
abstract  Element endElement(ParseContext context, java.lang.String uri, java.lang.String lName)
          Receive notification of the end of an element.
protected  void ensureDetermined()
          Ensure content determination.
protected  void fireElementStarted(ParseContext context, org.xml.sax.Attributes attrs)
          This notifies start of element.
 java.lang.String getAttribute(java.lang.String uri, java.lang.String lName)
          Get found attribute.
 Element getParent()
          Get parent element of this.
 boolean hasNotification(Notification notification)
          Remove custom notification procedure.
static boolean hasXSINil(org.xml.sax.Attributes attrs)
          Examine whether there is xsi:nil="true" in specified attribute.
 Element ignorableWhitespace(ParseContext context, char[] chars, int offset, int length)
          Receive notification of ignorable whitespace in element content.
 boolean isDetermined()
          Examine content of element is determined or not.
protected  void notifyDetermined()
          Deprecated. by notifyDetermined(ParseContext)
protected  void notifyDetermined(ParseContext context)
          Notify determination of element content.
 Element processingInstruction(ParseContext context, java.lang.String target, java.lang.String data)
          Receive notification of a processing instruction.
 void removeNotification(Notification notification)
          Remove custom notification procedure.
protected  void setDetermined(ParseContext context)
          Set internal status as "determined".
 Element skippedEntity(ParseContext context, java.lang.String name)
          Receive notification of a skipped entity.
abstract  Element startElement(ParseContext context, java.lang.String uri, java.lang.String lName, org.xml.sax.Attributes attrs)
          Receive notification of the beginning of an element.
 void unwatchAttribute(java.lang.String uri, java.lang.String lName)
          Stop to watch specified attribute.
 void watchAttribute(java.lang.String uri, java.lang.String lName)
          Start to watch specified attribute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractElement

public AbstractElement(Element parent)
Constructor.
Parameters:
parent - of this element.
Method Detail

addCleanup

public final void addCleanup(java.lang.Runnable cleanup)
Add custom clean up procedure as Runnable.

This method adds specified Runnable instance to cleanup procedure list, and invokes them at invocation of own clear() method.

This allows you to extend cleanup procedure unless method overriding with invocation of "super.cleanup()".

For example:

 public class CustomElement
     extends AbstractElement // or other derived class
 {
     final private Runnable CLEANUP = new Runnable(){
         public void run(){
             // custom clean up procedure
         }
     };

     // Constructor
     pubilc CustomElement(Element parent){
           :
           :
         addCleanup(CLEANUP);
         CLEANUP.run();
     }
 }
 

In above example, "CLEANUP.run()" is invoked instead of clear(), because the later causes invocation of all registrated cleanup procedures but only custom class local cleanup is required in this situation (base class cleanup procedures should be invoked in their own constructors.).

Parameters:
cleanup - procedure as Runnable.

addNotification

public final void addNotification(Notification notification)
Add custom notification procedure.

This method adds specified Notification instance to notification procedure list, and they will be invoked by classes derived from this.

This allows you to extend target (instance) fuctionality unless defining class nor method overriding with invocation of one of "super".

Parameters:
notification - encapsulated procedure.
Since:
SASAX 1.1
See Also:
removeNotification(jp.ne.dti.lares.foozy.sasax.Notification), hasNotification(jp.ne.dti.lares.foozy.sasax.Notification)

removeNotification

public final void removeNotification(Notification notification)
Remove custom notification procedure.
Since:
SASAX 1.5
See Also:
addNotification(jp.ne.dti.lares.foozy.sasax.Notification), hasNotification(jp.ne.dti.lares.foozy.sasax.Notification)

hasNotification

public final boolean hasNotification(Notification notification)
Remove custom notification procedure.
Since:
SASAX 1.5
See Also:
addNotification(jp.ne.dti.lares.foozy.sasax.Notification), removeNotification(jp.ne.dti.lares.foozy.sasax.Notification)

watchAttribute

public final void watchAttribute(java.lang.String uri,
                                 java.lang.String lName)
Start to watch specified attribute.

This tells target element to watch specified attribute.

By invocation of this, you can get attribute value (which is specified at startElement()) by getAttribute() after each element determination.

Parameters:
uri - namespace, of watch target attribute
lName - of watch target attribute
Since:
SASAX 1.5
See Also:
unwatchAttribute(java.lang.String, java.lang.String)

unwatchAttribute

public final void unwatchAttribute(java.lang.String uri,
                                   java.lang.String lName)
Stop to watch specified attribute.
Parameters:
uri - namespace, of watch target attribute
lName - of watch target attribute
Since:
SASAX 1.5
See Also:
watchAttribute(java.lang.String, java.lang.String)

getAttribute

public final java.lang.String getAttribute(java.lang.String uri,
                                           java.lang.String lName)
Get found attribute.

You can get value of specified attribute, if:

In other words, returned value may be null, if:

Attribute values are held from startElement() until clear() invocation on corresponded element. So, you can get appropriate value in elementEnded notification.

Parameters:
uri - namespace, of target attribute
lName - of target attribute
Returns:
value of attribute, or null
Since:
SASAX 1.5

hasXSINil

public static boolean hasXSINil(org.xml.sax.Attributes attrs)
Examine whether there is xsi:nil="true" in specified attribute.

In this explanation, xsi prefix means "http://www.w3.org/2001/XMLSchema-instance", but this implementation does not require that xsi prefix is used for http://www.w3.org/2001/XMLSchema-instance namespace.

Parameters:
attrs - to be looked up in
Since:
SASAX 1.5

setDetermined

protected final void setDetermined(ParseContext context)
                            throws org.xml.sax.SAXException
Set internal status as "determined".

This sets internal status as determined, then, invokes notifyDetermined(ParseContext) and Notification#elementEnded() of registrated ones to notify determination.

Parameters:
context - of SAX document parsing.
Throws:
org.xml.sax.SAXException - by notifyDetermined(ParseContext)

ensureDetermined

protected final void ensureDetermined()
Ensure content determination.
Throws:
IllegalStateExcepion - if content is not determined.

notifyDetermined

protected void notifyDetermined(ParseContext context)
                         throws org.xml.sax.SAXException
Notify determination of element content.

This method is invoked after determination of element content, which is notified by invocation of setDetermined(ParseContext), and invokes notifyDetermined() in default for backward compatibility.

You can customize behavior to override this in derived class, but should implement your custom logic as Notification, if you want re-use it.

Throws:
org.xml.sax.SAXException - if parsing should not be continued.
Since:
SASAX 1.1
See Also:
addNotification(Notification)

notifyDetermined

protected void notifyDetermined()
                         throws org.xml.sax.SAXException
Deprecated. by notifyDetermined(ParseContext)

Notify determination of element content.

This method is invoked from notifyDetermined(ParseContext), and does nothing in default. You should override it instead of this.

Throws:
org.xml.sax.SAXException - if parsing should not be continued.

fireElementStarted

protected final void fireElementStarted(ParseContext context,
                                        org.xml.sax.Attributes attrs)
                                 throws org.xml.sax.SAXException
This notifies start of element.

This invokes Notification#elementStarted of registrated ones.

Invocation of Notification#elementEnded() is done in setDetermined(ParseContext) automaticaly, but you should invoke this manualy in your custom class which is derived from this.

Parameters:
context - of SAX document parsing.
attrs - given by "startElement" SAX event.
Throws:
org.xml.sax.SAXException - by Notification

clear

public final void clear()
Reset state to re-use.

This invokes registrated procedures(as Runnable) one by one.

Specified by:
clear in interface Element
See Also:
addCleanup(Runnable)

getParent

public final Element getParent()
Get parent element of this.

This returns the element specified at construction.

Specified by:
getParent in interface Element
Returns:
parent element of this.

accepts

public abstract boolean accepts(ParseContext context,
                                java.lang.String uri,
                                java.lang.String lName)
Examine whether specified name is acceptable or not.

This does not examine whether specified name is acceptable or not at invocation time but in initial condition.

Specified by:
accepts in interface Element
Parameters:
context - of SAX document parsing.
uri - given by "startElement" SAX event.
lName - given by "startElement" SAX event.
Returns:
whether specified name is acceptable or not.
See Also:
Element.accepts(ParseContext, String, String)

startElement

public abstract Element startElement(ParseContext context,
                                     java.lang.String uri,
                                     java.lang.String lName,
                                     org.xml.sax.Attributes attrs)
                              throws org.xml.sax.SAXException
Receive notification of the beginning of an element.
Specified by:
startElement in interface Element
Parameters:
context - of SAX document parsing.
uri - given by "startElement" SAX event.
lName - given by "startElement" SAX event.
attrs - given by "startElement" SAX event.
Returns:
element to handle next event.
Throws:
org.xml.sax.SAXException - if parsing should not be continued.
See Also:
Element.startElement(ParseContext, String, String, Attributes)

endElement

public abstract Element endElement(ParseContext context,
                                   java.lang.String uri,
                                   java.lang.String lName)
                            throws org.xml.sax.SAXException
Receive notification of the end of an element.
Specified by:
endElement in interface Element
Parameters:
context - of SAX document parsing.
uri - given by "endElement" SAX event.
lName - given by "endElement" SAX event.
Returns:
element to handle next event.
Throws:
org.xml.sax.SAXException - if parsing should not be continued.
See Also:
Element.endElement(ParseContext, String, String)

characters

public Element characters(ParseContext context,
                          char[] chars,
                          int offset,
                          int length)
                   throws org.xml.sax.SAXException
Receive notification of character data.

This implementation throws SAXException (SAXNotRecognizedException in fact) always.

Specified by:
characters in interface Element
Parameters:
context - of SAX document parsing.
chars - given by "characters" SAX event
offset - given by "characters" SAX event
length - given by "characters" SAX event
Returns:
element to handle next event.
Throws:
org.xml.sax.SAXException - if parsing should not be continued.
See Also:
Element.characters(ParseContext, char[], int, int)

ignorableWhitespace

public Element ignorableWhitespace(ParseContext context,
                                   char[] chars,
                                   int offset,
                                   int length)
                            throws org.xml.sax.SAXException
Receive notification of ignorable whitespace in element content.
Specified by:
ignorableWhitespace in interface Element
Parameters:
context - of SAX document parsing.
chars - given by "ignorableWhitespace" SAX event
offset - given by "ignorableWhitespace" SAX event
length - given by "ignorableWhitespace" SAX event
Returns:
element to handle next event.
Throws:
org.xml.sax.SAXException - if parsing should not be continued.
See Also:
Element.ignorableWhitespace(ParseContext, char[], int, int)

processingInstruction

public Element processingInstruction(ParseContext context,
                                     java.lang.String target,
                                     java.lang.String data)
                              throws org.xml.sax.SAXException
Receive notification of a processing instruction.
Specified by:
processingInstruction in interface Element
Parameters:
context - of SAX document parsing.
target - given by "processingInstruction" SAX event
data - given by "processingInstruction" SAX event
Returns:
element to handle next event.
Throws:
org.xml.sax.SAXException - if parsing should not be continued.
See Also:
Element.processingInstruction(ParseContext, String, String)

skippedEntity

public Element skippedEntity(ParseContext context,
                             java.lang.String name)
                      throws org.xml.sax.SAXException
Receive notification of a skipped entity.
Specified by:
skippedEntity in interface Element
Parameters:
context - of SAX document parsing.
name - given by "skippedEntity" SAX event
Returns:
element to handle next event.
Throws:
org.xml.sax.SAXException - if parsing should not be continued.
See Also:
Element.skippedEntity(ParseContext, String)

isDetermined

public boolean isDetermined()
Examine content of element is determined or not.

This returns whether setDetermined(ParseContext) is invoked after last clear() invocation or not.

Specified by:
isDetermined in interface Element
Returns:
true if content is determined.

SASAX API - 1.5