Bug 283376 - GetTypeForControl API needs to return default type for node. Patch by aaronr@us.ibm.com, r=me,smaug

This commit is contained in:
doronr%us.ibm.com 2005-02-25 21:52:59 +00:00
parent 232863a81a
commit 3202dbcf56
3 changed files with 76 additions and 27 deletions

View File

@ -64,6 +64,15 @@ interface nsIModelElementPrivate : nsIXFormsModelElement
*/
nsISchemaType getTypeForControl(in nsIXFormsControl control);
/**
* This function takes an instance data node, finds the type bound to it, and
* returns the seperated out type and namespace URI. If no type is set for
* the node, then it returns the defaults: "http://www.w3.org/2001/XMLSchema"
* and "string"
*/
void getTypeAndNSFromNode(in nsIDOMNode instancenode, out AString type,
out AString namespaceURI);
/**
* Notification that an instance element has started or finished loading
* its instance data. Model contstruction cannot complete until all of

View File

@ -768,22 +768,13 @@ nsXFormsModelElement::GetTypeForControl(nsIXFormsControl *aControl,
NS_ENSURE_ARG_POINTER(aType);
*aType = nsnull;
nsAutoString schemaTypeName;
nsAutoString schemaTypePrefix;
nsCOMPtr<nsIDOMNode> boundNode;
aControl->GetBoundNode(getter_AddRefs(boundNode));
NS_ENSURE_TRUE(boundNode, NS_ERROR_FAILURE);
nsresult rv = nsXFormsUtils::ParseTypeFromNode(boundNode, schemaTypeName,
schemaTypePrefix);
NS_ENSURE_SUCCESS(rv, rv);
// get the namespace url from the prefix
nsCOMPtr<nsIDOM3Node> domNode3 = do_QueryInterface(mElement, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString schemaTypeNamespace;
rv = domNode3->LookupNamespaceURI(schemaTypePrefix, schemaTypeNamespace);
nsAutoString schemaTypeName, schemaTypeNamespace;
nsresult rv = GetTypeAndNSFromNode(boundNode, schemaTypeName,
schemaTypeNamespace);
NS_ENSURE_SUCCESS(rv, rv);
nsXFormsSchemaValidator validator;
@ -792,6 +783,29 @@ nsXFormsModelElement::GetTypeForControl(nsIXFormsControl *aControl,
return validator.GetType(schemaTypeName, schemaTypeNamespace, aType);
}
/* static */ nsresult
nsXFormsModelElement::GetTypeAndNSFromNode(nsIDOMNode *aInstanceData,
nsAString &aType, nsAString &aNSUri)
{
nsAutoString schemaTypePrefix;
nsresult rv = nsXFormsUtils::ParseTypeFromNode(aInstanceData, aType,
schemaTypePrefix);
if(rv == NS_ERROR_NOT_AVAILABLE) {
// if there is no type assigned, then assume that the type is 'string'
aNSUri.Assign(NS_LITERAL_STRING(NS_NAMESPACE_XML_SCHEMA));
aType.Assign(NS_LITERAL_STRING("string"));
rv = NS_OK;
} else {
// get the namespace url from the prefix
nsCOMPtr<nsIDOM3Node> domNode3 = do_QueryInterface(mElement, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = domNode3->LookupNamespaceURI(schemaTypePrefix, aNSUri);
}
return rv;
}
NS_IMETHODIMP
nsXFormsModelElement::InstanceLoadStarted()
{
@ -877,18 +891,9 @@ nsXFormsModelElement::ValidateNode(nsIDOMNode *aInstanceNode, PRBool *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
nsAutoString schemaTypeName;
nsAutoString schemaTypePrefix;
nsresult rv = nsXFormsUtils::ParseTypeFromNode(aInstanceNode, schemaTypeName,
schemaTypePrefix);
// get the namespace url from the prefix
nsCOMPtr<nsIDOM3Node> domNode3 = do_QueryInterface(mElement, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString schemaTypeNamespace;
rv = domNode3->LookupNamespaceURI(schemaTypePrefix, schemaTypeNamespace);
nsAutoString schemaTypeName, schemaTypeNamespace;
nsresult rv = GetTypeAndNSFromNode(aInstanceNode, schemaTypeName,
schemaTypeNamespace);
NS_ENSURE_SUCCESS(rv, rv);
nsXFormsSchemaValidator validator;

View File

@ -73,6 +73,8 @@
#include "nsIPermissionManager.h"
#include "nsServiceManagerUtils.h"
#include "nsIXFormsUtilityService.h"
#include "nsIDOMAttr.h"
#include "nsIDOM3Node.h"
#define CANCELABLE 0x01
#define BUBBLES 0x02
@ -1036,14 +1038,47 @@ nsXFormsUtils::ParseTypeFromNode(nsIDOMNode *aInstanceData,
nsAString &aType, nsAString &aNSPrefix)
{
nsresult rv;
// aInstanceData could be an instance data node or it could be an attribute
// on an instance data node (basically the node that a control is bound to).
// So first checking to see if it is a proper element node. If it isn't,
// making sure that it is at least an attribute.
//
// XXX - Once node type is set as a property on the element or attribute node,
// then we can treat elements and attributes the same. For now we are using
// an attribute on the instance data node to store the type. If we bind
// to an attribute on the instance data node there is nothing we can do but
// hope that it doesn't have a bound type until we get properties on
// attributes working. (bug 283004)
nsCOMPtr<nsIDOMElement> nodeElem = do_QueryInterface(aInstanceData, &rv);
NS_ENSURE_TRUE(nodeElem, NS_ERROR_FAILURE);
if (NS_FAILED(rv)) {
nsCOMPtr<nsIDOMAttr> attrNode = do_QueryInterface(aInstanceData, &rv);
if(NS_SUCCEEDED(rv)){
// right now we can't handle having a 'type' property on attribute nodes.
// For now we'll treat this condition as not having a 'type' property
// on the given node at all. This will allow a lot of testcases to still
// work ok as the caller will usually assign a default type of
// 'xsd:string' when we return NS_ERROR_NOT_AVAILABLE here.
return NS_ERROR_NOT_AVAILABLE;
} else {
// can't have a 'type' property on anything other than an element or an
// attribute. Return failure
return NS_ERROR_FAILURE;
}
}
// right now type is stored as an attribute on the instance node. In the
// future it will be a property.
PRBool typeExists = PR_FALSE;
NS_NAMED_LITERAL_STRING(schemaInstanceURI, NS_NAMESPACE_XML_SCHEMA_INSTANCE);
NS_NAMED_LITERAL_STRING(type, "type");
nodeElem->HasAttributeNS(schemaInstanceURI, type, &typeExists);
if (!typeExists) {
return NS_ERROR_NOT_AVAILABLE;
}
nsAutoString typeAttribute;
nodeElem->GetAttributeNS(NS_LITERAL_STRING(NS_NAMESPACE_XML_SCHEMA_INSTANCE),
NS_LITERAL_STRING("type"), typeAttribute);
nodeElem->GetAttributeNS(schemaInstanceURI, type, typeAttribute);
// split type (ns:type) into namespace and type.
PRInt32 separator = typeAttribute.FindChar(':');