KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > util > tree > SyntaxAnalyser


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.util.tree;
24
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.apache.log4j.Category;
32
33 import com.wutka.dtd.DTD;
34 import com.wutka.dtd.DTDCardinal;
35 import com.wutka.dtd.DTDChoice;
36 import com.wutka.dtd.DTDElement;
37 import com.wutka.dtd.DTDItem;
38 import com.wutka.dtd.DTDMixed;
39 import com.wutka.dtd.DTDName;
40 import com.wutka.dtd.DTDParser;
41 import com.wutka.dtd.DTDSequence;
42 /**
43  * This object analyse a DTD file and store the structure of the XML tree, to be
44  * able to know the rigth structure of a scenario tree
45  *
46  * @author JC Meillaud
47  * @author A Peyrard
48  */

49 public class SyntaxAnalyser {
50     static Category cat = Category.getInstance(SyntaxAnalyser.class.getName()) ;
51     
52     /**
53      * Define the differents occurences of a node type
54      */

55     public final static char MULTIPLE = '*';
56     public final static char UNIQUE = '!';
57     public final static char MAYBE = '?';
58     public final static char STRICT_MULTIPLE = '+';
59
60     private Hashtable JavaDoc types;
61     /**
62      * Build a new object SyntaxAnalyser, it will analyse the DTD file, which
63      * the name is given in parameter
64      *
65      * @param fileName
66      * The name of the DTD file
67      */

68     public SyntaxAnalyser(String JavaDoc fileName) {
69         cat.debug("-> constructor") ;
70         // init the table which will store the types
71
this.types = new Hashtable JavaDoc();
72         this.init(fileName);
73     }
74
75     /**
76      * Initialise the table which will store all the types and them sub-types
77      *
78      * @param fileName
79      * The dtd file to be parsed
80      */

81     private void init(String JavaDoc fileName) {
82         cat.debug("-> init") ;
83         try {
84             InputStream JavaDoc is = SyntaxAnalyser.class.getClassLoader().getResourceAsStream(fileName) ;
85             InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(is) ;
86             DTDParser dtdParser = new DTDParser(reader);
87             DTD dtd = dtdParser.parse(true);
88             Enumeration JavaDoc e = dtd.elements.elements();
89             while (e.hasMoreElements()) {
90                 DTDElement elem = (DTDElement) e.nextElement();
91                 if (Node.isBehaviorsNode(elem.name)) {
92                     Hashtable JavaDoc children = new Hashtable JavaDoc();
93                     dumpDTDItem(elem.content, children, UNIQUE);
94                     this.types.put(elem.name, children);
95                 }
96             }
97         } catch (Exception JavaDoc e) {
98             cat.fatal("Unable to find the file or bad syntax : " + fileName + " -> " + e);
99         }
100     }
101
102     /**
103      * Analyse a DTDItem, and look for children items
104      *
105      * @param item
106      * The item to be analised
107      * @param children
108      * The item children
109      */

110     private static void dumpDTDItem(DTDItem item, Hashtable JavaDoc children, char card) {
111         cat.debug("-> dumpDTDItem") ;
112         if (item == null)
113             return;
114
115         if (item instanceof DTDName) {
116             if (Node.isBehaviorsNode(((DTDName) item).value)) {
117                 // analyse the cardinality of the item
118
if (item.cardinal == DTDCardinal.OPTIONAL)
119                     card = MAYBE;
120                 else if (item.cardinal == DTDCardinal.ZEROMANY)
121                     card = MULTIPLE;
122                 else if (item.cardinal == DTDCardinal.ONEMANY)
123                     card = STRICT_MULTIPLE;
124                 children.put(((DTDName) item).value, new Character JavaDoc(card));
125             }
126         } else if (item instanceof DTDChoice) {
127             DTDItem[] items = ((DTDChoice) item).getItems();
128             // analyse the cardinality of the item
129
if (item.cardinal == DTDCardinal.OPTIONAL)
130                 card = MAYBE;
131             else if (item.cardinal == DTDCardinal.ZEROMANY)
132                 card = MULTIPLE;
133             else if (item.cardinal == DTDCardinal.ONEMANY)
134                 card = STRICT_MULTIPLE;
135             for (int i = 0; i < items.length; i++) {
136                 dumpDTDItem(items[i], children, card);
137             }
138         } else if (item instanceof DTDSequence) {
139             DTDItem[] items = ((DTDSequence) item).getItems();
140             // analyse the cardinality of the item
141
if (item.cardinal == DTDCardinal.OPTIONAL)
142                 card = MAYBE;
143             else if (item.cardinal == DTDCardinal.ZEROMANY)
144                 card = MULTIPLE;
145             else if (item.cardinal == DTDCardinal.ONEMANY)
146                 card = STRICT_MULTIPLE;
147
148             for (int i = 0; i < items.length; i++) {
149                 dumpDTDItem(items[i], children, card);
150             }
151         } else if (item instanceof DTDMixed) {
152             DTDItem[] items = ((DTDMixed) item).getItems();
153
154             for (int i = 0; i < items.length; i++) {
155                 dumpDTDItem(items[i], children, card);
156             }
157         }
158     }
159
160     /**
161      * Use to know the sub-node that can be added to a specific node
162      *
163      * @param type
164      * The type of the node
165      * @return A vector containing the nodes types allowed
166      */

167     public Hashtable JavaDoc childrenAllowed(String JavaDoc type) {
168         cat.debug("-> childrenAllowed" + type) ;
169         if (types.containsKey(type))
170             return (Hashtable JavaDoc) types.get(type);
171         return new Hashtable JavaDoc();
172     }
173
174     /**
175      * This method sort the different types by type
176      *
177      * @param entries
178      * The entries table
179      * @return The hashtable sorted
180      */

181     public static Vector JavaDoc sortByType(Vector JavaDoc entries) {
182         cat.debug("-> sortByType") ;
183         if (entries == null)
184             return null ;
185         // build all the Vector to store each nodes types
186
Vector JavaDoc result = new Vector JavaDoc();
187         Vector JavaDoc objects = new Vector JavaDoc();
188         Vector JavaDoc samples = new Vector JavaDoc();
189         Vector JavaDoc timers = new Vector JavaDoc();
190         Vector JavaDoc controlers = new Vector JavaDoc();
191         Vector JavaDoc nonePlugins = new Vector JavaDoc();
192         Vector JavaDoc structures = new Vector JavaDoc();
193         // enumerate each elements
194
Enumeration JavaDoc keys = entries.elements();
195         while (keys.hasMoreElements()) {
196             String JavaDoc type = (String JavaDoc) keys.nextElement();
197             if (Node.isControllerNode(type)) {
198                 controlers.add(type);
199                 continue;
200             }
201             if (type.equals(Node.SAMPLE)) {
202                 samples.add(type);
203                 continue;
204             }
205             if (type.equals(Node.TIMER)) {
206                 timers.add(type);
207                 continue;
208             }
209             if (type.equals(Node.TIMER)) {
210                 timers.add(type);
211                 continue;
212             }
213             if (Node.isStructureNode(type)) {
214                 structures.add(type);
215                 continue;
216             }
217             if (type.equals(Node.USE)) {
218                 objects.add(type) ;
219                 continue ;
220             }
221             nonePlugins.add(type) ;
222         }
223         // concat all the table
224
result.addAll(objects) ;
225         result.addAll(samples) ;
226         result.addAll(timers) ;
227         result.addAll(controlers) ;
228         result.addAll(nonePlugins) ;
229         result.addAll(structures) ;
230         
231         return result ;
232     }
233
234 }
Popular Tags