-
-
Notifications
You must be signed in to change notification settings - Fork 188
[bugfix] declare copy-namespaces no-inherit breaking element constructors #6222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,28 +21,38 @@ | |
| */ | ||
| package org.exist.xquery; | ||
|
|
||
| import org.exist.dom.QName; | ||
| import org.exist.dom.memtree.DocumentBuilderReceiver; | ||
| import org.exist.dom.memtree.DocumentImpl; | ||
| import org.exist.dom.memtree.MemTreeBuilder; | ||
| import org.exist.dom.memtree.NodeImpl; | ||
| import org.exist.dom.memtree.TextImpl; | ||
| import org.exist.util.serializer.AttrList; | ||
| import org.exist.xquery.functions.array.ArrayType; | ||
| import org.exist.xquery.util.ExpressionDumper; | ||
| import org.exist.xquery.value.*; | ||
| import org.w3c.dom.DOMException; | ||
| import org.xml.sax.SAXException; | ||
|
|
||
| import javax.xml.XMLConstants; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Represents an enclosed expression <code>{expr}</code> inside element | ||
| * content. Enclosed expressions within attribute values are processed by | ||
| * {@link org.exist.xquery.AttributeConstructor}. | ||
| * | ||
| * | ||
| * @author <a href="mailto:wolfgang@exist-db.org">Wolfgang Meier</a> | ||
| */ | ||
| public class EnclosedExpr extends PathExpr { | ||
|
|
||
| public EnclosedExpr(XQueryContext context) { | ||
| super(context); | ||
| } | ||
|
|
||
| public void analyze(AnalyzeContextInfo contextInfo) throws XPathException { | ||
| final AnalyzeContextInfo newContextInfo = new AnalyzeContextInfo(contextInfo); | ||
| newContextInfo.removeFlag(IN_NODE_CONSTRUCTOR); | ||
|
|
@@ -76,12 +86,33 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc | |
| try { | ||
| context.pushDocumentContext(); | ||
|
|
||
| MemTreeBuilder innerBuilder = null; | ||
| try { | ||
| result = super.eval(contextSequence, null); | ||
| // Capture the builder that may have been created by direct constructors inside | ||
| // this enclosed expression. If null, no direct constructors ran — the result | ||
| // items are all pre-existing nodes (variable references / copies). | ||
| innerBuilder = context.peekDocumentBuilder(); | ||
| } finally { | ||
| context.popDocumentContext(); | ||
| } | ||
|
|
||
| // Compute ancestor namespace context for no-inherit copy handling. | ||
| // This is the union of inherited namespaces (from outer constructors) and | ||
| // in-scope namespaces (from the immediately enclosing constructor), i.e. all | ||
| // namespace bindings that an ancestor traversal from this element would find. | ||
| final boolean noInherit = !context.inheritNamespaces(); | ||
| Map<String, String> ancestorNS = null; | ||
| if (noInherit) { | ||
| final Map<String, String> inherited = context.getAllInheritedNamespaces(); | ||
| final Map<String, String> inScope = context.getInScopeNamespaces(); | ||
| if ((inherited != null && !inherited.isEmpty()) || (inScope != null && !inScope.isEmpty())) { | ||
| ancestorNS = new HashMap<>(); | ||
| if (inherited != null) { ancestorNS.putAll(inherited); } | ||
| if (inScope != null) { ancestorNS.putAll(inScope); } | ||
| } | ||
| } | ||
|
|
||
| // create the output | ||
| final MemTreeBuilder builder = context.getDocumentBuilder(); | ||
| final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(this, builder); | ||
|
|
@@ -130,7 +161,25 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc | |
| } | ||
| try { | ||
| receiver.setCheckNS(false); | ||
| next.copyTo(context.getBroker(), receiver); | ||
| // When copy-namespaces no-inherit is active, pre-existing element nodes | ||
| // (i.e. nodes from variable references, not direct constructors) must have | ||
| // namespace undeclarations injected so that ancestor-traversal in | ||
| // in-scope-prefixes() is neutralized for inherited namespace bindings. | ||
| if (noInherit && ancestorNS != null | ||
| && next.getType() == Type.ELEMENT | ||
| && next instanceof NodeImpl) { | ||
| final NodeImpl nodeImpl = (NodeImpl) next; | ||
| final boolean isPreExisting = (innerBuilder == null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to populate innerBuilder lazily on first access instead of always regardless if it will ever be used. |
||
| || (nodeImpl.getOwnerDocument() != innerBuilder.getDocument()); | ||
| if (isPreExisting) { | ||
| next.copyTo(context.getBroker(), | ||
| new NoInheritCopyReceiver(this, builder, ancestorNS)); | ||
| } else { | ||
| next.copyTo(context.getBroker(), receiver); | ||
| } | ||
| } else { | ||
| next.copyTo(context.getBroker(), receiver); | ||
| } | ||
| receiver.setCheckNS(true); | ||
| } catch (DOMException e) { | ||
| if (e.code == DOMException.NAMESPACE_ERR) { | ||
|
|
@@ -194,4 +243,104 @@ public Expression simplify() { | |
| public boolean evalNextExpressionOnEmptyContextSequence() { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * A {@link DocumentBuilderReceiver} that injects namespace undeclarations onto the | ||
| * root element of a copied pre-existing node when {@code declare copy-namespaces no-inherit} | ||
| * is active. The undeclarations neutralize ancestor namespace bindings so that | ||
| * {@code fn:in-scope-prefixes()} traversing the ancestor chain returns the correct result. | ||
| * | ||
| * <p>Only prefixes present in {@code ancestorNS} but absent from the root element's own | ||
| * namespace declarations are undeclared (i.e. recorded as {@code xmlns:prefix=""}).</p> | ||
| */ | ||
| private static final class NoInheritCopyReceiver extends DocumentBuilderReceiver { | ||
|
|
||
| private final Map<String, String> ancestorNS; | ||
| /** True once the root element's startElement event has been seen. */ | ||
| private boolean rootSeen = false; | ||
| /** True once undeclarations have been flushed (happens before first non-namespace event). */ | ||
| private boolean undeclsFlushed = false; | ||
| /** Prefixes that the root element itself declares (element prefix + xmlns:* nodes). */ | ||
| private final Set<String> rootOwnPrefixes = new HashSet<>(); | ||
|
|
||
| NoInheritCopyReceiver(final Expression expr, final MemTreeBuilder builder, | ||
| final Map<String, String> ancestorNS) { | ||
| super(expr, builder); | ||
| this.ancestorNS = ancestorNS; | ||
| } | ||
|
|
||
| @Override | ||
| public void startElement(final QName qname, final AttrList attribs) { | ||
| if (!rootSeen) { | ||
| rootSeen = true; | ||
| // Track the element's own namespace prefix so we don't undeclare it. | ||
| final String prefix = qname.getPrefix(); | ||
| if (prefix != null && !prefix.isEmpty()) { | ||
| rootOwnPrefixes.add(prefix); | ||
| } | ||
| // Note: namespace declaration nodes for this element come via addNamespaceNode | ||
| // after startElement; they are collected in rootOwnPrefixes there. | ||
| } else { | ||
| maybeFlushUndeclarations(); | ||
| } | ||
| super.startElement(qname, attribs); | ||
| } | ||
|
|
||
| @Override | ||
| public void addNamespaceNode(final QName qname) throws SAXException { | ||
| if (rootSeen && !undeclsFlushed) { | ||
| // Collect the prefix that this namespace node declares on the root element. | ||
| rootOwnPrefixes.add(qname.getLocalPart()); | ||
| } | ||
| super.addNamespaceNode(qname); | ||
| } | ||
|
|
||
| @Override | ||
| public void characters(final CharSequence seq) throws SAXException { | ||
| maybeFlushUndeclarations(); | ||
| super.characters(seq); | ||
| } | ||
|
|
||
| @Override | ||
| public void characters(final char[] ch, final int start, final int len) throws SAXException { | ||
| maybeFlushUndeclarations(); | ||
| super.characters(ch, start, len); | ||
| } | ||
|
|
||
| @Override | ||
| public void endElement(final String ns, final String local, final String qname) throws SAXException { | ||
| maybeFlushUndeclarations(); | ||
| super.endElement(ns, local, qname); | ||
| } | ||
|
|
||
| @Override | ||
| public void endElement(final QName qname) throws SAXException { | ||
| maybeFlushUndeclarations(); | ||
| super.endElement(qname); | ||
| } | ||
|
|
||
| /** | ||
| * Emit namespace undeclarations for every ancestor namespace binding whose prefix is | ||
| * not already declared by the root element itself. Called before the first non-namespace | ||
| * event on the root element (child, text, endElement) to ensure the nodes attach to the | ||
| * root element node number in the MemTree. | ||
| */ | ||
| private void maybeFlushUndeclarations() { | ||
| if (rootSeen && !undeclsFlushed) { | ||
| undeclsFlushed = true; | ||
| for (final Map.Entry<String, String> entry : ancestorNS.entrySet()) { | ||
| final String prefix = entry.getKey(); | ||
| if (!rootOwnPrefixes.contains(prefix)) { | ||
| try { | ||
| super.addNamespaceNode( | ||
| new QName(prefix, XMLConstants.NULL_NS_URI, XMLConstants.XMLNS_ATTRIBUTE)); | ||
| } catch (final SAXException e) { | ||
| // Silently skip — undeclaration is best-effort; worst case | ||
| // in-scope-prefixes() returns a superset which is handled by cleanup. | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -969,6 +969,18 @@ public String getInheritedNamespace(final String prefix) { | |
| return inheritedInScopeNamespaces == null ? null : inheritedInScopeNamespaces.get(prefix); | ||
| } | ||
|
|
||
| public Map<String, String> getAllInheritedNamespaces() { | ||
| return inheritedInScopeNamespaces; | ||
| } | ||
|
|
||
| public Map<String, String> getInScopeNamespaces() { | ||
| return inScopeNamespaces; | ||
| } | ||
|
|
||
| public MemTreeBuilder peekDocumentBuilder() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this method named _peek_DocumentBuilder? |
||
| return documentBuilder; | ||
| } | ||
|
|
||
| @Override | ||
| public String getInheritedPrefix(final String uri) { | ||
| return inheritedInScopePrefixes == null ? null : inheritedInScopePrefixes.get(uri); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dead code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it, I would be in favour of removing this.