Re: Parsing a MODS-document with validation fails
Jason Hunter <[email protected]>
| Newsgroups | gmane.comp.java.jdom.general |
|---|---|
| Message-ID | <[email protected]> |
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/[email protected]