KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > idltree > XmlNode


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.idltree;
26
27 import javax.xml.parsers.DocumentBuilder JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.SAXParseException JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34 import org.w3c.dom.*;
35 import javax.swing.tree.*;
36
37 import java.util.*;
38 import java.io.*;
39 import java.lang.reflect.*;
40
41 public class XmlNode {
42     static org.objectweb.ccm.IDL3.Repository repository;
43     static org.objectweb.openccm.ir3.api.ComponentRepository repositoryRef;
44     static Hashtable repositoryCache = new Hashtable();
45     static org.omg.CORBA.ORB JavaDoc orb = null;
46
47     static {
48         try {
49             if ( orb == null ){
50                 orb = org.objectweb.openccm.corba.TheORB.getORB();
51             }
52             // Obtain the Interface Repository.
53
repositoryRef =
54                 org.objectweb.openccm.ir3.api.ComponentRepositoryHelper.narrow(
55                     org.objectweb.openccm.corba.TheInterfaceRepository.getRepository());
56
57             // Start an IDL3 Repository
58
repository = new org.objectweb.ccm.IDL3.Repository(repositoryRef.as_IDL2_repository());
59
60         } catch (Throwable JavaDoc ex) {
61             ex.printStackTrace();
62         }
63     }
64
65     public static void setOrb(org.omg.CORBA.ORB JavaDoc ob) {
66         orb = ob;
67     }
68
69     static Node getNode(String JavaDoc xml) {
70         Node node = null;
71         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
72         factory.setValidating(true);
73         try {
74             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
75             InputSource JavaDoc is = new InputSource JavaDoc(new StringReader(xml));
76             is.setSystemId("file://" + System.getProperty("dsc.home") + "/etc/");
77             node = builder.parse(is);
78             node.normalize();
79         } catch (SAXParseException JavaDoc spe) {
80             throw new RuntimeException JavaDoc("Xml parsing error, line " + spe.getLineNumber() + ", uri " + spe.getSystemId() + " " + spe.getMessage());
81         } catch (Exception JavaDoc x) {
82             x.printStackTrace();
83         }
84         return firstChildElement(node);
85     }
86
87     static String JavaDoc getId(Node n) {
88         return getAttribute(n, "id");
89     }
90
91     static String JavaDoc getName(Node n) {
92         return getAttribute(n, "name");
93     }
94
95     static int getLength(Node n) {
96         String JavaDoc a = getAttribute(n, "length");
97         try {
98             return Integer.parseInt(a);
99         } catch (Exception JavaDoc e) {
100             throw new RuntimeException JavaDoc("unable to read length attribute");
101         }
102     }
103
104     static String JavaDoc getValue(Node n) {
105         return getAttribute(n, "value");
106     }
107
108     /**
109      * Returns a list of operations for the IDL interface indicated by the repository id.
110      *
111      * @return An array with operation names.
112      */

113     static String JavaDoc[] getOperations(String JavaDoc id) {
114         String JavaDoc[] operations = new String JavaDoc[0];
115         if (repositoryRef != null) {
116             try {
117                 org.omg.CORBA.Contained JavaDoc c = lookup(id);
118                 HashSet v = new HashSet();
119                 addOperations(v, id);
120                 operations = new String JavaDoc[v.size()];
121                 v.toArray(operations);
122             } catch (Exception JavaDoc ex) {
123                 System.err.println(ex.toString());
124             }
125         }
126         return operations;
127     }
128
129     static void addOperations(HashSet v, String JavaDoc id) {
130         if (repositoryRef != null) {
131             try {
132                 org.omg.CORBA.Contained JavaDoc c = lookup(id);
133                 org.omg.CORBA.InterfaceDef JavaDoc interfaceDef = org.omg.CORBA.InterfaceDefHelper.narrow(c);
134                 org.omg.CORBA.InterfaceDefPackage.FullInterfaceDescription desc = interfaceDef.describe_interface();
135                 for (int i = 0; i < desc.operations.length; i++) {
136                     v.add(desc.operations[i].name);
137                 }
138                 for (int i = 0; i < desc.attributes.length; i++) {
139                     v.add("_get_" + desc.attributes[i].name);
140                     if (desc.attributes[i].mode == org.omg.CORBA.AttributeMode.ATTR_NORMAL) {
141                         v.add("_set_" + desc.attributes[i].name);
142                     }
143                 }
144                 for(int i = 0; i < desc.base_interfaces.length; i++) {
145                     addOperations(v, desc.base_interfaces[i]);
146                 }
147             } catch (Exception JavaDoc ex) {
148                 System.err.println(ex.toString());
149             }
150         }
151     }
152
153     static void addOperations(Hashtable v, String JavaDoc id) {
154         if (repositoryRef != null) {
155             try {
156                 org.omg.CORBA.Contained JavaDoc c = lookup(id);
157                 org.omg.CORBA.InterfaceDef JavaDoc interfaceDef = org.omg.CORBA.InterfaceDefHelper.narrow(c);
158                 org.omg.CORBA.InterfaceDefPackage.FullInterfaceDescription desc = interfaceDef.describe_interface();
159                 for (int i = 0; i < desc.operations.length; i++) {
160                     v.put(desc.operations[i].name, desc.operations[i]);
161                 }
162                 for (int i = 0; i < desc.attributes.length; i++) {
163                     v.put("_get_" + desc.attributes[i].name, desc.attributes[i]);
164                     if (desc.attributes[i].mode == org.omg.CORBA.AttributeMode.ATTR_NORMAL) {
165                         v.put("_set_" + desc.attributes[i].name, desc.attributes[i]);
166                     }
167                 }
168                 for(int i = 0; i < desc.base_interfaces.length; i++) {
169                     addOperations(v, desc.base_interfaces[i]);
170                 }
171             } catch (Exception JavaDoc ex) {
172                 System.err.println(ex.toString());
173             }
174         }
175     }
176
177     /**
178      * Returns a list of parameter names for the IDL interface indicated by the repository id.
179      *
180      * @return An array with parameter names.
181      */

182     static String JavaDoc[] getParameterNames(String JavaDoc id, String JavaDoc name) {
183         String JavaDoc[] names = new String JavaDoc[0];
184         if (repositoryRef != null) {
185             try {
186                 Hashtable t = new Hashtable();
187                 addOperations(t, id);
188                 Object JavaDoc desc = t.get(name);
189                 if (desc == null) {
190                     throw new RuntimeException JavaDoc("no operation " + name + " found in interface " + id);
191                 }
192                 if (desc instanceof org.omg.CORBA.OperationDescription JavaDoc) {
193                     org.omg.CORBA.OperationDescription JavaDoc opDesc = (org.omg.CORBA.OperationDescription JavaDoc)desc;
194                     names = new String JavaDoc[opDesc.parameters.length + 1];
195                     for (int j = 0; j < opDesc.parameters.length; j++) {
196                         names[j] = opDesc.parameters[j].name;
197                     }
198                     if (opDesc.mode == org.omg.CORBA.OperationMode.OP_ONEWAY) {
199                         names[names.length - 1] = "oneway";
200                     } else {
201                         names[names.length - 1] = "return";
202                     }
203                 }
204                 if (desc instanceof org.omg.CORBA.AttributeDescription JavaDoc) {
205                     if (name.startsWith("_set_")) {
206                         names = new String JavaDoc[] {
207                           "val",
208                           "return"
209                         };
210                     } else {
211                         names = new String JavaDoc[] {
212                           "return"
213                         };
214                     }
215                 }
216             } catch (Exception JavaDoc ex) {
217                 System.err.println(ex.toString());
218             }
219         }
220         return names;
221     }
222
223     /**
224      * Returns a list of parameter directions for the IDL interface indicated by the repository id.
225      *
226      * Directions are in, out, inout, return, oneway
227      * @return An array with parameter directions.
228      */

229     static String JavaDoc[] getParameterDirections(String JavaDoc id, String JavaDoc name) {
230         String JavaDoc[] directions = new String JavaDoc[0];
231         if (repositoryRef != null) {
232             try {
233                 Hashtable t = new Hashtable();
234                 addOperations(t, id);
235                 Object JavaDoc desc = t.get(name);
236                 if (desc == null) {
237                     throw new RuntimeException JavaDoc("no operation " + name + " found in interface " + id);
238                 }
239                 if (desc instanceof org.omg.CORBA.OperationDescription JavaDoc) {
240                     org.omg.CORBA.OperationDescription JavaDoc opDesc = (org.omg.CORBA.OperationDescription JavaDoc)desc;
241                     directions = new String JavaDoc[opDesc.parameters.length + 1];
242                     for (int j = 0; j < opDesc.parameters.length; j++) {
243                         directions[j] = parameterMode(opDesc.parameters[j].mode);
244                     }
245                     if (opDesc.mode == org.omg.CORBA.OperationMode.OP_ONEWAY) {
246                         directions[directions.length - 1] = "oneway";
247                     } else {
248                         directions[directions.length - 1] = "return";
249                     }
250                 }
251                 if (desc instanceof org.omg.CORBA.AttributeDescription JavaDoc) {
252                     if (name.startsWith("_set_")) {
253                         directions = new String JavaDoc[] {
254                           "in",
255                           "return"
256                         };
257                     } else {
258                         directions = new String JavaDoc[] {
259                           "return"
260                         };
261                     }
262                 }
263             } catch (Exception JavaDoc ex) {
264                 System.err.println(ex.toString());
265             }
266         }
267
268         return directions;
269     }
270
271     private static String JavaDoc parameterMode(org.omg.CORBA.ParameterMode JavaDoc m) {
272         if (m == org.omg.CORBA.ParameterMode.PARAM_IN) {
273             return "in";
274         }
275         if (m == org.omg.CORBA.ParameterMode.PARAM_OUT) {
276             return "out";
277         }
278         if (m == org.omg.CORBA.ParameterMode.PARAM_INOUT) {
279             return "inout";
280         }
281         return "";
282     }
283
284     /**
285      * Returns a list of parameter types for the IDL interface indicated by the repository id.
286      *
287      * The type names can be primitive types such as string, short, ulong or the repository id of a user defined type.
288      *
289      * @return An array with parameter type names.
290      */

291     static String JavaDoc[] getParameterTypes(String JavaDoc id, String JavaDoc name) {
292         String JavaDoc[] types = new String JavaDoc[0];
293         if (repositoryRef != null) {
294             try {
295                 Hashtable t = new Hashtable();
296                 addOperations(t, id);
297                 Object JavaDoc desc = t.get(name);
298                 if (desc == null) {
299                     throw new RuntimeException JavaDoc("no operation " + name + " found in interface " + id);
300                 }
301                 if (desc instanceof org.omg.CORBA.OperationDescription JavaDoc) {
302                     org.omg.CORBA.OperationDescription JavaDoc opDesc = (org.omg.CORBA.OperationDescription JavaDoc)desc;
303                     types = new String JavaDoc[opDesc.parameters.length + 1];
304                     for (int j = 0; j < opDesc.parameters.length; j++) {
305                         types[j] = parameterType(opDesc.parameters[j].type);
306                     }
307                     if (opDesc.mode == org.omg.CORBA.OperationMode.OP_ONEWAY) {
308                         types[types.length - 1] = "";
309                     } else {
310                         types[types.length - 1] = parameterType(opDesc.result);
311                     }
312                 }
313                 if (desc instanceof org.omg.CORBA.AttributeDescription JavaDoc) {
314                     org.omg.CORBA.AttributeDescription JavaDoc attDesc = (org.omg.CORBA.AttributeDescription JavaDoc)desc;
315                     if (name.startsWith("_set_")) {
316                         types = new String JavaDoc[] {
317                           parameterType(attDesc.type),
318                           "void"
319                         };
320                     } else {
321                         types = new String JavaDoc[] {
322                           parameterType(attDesc.type)
323                         };
324                     }
325                 }
326             } catch (Exception JavaDoc ex) {
327                 System.err.println(ex.toString());
328             }
329         }
330         return types;
331     }
332
333     static String JavaDoc[] getParameterExceptions(String JavaDoc id, String JavaDoc name) {
334         String JavaDoc[] exceptions = new String JavaDoc[0];
335         if (repositoryRef != null) {
336             try {
337                 Hashtable t = new Hashtable();
338                 addOperations(t, id);
339                 Object JavaDoc desc = t.get(name);
340                 if (desc == null) {
341                     throw new RuntimeException JavaDoc("no operation " + name + " found in interface " + id);
342                 }
343                 if (desc instanceof org.omg.CORBA.OperationDescription JavaDoc) {
344                     org.omg.CORBA.OperationDescription JavaDoc opDesc = (org.omg.CORBA.OperationDescription JavaDoc)desc;
345                     exceptions = new String JavaDoc[opDesc.exceptions.length];
346                     for (int j = 0; j < opDesc.exceptions.length; j++) {
347                         exceptions[j] = opDesc.exceptions[j].id;
348                     }
349                 }
350             } catch (Exception JavaDoc ex) {
351                 System.err.println(ex.toString());
352             }
353         }
354
355         return exceptions;
356     }
357
358     private static String JavaDoc parameterType(org.omg.CORBA.TypeCode JavaDoc tc) {
359         try {
360             switch (tc.kind().value()) {
361                 case org.omg.CORBA.TCKind._tk_any: {
362                     return "any";
363                 }
364                 case org.omg.CORBA.TCKind._tk_octet: {
365                     return "octet";
366                 }
367                 case org.omg.CORBA.TCKind._tk_short: {
368                     return "short";
369                 }
370                 case org.omg.CORBA.TCKind._tk_ushort: {
371                     return "ushort";
372                 }
373                 case org.omg.CORBA.TCKind._tk_long: {
374                     return "long";
375                 }
376                 case org.omg.CORBA.TCKind._tk_ulong: {
377                     return "ulong";
378                 }
379                 case org.omg.CORBA.TCKind._tk_longlong: {
380                     return "longlong";
381                 }
382                 case org.omg.CORBA.TCKind._tk_ulonglong: {
383                     return "ulonglong";
384                 }
385                 case org.omg.CORBA.TCKind._tk_float: {
386                     return "float";
387                 }
388                 case org.omg.CORBA.TCKind._tk_double: {
389                     return "double";
390                 }
391                 case org.omg.CORBA.TCKind._tk_longdouble: {
392                     return "longdouble";
393                 }
394                 case org.omg.CORBA.TCKind._tk_boolean: {
395                     return "boolean";
396                 }
397                 case org.omg.CORBA.TCKind._tk_char: {
398                     return "char";
399                 }
400                 case org.omg.CORBA.TCKind._tk_wchar: {
401                     return "wchar";
402                 }
403                 case org.omg.CORBA.TCKind._tk_string: {
404                     return "string";
405                 }
406                 case org.omg.CORBA.TCKind._tk_wstring: {
407                     return "wstring";
408                 }
409                 case org.omg.CORBA.TCKind._tk_void: {
410                     return "void";
411                 }
412                 case org.omg.CORBA.TCKind._tk_null: {
413                     return "null";
414                 }
415                 default: {
416                     return tc.id();
417                 }
418             }
419         } catch (Exception JavaDoc e) {
420             e.printStackTrace();
421         }
422         return null;
423     }
424     
425     /**
426      * Convert IDL:omg.org/CosNotification/StructuredEvent:1.0
427      * into org.omg.CosNotification.StructuredEvent
428      */

429     static String JavaDoc idToType(String JavaDoc id) {
430         String JavaDoc javaName = "";
431         // strip off IDL: en version :1.0 sections
432
if (id.indexOf(":") > 0) {
433             id = id.substring(id.indexOf(":") + 1, id.lastIndexOf(":"));
434         }
435         if (id.indexOf(".") > 0) {
436             // there is a pragma prefix
437
String JavaDoc pragma = id.substring(0, id.indexOf("/"));
438             String JavaDoc[] el = splitName(pragma, ".");
439             String JavaDoc prefix = el[el.length - 1];
440             for (int i = el.length - 2; i >= 0; i--) {
441                 prefix += "." + el[i];
442             }
443             javaName = prefix + id.substring(id.indexOf("/")).replace('/', '.');
444         } else {
445             javaName = id.replace('/', '.');
446         }
447         if (javaName.equals("org.omg.CORBA.Object")) {
448             javaName = "object";
449         }
450         return javaName;
451     }
452
453     static String JavaDoc[] splitName(String JavaDoc name, String JavaDoc separator) {
454         if (name == null) {
455             return new String JavaDoc[0];
456         }
457         StringTokenizer st = new StringTokenizer(name, separator);
458         Vector v = new Vector();
459         if (name.startsWith(separator)) {
460             v.addElement("");
461         }
462         while (st.hasMoreTokens()) {
463             v.addElement(st.nextToken());
464         }
465         String JavaDoc[] s = new String JavaDoc[v.size()];
466         v.copyInto(s);
467         return s;
468     }
469
470     static String JavaDoc getText(Node domNode) {
471         String JavaDoc text = "";
472         NodeList list = domNode.getChildNodes();
473         int items = list.getLength();
474         for (int i = 0; i < items; i++) {
475             if (list.item(i).getNodeType() == Node.TEXT_NODE) {
476                 text = list.item(i).getNodeValue().trim();
477                 if (!text.equals("")) {
478                     return list.item(i).getNodeValue().trim();
479                 }
480             }
481         }
482         return text;
483     }
484
485     static String JavaDoc getAttribute(Node domNode, String JavaDoc name) {
486         NamedNodeMap map = domNode.getAttributes();
487         Node attr = map.getNamedItem(name);
488         if (attr != null) {
489             return attr.getNodeValue();
490         } else {
491             return "";
492         }
493     }
494
495     static Node nextElement(Node n) {
496         if (n != null) {
497             n = n.getNextSibling();
498             while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
499                 n = n.getNextSibling();
500             }
501         }
502         return n;
503     }
504
505     static Node firstElement(Node n) {
506         while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
507             System.out.println("" + n.getNodeType() + " " + n.getNodeName());
508             n = n.getNextSibling();
509         }
510         return n;
511     }
512
513     static Node firstChildElement(Node n) {
514         NodeList list = n.getChildNodes();
515         int items = list.getLength();
516         for (int i = 0; i < items; i++) {
517             if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
518                 return list.item(i);
519             }
520         }
521         return null;
522     }
523
524     static Node[] childElements(Node n) {
525         Vector v = new Vector();
526         NodeList list = n.getChildNodes();
527         int items = list.getLength();
528         for (int i = 0; i < items; i++) {
529             if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
530                 v.add(list.item(i));
531             }
532         }
533         Node[] nn = new Node[v.size()];
534         v.toArray(nn);
535         return nn;
536     }
537
538     static org.omg.CORBA.Contained JavaDoc lookup(String JavaDoc id) {
539         org.omg.CORBA.Contained JavaDoc c = (org.omg.CORBA.Contained JavaDoc)repositoryCache.get(id);
540         if (c == null) {
541 // System.err.println("***** lookup_id(" + id + ") ******");
542
c = repositoryRef.lookup_id(id);
543             if (c == null) {
544                 javax.swing.JOptionPane.showMessageDialog(null, id + " not found in InterfaceRepository.\nUse ir3_feed to populate the InterfaceRepository with the proper idl files");
545                 return null;
546             }
547             repositoryCache.put(id, c);
548         }
549         return c;
550     }
551
552     static public org.omg.CORBA.TypeCode JavaDoc type(String JavaDoc id) {
553         org.omg.CORBA.TypeCode JavaDoc tc = null;
554         String JavaDoc type = idToType(id);
555         Integer JavaDoc index = (Integer JavaDoc)table.get(type.toLowerCase());
556
557         if (index == null) {
558             try {
559                 org.omg.CORBA.Contained JavaDoc c = lookup(id);
560                 if (c == null) {
561                     throw new RuntimeException JavaDoc(id + " not found in InterfaceRepository.\nUse ir3_feed to populate the InterfaceRepository with the proper idl files");
562                 }
563                 try {
564                     org.omg.CORBA.IDLType JavaDoc idlType = org.omg.CORBA.IDLTypeHelper.narrow(c);
565                     tc = idlType.type();
566                     return tc;
567                 } catch (Exception JavaDoc e1) {
568                 }
569                 org.omg.CORBA.ExceptionDef JavaDoc idlType = org.omg.CORBA.ExceptionDefHelper.narrow(c);
570                 tc = idlType.type();
571                 return tc;
572             } catch (Exception JavaDoc ex) {
573                 ex.printStackTrace();
574             }
575         } else {
576             switch (index.intValue()) {
577                 case 0: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_boolean);
578                 case 1: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_octet);
579                 case 2: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_short);
580                 case 3: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ushort);
581                 case 4: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long);
582                 case 5: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ulong);
583                 case 6: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_longlong);
584                 case 7: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ulonglong);
585                 case 8: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_float);
586                 case 9: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_double);
587                 case 10: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_longdouble);
588                 case 11: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_char);
589                 case 12: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_wchar);
590                 case 13: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_string);
591                 case 14: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_wstring);
592                 case 15: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_objref);
593                 case 17: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_fixed);
594                 case 18: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_any);
595                 case 24: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_void);
596             }
597         }
598 //System.out.println("XmlNode.type(" + id + ")" + " " + tc);
599
return tc;
600     }
601
602     /**
603      * Factory method to create a new IdlNode instance from a repository id value.
604      *
605      * @param id The repository value from which to create a new instance.
606      *
607      * @return A new IdlNode instance for the given repository id.
608      */

