OSDN Git Service

deprecated the org.xerial.cui.OptionParser.
[xerial/xerial-core.git] / src / main / java / org / xerial / util / xml / analyzer / XMLNodeRelationAnalyzer.java
1 /*--------------------------------------------------------------------------
2  *  Copyright 2004 Taro L. Saito
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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 //--------------------------------------
17 // XerialJ Project
18 //
19 // XMLNodeRelationAnalyzer.java
20 // Since: 2005/05/31
21 //
22 // $URL$ 
23 // $Author$
24 //--------------------------------------
25 package org.xerial.util.xml.analyzer;
26
27 import static org.xmlpull.v1.XmlPullParser.*;
28
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;
34
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;
46
47 /**
48  * Analyzes ancestor-descendant and parent-child relationships of XML data
49  * 
50  * @author leo
51  * 
52  */
53 public class XMLNodeRelationAnalyzer {
54     private enum Opt {
55         help
56     }
57
58     @Option(symbol = "h", longName = "help", description = "display help message")
59     private boolean displayHelp = false;
60
61     @Argument(index = 0, name = "XMLFile")
62     private String xmlFile = null;
63
64     private OptionParser _opt = new OptionParser(this);
65
66     /**
67      * @throws OptionParserException
68      * 
69      */
70     public XMLNodeRelationAnalyzer() throws OptionParserException {
71
72     }
73
74     public static void main(String[] args) {
75         try {
76             XMLNodeRelationAnalyzer instance = new XMLNodeRelationAnalyzer();
77             instance.perform(args);
78         }
79         catch (XerialException e) {
80             System.err.println(e.getMessage());
81         }
82         catch (IOException e) {
83             System.err.println(e.getMessage());
84         }
85     }
86
87     public void perform(String[] args) throws OptionParserException, IOException, XerialException {
88         _opt.parse(args);
89
90         if (displayHelp || xmlFile == null) {
91             printHelpMessage();
92             return;
93         }
94
95         Reader xmlFileReader = new FileReader(xmlFile);
96         XmlPullParser parser = PullParserUtil.newParser(xmlFileReader);
97
98         ParseProcess parseProcess = new ParseProcess(parser);
99         parseProcess.parse();
100
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 + " ");
105             }
106             System.out.println();
107         }
108
109         xmlFileReader.close();
110     }
111
112     class NodeRelation {
113
114     }
115
116     class ParseProcess {
117         XmlPullParser parser;
118         SinglePathBuilder currentPath = new SinglePathBuilder();
119         TreeMap<String, TreeSet<String>> tag2relatedTagNames = new TreeMap<String, TreeSet<String>>();
120         int pathCount = 0;
121
122         ParseProcess(XmlPullParser parser) {
123             this.parser = parser;
124         }
125
126         void parse() throws IOException, XMLException {
127
128             int state;
129
130             try {
131                 while ((state = parser.next()) != END_DOCUMENT) {
132                     switch (state) {
133                     case START_TAG:
134                         String currentTag = parser.getName();
135                         currentPath.addChild(currentTag);
136
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);
143                             }
144                         }
145                         // TODO attribute processing
146                         for (int i = 0; i < parser.getAttributeCount(); i++) {
147
148                         }
149                         break;
150                     case END_TAG:
151                         currentPath.removeLeaf();
152                         break;
153                     case START_DOCUMENT:
154                         break;
155                     case TEXT:
156                         break;
157                     }
158                 }
159             }
160             catch (XmlPullParserException e) {
161                 throw new XMLException(XMLErrorCode.PARSE_ERROR, e);
162             }
163
164         }
165
166         private TreeSet<String> getRelatedTagSet(String tag) {
167             TreeSet<String> s = tag2relatedTagNames.get(tag);
168             if (s == null) {
169                 s = new TreeSet<String>();
170                 tag2relatedTagNames.put(tag, s);
171             }
172             return s;
173         }
174
175     }
176
177     private void printHelpMessage() {
178         System.out.println("usage: > java XMLNodeRelationAnalyzer.jar [option] xml_file");
179     }
180
181 }