+++ /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
-// StrongDataGuide.java\r
-// Since: 2005/06/02\r
-//\r
-// $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/xml/index/StrongDataGuide.java $ \r
-// $Author: leo $\r
-//--------------------------------------\r
-package org.xerial.util.xml.index;\r
-\r
-import static org.xmlpull.v1.XmlPullParser.*;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.FileNotFoundException;\r
-import java.io.FileReader;\r
-import java.io.IOException;\r
-import java.io.OutputStream;\r
-import java.io.PrintWriter;\r
-import java.io.Reader;\r
-import java.util.Collection;\r
-import java.util.Stack;\r
-\r
-import org.xerial.core.XerialException;\r
-import org.xerial.util.StringUtil;\r
-import org.xerial.util.graph.AdjacencyList;\r
-import org.xerial.util.graph.Edge;\r
-import org.xerial.util.graph.Graph;\r
-import org.xerial.util.log.Logger;\r
-import org.xerial.util.opt.Argument;\r
-import org.xerial.util.opt.Option;\r
-import org.xerial.util.opt.OptionParser;\r
-import org.xerial.util.opt.OptionParserException;\r
-import org.xerial.util.xml.SinglePath;\r
-import org.xerial.util.xml.XMLErrorCode;\r
-import org.xerial.util.xml.XMLException;\r
-import org.xerial.util.xml.pullparser.PullParserUtil;\r
-import org.xmlpull.v1.XmlPullParser;\r
-import org.xmlpull.v1.XmlPullParserException;\r
-\r
-/**\r
- * StrongDataGuide, which aggregates XML nodes based on same paths\r
- * \r
- * @author leo\r
- * \r
- */\r
-public class StrongDataGuide {\r
- private Graph<SinglePath, String> _graph = new AdjacencyList<SinglePath, String>();\r
- private int _currentPathID = 0;\r
- private Stack<Integer> _cursorHistory = new Stack<Integer>();\r
- private SinglePath _currentPath;\r
- private SinglePath _rootPath;\r
- private int _rootPathID;\r
- private Logger _logger = Logger.getLogger(StrongDataGuide.class);\r
-\r
- /**\r
- * \r
- */\r
- public StrongDataGuide() {\r
- super();\r
- _rootPath = SinglePath.rootPath();\r
- _rootPathID = _graph.addNode(_rootPath);\r
- }\r
-\r
- /**\r
- * Generates the StrongDataGuide from the XML file\r
- * \r
- * @param xmlFile\r
- * @throws FileNotFoundException\r
- * @throws XMLParserException\r
- * @throws XMLException\r
- * @throws IOException\r
- * @throws XerialException\r
- */\r
- public void generateFrom(String xmlFile) throws FileNotFoundException, XMLException,\r
- IOException, XerialException {\r
- Reader reader = new BufferedReader(new FileReader(xmlFile));\r
- generateFrom(reader);\r
- }\r
-\r
- /**\r
- * Generates the {@link StrongDataGuide} from the {@link Reader}\r
- * \r
- * @param xmlReader\r
- * @throws FileNotFoundException\r
- * @throws XMLParserException\r
- */\r
- public void generateFrom(Reader xmlReader) throws XMLException, IOException, XerialException {\r
- // initialize\r
- _currentPathID = _rootPathID;\r
- _currentPath = _rootPath;\r
-\r
- XmlPullParser parser = PullParserUtil.newParser(xmlReader);\r
- try {\r
- int state;\r
- while ((state = parser.next()) != END_DOCUMENT) {\r
- switch (state) {\r
- case START_TAG:\r
- String name = parser.getName();\r
- _logger.trace("start tag: " + name);\r
- int pathID = getPathID(name);\r
- _logger.trace("path ID : " + pathID);\r
- moveCursor(pathID);\r
- // process attributes\r
- for (int i = 0; i < parser.getAttributeCount(); i++) {\r
- int attributeID = getPathID(String.format("%s@%s", name, parser\r
- .getAttributeName(i)));\r
- moveCursor(attributeID);\r
- traceBack();\r
- }\r
- break;\r
- case END_TAG:\r
- traceBack();\r
- break;\r
- case TEXT:\r
- break;\r
- }\r
- }\r
-\r
- }\r
- catch (XmlPullParserException e) {\r
- throw new XMLException(XMLErrorCode.PARSE_ERROR, e);\r
- }\r
- }\r
-\r
- private void moveCursor(int pathID) {\r
- Collection<Integer> destNodeID = _graph.getDestNodeIDSetOf(_currentPathID);\r
- if (!destNodeID.contains(pathID)) {\r
- _graph.addEdge(new Edge(_currentPathID, pathID), "edge");\r
- }\r
- _cursorHistory.push(_currentPathID);\r
- _currentPathID = pathID;\r
- _currentPath = _graph.getNodeLabel(pathID);\r
- }\r
-\r
- private void traceBack() {\r
- assert !_cursorHistory.empty();\r
- _currentPathID = _cursorHistory.pop();\r
- _currentPath = _graph.getNodeLabel(_currentPathID);\r
- }\r
-\r
- private int getPathID(String tagName) {\r
- SinglePath path = new SinglePath(_currentPath, tagName);\r
- int pathID = _graph.getNodeID(path);\r
- if (pathID == -1) {\r
- pathID = _graph.addNode(path);\r
- }\r
- return pathID;\r
- }\r
-\r
- public void outputGraphviz(OutputStream out) {\r
- PrintWriter gout = new PrintWriter(out);\r
- gout.println("digraph G {");\r
- // output node labels\r
- for (int pathID : _graph.getNodeIDSet()) {\r
- SinglePath path = _graph.getNodeLabel(pathID);\r
- gout.println(pathID + " [label=" + StringUtil.quote(path.getLeaf(), "\"") + "];");\r
- }\r
- for (int pathID : _graph.getNodeIDSet()) {\r
- for (int destNodeID : _graph.getDestNodeIDSetOf(pathID)) {\r
- gout.println(pathID + " -> " + destNodeID + ";");\r
- }\r
- }\r
-\r
- gout.println("}");\r
- gout.flush();\r
- }\r
-\r
- @Option(symbol = "h", longName = "help", description = "display help messsage")\r
- boolean displayHelp = false;\r
-\r
- @Argument(index = 0, required = false)\r
- String xmlFile = null;\r
-\r
- public static void main(String[] args) throws OptionParserException {\r
- StrongDataGuide sdg = new StrongDataGuide();\r
- OptionParser opt = new OptionParser(sdg);\r
-\r
- opt.parse(args);\r
- if (sdg.displayHelp || sdg.xmlFile == null) {\r
- printHelpMessage(opt);\r
- return;\r
- }\r
-\r
- try {\r
- sdg.generateFrom(sdg.xmlFile);\r
- sdg.outputGraphviz(System.out);\r
- }\r
- catch (XerialException e) {\r
- System.err.println(e.getMessage());\r
- }\r
- catch (IOException e) {\r
- System.err.println(e.getMessage());\r
- }\r
- }\r
-\r
- private static void printHelpMessage(OptionParser opt) {\r
- opt.printUsage();\r
- }\r
-\r
-}\r