Debian Codegraph @

net/sf/saxon/functions/UnparsedText.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.Controller;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.lib.Feature;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.om.ZeroOrOne;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.util.FastStringBuffer;
17 import net.sf.saxon.value.StringValue;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.InputStream;
22 import java.net.URI;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 /**
27 * Implementation of fn:unparsed-text() - with one argument or two
28 */
29 public class UnparsedText extends UnparsedTextFunction {
30
31 /**
32 * Evaluate the expression
33 *
34 * @param context the dynamic evaluation context
35 * @param arguments the values of the arguments, supplied as SequenceIterators
36 * @return the result of the evaluation, in the form of a SequenceIterator
37 * @throws net.sf.saxon.trans.XPathException
38 * if a dynamic error occurs during the evaluation of the expression
39 */
40 public ZeroOrOne<StringValue> call(XPathContext context, Sequence[] arguments) throws XPathException {
41 StringValue hrefVal = (StringValue) arguments[0].head();
42 String encoding = getArity() == 2 ? arguments[1].head().getStringValue() : null;
43 try {
44 return new ZeroOrOne<>(evalUnparsedText(hrefVal, getStaticBaseUriString(), encoding, context));
45 } catch (XPathException e) {
46 if (getArity() == 2 && e.getErrorCodeLocalPart().equals("FOUT1200")) {
47 e.setErrorCode("FOUT1190");
48 }
49 throw e;
50 }
51 }
52
53 private static final String errorValue = "\u0000";
54
55 public static StringValue evalUnparsedText(StringValue hrefVal, String base, String encoding, XPathContext context) throws XPathException {
56 CharSequence content;
57 StringValue result;
58 boolean stable = context.getConfiguration().getBooleanProperty(Feature.STABLE_UNPARSED_TEXT);
59 try {
60 if (hrefVal == null) {
61 return null;
62 }
63 String href = hrefVal.getStringValue();
64 URI absoluteURI = getAbsoluteURI(href, base, context);
65 if (stable) {
66 final Controller controller = context.getController();
67 //noinspection SynchronizationOnLocalVariableOrMethodParameter
68 synchronized(controller) {
69 Map<URI, String> cache = (Map<URI, String>)controller.getUserData("unparsed-text-cache", "");
70 if (cache != null) {
71 String existing = cache.get(absoluteURI);
72 if (existing != null) {
73 if (existing.startsWith(errorValue)) {
74 throw new XPathException(existing.substring(1), "FOUT1170");
75 }
76 return new StringValue(existing);
77 }
78 }
79 XPathException error = null;
80 try {
81 content = readFile(absoluteURI, encoding, context);
82 } catch (XPathException e) {
83 error = e;
84 content = errorValue + e.getMessage();
85 }
86 if (cache == null) {
87 cache = new HashMap<>();
88 controller.setUserData("unparsed-text-cache", "", cache);
89 cache.put(absoluteURI, content.toString());
90 }
91 if (error != null) {
92 throw error;
93 }
94 }
95 } else {
96 content = readFile(absoluteURI, encoding, context);
97 }
98 result = new StringValue(content);
99 } catch (XPathException err) {
100 err.maybeSetErrorCode("FOUT1170");
101 throw err;
102 }
103 return result;
104 }
105
106 // diagnostic method to output the octets of a file
107
108 public static void main(String[] args) throws Exception {
109 FastStringBuffer sb1 = new FastStringBuffer(FastStringBuffer.C256);
110 FastStringBuffer sb2 = new FastStringBuffer(FastStringBuffer.C256);
111 File file = new File(args[0]);
112 InputStream is = new FileInputStream(file);
113 while (true) {
114 int b = is.read();
115 if (b < 0) {
116 System.out.println(sb1);
117 System.out.println(sb2);
118 break;
119 }
120 sb1.append(Integer.toHexString(b) + " ");
121 sb2.append((char) b + " ");
122 if (sb1.length() > 80) {
123 System.out.println(sb1);
124 System.out.println(sb2);
125 sb1 = new FastStringBuffer(FastStringBuffer.C256);
126 sb2 = new FastStringBuffer(FastStringBuffer.C256);
127 }
128 }
129 is.close();
130 }
131
132 }