com.sun.xml.bind.v2.runtime.unmarshaller
Class UnmarshallingContext

java.lang.Object
  extended by com.sun.xml.bind.v2.runtime.Coordinator
      extended by com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext
All Implemented Interfaces:
XmlVisitor, javax.xml.bind.ValidationEventHandler, javax.xml.namespace.NamespaceContext, org.xml.sax.ErrorHandler

public final class UnmarshallingContext
extends Coordinator
implements javax.xml.namespace.NamespaceContext, javax.xml.bind.ValidationEventHandler, org.xml.sax.ErrorHandler, XmlVisitor

Center of the unmarshalling.

This object is responsible for coordinating Loaders to perform the whole unmarshalling.


Nested Class Summary
 class UnmarshallingContext.State
          State information for each element.
 
Field Summary
 UnmarshallerImpl parent
           
 
Constructor Summary
UnmarshallingContext(UnmarshallerImpl _parent, AssociationMap assoc)
          Creates a new unmarshaller.
 
Method Summary
 void addPatcher(Patcher job)
          Adds a job that will be executed at the last of the unmarshalling.
 java.lang.String addToIdTable(java.lang.String id)
          Adds the object which is currently being unmarshalled to the ID table.
 java.lang.Object createInstance(java.lang.Class clazz)
          Creates a new instance of the specified class.
 java.lang.Object createInstance(JaxBeanInfo beanInfo)
          Creates a new instance of the specified class.
 void endDocument()
           
 void endElement(TagName tagName)
           
 void endPrefixMapping(java.lang.String prefix)
          Called after XmlVisitor.endElement(com.sun.xml.bind.v2.runtime.unmarshaller.TagName) event to notify the end of a binding.
 void endScope(int frameSize)
          Ends the current packing scope.
 void errorUnresolvedIDREF(java.lang.Object bean, java.lang.String idref)
          Called when there's no corresponding ID value.
 boolean expectText()
          Returns true if the visitor is expecting a text event as the next event.
 java.lang.String[] getAllDeclaredPrefixes()
          Returns a list of all in-scope prefixes.
 UnmarshallingContext getContext()
          Returns the UnmarshallingContext at the end of the chain.
 UnmarshallingContext.State getCurrentState()
           
 java.lang.Object getInnerPeer()
          Gets the inner peer JAXB object associated with the current element.
static UnmarshallingContext getInstance()
          When called from within the realm of the unmarshaller, this method returns the current UnmarshallingContext in charge.
 JAXBContextImpl getJAXBContext()
           
protected  javax.xml.bind.ValidationEventLocator getLocation()
          Gets the current location.
 LocatorEx getLocator()
          Gets the current source location information in SAX Locator.
 java.lang.String getNamespaceURI(java.lang.String prefix)
           
 java.lang.String[] getNewlyDeclaredPrefixes()
          Returns a list of prefixes newly declared on the current element.
 java.util.concurrent.Callable getObjectFromId(java.lang.String id, java.lang.Class targetType)
          Looks up the ID table and gets associated object.
 java.lang.Object getOuterPeer()
          Gets the outer peer JAXB object associated with the current element.
 java.lang.String getPrefix(java.lang.String uri)
           
 java.util.Iterator<java.lang.String> getPrefixes(java.lang.String uri)
           
 java.lang.Object getResult()
          Gets the result of the unmarshalling
 Scope getScope(int offset)
          Gets the currently active Scope.
 java.lang.String getXMIMEContentType()
          Gets the xmime:contentType value for the current object.
 void handleError(java.lang.Exception e)
          Reports an exception found during the unmarshalling to the user.
 void handleError(java.lang.Exception e, boolean canRecover)
           
 void handleError(java.lang.String msg)
           
 boolean handleEvent(javax.xml.bind.ValidationEvent event)
           
 void handleEvent(javax.xml.bind.ValidationEvent event, boolean canRecover)
          Reports an error to the user, and asks if s/he wants to recover.
 void recordInnerPeer(java.lang.Object innerPeer)
          Notifies the context about the inner peer of the current element.
 void recordOuterPeer(java.lang.Object outerPeer)
          Notifies the context about the outer peer of the current element.
 void reset(InfosetScanner scanner, boolean isInplaceMode, JaxBeanInfo expectedType, IDResolver idResolver)
           
 void setFactories(java.lang.Object factoryInstances)
           
 void startDocument(LocatorEx locator, javax.xml.namespace.NamespaceContext nsContext)
          Notifies a start of the document.
 void startElement(TagName tagName)
          Notifies a start tag of a new element.
 void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
          Called before XmlVisitor.startElement(com.sun.xml.bind.v2.runtime.unmarshaller.TagName) event to notify a new namespace binding.
 void startScope(int frameSize)
          Starts a new packing scope.
 void text(java.lang.CharSequence pcdata)
          Text events.
 
