| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 28 |
|
| 29 |
public class UnparsedText extends UnparsedTextFunction { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 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 |
|
| 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 |
} |