Project

Profile

Help

Revision 7de83e5c

Added by Michael Kay over 12 years ago

Fix bug 2779393 - JAXP XPath extension returning NodeList

View differences:

latest9.1/bj/net/sf/saxon/dom/DOMObjectModel.java
13 13
import net.sf.saxon.type.ItemType;
14 14
import net.sf.saxon.value.SequenceExtent;
15 15
import net.sf.saxon.value.Value;
16
import net.sf.saxon.xpath.XPathEvaluator;
16 17
import org.w3c.dom.Document;
17 18
import org.w3c.dom.DocumentFragment;
18 19
import org.w3c.dom.Node;
19 20
import org.w3c.dom.NodeList;
20 21

  
22
import javax.xml.namespace.NamespaceContext;
21 23
import javax.xml.parsers.DocumentBuilder;
22 24
import javax.xml.parsers.DocumentBuilderFactory;
23 25
import javax.xml.parsers.ParserConfigurationException;
......
29 31
import java.io.Serializable;
30 32
import java.lang.reflect.InvocationTargetException;
31 33
import java.lang.reflect.Method;
32
import java.util.ArrayList;
33
import java.util.HashSet;
34
import java.util.List;
34
import java.util.*;
35 35

  
36 36
/**
37 37
 * This interface must be implemented by any third-party object model that can
......
125 125
                    return AnyNodeTest.getInstance();
126 126
                }
127 127
            };
128
        } else if (NodeList.class == targetClass) {
128
        } else if (NodeList.class.isAssignableFrom(targetClass)) {
129 129
            return new JPConverter() {
130 130
                public ValueRepresentation convert(Object obj, XPathContext context) throws XPathException {
131 131
                    Configuration config = context.getConfiguration();
......
476 476
        return ((DocumentWrapper)document).wrap((Node)node);
477 477
    }
478 478

  
479
   /**
480
     * Test showing a DOM NodeList returned by an extension function
481
     */
482

  
483
    public static void main (String[] args) throws Exception {
484
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
485
        factory.setNamespaceAware(true);
486
        DocumentBuilder db = factory.newDocumentBuilder();
487
        Document doc = db.newDocument();
488
        XPathEvaluator xpe = new XPathEvaluator();
489
        String exp = "ext:sortArrayToNodeList(('fred', 'jane', 'anne', 'sue'))";
490
        xpe.setNamespaceContext(new NamespaceContext() {
491
            public String getNamespaceURI(String prefix) {
492
                return (prefix.equals("ext") ? "java:net.sf.saxon.dom.DOMObjectModel" : null);
493
            }
494
            public String getPrefix(String namespaceURI) {
495
                return null;
496
            }
497
            public Iterator getPrefixes(String namespaceURI) {
498
                return null;
499
            }
500
        });
501
        NodeList isList = (NodeList)xpe.evaluate(exp, doc, XPathConstants.NODESET);
502
        System.err.println("length " + isList.getLength());
503
    }
504

  
505
    /**
506
     * Sample extension function
507
     * @param source
508
     * @return
509
     * @throws Exception
510
     */
511
    public static NodeList sortArrayToNodeList(net.sf.saxon.value.Value source) throws Exception {
512
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
513
        factory.setNamespaceAware(true);
514
        DocumentBuilder db = factory.newDocumentBuilder();
515
        Document doc = db.newDocument();
516
        String[] items = new String[source.getLength()];
517
        for (int i = 0; i < source.getLength(); i++) {
518
            items[i] = source.itemAt(i).getStringValue();
519
        }
520
        Arrays.sort(items);
521
        List list = new ArrayList();
522
        for (int i = 0; i < items.length; i++) {
523
                list.add(doc.createTextNode(items[i]));
524
            }
525
        DOMNodeList resultSet = new DOMNodeList(list);
526
        return resultSet;
527
    }
479 528

  
480 529
}
481 530

  

Also available in: Unified diff