Methods inherited from class com.sun.xml.bind.v2.runtime.Coordinator
_getInstance, containsAdapter, error, fatalError, getAdapter, popCoordinator, pushCoordinator, putAdapter, resetThreadAffinity, setThreadAffinity, warning
 
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.ErrorHandler
error, fatalError, warning
 

Field Detail

parent

public final UnmarshallerImpl parent
Constructor Detail

UnmarshallingContext

public UnmarshallingContext(UnmarshallerImpl _parent,
                            AssociationMap assoc)
Creates a new unmarshaller.

Parameters:
assoc - Must be both non-null when the unmarshaller does the in-place unmarshalling. Otherwise must be both null.
Method Detail

reset

public void reset(InfosetScanner scanner,
                  boolean isInplaceMode,
                  JaxBeanInfo expectedType,
                  IDResolver idResolver)

getJAXBContext

public JAXBContextImpl getJAXBContext()

getCurrentState

public UnmarshallingContext.State getCurrentState()

setFactories

public void setFactories(java.lang.Object factoryInstances)

startDocument

public void startDocument(LocatorEx locator,
                          javax.xml.namespace.NamespaceContext nsContext)
                   throws org.xml.sax.SAXException
Description copied from interface: XmlVisitor
Notifies a start of the document.

Specified by:
startDocument in interface XmlVisitor
Parameters:
locator - This live object returns the location information as the parsing progresses. must not be null.
nsContext - Some broken XML APIs can't iterate all the in-scope namespace bindings, which makes it impossible to emulate XmlVisitor.startPrefixMapping(String, String) correctly when unmarshalling a subtree. Connectors that use such an API can pass in additional NamespaceContext object that knows about the in-scope namespace bindings. Otherwise (and normally) it is null.

Ideally this object should be immutable and only represent the namespace URI bindings in the context (those done above the element that JAXB started unmarshalling), but it can also work even if it changes as the parsing progress (to include namespaces declared on the current element being parsed.)

Throws:
org.xml.sax.SAXException

startElement

public void startElement(TagName tagName)
                  throws org.xml.sax.SAXException
Description copied from interface: XmlVisitor
Notifies a start tag of a new element. namespace URIs and local names must be interned.

Specified by:
startElement in interface XmlVisitor
Throws:
org.xml.sax.SAXException

text

public void text(java.lang.CharSequence pcdata)
          throws org.xml.sax.SAXException
Description copied from interface: XmlVisitor
Text events.

The caller should consult getContext().expectText() to see if the unmarshaller is expecting any PCDATA. If the above is returning false, the caller is OK to skip any text in XML. The net effect is that we can ignore whitespaces quickly.

Specified by:
text in interface XmlVisitor
Parameters:
pcdata - represents character data. This object can be mutable (such as StringBuilder); it only needs to be fixed while this method is executing.
Throws:
org.xml.sax.SAXException

endElement

public final void endElement(TagName tagName)
                      throws org.xml.sax.SAXException
