| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import net.sf.saxon.event.PipelineConfiguration; |
| 11 |
import net.sf.saxon.event.ReceiverWithOutputProperties; |
| 12 |
import net.sf.saxon.event.SequenceWriter; |
| 13 |
import net.sf.saxon.lib.SaxonOutputKeys; |
| 14 |
import net.sf.saxon.ma.arrays.ArrayItem; |
| 15 |
import net.sf.saxon.ma.map.KeyValuePair; |
| 16 |
import net.sf.saxon.ma.map.MapItem; |
| 17 |
import net.sf.saxon.om.GroundedValue; |
| 18 |
import net.sf.saxon.om.Item; |
| 19 |
import net.sf.saxon.om.NodeInfo; |
| 20 |
import net.sf.saxon.om.Sequence; |
| 21 |
import net.sf.saxon.query.QueryResult; |
| 22 |
import net.sf.saxon.serialize.charcode.CharacterSet; |
| 23 |
import net.sf.saxon.serialize.codenorm.Normalizer; |
| 24 |
import net.sf.saxon.trans.Err; |
| 25 |
import net.sf.saxon.trans.XPathException; |
| 26 |
import net.sf.saxon.value.AtomicValue; |
| 27 |
import net.sf.saxon.value.StringValue; |
| 28 |
|
| 29 |
import javax.xml.transform.OutputKeys; |
| 30 |
import javax.xml.transform.stream.StreamResult; |
| 31 |
import java.io.StringWriter; |
| 32 |
import java.util.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class JSONSerializer extends SequenceWriter implements ReceiverWithOutputProperties { |
| 41 |
|
| 42 |
private boolean allowDuplicateKeys = false; |
| 43 |
private String nodeOutputMethod = "xml"; |
| 44 |
private int level = 0; |
| 45 |
private int topLevelCount = 0; |
| 46 |
private int maxLineLength = 80; |
| 47 |
|
| 48 |
private JSONEmitter emitter; |
| 49 |
private Properties outputProperties; |
| 50 |
private CharacterSet characterSet; |
| 51 |
private boolean isIndenting; |
| 52 |
private Comparator<AtomicValue> propertySorter; |
| 53 |
|
| 54 |
private boolean unfailing = false; |
| 55 |
|
| 56 |
public JSONSerializer(PipelineConfiguration pipe, JSONEmitter emitter, Properties outputProperties) throws XPathException { |
| 57 |
super(pipe); |
| 58 |
setOutputProperties(outputProperties); |
| 59 |
this.emitter = emitter; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public void setOutputProperties(Properties details) { |
| 69 |
this.outputProperties = details; |
| 70 |
if ("yes".equals(details.getProperty(SaxonOutputKeys.ALLOW_DUPLICATE_NAMES))) { |
| 71 |
allowDuplicateKeys = true; |
| 72 |
} |
| 73 |
if ("yes".equals(details.getProperty(OutputKeys.INDENT))) { |
| 74 |
isIndenting = true; |
| 75 |
} |
| 76 |
if ("yes".equals(details.getProperty(SaxonOutputKeys.UNFAILING))) { |
| 77 |
unfailing = true; |
| 78 |
allowDuplicateKeys = true; |
| 79 |
} |
| 80 |
String jnom = details.getProperty(SaxonOutputKeys.JSON_NODE_OUTPUT_METHOD); |
| 81 |
if (jnom != null) { |
| 82 |
nodeOutputMethod = jnom; |
| 83 |
} |
| 84 |
String max = details.getProperty(SaxonOutputKeys.LINE_LENGTH); |
| 85 |
if (max != null) { |
| 86 |
try { |
| 87 |
maxLineLength = Integer.parseInt(max); |
| 88 |
} catch (NumberFormatException err) { |
| 89 |
|
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public void setPropertySorter(Comparator<AtomicValue> sorter) { |
| 95 |
this.propertySorter = sorter; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public Properties getOutputProperties() { |
| 105 |
return outputProperties; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public void setNormalizer(Normalizer normalizer) { |
| 115 |
emitter.setNormalizer(normalizer); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public void setCharacterMap(CharacterMap map) { |
| 125 |
emitter.setCharacterMap(map); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
@Override |
| 135 |
public void write(Item item) throws XPathException { |
| 136 |
if (level == 0 && ++topLevelCount >= 2) { |
| 137 |
throw new XPathException("JSON output method cannot handle sequences of two or more items", "SERE0023"); |
| 138 |
} |
| 139 |
|
| 140 |
if (item instanceof AtomicValue) { |
| 141 |
emitter.writeAtomicValue((AtomicValue) item); |
| 142 |
} else if (item instanceof MapItem) { |
| 143 |
Set<String> keys = null; |
| 144 |
if (!allowDuplicateKeys) { |
| 145 |
keys = new HashSet<>(); |
| 146 |
} |
| 147 |
boolean oneLiner = !isIndenting || isOneLinerMap((MapItem) item); |
| 148 |
emitter.startMap(oneLiner); |
| 149 |
boolean first = true; |
| 150 |
List<AtomicValue> keyList = new ArrayList<>(); |
| 151 |
for (KeyValuePair pair : ((MapItem) item).keyValuePairs()) { |
| 152 |
keyList.add(pair.key); |
| 153 |
} |
| 154 |
if (propertySorter != null) { |
| 155 |
keyList.sort(propertySorter); |
| 156 |
} |
| 157 |
for (AtomicValue key : keyList) { |
| 158 |
String stringKey = key.getStringValue(); |
| 159 |
emitter.writeKey(stringKey); |
| 160 |
if (!allowDuplicateKeys && !keys.add(stringKey)) { |
| 161 |
throw new XPathException("Key value \"" + stringKey + "\" occurs more than once in JSON map", "SERE0022"); |
| 162 |
} |
| 163 |
Sequence<?> value = ((MapItem) item).get(key); |
| 164 |
writeSequence(value.materialize()); |
| 165 |
} |
| 166 |
emitter.endMap(); |
| 167 |
} else if (item instanceof ArrayItem) { |
| 168 |
boolean oneLiner = !isIndenting || isOneLinerArray((ArrayItem) item); |
| 169 |
emitter.startArray(oneLiner); |
| 170 |
boolean first = true; |
| 171 |
for (Sequence<?> member : ((ArrayItem) item).members()) { |
| 172 |
writeSequence(member.materialize()); |
| 173 |
} |
| 174 |
emitter.endArray(); |
| 175 |
} else if (item instanceof NodeInfo) { |
| 176 |
String s = serializeNode((NodeInfo) item); |
| 177 |
emitter.writeAtomicValue(new StringValue(s)); |
| 178 |
} else if (unfailing) { |
| 179 |
String s = item.getStringValue(); |
| 180 |
emitter.writeAtomicValue(new StringValue(s)); |
| 181 |
} else { |
| 182 |
throw new XPathException("JSON output method cannot handle an item of type " + item.getClass(), "SERE0021"); |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
private boolean isOneLinerArray(ArrayItem array) { |
| 188 |
int totalSize = 0; |
| 189 |
if (array.arrayLength() < 2) { |
| 190 |
return true; |
| 191 |
} |
| 192 |
for (Sequence<?> member : array.members()) { |
| 193 |
if (!(member instanceof AtomicValue)) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
totalSize += ((AtomicValue) member).getStringValueCS().length() + 1; |
| 197 |
if (totalSize > maxLineLength) { |
| 198 |
return false; |
| 199 |
} |
| 200 |
} |
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
private boolean isOneLinerMap(MapItem map) { |
| 205 |
int totalSize = 0; |
| 206 |
if (map.size() < 2) { |
| 207 |
return true; |
| 208 |
} |
| 209 |
for (KeyValuePair entry : map.keyValuePairs()) { |
| 210 |
if (!(entry.value instanceof AtomicValue)) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
totalSize += entry.key.getStringValueCS().length() + ((AtomicValue) entry.value).getStringValueCS().length() + 4; |
| 214 |
if (totalSize > maxLineLength) { |
| 215 |
return false; |
| 216 |
} |
| 217 |
} |
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
private String serializeNode(NodeInfo node) throws XPathException { |
| 223 |
StringWriter sw = new StringWriter(); |
| 224 |
Properties props = new Properties(); |
| 225 |
props.setProperty("method", nodeOutputMethod); |
| 226 |
props.setProperty("indent", "no"); |
| 227 |
props.setProperty("omit-xml-declaration", "yes"); |
| 228 |
QueryResult.serialize(node, new StreamResult(sw), props); |
| 229 |
return sw.toString().trim(); |
| 230 |
} |
| 231 |
|
| 232 |
private void writeSequence(GroundedValue<?> seq) throws XPathException { |
| 233 |
int len = seq.getLength(); |
| 234 |
if (len == 0) { |
| 235 |
emitter.writeAtomicValue(null); |
| 236 |
} else if (len == 1) { |
| 237 |
level++; |
| 238 |
write(seq.head()); |
| 239 |
level--; |
| 240 |
} else { |
| 241 |
throw new XPathException("JSON serialization: cannot handle a sequence of length " |
| 242 |
+ len + Err.depictSequence(seq), "SERE0023"); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
@Override |
| 250 |
public void close() throws XPathException { |
| 251 |
if (topLevelCount == 0) { |
| 252 |
emitter.writeAtomicValue(null); |
| 253 |
} |
| 254 |
emitter.close(); |
| 255 |
super.close(); |
| 256 |
} |
| 257 |
} |
| 258 |
|