Debian Codegraph @

net/sf/saxon/ma/json/JsonHandlerMap.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.ma.json;
9
10 import net.sf.saxon.expr.XPathContext;
11 import net.sf.saxon.ma.arrays.ArrayItem;
12 import net.sf.saxon.ma.arrays.SimpleArrayItem;
13 import net.sf.saxon.ma.map.DictionaryMap;
14 import net.sf.saxon.ma.map.MapItem;
15 import net.sf.saxon.om.GroundedValue;
16 import net.sf.saxon.om.Sequence;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.value.BooleanValue;
19 import net.sf.saxon.value.DoubleValue;
20 import net.sf.saxon.value.EmptySequence;
21 import net.sf.saxon.value.StringValue;
22
23 import java.util.ArrayList;
24 import java.util.Stack;
25
26 /**
27 * Event handler for the JSON parser which constructs a structure of maps and arrays
28 * representing the content of the JSON text.
29 */
30 public class JsonHandlerMap extends JsonHandler {
31 Stack<Sequence<?>> stack;
32
33 protected Stack<String> keys;
34
35 public JsonHandlerMap(XPathContext context, int flags) {
36 setContext(context);
37 stack = new Stack<>();
38 keys = new Stack<>();
39 escape = (flags & JsonParser.ESCAPE) != 0;
40 charChecker = context.getConfiguration().getValidCharacterChecker();
41 }
42
43 public Sequence<?> getResult() {
44 return stack.peek();
45 }
46
47 /**
48 * Set the key to be written for the next entry in an object/map
49 *
50 * @param unEscaped the key for the entry (null implies no key) in unescaped form (backslashes,
51 * if present, do not signal an escape sequence)
52 * @param reEscaped the key for the entry (null implies no key) in reescaped form. In this form
53 * special characters are represented as backslash-escaped sequences if the escape
54 * option is yes; if escape=no, the reEscaped form is the same as the unEscaped form.
55 * @return true if the key is already present in the map, false if it is not
56 */
57 public boolean setKey(String unEscaped, String reEscaped) {
58 this.keys.push(reEscaped);
59 MapItem map = (MapItem) stack.peek();
60 return map.get(new StringValue(reEscaped)) != null;
61 }
62
63 /**
64 * Open a new array
65 *
66 */
67 public void startArray() {
68 ArrayItem map = new SimpleArrayItem(new ArrayList<>());
69 stack.push(map);
70 }
71
72 /**
73 * Close the current array
74 */
75 public void endArray() {
76 ArrayItem map = (ArrayItem) stack.pop();
77 if (stack.empty()) {
78 stack.push(map); // the end
79 } else {
80 writeItem(map);
81 }
82 }
83
84 /**
85 * Start a new object/map
86 */
87 public void startMap() {
88 DictionaryMap map = new DictionaryMap();
89 stack.push(map);
90 }
91
92 /**
93 * Close the current object/map
94 */
95 public void endMap() {
96 DictionaryMap map = (DictionaryMap) stack.pop();
97 if (stack.empty()) {
98 stack.push(map); // the end
99 } else {
100 writeItem(map);
101 }
102 }
103
104 /**
105 * Write an item into the current map, with the preselected key
106 * @param val the value/map to be written
107 */
108 private void writeItem(GroundedValue<?> val) {
109 if (stack.empty()) {
110 stack.push(val);
111 } else if (stack.peek() instanceof ArrayItem) {
112 SimpleArrayItem array = (SimpleArrayItem) stack.peek();
113 array.getMembers().add(val.materialize());
114 } else {
115 DictionaryMap map = (DictionaryMap) stack.peek();
116 //StringValue key = new StringValue(reEscape(keys.pop(), true, false, false));
117 //StringValue key = new StringValue(keys.pop());
118 map.initialPut(keys.pop(), val);
119 }
120 }
121
122 /**
123 * Write a numeric value
124 *
125 * @param asString the string representation of the value
126 * @param asDouble the double representation of the value
127 */
128 public void writeNumeric(String asString, double asDouble) {
129 writeItem(new DoubleValue(asDouble));
130 }
131
132 /**
133 * Write a string value
134 *
135 * @param val The string to be written (which may or may not contain JSON escape sequences, according to the
136 * options that were set)
137 * @throws XPathException if a dynamic error occurs
138 */
139 public void writeString(String val) throws XPathException {
140 writeItem(new StringValue(reEscape(val)));
141 }
142
143 /**
144 * Write a boolean value
145 *
146 * @param value the boolean value to be written
147 */
148 public void writeBoolean(boolean value) {
149 writeItem(BooleanValue.get(value));
150 }
151
152 /**
153 * Write a null value
154 */
155 public void writeNull() {
156 writeItem(EmptySequence.getInstance());
157 }
158
159
160 }