Specified by:
endElement in interface XmlVisitor
Throws:
org.xml.sax.SAXException

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Specified by:
endDocument in interface XmlVisitor
Throws:
org.xml.sax.SAXException

expectText

public boolean expectText()
Returns true if the visitor is expecting a text event as the next event.

This is primarily intended to be used for optimization to avoid buffering characters unnecessarily. If this method returns false and the connector sees whitespace it can safely skip it.

If this method returns true, all the whitespaces are considered significant and thus need to be reported as a text(java.lang.CharSequence) event. Furthermore, if the element has no children (like <foo/>), then it has to be reported an empty text(java.lang.CharSequence) event.


getContext

public UnmarshallingContext getContext()
Description copied from interface: XmlVisitor
Returns the UnmarshallingContext at the end of the chain.

Specified by:
getContext in interface XmlVisitor
Returns:
always return the same object, so caching the result is recommended.

getResult

public java.lang.Object getResult()
                           throws javax.xml.bind.UnmarshalException
Gets the result of the unmarshalling

Throws:
javax.xml.bind.UnmarshalException

createInstance

public java.lang.Object createInstance(java.lang.Class clazz)
                                throws org.xml.sax.SAXException
Creates a new instance of the specified class. In the unmarshaller, we need to check the user-specified factory class.

Throws:
org.xml.sax.SAXException

createInstance

public java.lang.Object createInstance(JaxBeanInfo beanInfo)
                                throws org.xml.sax.SAXException
Creates a new instance of the specified class. In the unmarshaller, we need to check the user-specified factory class.

Throws:
org.xml.sax.SAXException

handleEvent

public void handleEvent(javax.xml.bind.ValidationEvent event,
                        boolean canRecover)
                 throws org.xml.sax.SAXException
Reports an error to the user, and asks if s/he wants to recover. If the canRecover flag is false, regardless of the client instruction, an exception will be thrown. Only if the flag is true and the user wants to recover from an error, the method returns normally. The thrown exception will be catched by the unmarshaller.

Throws:
org.xml.sax.SAXException

handleEvent

public boolean handleEvent(javax.xml.bind.ValidationEvent event)
Specified by:
handleEvent in interface javax.xml.bind.ValidationEventHandler

handleError

public void handleError(java.lang.Exception e)
                 throws org.xml.sax.SAXException
Reports an exception found during the unmarshalling to the user. This method is a convenience method that calls into handleEvent(ValidationEvent, boolean)

Throws:
org.xml.sax.SAXException

handleError

public void handleError(java.lang.Exception e,
                        boolean canRecover)
                 throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

handleError

public void handleError(java.lang.String msg)

getLocation

protected javax.xml.bind.ValidationEventLocator getLocation()
Description copied from class: Coordinator
Gets the current location. Used for reporting the error source location.

Specified by:
getLocation in class Coordinator

getLocator

public LocatorEx getLocator()
Gets the current source location information in SAX Locator.

Sometimes the unmarshaller works against a different kind of XML source, making this information meaningless.


errorUnresolvedIDREF

public void errorUnresolvedIDREF(java.lang.Object bean,
                                 java.lang.String idref)
                          throws org.xml.sax.SAXException
Called when there's no corresponding ID value.

Throws:
org.xml.sax.SAXException

addPatcher

public void addPatcher(Patcher job)
Adds a job that will be executed at the last of the unmarshalling. This method is used to support ID/IDREF feature, but it can be used for other purposes as well.

Parameters:
job - The run method of this object is called.

addToIdTable

public java.lang.String addToIdTable(java.lang.String id)
                              throws org.xml.sax.SAXException
Adds the object which is currently being unmarshalled to the ID table.

Returns:
Returns the value passed as the parameter. This is a hack, but this makes it easier for ID transducer to do its job.
Throws:
org.xml.sax.SAXException

getObjectFromId

