[jdom-interest] Parsing a MODS-document with validation fails

Jason Hunter jhunter at servlets.com
Wed Aug 10 22:37:07 PDT 2011


Yes, that matches what I saw.

If our setAttribute() didn't implicitly replace a pre-existing attribute of the same name, I expect we'd produce a malformed result like DOM.

-jh-

On Aug 10, 2011, at 7:19 PM, Rolf wrote:

> Hi Jason, all.
> 
> I've run through on the DOMBuilder side too.
> 
> It is also a victim of this problem. In this case, the DOM model has is pretty much exactly what I expected to see given that it's built through the same xerces parser. Like the sax case, the DOM Attributes have the correct namespace declared, but they are all unprefixed.
> 
> When processed through JDOM's DOMBuilder, these namespaces are all 'lost'. So, in effect, the DOMBuilder is going to need a 'matching' patch.
> 
> Rolf
> 
> On 09/08/2011 7:14 PM, Jason Hunter wrote:
>> I assume you mean line 568?
>> 
>> I'll add some comments inline and you can tell me if I'm following the
>> logic right.
>> 
>>> 
>>> } else if (atts.getURI(i) != null && atts.getURI(i).length() > 0) {
>>> // the localname and qName are the same, but there is a
>>> // Namspace URI. We need to figure out the namespace prefix.
>>> // this is an unusual condition. Currently the only known trigger
>>> // is when there is a fixed/defaulted attribute from a validating
>>> // XMLSchema, and the attribute is in a different namespace
>>> // than the rest of the document, this happens whenever there
>>> // is an attribute definition that has form="qualified".
>>> // <xs:attribute name="attname" form="qualified" ... />
>>> // or the schema sets attributeFormDefault="qualified"
>> 
>> // The goal of this searching code is to find a good attNS namespace we
>> can use.
>> 
>>> String attURI = atts.getURI(i);
>>> Namespace attNS = null;
>>> Element p = element;
>>> // We need to ensure that a particular prefix has not been
>>> // overridden at a lower level than what we are expecting.
>>> // track all prefixes to ensure they are not changed lower
>>> // down.
>>> HashSet overrides = new HashSet();
>>> uploop: do {
>>> // Search up the Element tree looking for a prefixed namespace
>>> // matching our attURI
>> 
>> // First look at the element itself
>> 
>>> if (p.getNamespace().getURI().equals(attURI)
>>> && !overrides.contains(p.getNamespacePrefix())
>>> && !"".equals(element.getNamespace().getPrefix())) {
>>> // we need a prefix. It's impossible to have a namespaced
>>> // attribute if there is no prefix for that attribute.
>>> attNS = p.getNamespace();
>>> break uploop;
>>> }
>> 
>> // Then any additional namespaces defined on the element
>> 
>>> overrides.add(p.getNamespacePrefix());
>>> for (Iterator it = p.getAdditionalNamespaces().iterator();
>>> it.hasNext(); ) {
>>> Namespace ns = (Namespace)it.next();
>>> if (!overrides.contains(ns.getPrefix())
>>> && attURI.equals(ns.getURI())) {
>>> attNS = ns;
>>> break uploop;
>>> }
>>> overrides.add(ns.getPrefix());
>>> }
>> 
>> // If we haven't hit something yet, keep walking up the tree
>> 
>>> if (p == element) {
>>> p = currentElement;
>>> } else {
>>> p = p.getParentElement();
>>> }
>>> } while (p != null);
>> 
>> // If we still don't have attNS we need to invent a unique one
>> 
>>> if (attNS == null) {
>>> // we cannot find a 'prevailing' namespace that has a prefix
>>> // that is for this namespace.
>>> // This basically means that there's an XMLSchema, for the
>>> // DEFAULT namespace, and there's a defaulted/fixed
>>> // attribute definition in the XMLSchema that's targeted
>>> // for this namespace,... but, the user has either not
>>> // declared a prefixed version of the namespace, or has
>>> // re-declared the same prefix at a lower level with a
>>> // different namespace.
>>> // All of these things are possible.
>>> // Create some sort of default prefix.
>>> int cnt = 0;
>>> String base = "attns";
>>> String pfx = base + cnt;
>>> while (overrides.contains(pfx)) {
>>> cnt++;
>>> pfx = base + cnt;
>>> }
>>> attNS = Namespace.getNamespace(pfx, attURI);
>>> }
>> 
>> Finally build the attribute, using either a found attNS or an invented one
>> 
>>> attribute = factory.attribute(attLocalName, atts.getValue(i),
>>> attType, attNS);
>>> } else {
>> 
>> Lastly, DOMBuilder needs code like this too, no?
>> 
>> -jh-
>> 
>> 
>> 
>> _______________________________________________
>> To control your jdom-interest membership:
>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com




More information about the jdom-interest mailing list