if ((typeQName != null) && !elementType.getXmlType().getQname().equals(typeQName)) {
// i.e this is an extension type
if (this.qNameToTypeMap.containsKey(typeQName)) {
actualElementType = (Type) this.qNameToTypeMap.get(typeQName);
} else {
throw new XmlParsingException("Unknown type found ==> " + typeQName);
}
}
// point to the complex type elements
if (isArray) {
Collection objectsCollection = getCollectionObject(classType, javaClass);
// read the first element
if ("true".equals(nillble) || "1".equals(nillble)) {
// this is a nill attribute
while (!reader.isEndElement()) {
reader.next();
}
if (isNillable){
objectsCollection.add(null);
} else {
throw new XmlParsingException("Element " + elementQName + " can not be null");
}
} else {
Object returnObject = getElementObjectFromReader(actualElementType, reader);
objectsCollection.add(returnObject);
// we have to move the cursor until the end element of this attribute
while (!reader.isEndElement() || !reader.getName().equals(elementQName)) {
reader.next();
}
}
boolean loop = true;
while (loop) {
while (!reader.isEndElement()) {
reader.next();
}
reader.next();
// now we are at the end element of the first element
while (!reader.isStartElement() && !reader.isEndElement()) {
reader.next();
}
// in this step if it is an end element we have found an end element
// so have to exit from the loop
if (reader.isEndElement()) {
loop = false;
} else {
// now it should be in a start element
// check whether still we read the original element attributes. otherwise return
if (reader.getName().equals(elementQName)) {
nillble = reader.getAttributeValue(Constants.URI_DEFAULT_SCHEMA_XSI, "nil");
// since this is a new element we check for extensions
actualElementType = elementType;
typeQName = getTypeQName(reader);
if ((typeQName != null) && !elementType.getXmlType().getQname().equals(typeQName)) {
// i.e this is an extension type
if (this.qNameToTypeMap.containsKey(typeQName)) {
actualElementType = (Type) this.qNameToTypeMap.get(typeQName);
} else {
throw new XmlParsingException("Unknown type found ==> " + typeQName);
}
}
if ("true".equals(nillble) || "1".equals(nillble)) {
// this is a nill attribute
while (!reader.isEndElement()) {
reader.next();
}
if (isNillable) {
objectsCollection.add(null);
} else {
throw new XmlParsingException("Element " + elementQName + " can not be null");
}
} else {
Object returnObject = getElementObjectFromReader(actualElementType, reader);
objectsCollection.add(returnObject);
// we have to move the cursor until the end element of this attribute
while (!reader.isEndElement() || !reader.getName().equals(elementQName)) {
reader.next();
}
}
} else {
loop = false;
}
}
}
// this is very important in handling primitives
// they can not have null values so if array then we have to return the
// array object is null
// for other also it is convenient to assume like that.
if ((Constants.OTHER_TYPE & classType) == Constants.OTHER_TYPE){
// i.e this is not a collection type
List objectsList = (List) objectsCollection;
if ((objectsCollection.size() == 0) ||
((objectsCollection.size() == 1) && (objectsList.get(0) == null))) {
return null;
} else {
// create an array with the original element type
Object objectArray = Array.newInstance(elementType.getJavaClass(), objectsCollection.size());
for (int i = 0; i < objectsCollection.size(); i++) {
Array.set(objectArray, i, objectsList.get(i));
}
return objectArray;
}
} else if ((Constants.COLLECTION_TYPE & classType) == Constants.COLLECTION_TYPE){
if ((objectsCollection.size() == 0) ||
((objectsCollection.size() == 1) && (objectsCollection.iterator().next() == null))){
return null;
} else {
return objectsCollection;
}
} else if ((Constants.MAP_TYPE & classType) == Constants.MAP_TYPE){
if ((objectsCollection.size() == 0) ||
((objectsCollection.size() == 1) && (objectsCollection.iterator().next() == null))) {
return null;
} else {
List mapObjectsList = (List) objectsCollection;
MapType mapType = null;
Map mapObject = null;
if (javaClass.isInterface()) {
mapObject = new HashMap();
} else {
try {
mapObject = (Map) javaClass.newInstance();
} catch (InstantiationException e) {
throw new XmlParsingException("Can not instantiate the java class " + javaClass.getName(), e);
} catch (IllegalAccessException e) {
throw new XmlParsingException("Can not instantiate the java class " + javaClass.getName(), e);
}
}
for (Iterator iter = mapObjectsList.iterator(); iter.hasNext();) {
mapType = (MapType) iter.next();
mapObject.put(mapType.getKey(), mapType.getValue());
}
return mapObject;
}
} else {
throw new XmlParsingException("Unknow class type " + classType);
}
} else {
if ("true".equals(nillble) || "1".equals(nillble)) {
// this is a nill attribute
while (!reader.isEndElement()) {
reader.next();
}
reader.next();
if (isNillable) {
return null;
} else {
throw new XmlParsingException("Element " + elementQName + " can not be null");
}
} else {
Object returnObject = getElementObjectFromReader(actualElementType, reader);
// we have to move the cursor until the end element of this attribute
while (!reader.isEndElement() || !reader.getName().equals(elementQName)) {
reader.next();
}
return returnObject;
}
}
} else {
if (isMinOccurs0) {
return null;
} else {
throw new XmlParsingException("Unexpected Subelement " + reader.getName() + " but " +
"expected " + elementQName.getLocalPart());
}
}
}