1 | package org.greenstone.gsdl3.gs3build.xpointer;
|
---|
2 |
|
---|
3 | import org.w3c.dom.*;
|
---|
4 | import org.apache.xpath.*;
|
---|
5 |
|
---|
6 | public class RangeToXPointer extends XPointer
|
---|
7 | {
|
---|
8 | public static final String RANGE_TO = "range-to(";
|
---|
9 | NodeList startNodes;
|
---|
10 | NodeList endNodes;
|
---|
11 |
|
---|
12 | public RangeToXPointer(Document document, String xpointer)
|
---|
13 | { super(document, xpointer);
|
---|
14 |
|
---|
15 | String fromPointer, toPointer;
|
---|
16 |
|
---|
17 | int rangeAt = xpointer.indexOf(RANGE_TO);
|
---|
18 |
|
---|
19 | fromPointer = xpointer.substring(0, rangeAt-1);
|
---|
20 | toPointer = xpointer.substring(rangeAt+RANGE_TO.length(), xpointer.length()-1);
|
---|
21 |
|
---|
22 | fromPointer = XPointer.expandXPath(fromPointer);
|
---|
23 | toPointer = XPointer.expandXPath(toPointer);
|
---|
24 | // System.out.println(fromPointer + " " + toPointer);
|
---|
25 |
|
---|
26 | this.startNodes = this.getNodes(fromPointer);
|
---|
27 | this.endNodes = this.getNodes(toPointer);
|
---|
28 |
|
---|
29 | // System.out.println(this.startNodes.item(0).toString() + " " + this.endNodes.item(0).toString());
|
---|
30 | }
|
---|
31 |
|
---|
32 | public Node getStartNode()
|
---|
33 | { return this.startNodes.item(0);
|
---|
34 | }
|
---|
35 |
|
---|
36 | private NodeList getNodes(String pointer)
|
---|
37 | {
|
---|
38 | try {
|
---|
39 | NodeList nodes = XPathAPI.selectNodeList(document, pointer, document.getDocumentElement());
|
---|
40 | return nodes;
|
---|
41 | }
|
---|
42 | catch (Exception ex)
|
---|
43 | { System.out.println(ex.toString());
|
---|
44 | }
|
---|
45 | return null;
|
---|
46 | }
|
---|
47 |
|
---|
48 | private void recurseStringify(Node at, Node end, StringBuffer reply)
|
---|
49 | {
|
---|
50 | while (at != end && at != null) {
|
---|
51 | this.printNode(at, reply, false);
|
---|
52 | at = this.getNextNode(at, reply);
|
---|
53 | // System.out.println(at + " " + end);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public String toString()
|
---|
58 | { StringBuffer reply = new StringBuffer();
|
---|
59 |
|
---|
60 | // skip the first node
|
---|
61 | Node firstNode = this.startNodes.item(0);
|
---|
62 | firstNode = this.getNextNode(firstNode, reply);
|
---|
63 |
|
---|
64 | // actually, we don't, as otherwise the indexed copy of
|
---|
65 | // the document would be incomplete...
|
---|
66 | this.recurseStringify(this.startNodes.item(0), this.endNodes.item(0), reply);
|
---|
67 | return reply.toString();
|
---|
68 | }
|
---|
69 | }
|
---|