}
}
try {
FieldHandler handler = cdesc.getHandler();
boolean addObject = true;
if (_reuseObjects) {
//-- check to see if we need to
//-- add the object or not
Object tmp = handler.getValue(state.object);
if (tmp != null) {
//-- Do not add object if values
//-- are equal
addObject = (!tmp.equals(value));
}
}
if (addObject) handler.setValue(state.object, value);
}
catch(java.lang.IllegalStateException ise) {
String err = "unable to add text content to ";
err += descriptor.getXMLName();
err += " due to the following error: " + ise;
throw new SAXException(err);
}
}
//-- Handle references
else if (descriptor.isReference()) {
UnmarshalState pState = (UnmarshalState)_stateInfo.peek();
processIDREF(state.buffer.toString(), descriptor, pState.object);
_namespaces = _namespaces.getParent();
freeState(state);
return;
}
else {
//-- check for non-whitespace...and report error
if (!isWhitespace(state.buffer)) {
String err = "Illegal Text data found as child of: "
+ name;
err += "\n value: \"" + state.buffer + "\"";
throw new SAXException(err);
}
}
}
//-- We're finished processing the object, so notify the
//-- Listener (if any).
if ( _unmarshalListener != null && state.object != null ) {
_unmarshalListener.unmarshalled(state.object);
}
//-- if we are at root....just validate and we are done
if (_stateInfo.empty()) {
if (_validate) {
ValidationException first = null;
ValidationException last = null;
//-- check unresolved references
if (_resolveTable != null) {
Enumeration enumeration = _resolveTable.keys();
while (enumeration.hasMoreElements()) {
Object ref = enumeration.nextElement();
//if (ref.toString().startsWith(MapItem.class.getName())) continue;
String msg = "unable to resolve reference: " + ref;
if (first == null) {
first = new ValidationException(msg);
last = first;
}
else {
last.setNext(new ValidationException(msg));
last = last.getNext();
}
}
}
try {
Validator validator = new Validator();
ValidationContext context = new ValidationContext();
context.setResolver(_cdResolver);
context.setConfiguration(_config);
validator.validate(state.object, context);
}
catch(ValidationException vEx) {
if (first == null)
first = vEx;
else
last.setNext(vEx);
}
if (first != null) {
throw new SAXException(first);
}
}
return;
}
//-- Add object to parent if necessary
if (descriptor.isIncremental()) {
//-- remove current namespace scoping
_namespaces = _namespaces.getParent();
freeState(state);
return; //-- already added
}
Object val = state.object;
//--special code for AnyNode handling
if (_node != null) {
val = _node;
_node = null;
}
//-- save fieldState
UnmarshalState fieldState = state;
//-- have we seen this object before?
boolean firstOccurance = false;
//-- get target object
state = (UnmarshalState) _stateInfo.peek();
if (state.wrapper) {
state = fieldState.targetState;
}
//-- check to see if we have already read in
//-- an element of this type.
//-- (Q: if we have a container, do we possibly need to
//-- also check the container's multivalued status?)
if ( ! descriptor.isMultivalued() ) {
if (state.isUsed(descriptor)) {
String err = "element \"" + name;
err += "\" occurs more than once. (parent class: " + state.type.getName() + ")";
String location = name;
while (!_stateInfo.isEmpty()) {
UnmarshalState tmpState = (UnmarshalState)_stateInfo.pop();
if (tmpState.fieldDesc.isContainer()) continue;
location = state.elementName + "/" + location;
}
err += "\n location: /" + location;
ValidationException vx =
new ValidationException(err);
throw new SAXException(vx);
}
state.markAsUsed(descriptor);
//-- if this is the identity then save id
if (state.classDesc.getIdentity() == descriptor) {
state.key = val;
}
}
else {
//-- check occurance of descriptor
if (!state.isUsed(descriptor)) {
firstOccurance = true;
}
//-- record usage of descriptor
state.markAsUsed(descriptor);
}
try {
FieldHandler handler = descriptor.getHandler();
//check if the value is a QName that needs to
//be resolved (ns:value -> {URI}value)
String valueType = descriptor.getSchemaType();
if ((valueType != null) && (valueType.equals(QNAME_NAME))) {
val = resolveNamespace(val);
}
boolean addObject = true;
if (_reuseObjects && fieldState.primitiveOrImmutable) {
//-- check to see if we need to
//-- add the object or not
Object tmp = handler.getValue(state.object);
if (tmp != null) {
//-- Do not add object if values
//-- are equal
addObject = (!tmp.equals(val));
}
}
//-- special handling for mapped objects
if (descriptor.isMapped()) {
if (!(val instanceof MapItem)) {
MapItem mapItem = new MapItem(fieldState.key, val);
val = mapItem;
}
else {
//-- make sure value exists (could be a reference)
MapItem mapItem = (MapItem)val;
if (mapItem.getValue() == null) {
//-- save for later...
addObject = false;
addReference(mapItem.toString(), state.object, descriptor);
}
}
}
if (addObject) {
//-- clear any collections if necessary
if (firstOccurance && _clearCollections) {
handler.resetValue(state.object);
}
//-- finally set the value!!
handler.setValue(state.object, val);
// If there is a parent for this object, pass along
// a notification that we've finished adding a child
if ( _unmarshalListener != null ) {
_unmarshalListener.fieldAdded(descriptor.getFieldName(), state.object, fieldState.object);