Attributes that have default values will be added during validation.
They can be recognized because getSpecified() gives false.
So I wrote a method to find this attributes. But I was surprised that the ownerElement of this attributes = null.


I' am using xerces 2.7.1 to develop a new XMLEditor called XMLDonkey.

Dick Deneer


See this code:

/**
* Find attributes that were inserted by validator
* and notify other components beacuse they must stay
* synchronized with the tree.
* We are here dealing with the problem that we receive no
* mutationEvent for attributes (with default in xsd) that
* are inserted by the validator
* This method traverses the domtree, looking for this attributes
* and notifies the model.
* The models are notified by reinserting the attribute, because
* there is another bug: these attributes do not have a ownerElement
*
*
* @param node
* rootnode for NodeIterator
*/
public void findNewAttributes(Node node)
{

// Set up the iterator

Document doc;
if (node.getNodeType() != Node.DOCUMENT_NODE)
{
doc = node.getOwnerDocument();
}
else
{
doc = (Document) node;
}
if (node == null)
{
throw new NullPointerException("nullNode");
}
DocumentTraversal traversable = (DocumentTraversal) doc;
int whatToShow = NodeFilter.SHOW_ELEMENT;
NodeIterator iterator =
traversable.createNodeIterator(node, whatToShow, null, true);

NodeImpl element;
while ((element = (NodeImpl) iterator.nextNode()) != null)
{

AttrImpl attr;
NamedNodeMap attrs = element.getAttributes();
if (attrs != null)
{
for (int i = 0; i < attrs.getLength(); i++)
{
attr = (AttrImpl) attrs.item(i);
if (!attr.getSpecified())
{
//System.out.println("parent " + attr.getOwnerElement());
//System.out.println(
// "nameSpace " + attr.getNamespaceURI());
//System.out.println("Name " + attr.getNodeName());
System.out.println(
"attibute "
+ attr.getName()
+ " for element "
+ element.getNodeName()
+ " was new inserted!");
//Remove the attribute and set the index to -1
//so the DocEventLister does not fire an event.
isRemovingAttr = true;
indexRemovedAttr = -1;
ownerRemovedAttr = element;
((Element) element).removeAttributeNode(attr);
isRemovingAttr = false;

//reinsert the attribute
insertAttribute(
this.handler,
element,
attr.getNamespaceURI(),
attr.getNodeName(),
attr.getValue());


//we could hav use this code if the attributes
//had normal OwnerElement
//nodeWasInserted(null, attr);
//in fact settin a user attritute is nicer
//attr.setSpecified(true);
}

}
}

}

}

Reply via email to