Debian Codegraph @

net/sf/saxon/functions/Path_1.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.functions;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.lib.NamespaceConstant;
12 import net.sf.saxon.om.*;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.iter.AxisIterator;
15 import net.sf.saxon.tree.util.FastStringBuffer;
16 import net.sf.saxon.tree.util.Navigator;
17 import net.sf.saxon.type.Type;
18 import net.sf.saxon.value.AtomicValue;
19 import net.sf.saxon.value.StringValue;
20
21 /**
22 * Implement the fn:path function with one argument
23 */
24 public class Path_1 extends ScalarSystemFunction {
25
26 @Override
27 public AtomicValue evaluate(Item arg, XPathContext context) throws XPathException {
28 return makePath((NodeInfo)arg, context);
29 }
30
31 public static StringValue makePath(NodeInfo node, XPathContext context) {
32 if (node.getNodeKind() == Type.DOCUMENT) {
33 return StringValue.makeStringValue("/");
34 }
35 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256);
36 AxisIterator iter = node.iterateAxis(AxisInfo.ANCESTOR_OR_SELF);
37 NodeInfo n;
38 while ((n = iter.next()) != null) {
39 if (n.getParent() == null) {
40 if (n.getNodeKind() == Type.DOCUMENT) {
41 return new StringValue(fsb);
42 } else {
43 fsb.prepend("Q{http://www.w3.org/2005/xpath-functions}root()");
44 return new StringValue(fsb);
45 }
46 }
47 FastStringBuffer fsb2 = new FastStringBuffer(FastStringBuffer.C256);
48 switch (n.getNodeKind()) {
49 case Type.DOCUMENT:
50 return new StringValue(fsb);
51 case Type.ELEMENT:
52 fsb2.append("/Q{");
53 fsb2.append(n.getURI());
54 fsb2.append("}");
55 fsb2.append(n.getLocalPart());
56 fsb2.append("[" + Navigator.getNumberSimple(n, context) + "]");
57 fsb2.append(fsb);
58 fsb = fsb2;
59 break;
60 case Type.ATTRIBUTE:
61 fsb2.append("/@");
62 String attURI = n.getURI();
63 if (!"".equals(attURI)) {
64 fsb2.append("Q{");
65 fsb2.append(attURI);
66 fsb2.append("}");
67 }
68 fsb2.append(n.getLocalPart());
69 fsb2.append(fsb);
70 fsb = fsb2;
71 break;
72 case Type.TEXT:
73 fsb2.append("/text()[");
74 fsb2.append(Navigator.getNumberSimple(n, context) + "]");
75 fsb2.append(fsb);
76 fsb = fsb2;
77 break;
78 case Type.COMMENT:
79 fsb2.append("/comment()[");
80 fsb2.append(Navigator.getNumberSimple(n, context) + "]");
81 fsb2.append(fsb);
82 fsb = fsb2;
83 break;
84 case Type.PROCESSING_INSTRUCTION:
85 fsb2.append("/processing-instruction(");
86 fsb2.append(n.getLocalPart());
87 fsb2.append(")[");
88 fsb2.append(Navigator.getNumberSimple(n, context) + "]");
89 fsb2.append(fsb);
90 fsb = fsb2;
91 break;
92 case Type.NAMESPACE:
93 fsb2.append("/namespace::");
94 if (n.getLocalPart().isEmpty()) {
95 fsb2.append("*[Q{" + NamespaceConstant.FN + "}local-name()=\"\"]");
96 } else {
97 fsb.append(n.getLocalPart());
98 }
99 fsb2.append(fsb);
100 fsb = fsb2;
101 break;
102 default:
103 throw new AssertionError();
104 }
105 }
106 // should not reach here...
107 fsb.prepend("Q{http://www.w3.org/2005/xpath-functions}root()");
108 return new StringValue(fsb);
109 }
110
111 }
112
113 // Copyright (c) 2012-2015 Saxonica Limited.
114