KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > generator > ElementBindings


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.tools.generator;
20
21 import java.util.*;
22
23
24 /**
25  * Holds binding: element => {handler type, method name, parslet}.
26  *
27  * <!ELEMENT bind (attbind*)>
28  * <!ATTLIST bind element ID #REQUIRED>
29  * <!ATTLIST bind method CDATA #REQUIRED>
30  * <!ATTLIST bind type (data, container, ignore, mix, empty) "data">
31  * <!ATTLIST bind parslet IDREF #IMPLIED>
32  *
33  * @author Petr Kuzel
34  * @version 1.0
35  */

36 public class ElementBindings extends HashMap {
37
38     /** Serial Version UID */
39     private static final long serialVersionUID =-1529089675411853246L;
40
41     // some display names
42

43     public static final String JavaDoc DELIMITER_HANDLING =
44         Util.THIS.getString ("DELIMITER_HANDLING");
45     public static final String JavaDoc DATA_HANDLING =
46         Util.THIS.getString ("DATA_HANDLING");
47     public static final String JavaDoc IGNORE_HANDLING =
48         Util.THIS.getString ("IGNORE_HANDLING");
49     public static final String JavaDoc MIXED_HANDLING =
50         Util.THIS.getString ("MIXED_HANDLING");
51     public static final String JavaDoc EMPTY_HANDLING =
52         Util.THIS.getString ("EMPTY_HANDLING");
53     
54     /** Create empty map. */
55     public ElementBindings() {
56     }
57
58     /**
59       * Typed put.
60       * @see java.util.Map#put(Object,Object)
61       */

62     public Entry put(String JavaDoc element, Entry entry) {
63         if (element == null) return null;
64         if (element.equals(entry.getElement()) == false) return null;
65         return (Entry) super.put(element, entry);
66     }
67
68     /**
69      * Settle the map by following values.
70      */

71     public Entry put(String JavaDoc element, String JavaDoc method, String JavaDoc parslet, String JavaDoc type) {
72         return (Entry) super.put(element, new Entry(element, method, parslet, type));
73     }
74
75     /**
76      * Get a method postfix by element name.
77      */

78     public String JavaDoc getMethod(String JavaDoc element) {
79         Entry entry = getEntry(element);
80         if (entry == null) {
81             return null;
82         } else {
83             return entry.getMethod();
84         }
85     }
86
87     /**
88      * Get a parslet name by
89      */

90     public String JavaDoc getParslet(String JavaDoc element) {
91         Entry entry = getEntry(element);
92         if (entry == null) {
93             return null;
94         } else {
95             return entry.getParslet();
96         }
97     }
98     
99     /*
100      * @return true it there is used given parslet
101      */

102     public boolean containsParslet(String JavaDoc name) {
103         
104         if (name == null) return false;
105         
106         Iterator it = values().iterator();
107         while (it.hasNext()) {
108             Entry next = (Entry) it.next();
109             if (name.equals(next.parslet)) return true;
110         }
111         
112         return false;
113     }
114     
115     /**
116      * @return number of Entires using given parslet
117      */

118     public int getParsletUsageCount(String JavaDoc parslet) {
119         int toret = 0;
120         Iterator it = values().iterator();
121         
122         while (it.hasNext()) {
123             Entry next = (Entry) it.next();
124             if (parslet != null && parslet.equals(next.parslet)) {
125                 toret++;
126             }
127         }
128         
129         return toret;
130     }
131
132     /**
133      * Get entry keyed by given element name.
134      */

135     public final Entry getEntry(String JavaDoc element) {
136         return (Entry) get(element);
137     }
138
139     /**
140      * Get entry keyed by given index. Suitable for table models.
141      * @param index 0 based index
142      */

143     public final Entry getEntry(final int index) {
144         int myindex = index;
145         Iterator it = values().iterator();
146         while (it.hasNext()) {
147             Entry next = (Entry) it.next();
148             if (myindex-- == 0)
149                 return next;
150         }
151         return null;
152     }
153     
154     public String JavaDoc toString() {
155         Iterator it = values().iterator();
156         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
157         sb.append("{"); // NOI18N
158
while (it.hasNext()) {
159             sb.append(it.next());
160         }
161         sb.append("}"); // NOI18N
162
return sb.toString();
163     }
164
165     /**
166      * Holds binding element name -> (method name, hadler type, parslet name).
167      */

168     public static final class Entry {
169
170         // Entry types as clasified by a user
171

172         public static final String JavaDoc EMPTY = "EMPTY"; // NOI18N
173
public static final String JavaDoc CONTAINER = "CONTAINER"; // NOI18N
174
public static final String JavaDoc DATA = "DATA"; // NOI18N
175
public static final String JavaDoc MIXED = "MIXED"; // NOI18N
176
public static final String JavaDoc IGNORE = "IGNORE"; // NOI18N
177

178         private String JavaDoc type;
179
180         // other properties
181

182         private String JavaDoc method;
183
184         private String JavaDoc element;
185
186         private String JavaDoc parslet;
187
188         
189         /** Creates new HandlerMappingEntry */
190         public Entry(String JavaDoc element, String JavaDoc method, String JavaDoc parslet, String JavaDoc type) {
191             this.method = method;
192             this.element = element;
193             this.parslet = parslet;
194             this.type = type;
195         }
196
197         /**
198           * Getter for property method.
199           * @return Value of property method.
200           */

201         public String JavaDoc getMethod() {
202             return method;
203         }
204
205         /**
206           * Getter for property element.
207           * @return Value of property element.
208           */

209         public String JavaDoc getElement() {
210             return element;
211         }
212
213         /**
214           * Getter for property parslet.
215           * @return Value of property parslet.
216           */

217         public String JavaDoc getParslet() {
218             return parslet;
219         }
220         
221         /** Getter for property type.
222           * @return Value of property type.
223           */

224         public String JavaDoc getType() {
225             return type;
226         }
227                 
228         public String JavaDoc toString() {
229             return element + " => (" + method + ", " + parslet + ", " + type + ")"; // NOI18N
230
}
231         
232         void setType(String JavaDoc type) {
233             this.type = type;
234         }
235         
236         void setMethod(String JavaDoc method) {
237             this.method = method;
238         }
239         
240         void setParslet(String JavaDoc parslet) {
241             this.parslet = parslet;
242         }
243         
244         /**
245          * Translate type code into its textual representation
246          */

247         public static final String JavaDoc displayTypeFor(String JavaDoc type) {
248
249             if (ElementBindings.Entry.CONTAINER.equals(type)) {
250                 return DELIMITER_HANDLING;
251             } else if (ElementBindings.Entry.DATA.equals(type)) {
252                  return DATA_HANDLING;
253             } else if (ElementBindings.Entry.MIXED.equals(type)) {
254                 return MIXED_HANDLING;
255             } else if (ElementBindings.Entry.IGNORE.equals(type)) {
256                 return IGNORE_HANDLING;
257             } else if (ElementBindings.Entry.EMPTY.equals(type)) {
258                 return EMPTY_HANDLING;
259             } else {
260                 return IGNORE_HANDLING;
261             }
262         }
263
264         /**
265          * Translate type sring to type code int
266          */

267         public static final String JavaDoc typeFor(String JavaDoc type) {
268             if (DELIMITER_HANDLING.equals(type)) {
269                 return ElementBindings.Entry.CONTAINER;
270             } else if (DATA_HANDLING.equals(type)) {
271                 return ElementBindings.Entry.DATA;
272             } else if (MIXED_HANDLING.equals(type)) {
273                 return ElementBindings.Entry.MIXED;
274             } else if (EMPTY_HANDLING.equals(type)) {
275                 return ElementBindings.Entry.EMPTY;
276             } else {
277                 return ElementBindings.Entry.IGNORE;
278             }
279         }
280
281         /**
282          * Return suitable types (Vector<Strings>) for given element.
283          */

284         public static final Vector displayTypesFor(ElementDeclarations.Entry entry) {
285             Vector list = new Vector(4);
286             list.add(IGNORE_HANDLING);
287             if ((entry.getType() & ElementDeclarations.Entry.DATA) == 1)
288                 list.add(DATA_HANDLING);
289             if ((entry.getType() & ElementDeclarations.Entry.CONTAINER) == 2)
290                 list.add(DELIMITER_HANDLING);
291             if (entry.getType() == ElementDeclarations.Entry.MIXED)
292                 list.add(MIXED_HANDLING);
293             if (entry.getType() == ElementDeclarations.Entry.EMPTY)
294                 list.add(EMPTY_HANDLING);
295             return list;
296         }
297     }
298 }
299
Popular Tags