You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Gary L Peskin <ga...@firstech.com> on 2000/07/13 19:15:51 UTC

Xalan: result tree fragments / node-set

This is a follow up to my post from earlier this morning in the
xsl-list.  Since it is Xalan-specific, I moved it over here.

I have a Nodeset extension which should now work reliably even when
Xalan is fixed to properly detect invalid uses of Result Tree
Fragments.  This implements the node-set function found in xt and saxon
as a Xalan extension.

The inner class "fools" Xalan into thinking that a result tree fragment
is actually a Document root.

Here is the code:

package org.apache.xalan.xslt.extensions;

import org.w3c.dom.*;
import org.apache.xalan.xslt.ResultTreeFrag;
import org.apache.xalan.xpath.MutableNodeListImpl;

public class Nodeset {

  private static class RootNode implements DocumentFragment {
    private DocumentFragment actualFrag;
    private RootNode(DocumentFragment actualFragP) {
      this.actualFrag = actualFragP;
    }
    public Node appendChild(Node newChild) throws DOMException {
      return this.actualFrag.appendChild(newChild);
    }
    public Node cloneNode(boolean deep) {
      return this.actualFrag.cloneNode(deep);
    }
    public NamedNodeMap getAttributes() {
      return this.actualFrag.getAttributes();
    }
    public NodeList getChildNodes() {
      return this.actualFrag.getChildNodes();
    }
    public Node getFirstChild() {
      return this.actualFrag.getFirstChild();
    }
    public Node getLastChild() {
      return this.actualFrag.getLastChild();
    }
    public String getLocalName() {
      return this.actualFrag.getLocalName();
    }
    public String getNamespaceURI() {
      return this.actualFrag.getNamespaceURI();
    }
    public Node getNextSibling() {
      return this.actualFrag.getNextSibling();
    }
    public String getNodeName() {
      return "#document";
    }
    public short getNodeType() {
     return Node.DOCUMENT_NODE;
    }
    public String getNodeValue() throws DOMException {
     return null;
    }
    public Document getOwnerDocument() {
     return null;
    }
    public Node getParentNode() {
      return this.actualFrag.getParentNode();
    }
    public String getPrefix() {
      return this.actualFrag.getPrefix();
    }
    public Node getPreviousSibling() {
      return this.actualFrag.getPreviousSibling();
    }
    public boolean hasChildNodes() {
      return this.actualFrag.hasChildNodes();
    }
    public Node insertBefore(Node newChild, Node refChild) throws
DOMException {
      return this.actualFrag.insertBefore(newChild, refChild);
    }
    public void normalize() {
      this.actualFrag.normalize();
    }
    public Node removeChild(Node oldChild) throws DOMException {
      return this.actualFrag.removeChild(oldChild);
    }
    public Node replaceChild(Node newChild, Node oldChild) throws
DOMException {
      return this.actualFrag.replaceChild(newChild, oldChild);
    }
    public void setNodeValue(String nodeValue) throws DOMException {
      this.actualFrag.setNodeValue(nodeValue);
    }
    public void setPrefix(String prefix) throws DOMException {
      this.actualFrag.setPrefix(prefix);
    }
    public boolean supports(String feature, String version) {
      return this.actualFrag.supports(feature, version);
    }
  }

  public NodeList nodeset(Node rtf) {

    DocumentFragment cloneFrag;
    DocumentFragment retFrag;
    NodeList fragElementList;
    int i;

    if (rtf instanceof DocumentFragment) {
      cloneFrag = (DocumentFragment) rtf.cloneNode(true);
      fragElementList = cloneFrag.getChildNodes();
      retFrag = cloneFrag.getOwnerDocument().createDocumentFragment();
      // Note that retFrag.appendChild(cloneFrag) doesn't work because
      // cloneFrag is actually a ResultTreeFrag which doesn't properly
implement
      // getNextSibling() in its children.
      for (i = 0; i < fragElementList.getLength(); i++)
        retFrag.appendChild(fragElementList.item(i));
    }
    else {
      retFrag = rtf.getOwnerDocument().createDocumentFragment();
      retFrag.appendChild(rtf.cloneNode(true));
    }

    return new MutableNodeListImpl(new RootNode(retFrag));
  }                    
}

Gary