QName factoryElemName = factoryType.getDocumentElementName();
if (!factoryElemName.equals(docElemName) &&
!factoryType.isValidSubstitution(docElemName))
throw new XmlException("Element " + QNameHelper.pretty(docElemName) +
" is not a valid " + QNameHelper.pretty(factoryElemName) +
" document or a valid substitution.");
}
}
if (theType == null)
{
// infer type based on root attr
QName attrName = null;
XmlCursor c = createCursor();
if (c.toFirstAttribute() && !c.toNextAttribute())
attrName = c.getName();
c.dispose();
if (attrName != null)
theType = _schemaTypeSystem.findAttributeType( attrName );
}
}
// sniffing doesn't say anything: assume the expected type
if (theType == null)
theType = factoryType;
// Still nothing: the no type.
if (theType == null)
theType = XmlBeans.NO_TYPE;
// assign type
_doc.setType( this, theType );
// todo: Have a similar attribute type check
if (factoryType != null)
{
if (theType.isDocumentType())
verifyDocumentType( theType.getDocumentElementName() );
else if (theType.isAttributeType())
verifyAttributeType( theType.getAttributeTypeAttributeName() );
}
//
// If a type was passed in, usually from generated code which needs the
// type to be something specific because of a pending cast, then check
// the resulting type and throw a nice exception.
//
if (factoryType != null && !factoryType.isAssignableFrom(theType))
{
/*
System.out.println("Factory type = " + factoryType);
System.out.println("The type = " + theType);
System.out.println("basetype of the type = " + theType.getBaseType());
*/
throw
new XmlException( "XML object is not of type " + factoryType );
}
return getObject();
}