| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.ma.json; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.functions.SystemFunction; |
| 12 |
import net.sf.saxon.om.Function; |
| 13 |
import net.sf.saxon.om.Item; |
| 14 |
import net.sf.saxon.om.Sequence; |
| 15 |
import net.sf.saxon.serialize.charcode.UTF16CharacterSet; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 18 |
import net.sf.saxon.type.SpecificFunctionType; |
| 19 |
import net.sf.saxon.value.SequenceType; |
| 20 |
import net.sf.saxon.value.StringValue; |
| 21 |
|
| 22 |
import java.util.function.IntPredicate; |
| 23 |
|
| 24 |
import java.util.Map; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class JsonHandler { |
| 30 |
|
| 31 |
public boolean escape; |
| 32 |
protected IntPredicate charChecker; |
| 33 |
private XPathContext context; |
| 34 |
|
| 35 |
private Function fallbackFunction = null; |
| 36 |
private static final String REPLACEMENT = "\ufffd"; |
| 37 |
|
| 38 |
public void setContext(XPathContext context) { |
| 39 |
this.context = context; |
| 40 |
} |
| 41 |
|
| 42 |
public XPathContext getContext() { |
| 43 |
return context; |
| 44 |
} |
| 45 |
|
| 46 |
public Sequence<?> getResult() throws XPathException { |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public boolean setKey(String unEscaped, String reEscaped) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public void startArray() throws XPathException {} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public void endArray() throws XPathException {} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public void startMap() throws XPathException {} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public void endMap() throws XPathException {} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public void writeNumeric(String asString, double asDouble) throws XPathException {} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public void writeString(String val) throws XPathException {} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public String reEscape(String val) throws XPathException { |
| 118 |
CharSequence escaped; |
| 119 |
if (escape) { |
| 120 |
escaped = JsonReceiver.escape(val, true, new IntPredicate() { |
| 121 |
public boolean test(int value) { |
| 122 |
return (value >= 0 && value <= 0x1F) || |
| 123 |
(value >= 0x7F && value <= 0x9F) || |
| 124 |
!charChecker.test(value) || |
| 125 |
(value == 0x5C); |
| 126 |
} |
| 127 |
}); |
| 128 |
} else { |
| 129 |
FastStringBuffer buffer = new FastStringBuffer(val); |
| 130 |
handleInvalidCharacters(buffer); |
| 131 |
escaped = buffer; |
| 132 |
} |
| 133 |
return escaped.toString(); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public void writeBoolean(boolean value) throws XPathException {} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public void writeNull() throws XPathException {} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
protected void handleInvalidCharacters(FastStringBuffer buffer) throws XPathException { |
| 156 |
|
| 157 |
IntPredicate charChecker = context.getConfiguration().getValidCharacterChecker(); |
| 158 |
for (int i = 0; i < buffer.length(); i++) { |
| 159 |
char ch = buffer.charAt(i); |
| 160 |
if (UTF16CharacterSet.isHighSurrogate(ch)) { |
| 161 |
if (i + 1 >= buffer.length() || !UTF16CharacterSet.isLowSurrogate(buffer.charAt(i + 1))) { |
| 162 |
substitute(buffer, i, 1, context); |
| 163 |
} |
| 164 |
} else if (UTF16CharacterSet.isLowSurrogate(ch)) { |
| 165 |
if (i == 0 || !UTF16CharacterSet.isHighSurrogate(buffer.charAt(i - 1))) { |
| 166 |
substitute(buffer, i, 1, context); |
| 167 |
} else { |
| 168 |
int pair = UTF16CharacterSet.combinePair(buffer.charAt(i - 1), ch); |
| 169 |
if (!charChecker.test(pair)) { |
| 170 |
substitute(buffer, i - 1, 2, context); |
| 171 |
} |
| 172 |
} |
| 173 |
} else { |
| 174 |
if (!charChecker.test(ch)) { |
| 175 |
substitute(buffer, i, 1, context); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
protected void markAsEscaped(CharSequence escaped, boolean isKey) throws XPathException { |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
private void substitute(FastStringBuffer buffer, int offset, int count, XPathContext context) throws XPathException { |
| 197 |
FastStringBuffer escaped = new FastStringBuffer(count*6); |
| 198 |
for (int j=0; j<count; j++) { |
| 199 |
escaped.append("\\u"); |
| 200 |
String hex = Integer.toHexString(buffer.charAt(offset + j)); |
| 201 |
while (hex.length() < 4) { |
| 202 |
hex = "0" + hex; |
| 203 |
} |
| 204 |
hex = hex.toUpperCase(); |
| 205 |
escaped.append(hex); |
| 206 |
} |
| 207 |
String replacement = replace(escaped.toString(), context); |
| 208 |
if (replacement.length() == count) { |
| 209 |
for (int j = 0; j < count; j++) { |
| 210 |
buffer.setCharAt(offset + j, replacement.charAt(j)); |
| 211 |
} |
| 212 |
} else { |
| 213 |
for (int j = 0; j < count; j++) { |
| 214 |
buffer.removeCharAt(offset + j); |
| 215 |
} |
| 216 |
for (int j=0; j < replacement.length(); j++) { |
| 217 |
buffer.insert(offset + j, replacement.charAt(j)); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
private String replace(String s, XPathContext context) throws XPathException { |
| 232 |
if (fallbackFunction != null) { |
| 233 |
Sequence<?>[] args = new Sequence[1]; |
| 234 |
args[0] = new StringValue(s); |
| 235 |
Sequence<?> result = SystemFunction.dynamicCall(fallbackFunction, context, args).head(); |
| 236 |
Item first = result.head(); |
| 237 |
return first == null ? "" : first.getStringValue(); |
| 238 |
} else { |
| 239 |
return REPLACEMENT; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
public void setFallbackFunction(Map<String, Sequence<?>> options, XPathContext context) throws XPathException { |
| 244 |
Sequence<?> val = options.get("fallback"); |
| 245 |
if (val != null) { |
| 246 |
Item fn = val.head(); |
| 247 |
if (fn instanceof Function) { |
| 248 |
fallbackFunction = (Function) fn; |
| 249 |
if (fallbackFunction.getArity() != 1) { |
| 250 |
throw new XPathException("Fallback function must have arity=1", "FOJS0005"); |
| 251 |
} |
| 252 |
SpecificFunctionType required = new SpecificFunctionType( |
| 253 |
new SequenceType[]{SequenceType.SINGLE_STRING}, SequenceType.ANY_SEQUENCE); |
| 254 |
if (!required.matches(fallbackFunction, context.getConfiguration().getTypeHierarchy())) { |
| 255 |
throw new XPathException("Fallback function does not match the required type", "FOJS0005"); |
| 256 |
} |
| 257 |
} else { |
| 258 |
throw new XPathException("Value of option 'fallback' is not a function", "FOJS0005"); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|