1 /*--------------------------------------------------------------------------
2 * Copyright 2008 Taro L. Saito
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *--------------------------------------------------------------------------*/
16 //--------------------------------------
19 // JSONStreamWalkerTest.java
20 // Since: Jan 23, 2008 10:42:40 AM
24 //--------------------------------------
25 package org.xerial.util.bean;
27 import java.io.IOException;
28 import java.io.Reader;
30 import org.antlr.runtime.ANTLRReaderStream;
31 import org.antlr.runtime.Token;
32 import org.junit.Test;
33 import org.xerial.core.XerialException;
34 import org.xerial.json.JSONEvent;
35 import org.xerial.json.JSONException;
36 import org.xerial.json.JSONPullParser;
37 import org.xerial.json.impl.JSONLexer;
38 import org.xerial.util.FileResource;
39 import org.xerial.util.StopWatch;
40 import org.xerial.util.bean.sample.Gene;
41 import org.xerial.util.log.Logger;
42 import org.xerial.util.tree.TreeVisitor;
43 import org.xerial.util.tree.TreeWalker;
45 public class JSONStreamWalkerTest
47 private static Logger _logger = Logger.getLogger(JSONStreamWalkerTest.class);
49 class MyVisitor implements TreeVisitor
52 public void finish(TreeWalker walker) throws XerialException {
53 // TODO Auto-generated method stub
57 public void init(TreeWalker walker) throws XerialException {
58 // TODO Auto-generated method stub
62 public void leaveNode(String nodeName, TreeWalker walker) throws XerialException {
63 // TODO Auto-generated method stub
67 public void visitNode(String nodeName, String nodeValue, TreeWalker walker) throws XerialException {
68 // TODO Auto-generated method stub
72 public void text(String nodeName, String nodeValue, TreeWalker walker) throws XerialException {
73 // TODO Auto-generated method stub
79 private StopWatch stopWatch = new StopWatch();
81 public Reader getSampleData() throws IOException {
82 return FileResource.open(JSONStreamWalkerTest.class, "chr1.json");
86 public void walk() throws IOException, XerialException {
87 JSONStreamWalker walker = new JSONStreamWalker(getSampleData());
90 walker.walk(new MyVisitor());
91 _logger.debug("walk time: " + stopWatch.getElapsedTime());
95 public void lexerPerf() throws IOException {
96 JSONLexer lexer = new JSONLexer(new ANTLRReaderStream(getSampleData()));
98 while ((lexer.nextToken() != Token.EOF_TOKEN)) {
101 _logger.debug("lexical analysis time: " + stopWatch.getElapsedTime());
106 public void pullParserPerf() throws IOException, JSONException {
107 JSONPullParser parser = new JSONPullParser(getSampleData());
109 while (parser.next() != JSONEvent.EndJSON) {
112 _logger.debug("pull parsing time: " + stopWatch.getElapsedTime());
116 public void loadJSONPerf() throws XerialException, IOException {
118 BeanUtil.loadJSON(getSampleData(), Gene.class, new BeanHandler<Gene>() {
120 public void handle(Gene bean) throws Exception {
121 // TODO Auto-generated method stub
125 _logger.debug("loadJSON time: " + stopWatch.getElapsedTime());