609     static IdlNode getIdlNode(String JavaDoc id) {
610         if (id == null) {
611             throw new RuntimeException JavaDoc("invalid id");
612         }
613
614         String JavaDoc type = idToType(id);
615         Integer JavaDoc index = (Integer JavaDoc)table.get(type.toLowerCase());
616
617         if (index == null) {
618             // The type is not a CORBA primitive
619
try {
620                 return IdlNode.create(XmlNode.type(id));
621             } catch (Exception JavaDoc e) {
622                 e.printStackTrace();
623                 return null;
624             }
625         }
626
627         switch (index.intValue()) {
628             case 0: return new IdlBoolean();
629             case 1: return new IdlOctet();
630             case 2: return new IdlShort();
631             case 3: return new IdlUshort();
632             case 4: return new IdlLong();
633             case 5: return new IdlUlong();
634             case 6: return new IdlLonglong();
635             case 7: return new IdlUlonglong();
636             case 8: return new IdlFloat();
637             case 9: return new IdlDouble();
638             case 10: return new IdlLongdouble();
639             case 11: return new IdlChar();
640             case 12: return new IdlWchar();
641             case 13: return new IdlString();
642             case 14: return new IdlWstring();
643             case 15: return new IdlObject();
644             case 17: return new IdlFixed();
645             case 18: return new IdlAny();
646             default: return null;
647         }
648     }
649
650     static public IdlNode getIdlNodeXml(String JavaDoc xml) {
651         return getIdlNodeXml(null, xml);
652     }
653
654     static IdlNode getIdlNodeXml(org.omg.CORBA.TypeCode JavaDoc tc, String JavaDoc xml) {
655         return getIdlNode(tc, getNode(xml));
656     }
657
658     static IdlNode getIdlNode(Node node) {
659         return getIdlNode(null, node);
660     }
661
662     static IdlNode getIdlNode(org.omg.CORBA.TypeCode JavaDoc tc, Node node) {
663         if (node == null) {
664             throw new RuntimeException JavaDoc("empty node");
665         }
666
667         String JavaDoc kind = node.getNodeName().toLowerCase().trim();
668         if (kind.equals("idlxml")) {
669             node = firstChildElement(node);
670             kind = node.getNodeName().toLowerCase().trim();
671         }
672         Integer JavaDoc index = (Integer JavaDoc)table.get(kind);
673
674         if (index == null) {
675             throw new RuntimeException JavaDoc("invalid node: " + kind);
676         }
677
678         switch (index.intValue()) {
679             case 0: return new IdlBoolean(node);
680             case 1: return new IdlOctet(node);
681             case 2: return new IdlShort(node);
682             case 3: return new IdlUshort(node);
683             case 4: return new IdlLong(node);
684             case 5: return new IdlUlong(node);
685             case 6: return new IdlLonglong(node);
686             case 7: return new IdlUlonglong(node);
687             case 8: return new IdlFloat(node);
688             case 9: return new IdlDouble(node);
689             case 10: return new IdlLongdouble(node);
690             case 11: return new IdlChar(node);
691             case 12: return new IdlWchar(node);
692             case 13: return new IdlString(node);
693             case 14: return new IdlWstring(node);
694             case 15: return new IdlObject(node);
695             case 16: return new IdlInterface(node);
696             case 17: return new IdlFixed(node);
697             case 18: return new IdlAny(node);
698             case 19: return new IdlEnum(node);
699             case 20: return new IdlStruct(node);
700             case 21: return new IdlUnion(node);
701             case 22: return new IdlSequence(tc, node);
702             case 23: return new IdlArray(tc, node);
703             case 25: return new IdlOperation(node);
704             case 26: return new IdlReply(node);
705             case 27: return new IdlException(node);
706             default: return null;
707         }
708     }
709
710
711     static private final java.util.Hashtable JavaDoc table = new java.util.Hashtable JavaDoc();
712     static {
713         table.put("boolean", new Integer JavaDoc(0));
714         table.put("octet", new Integer JavaDoc(1));
715         table.put("short", new Integer JavaDoc(2));
716         table.put("ushort", new Integer JavaDoc(3));
717         table.put("unsigned short", new Integer JavaDoc(3));
718         table.put("long", new Integer JavaDoc(4));
719         table.put("ulong", new Integer JavaDoc(5));
720         table.put("unsigned long", new Integer JavaDoc(5));
721         table.put("longlong", new Integer JavaDoc(6));
722         table.put("long long", new Integer JavaDoc(6));
723         table.put("ulonglong", new Integer JavaDoc(7));
724         table.put("unsigned long long", new Integer JavaDoc(7));
725         table.put("float", new Integer JavaDoc(8));
726         table.put("double", new Integer JavaDoc(9));
727         table.put("longdouble", new Integer JavaDoc(10));
728         table.put("long double", new Integer JavaDoc(10));
729         table.put("char", new Integer JavaDoc(11));
730         table.put("wchar", new Integer JavaDoc(12));
731         table.put("string", new Integer JavaDoc(13));
732         table.put("wstring", new Integer JavaDoc(14));
733         table.put("object", new Integer JavaDoc(15));
734         table.put("interface", new Integer JavaDoc(16));
735         table.put("fixed", new Integer JavaDoc(17));
736         table.put("any", new Integer JavaDoc(18));
737         table.put("enum", new Integer JavaDoc(19));
738         table.put("struct", new Integer JavaDoc(20));
739         table.put("union", new Integer JavaDoc(21));
740         table.put("sequence", new Integer JavaDoc(22));
741         table.put("array", new Integer JavaDoc(23));
742         table.put("void", new Integer JavaDoc(24));
743         table.put("operation", new Integer JavaDoc(25));
744         table.put("reply", new Integer JavaDoc(26));
745         table.put("exception", new Integer JavaDoc(27));
746         table.put("idlxml", new Integer JavaDoc(28));
747     }
748
749     static String JavaDoc replaceAll(String JavaDoc source, String JavaDoc what, String JavaDoc into) {
750         int index = source.indexOf(what);
751         if (index == -1) return source;
752         String JavaDoc s1 = source.substring(0, index);
753         String JavaDoc s2 = source.substring(index + what.length(), source.length());
754         return s1 + into + replaceAll(s2, what, into);
755     }
756
757     public static void write(IdlNode n, IdlWriter w) {
758         if (n instanceof IdlObject) {
759             w.write_Object(n.getValue());
760             return;
761         }
762         if (n instanceof IdlInterface) {
763             w.write_interface(((IdlSequence)n).getId(), n.getValue());
764             return;
765         }
766         if (n instanceof IdlComponent) {
767             w.write_interface(((IdlComponent)n).getId(), n.getValue());
768             return;
769         }
770         if (n instanceof IdlValue) {
771             IdlValue.write((IdlValue)n, w);
772             return;
773         }
774         if (n instanceof IdlSequence) {
775             IdlSequence.write((IdlSequence)n, w);
776             return;
777         }
778         if (n instanceof IdlStruct) {
779             IdlStruct.write((IdlStruct)n, w);
780             return;
781         }
782         if (n instanceof IdlException) {
783             IdlException.write((IdlException)n, w);
784             return;
785         }
786         if (n instanceof IdlUnion) {
787             IdlUnion.write((IdlUnion)n, w);
788             return;
789         }
790         if (n instanceof IdlArray) {
791             IdlArray.write((IdlArray)n, w);
792             return;
793         }
794         if (n instanceof IdlOperation) {
795             IdlOperation.write((IdlOperation)n, w);
796             return;
797         }
798         if (n instanceof IdlParameter) {
799             IdlParameter.write((IdlParameter)n, w);
800             return;
801         }
802         if (n instanceof IdlReply) {
803             IdlReply.write((IdlReply)n, w);
804             return;
805         }
806         if (n instanceof IdlAny) {
807             IdlAny.write((IdlAny)n, w);
808             return;
809         }
810         if (n instanceof IdlEnum) {
811             w.write_enum(n.getId(), n.getValue());
812             return;
813         }
814         if (n instanceof IdlBoolean) {
815             w.write_boolean(n.getValue());
816             return;
817         }
818         if (n instanceof IdlOctet) {
819             w.write_octet(n.getValue());
820             return;
821         }
822         if (n instanceof IdlShort) {
823             w.write_short(n.getValue());
824             return;
825         }
826         if (n instanceof IdlUshort) {
827             w.write_ushort(n.getValue());
828             return;
829         }
830         if (n instanceof IdlLong) {
831             w.write_long(n.getValue());
832             return;
833         }
834         if (n instanceof IdlUlong) {
835             w.write_ulong(n.getValue());
836             return;
837         }
838         if (n instanceof IdlLonglong) {
839             w.write_longlong(n.getValue());
840             return;
841         }
842         if (n instanceof IdlUlonglong) {
843             w.write_ulonglong(n.getValue());
844             return;
845         }
846         if (n instanceof IdlChar) {
847             w.write_char(n.getValue());
848             return;
849         }
850         if (n instanceof IdlWchar) {
851             w.write_wchar(n.getValue());
852             return;
853         }
854         if (n instanceof IdlString) {
855             w.write_string(n.getValue());
856             return;
857         }
858         if (n instanceof IdlWstring) {
859             w.write_wstring(n.getValue());
860             return;
861         }
862         if (n instanceof IdlFloat) {
863             w.write_float(n.getValue());
864             return;
865         }
866         if (n instanceof IdlDouble) {
867             w.write_double(n.getValue());
868             return;
869         }
870         if (n instanceof IdlLongdouble) {
871             w.write_longdouble(n.getValue());
872             return;
873         }
874         if (n instanceof IdlFixed) {
875             w.write_fixed(n.getValue());
876             return;
877         }
878     }
879 }
Popular Tags