public java.util.concurrent.Callable getObjectFromId(java.lang.String id,
                                                     java.lang.Class targetType)
                                              throws org.xml.sax.SAXException
Looks up the ID table and gets associated object.

The exception thrown from Callable.call() means the unmarshaller should abort right away.

Throws:
org.xml.sax.SAXException
See Also:
IDResolver.resolve(String, Class)

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String uri)
Description copied from interface: XmlVisitor
Called before XmlVisitor.startElement(com.sun.xml.bind.v2.runtime.unmarshaller.TagName) event to notify a new namespace binding.

Specified by:
startPrefixMapping in interface XmlVisitor

endPrefixMapping

public void endPrefixMapping(java.lang.String prefix)
Description copied from interface: XmlVisitor
Called after XmlVisitor.endElement(com.sun.xml.bind.v2.runtime.unmarshaller.TagName) event to notify the end of a binding.

Specified by:
endPrefixMapping in interface XmlVisitor

getNewlyDeclaredPrefixes

public java.lang.String[] getNewlyDeclaredPrefixes()
Returns a list of prefixes newly declared on the current element.

Returns:
A possible zero-length array of prefixes. The default prefix is represented by the empty string.

getAllDeclaredPrefixes

public java.lang.String[] getAllDeclaredPrefixes()
Returns a list of all in-scope prefixes.

Returns:
A possible zero-length array of prefixes. The default prefix is represented by the empty string.

getPrefixes

public java.util.Iterator<java.lang.String> getPrefixes(java.lang.String uri)
Specified by:
getPrefixes in interface javax.xml.namespace.NamespaceContext

getPrefix

public java.lang.String getPrefix(java.lang.String uri)
Specified by:
getPrefix in interface javax.xml.namespace.NamespaceContext

getNamespaceURI

public java.lang.String getNamespaceURI(java.lang.String prefix)
Specified by:
getNamespaceURI in interface javax.xml.namespace.NamespaceContext

startScope

public void startScope(int frameSize)
Starts a new packing scope.

This method allocates a specified number of fresh Scope objects. They can be accessed by the getScope(int) method until the corresponding endScope(int) method is invoked.

A new scope will mask the currently active scope. Only one frame of Scopes can be accessed at any given time.

Parameters:
frameSize - The # of slots to be allocated.

endScope

public void endScope(int frameSize)
              throws org.xml.sax.SAXException
Ends the current packing scope.

If any packing in progress will be finalized by this method.

Parameters:
frameSize - The same size that gets passed to the startScope(int) method.
Throws:
org.xml.sax.SAXException

getScope

public Scope getScope(int offset)
Gets the currently active Scope.

Parameters:
offset - a number between [0,frameSize)
Returns:
always a valid Scope object.

recordInnerPeer

public void recordInnerPeer(java.lang.Object innerPeer)
Notifies the context about the inner peer of the current element.

If the unmarshalling is building the association, the context will use this information. Otherwise it will be just ignored.


getInnerPeer

public java.lang.Object getInnerPeer()
Gets the inner peer JAXB object associated with the current element.

Returns:
null if the current element doesn't have an inner peer, or if we are not doing the in-place unmarshalling.

recordOuterPeer

public void recordOuterPeer(java.lang.Object outerPeer)
Notifies the context about the outer peer of the current element.

If the unmarshalling is building the association, the context will use this information. Otherwise it will be just ignored.


getOuterPeer

public java.lang.Object getOuterPeer()
Gets the outer peer JAXB object associated with the current element.

Returns:
null if the current element doesn't have an inner peer, or if we are not doing the in-place unmarshalling.

getXMIMEContentType

public java.lang.String getXMIMEContentType()
Gets the xmime:contentType value for the current object.

See Also:
JAXBContextImpl.getXMIMEContentType(Object)

getInstance

public static UnmarshallingContext getInstance()
When called from within the realm of the unmarshaller, this method returns the current UnmarshallingContext in charge.