+++ /dev/null
-/*--------------------------------------------------------------------------\r
- * Copyright 2004 Taro L. Saito\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- *--------------------------------------------------------------------------*/\r
-//--------------------------------------\r
-// XerialJ Project\r
-//\r
-// PrePostOrderIndexer.java\r
-// Since: 2005/06/03\r
-//\r
-// $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/xml/index/PrePostOrderIndexer.java $ \r
-// $Author: leo $\r
-//--------------------------------------\r
-package org.xerial.util.xml.index;\r
-\r
-import java.io.OutputStream;\r
-import java.io.PrintStream;\r
-\r
-import org.xerial.util.ArrayDeque;\r
-import org.xerial.util.Deque;\r
-import org.xerial.util.Pair;\r
-import org.xerial.util.xml.XMLException;\r
-import org.xerial.util.xml.pullparser.AbstractSAXEventHandler;\r
-import org.xmlpull.v1.XmlPullParser;\r
-\r
-/**\r
- * pre-post order labeling\r
- * \r
- * @author leo\r
- * \r
- */\r
-public class PrePostOrderIndexer extends AbstractSAXEventHandler\r
-{\r
- private final static String EMPTY_STR = "";\r
- private int _preOrder = 0;\r
- private int _postOrder = 0;\r
- private Deque<Pair<Integer, String>> _nodeStack;\r
-\r
- private final PrintStream out;\r
-\r
- /**\r
- * \r
- */\r
- public PrePostOrderIndexer(OutputStream out)\r
- {\r
- this.out = new PrintStream(out);\r
- }\r
-\r
- @Override\r
- public void startDocument(XmlPullParser parser) throws XMLException\r
- {\r
- _nodeStack = new ArrayDeque<Pair<Integer, String>>();\r
- _preOrder = 0;\r
- _postOrder = 0;\r
- pushStack();\r
- }\r
-\r
- private void pushStack()\r
- {\r
- _nodeStack.add(new Pair<Integer, String>(_preOrder++, EMPTY_STR));\r
- }\r
-\r
- private void popStack(String tagName)\r
- {\r
- Pair<Integer, String> node = _nodeStack.removeLast();\r
- int preOrder = node.getFirst();\r
- int postOrder = _postOrder++;\r
- // output node info\r
- out.println(preOrder + "\t" + postOrder + "\t" + tagName + "\t"\r
- + (node.getSecond() != null ? node.getSecond().trim() : ""));\r
- }\r
-\r
- @Override\r
- public void endDocument(XmlPullParser parser) throws XMLException\r
- {\r
- popStack("_root");\r
- }\r
-\r
- @Override\r
- public void startTag(XmlPullParser parser) throws XMLException\r
- {\r
- pushStack();\r
- }\r
-\r
- @Override\r
- public void endTag(XmlPullParser parser) throws XMLException\r
- {\r
- popStack(parser.getName());\r
- }\r
-\r
- @Override\r
- public void text(XmlPullParser parser) throws XMLException\r
- {\r
- Pair<Integer, String> currentNode = _nodeStack.removeLast();\r
- _nodeStack.addLast(new Pair<Integer, String>(currentNode.getFirst(), parser.getText()));\r
- }\r
-\r
-}\r