1 /*--------------------------------------------------------------------------
2 * Copyright 2004 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 // XMLNodeRelationAnalyzer.java
24 //--------------------------------------
25 package org.xerial.util.xml.analyzer;
27 import static org.xmlpull.v1.XmlPullParser.*;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.io.Reader;
32 import java.util.TreeMap;
33 import java.util.TreeSet;
35 import org.xerial.core.XerialException;
36 import org.xerial.util.opt.Argument;
37 import org.xerial.util.opt.Option;
38 import org.xerial.util.opt.OptionParser;
39 import org.xerial.util.opt.OptionParserException;
40 import org.xerial.util.xml.SinglePathBuilder;
41 import org.xerial.util.xml.XMLErrorCode;
42 import org.xerial.util.xml.XMLException;
43 import org.xerial.util.xml.pullparser.PullParserUtil;
44 import org.xmlpull.v1.XmlPullParser;
45 import org.xmlpull.v1.XmlPullParserException;
48 * Analyzes ancestor-descendant and parent-child relationships of XML data
53 public class XMLNodeRelationAnalyzer {
58 @Option(symbol = "h", longName = "help", description = "display help message")
59 private boolean displayHelp = false;
61 @Argument(index = 0, name = "XMLFile")
62 private String xmlFile = null;
64 private OptionParser _opt = new OptionParser(this);
67 * @throws OptionParserException
70 public XMLNodeRelationAnalyzer() throws OptionParserException {
74 public static void main(String[] args) {
76 XMLNodeRelationAnalyzer instance = new XMLNodeRelationAnalyzer();
77 instance.perform(args);
79 catch (XerialException e) {
80 System.err.println(e.getMessage());
82 catch (IOException e) {
83 System.err.println(e.getMessage());
87 public void perform(String[] args) throws OptionParserException, IOException, XerialException {
90 if (displayHelp || xmlFile == null) {
95 Reader xmlFileReader = new FileReader(xmlFile);
96 XmlPullParser parser = PullParserUtil.newParser(xmlFileReader);
98 ParseProcess parseProcess = new ParseProcess(parser);
101 for (String tag : parseProcess.tag2relatedTagNames.keySet()) {
102 System.out.print(tag + ":\t");
103 for (String related : parseProcess.tag2relatedTagNames.get(tag)) {
104 System.out.print(related + " ");
106 System.out.println();
109 xmlFileReader.close();
117 XmlPullParser parser;
118 SinglePathBuilder currentPath = new SinglePathBuilder();
119 TreeMap<String, TreeSet<String>> tag2relatedTagNames = new TreeMap<String, TreeSet<String>>();
122 ParseProcess(XmlPullParser parser) {
123 this.parser = parser;
126 void parse() throws IOException, XMLException {
131 while ((state = parser.next()) != END_DOCUMENT) {
134 String currentTag = parser.getName();
135 currentPath.addChild(currentTag);
137 TreeSet<String> relatedTagSet = getRelatedTagSet(currentTag);
138 for (String t : currentPath) {
139 if (!t.equals(currentTag)) {
140 relatedTagSet.add(t);
141 TreeSet<String> relatedTagSet2 = getRelatedTagSet(t);
142 relatedTagSet2.add(currentTag);
145 // TODO attribute processing
146 for (int i = 0; i < parser.getAttributeCount(); i++) {
151 currentPath.removeLeaf();
160 catch (XmlPullParserException e) {
161 throw new XMLException(XMLErrorCode.PARSE_ERROR, e);
166 private TreeSet<String> getRelatedTagSet(String tag) {
167 TreeSet<String> s = tag2relatedTagNames.get(tag);
169 s = new TreeSet<String>();
170 tag2relatedTagNames.put(tag, s);
177 private void printHelpMessage() {
178 System.out.println("usage: > java XMLNodeRelationAnalyzer.jar [option] xml_file");