| 1 |
package com.fasterxml.jackson.jr.stree.util; |
| 2 |
|
| 3 |
import java.util.*; |
| 4 |
|
| 5 |
import com.fasterxml.jackson.core.*; |
| 6 |
import com.fasterxml.jackson.jr.stree.JrsArray; |
| 7 |
import com.fasterxml.jackson.jr.stree.JrsObject; |
| 8 |
import com.fasterxml.jackson.jr.stree.JrsValue; |
| 9 |
|
| 10 |
/** |
| 11 |
* Helper class used by {@link JrsTreeTraversingParser} to keep track |
| 12 |
* of current location within traversed JSON tree. |
| 13 |
*/ |
| 14 |
abstract class JrsValueCursor |
| 15 |
extends JsonStreamContext |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Parent cursor of this cursor, if any; null for root |
| 19 |
* cursors. |
| 20 |
*/ |
| 21 |
protected final JrsValueCursor _parent; |
| 22 |
|
| 23 |
/** |
| 24 |
* Current field name |
| 25 |
*/ |
| 26 |
protected String _currentName; |
| 27 |
|
| 28 |
/** |
| 29 |
* @since 2.5 |
| 30 |
*/ |
| 31 |
protected java.lang.Object _currentValue; |
| 32 |
|
| 33 |
public JrsValueCursor(int contextType, JrsValueCursor p) |
| 34 |
{ |
| 35 |
super(); |
| 36 |
_type = contextType; |
| 37 |
_index = -1; |
| 38 |
_parent = p; |
| 39 |
} |
| 40 |
|
| 41 |
/* |
| 42 |
/********************************************************************** |
| 43 |
/* JsonStreamContext impl |
| 44 |
/********************************************************************** |
| 45 |
*/ |
| 46 |
|
| 47 |
// note: co-variant return type |
| 48 |
@Override |
| 49 |
public final JrsValueCursor getParent() { return _parent; } |
| 50 |
|
| 51 |
@Override |
| 52 |
public final String getCurrentName() { |
| 53 |
return _currentName; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @since 2.0 |
| 58 |
*/ |
| 59 |
public void overrideCurrentName(String name) { |
| 60 |
_currentName = name; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public java.lang.Object getCurrentValue() { |
| 65 |
return _currentValue; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void setCurrentValue(java.lang.Object v) { |
| 70 |
_currentValue = v; |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
/********************************************************** |
| 75 |
/* Extended API |
| 76 |
/********************************************************** |
| 77 |
*/ |
| 78 |
|
| 79 |
public abstract JsonToken nextToken(); |
| 80 |
public abstract JsonToken nextValue(); |
| 81 |
public abstract JsonToken endToken(); |
| 82 |
|
| 83 |
public abstract JrsValue currentNode(); |
| 84 |
public abstract boolean currentHasChildren(); |
| 85 |
|
| 86 |
/** |
| 87 |
* Method called to create a new context for iterating all |
| 88 |
* contents of the current structured value (JSON array or object) |
| 89 |
*/ |
| 90 |
public final JrsValueCursor iterateChildren() { |
| 91 |
JrsValue n = currentNode(); |
| 92 |
if (n == null) throw new IllegalStateException("No current node"); |
| 93 |
if (n.isArray()) { // false since we have already returned START_ARRAY |
| 94 |
return new ArrayCursor((JrsArray) n, this); |
| 95 |
} |
| 96 |
if (n.isObject()) { |
| 97 |
return new ObjectCursor((JrsObject) n, this); |
| 98 |
} |
| 99 |
throw new IllegalStateException("Current node of type "+n.getClass().getName()); |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
/********************************************************** |
| 104 |
/* Concrete implementations |
| 105 |
/********************************************************** |
| 106 |
*/ |
| 107 |
|
| 108 |
/** |
| 109 |
* Context matching root-level value nodes (i.e. anything other |
| 110 |
* than JSON Object and Array). |
| 111 |
* Note that context is NOT created for leaf values. |
| 112 |
*/ |
| 113 |
protected final static class RootCursor |
| 114 |
extends JrsValueCursor |
| 115 |
{ |
| 116 |
protected JrsValue _node; |
| 117 |
|
| 118 |
protected boolean _done = false; |
| 119 |
|
| 120 |
public RootCursor(JrsValue n, JrsValueCursor p) { |
| 121 |
super(JsonStreamContext.TYPE_ROOT, p); |
| 122 |
_node = n; |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public void overrideCurrentName(String name) { |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public JsonToken nextToken() { |
| 132 |
if (!_done) { |
| 133 |
_done = true; |
| 134 |
return _node.asToken(); |
| 135 |
} |
| 136 |
_node = null; |
| 137 |
return null; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public JsonToken nextValue() { return nextToken(); } |
| 142 |
@Override |
| 143 |
public JsonToken endToken() { return null; } |
| 144 |
@Override |
| 145 |
public JrsValue currentNode() { return _node; } |
| 146 |
@Override |
| 147 |
public boolean currentHasChildren() { return false; } |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Cursor used for traversing non-empty JSON Array nodes |
| 152 |
*/ |
| 153 |
protected final static class ArrayCursor |
| 154 |
extends JrsValueCursor |
| 155 |
{ |
| 156 |
protected Iterator<JrsValue> _contents; |
| 157 |
|
| 158 |
protected JrsValue _currentNode; |
| 159 |
|
| 160 |
public ArrayCursor(JrsArray n, JrsValueCursor p) { |
| 161 |
super(JsonStreamContext.TYPE_ARRAY, p); |
| 162 |
_contents = n.elements(); |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public JsonToken nextToken() |
| 167 |
{ |
| 168 |
if (!_contents.hasNext()) { |
| 169 |
_currentNode = null; |
| 170 |
return null; |
| 171 |
} |
| 172 |
_currentNode = _contents.next(); |
| 173 |
return _currentNode.asToken(); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public JsonToken nextValue() { return nextToken(); } |
| 178 |
@Override |
| 179 |
public JsonToken endToken() { return JsonToken.END_ARRAY; } |
| 180 |
|
| 181 |
@Override |
| 182 |
public JrsValue currentNode() { return _currentNode; } |
| 183 |
@Override |
| 184 |
public boolean currentHasChildren() { |
| 185 |
// note: ONLY to be called for container nodes |
| 186 |
return currentNode().size() > 0; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Cursor used for traversing non-empty JSON Object nodes |
| 192 |
*/ |
| 193 |
protected final static class ObjectCursor |
| 194 |
extends JrsValueCursor |
| 195 |
{ |
| 196 |
protected Iterator<Map.Entry<String, JrsValue>> _contents; |
| 197 |
protected Map.Entry<String, JrsValue> _current; |
| 198 |
|
| 199 |
protected boolean _needEntry; |
| 200 |
|
| 201 |
public ObjectCursor(JrsValue n, JrsValueCursor p) |
| 202 |
{ |
| 203 |
super(JsonStreamContext.TYPE_OBJECT, p); |
| 204 |
_contents = ((JrsObject) n).fields(); |
| 205 |
_needEntry = true; |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public JsonToken nextToken() |
| 210 |
{ |
| 211 |
// Need a new entry? |
| 212 |
if (_needEntry) { |
| 213 |
if (!_contents.hasNext()) { |
| 214 |
_currentName = null; |
| 215 |
_current = null; |
| 216 |
return null; |
| 217 |
} |
| 218 |
_needEntry = false; |
| 219 |
_current = _contents.next(); |
| 220 |
_currentName = (_current == null) ? null : _current.getKey(); |
| 221 |
return JsonToken.FIELD_NAME; |
| 222 |
} |
| 223 |
_needEntry = true; |
| 224 |
return _current.getValue().asToken(); |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public JsonToken nextValue() |
| 229 |
{ |
| 230 |
JsonToken t = nextToken(); |
| 231 |
if (t == JsonToken.FIELD_NAME) { |
| 232 |
t = nextToken(); |
| 233 |
} |
| 234 |
return t; |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public JsonToken endToken() { return JsonToken.END_OBJECT; } |
| 239 |
|
| 240 |
@Override |
| 241 |
public JrsValue currentNode() { |
| 242 |
return (_current == null) ? null : _current.getValue(); |
| 243 |
} |
| 244 |
@Override |
| 245 |
public boolean currentHasChildren() { |
| 246 |
// note: ONLY to be called for container nodes |
| 247 |
return currentNode().size() > 0; |
| 248 |
} |
| 249 |
} |
| 250 |
} |