1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2009 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
19 // JSONStreamReader.java
\r
20 // Since: Mar 31, 2009 2:06:34 PM
\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/json/JSONStreamReader.java $
\r
24 //--------------------------------------
\r
25 package org.xerial.json;
\r
27 import java.io.IOException;
\r
28 import java.io.Reader;
\r
30 import org.xerial.core.XerialException;
\r
31 import org.xerial.util.ArrayDeque;
\r
32 import org.xerial.util.log.Logger;
\r
33 import org.xerial.util.tree.TreeEvent;
\r
34 import org.xerial.util.tree.TreeStreamReader;
\r
35 import org.xerial.util.tree.TreeEvent.EventType;
\r
36 import org.xerial.util.xml.impl.TreeEventQueue;
\r
39 * JSON stream reader
\r
44 public class JSONStreamReader implements TreeStreamReader
\r
46 private static Logger _logger = Logger.getLogger(JSONStreamReader.class);
\r
48 private final JSONPullParser jsonPullParser;
\r
49 private final TreeEventQueue eventQueue = new TreeEventQueue();
\r
50 private JSONEvent lastEvent = null;
\r
52 private ArrayDeque<TreeEvent> pendingEventQueue = new ArrayDeque<TreeEvent>();
\r
54 public JSONStreamReader(Reader jsonStream) throws IOException
\r
56 jsonPullParser = new JSONPullParser(jsonStream);
\r
59 public TreeEvent peekNext() throws XerialException
\r
61 if (!eventQueue.isEmpty())
\r
63 return eventQueue.peekFirst();
\r
66 if (lastEvent == JSONEvent.EndJSON)
\r
74 public TreeEvent next() throws XerialException
\r
76 if (!eventQueue.isEmpty())
\r
78 TreeEvent e = eventQueue.pop();
\r
82 if (lastEvent == JSONEvent.EndJSON)
\r
91 private void flushPendingEvent()
\r
93 while (!pendingEventQueue.isEmpty())
\r
94 eventQueue.push(pendingEventQueue.removeFirst());
\r
98 private void readNext() throws XerialException
\r
100 if (lastEvent == JSONEvent.EndJSON)
\r
103 lastEvent = jsonPullParser.next();
\r
108 flushPendingEvent();
\r
110 String key = jsonPullParser.getKeyName();
\r
111 pendingEventQueue.addLast(TreeEvent.newVisitEvent(key, null));
\r
116 flushPendingEvent();
\r
117 String key = jsonPullParser.getKeyName();
\r
118 eventQueue.push(TreeEvent.newLeaveEvent(key));
\r
127 String key = jsonPullParser.getKeyName();
\r
128 String value = lastEvent != JSONEvent.Null ? jsonPullParser.getText() : null;
\r
130 // if first child element is value attribute
\r
133 if (key.equals("value") && !pendingEventQueue.isEmpty())
\r
135 TreeEvent e = pendingEventQueue.peekLast();
\r
136 if (e.event == EventType.VISIT)
\r
138 pendingEventQueue.removeLast();
\r
139 pendingEventQueue.addLast(TreeEvent.newVisitEvent(e.nodeName, value));
\r
145 flushPendingEvent();
\r
146 eventQueue.push(TreeEvent.newVisitEvent(key, value));
\r
147 eventQueue.push(TreeEvent.newLeaveEvent(key));
\r