From: Wolfgang M. M. <wol...@us...> - 2004-08-07 16:28:12
|
Update of /cvsroot/exist/eXist-1.0/src/org/exist/memtree In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14978/src/org/exist/memtree Modified Files: DocumentImpl.java NodeImpl.java NamespaceNode.java ElementImpl.java Receiver.java MemTreeBuilder.java Log Message: Namespace declarations in direct or computed element constructors were only copied to the output if they were referenced by a element or attribute qname following the declaration. This made it difficult to write - for example - an XSLT fragment that used the namespace in an xsl:template. Namespace declarations are now preserved. They are attached to the element node in which they were declared. Index: NodeImpl.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/memtree/NodeImpl.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** NodeImpl.java 29 Jun 2004 14:25:12 -0000 1.16 --- NodeImpl.java 7 Aug 2004 16:28:02 -0000 1.17 *************** *** 93,97 **** case Node.DOCUMENT_NODE : return "#document"; - case Node.ATTRIBUTE_NODE : case Node.ELEMENT_NODE : case Node.PROCESSING_INSTRUCTION_NODE : --- 93,96 ---- *************** *** 99,102 **** --- 98,105 ---- document.namePool.get(document.nodeName[nodeNumber]); return qn.toString(); + case Node.ATTRIBUTE_NODE: + return document.namePool.get(document.attrName[nodeNumber]).toString(); + case NodeImpl.NAMESPACE_NODE: + return document.namePool.get(document.namespaceCode[nodeNumber]).toString(); case Node.TEXT_NODE : return "#text"; Index: DocumentImpl.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/memtree/DocumentImpl.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** DocumentImpl.java 29 Jun 2004 14:25:12 -0000 1.11 --- DocumentImpl.java 7 Aug 2004 16:28:02 -0000 1.12 *************** *** 23,32 **** import java.util.Arrays; - import org.apache.xerces.dom.AttrNSImpl; import org.exist.dom.NodeProxy; import org.exist.dom.QName; import org.exist.util.hashtable.NamePool; import org.exist.xquery.XQueryContext; - import org.exist.xquery.value.Type; import org.w3c.dom.Attr; import org.w3c.dom.CDATASection; --- 23,30 ---- *************** *** 60,69 **** --- 58,71 ---- protected NamePool namePool = new NamePool(); + // holds the node type of a node protected short[] nodeKind = null; + // the tree level of a node protected short[] treeLevel; + // the node number of the next sibling protected int[] next; + // pointer into the namePool protected int[] nodeName; *************** *** 76,79 **** --- 78,82 ---- protected int nextChar = 0; + // attributes protected int[] attrName; *************** *** 84,91 **** --- 87,103 ---- protected int nextAttr = 0; + // namespaces + protected int[] namespaceParent; + + protected int[] namespaceCode; + + protected int nextNamespace = 0; + + // the current number of nodes in the doc protected int size = 1; protected int documentRootNode = -1; + // reference nodes (link to an external, persistent document fragment) protected NodeProxy references[]; *************** *** 116,119 **** --- 128,134 ---- attrValue = new String[ATTR_SIZE]; + namespaceCode = new int[5]; + namespaceParent = new int[5]; + references = new NodeProxy[REF_SIZE]; *************** *** 195,198 **** --- 210,222 ---- } + public int addNamespace(int nodeNr, QName qname) { + if(nodeKind == null) init(); + if(nextNamespace == namespaceCode.length) growNamespaces(); + namespaceCode[nextNamespace] = namePool.add(qname); + namespaceParent[nextNamespace] = nodeNr; + if(alphaLen[nodeNr] < 0) alphaLen[nodeNr] = nextNamespace; + return nextNamespace++; + } + public int getLastNode() { return size - 1; *************** *** 254,261 **** --- 278,302 ---- } + private void growNamespaces() { + int size = namespaceCode.length; + int newSize = (size * 3) / 2; + + int[] newCodes = new int[newSize]; + System.arraycopy(namespaceCode, 0, newCodes, 0, size); + namespaceCode = newCodes; + + int[] newParents = new int[newSize]; + System.arraycopy(namespaceParent, 0, newParents, 0, size); + namespaceParent = newParents; + } + public NodeImpl getAttribute(int nodeNr) throws DOMException { return new AttributeImpl(this, nodeNr); } + public NodeImpl getNamespaceNode(int nodeNr) throws DOMException { + return new NamespaceNode(this, nodeNr); + } + public NodeImpl getNode(int nodeNr) throws DOMException { if (nodeNr == 0) return this; *************** *** 280,286 **** node = new ReferenceNode(this, nodeNr); break; - case NodeImpl.NAMESPACE_NODE: - node = new NamespaceNode(this, nodeNr); - break; default: throw new DOMException(DOMException.NOT_FOUND_ERR, --- 321,324 ---- *************** *** 549,552 **** --- 587,603 ---- } } + int ns = document.alphaLen[nr]; + if (-1 < ns) { + XQueryContext context = receiver.getContext(); + while (ns < document.nextNamespace + && document.namespaceParent[ns] == nr) { + QName nsQName = (QName) document.namePool + .get(document.namespaceCode[ns]); + System.out.println("copying namespace node " + nsQName); + receiver.addNamespaceNode(nsQName); + context.declareInScopeNamespace(nsQName.getLocalName(), nsQName.getNamespaceURI()); + ++ns; + } + } break; case Node.TEXT_NODE: *************** *** 572,582 **** .addReferenceNode(document.references[document.alpha[nr]]); break; - case NodeImpl.NAMESPACE_NODE: - XQueryContext context = receiver.getContext(); - QName prefix = (QName) document.namePool.get(document.nodeName[nr]); - String uri = new String(document.characters, - document.alpha[nr], document.alphaLen[nr]); - context.declareInScopeNamespace(prefix.getLocalName(), uri); - break; } } --- 623,626 ---- Index: Receiver.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/memtree/Receiver.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Receiver.java 29 Jun 2004 14:25:12 -0000 1.5 --- Receiver.java 7 Aug 2004 16:28:02 -0000 1.6 *************** *** 127,130 **** --- 127,134 ---- } + public void addNamespaceNode(QName qname) throws SAXException { + builder.namespaceNode(qname); + } + public void characters(CharSequence seq) throws SAXException { builder.characters(seq); Index: ElementImpl.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/memtree/ElementImpl.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ElementImpl.java 3 May 2004 13:08:43 -0000 1.4 --- ElementImpl.java 7 Aug 2004 16:28:02 -0000 1.5 *************** *** 137,146 **** NamedNodeMapImpl map = new NamedNodeMapImpl(); int attr = document.alpha[nodeNumber]; ! if (attr < 0) return map; ! while (attr < document.nextAttr ! && document.attrParent[attr] == nodeNumber) { ! map.add(new AttributeImpl(document, attr)); ! ++attr; } return map; --- 137,155 ---- NamedNodeMapImpl map = new NamedNodeMapImpl(); int attr = document.alpha[nodeNumber]; ! if(-1 < attr) { ! while (attr < document.nextAttr ! && document.attrParent[attr] == nodeNumber) { ! map.add(new AttributeImpl(document, attr)); ! ++attr; ! } ! } ! // add namespace declarations attached to this element ! int ns = document.alphaLen[nodeNumber]; ! if (ns < 0) return map; ! while (ns < document.nextNamespace ! && document.namespaceParent[ns] == nodeNumber) { ! map.add(new NamespaceNode(document, ns)); ! ++ns; } return map; Index: MemTreeBuilder.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/memtree/MemTreeBuilder.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** MemTreeBuilder.java 29 Jun 2004 14:25:12 -0000 1.12 --- MemTreeBuilder.java 7 Aug 2004 16:28:02 -0000 1.13 *************** *** 28,32 **** import org.exist.dom.QName; import org.exist.xquery.XQueryContext; - import org.exist.xquery.value.Type; import org.w3c.dom.Node; import org.xml.sax.Attributes; --- 28,31 ---- *************** *** 249,260 **** public int namespaceNode(String prefix, String uri) { ! QName qn = new QName(prefix, null, null); ! int nodeNr = doc.addNode(NodeImpl.NAMESPACE_NODE, level, qn); ! doc.addChars(nodeNr, uri); ! int prevNr = prevNodeInLevel[level]; ! if (prevNr > -1) ! doc.next[prevNr] = nodeNr; ! doc.next[nodeNr] = prevNodeInLevel[level - 1]; ! prevNodeInLevel[level] = nodeNr; return nodeNr; } --- 248,257 ---- public int namespaceNode(String prefix, String uri) { ! return namespaceNode(new QName(prefix, uri, "xmlns")); ! } ! ! public int namespaceNode(QName qn) { ! int lastNode = doc.getLastNode(); ! int nodeNr = doc.addNamespace(lastNode, qn); return nodeNr; } Index: NamespaceNode.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/memtree/NamespaceNode.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** NamespaceNode.java 29 Jun 2004 14:25:12 -0000 1.1 --- NamespaceNode.java 7 Aug 2004 16:28:02 -0000 1.2 *************** *** 25,36 **** import org.exist.dom.QName; import org.exist.xquery.value.Type; /** ! * A dynamically constructed namespace node. * * @author wolf */ ! public class NamespaceNode extends NodeImpl { /** --- 25,41 ---- import org.exist.dom.QName; import org.exist.xquery.value.Type; + import org.w3c.dom.Attr; + import org.w3c.dom.DOMException; + import org.w3c.dom.Element; /** ! * A dynamically constructed namespace node. Used to track namespace ! * declarations in elements. Implements Attr, so it can be treated as a normal ! * attribute. * * @author wolf */ ! public class NamespaceNode extends NodeImpl implements Attr { /** *************** *** 57,67 **** public String getPrefix() { ! QName qn = (QName)document.namePool.get(document.nodeName[nodeNumber]); return qn != null ? qn.getLocalName() : null; } public String getNamespaceURI() { ! return new String(document.characters, document.alpha[nodeNumber], ! document.alphaLen[nodeNumber]); } } --- 62,104 ---- public String getPrefix() { ! QName qn = getQName(); return qn != null ? qn.getLocalName() : null; } public String getNamespaceURI() { ! QName qn = getQName(); ! return qn != null ? qn.getNamespaceURI() : null; } + + public boolean getSpecified() { + return true; + } + + public QName getQName() { + return (QName)document.namePool.get(document.namespaceCode[nodeNumber]); + } + + public String getName() { + return getQName().toString(); + } + + /* (non-Javadoc) + * @see org.w3c.dom.Attr#getValue() + */ + public String getValue() { + return getQName().getNamespaceURI(); + } + + /* (non-Javadoc) + * @see org.w3c.dom.Attr#setValue(java.lang.String) + */ + public void setValue(String value) throws DOMException { + } + + /* (non-Javadoc) + * @see org.w3c.dom.Attr#getOwnerElement() + */ + public Element getOwnerElement() { + return (Element)document.getNode(document.namespaceParent[nodeNumber]); + } } |