KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
28
29 /**
30  * Class XmlWriter is used to write the content of an IdlNode derived type
31  * as an XML string.
32  *
33  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
34  */

35 public class XmlWriter implements IdlWriter {
36     private StringBuffer JavaDoc sb;
37     private int indent = 1;
38
39     /**
40      * Construct a XmlWriter object starting with an initial indentation.
41      *
42      * @param indent The initial indentation as number of space characters.
43      */

44     public XmlWriter(int indent) {
45         clear();
46         this.indent = indent;
47     }
48
49     /**
50      * Construct a XmlWriter object starting with an initial indentation odf zero.
51      */

52     public XmlWriter() {
53         clear();
54     }
55     
56     /**
57      * Clears the XmlWriter buffer.
58      */

59     public void clear() {
60         sb = new StringBuffer JavaDoc(
61 // "<?xml version='1.0' encoding='us-ascii'?>\n" +
62
"<!DOCTYPE idlxml SYSTEM \"idlxml.dtd\">\n" +
63             "<idlxml>\n");
64     }
65     
66     private String JavaDoc indent() {
67         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
68         for (int i = 0; i < indent; i++) {
69             sb.append(" ");
70         }
71         return sb.toString();
72     }
73
74     /**
75      * Writes the value of a fixed IDL type to the IdlWriter.
76      *
77      * @param v The value of the fixed type.
78      */

79     public void write_fixed(String JavaDoc v) {
80         sb.append(indent() + "<fixed>" + v + "</fixed>\n");
81     }
82     
83     /**
84      * Writes the value of a double IDL type to the IdlWriter.
85      *
86      * @param v The value of the double type.
87      */

88     public void write_double(String JavaDoc v) {
89         sb.append(indent() + "<double>" + v + "</double>\n");
90     }
91
92     /**
93      * Writes the value of a float IDL type to the IdlWriter.
94      *
95      * @param v The value of the float type.
96      */

97     public void write_float(String JavaDoc v) {
98         sb.append(indent() + "<float>" + v + "</float>\n");
99     }
100
101     /**
102      * Writes the value of a long double IDL type to the IdlWriter.
103      *
104      * @param v The value of the long double type.
105      */

106     public void write_longdouble(String JavaDoc v) {
107         sb.append(indent() + "<longdouble>" + v + "</longdouble>\n");
108     }
109
110     /**
111      * Writes the value of a unsigned long long IDL type to the IdlWriter.
112      *
113      * @param v The value of the unsigned long long type.
114      */

115     public void write_ulonglong(String JavaDoc v) {
116         sb.append(indent() + "<ulonglong>" + v + "</ulonglong>\n");
117     }
118
119     /**
120      * Writes the value of a long long IDL type to the IdlWriter.
121      *
122      * @param v The value of the long long type.
123      */

124     public void write_longlong(String JavaDoc v) {
125         sb.append(indent() + "<longlong>" + v + "</longlong>\n");
126     }
127
128     /**
129      * Writes the value of a unsigned long IDL type to the IdlWriter.
130      *
131      * @param v The value of the unsigned long type.
132      */

133     public void write_ulong(String JavaDoc v) {
134         sb.append(indent() + "<ulong>" + v + "</ulong>\n");
135     }
136
137     /**
138      * Writes the value of a long IDL type to the IdlWriter.
139      *
140      * @param v The value of the long type.
141      */

142     public void write_long(String JavaDoc v) {
143         sb.append(indent() + "<long>" + v + "</long>\n");
144     }
145
146     /**
147      * Writes the value of a unsigned short IDL type to the IdlWriter.
148      *
149      * @param v The value of the unsigned short type.
150      */

151     public void write_ushort(String JavaDoc v) {
152         sb.append(indent() + "<ushort>" + v + "</ushort>\n");
153     }
154
155     /**
156      * Writes the value of a short IDL type to the IdlWriter.
157      *
158      * @param v The value of the short type.
159      */

160     public void write_short(String JavaDoc v) {
161         sb.append(indent() + "<short>" + v + "</short>\n");
162     }
163
164     /**
165      * Writes the value of a octet IDL type to the IdlWriter.
166      *
167      * @param v The value of the octet type.
168      */

169     public void write_octet(String JavaDoc v) {
170         sb.append(indent() + "<octet>" + v + "</octet>\n");
171     }
172
173     /**
174      * Writes the value of a boolean IDL type to the IdlWriter.
175      *
176      * @param v The value of the boolean type.
177      */

178     public void write_boolean(String JavaDoc v) {
179         sb.append(indent() + "<boolean>" + v + "</boolean>\n");
180     }
181
182     /**
183      * Writes the value of a char IDL type to the IdlWriter.
184      *
185      * @param v The value of the char type.
186      */

187     public void write_char(String JavaDoc v) {
188         sb.append(indent() + "<char>" + v + "</char>\n");
189     }
190
191     /**
192      * Writes the value of a wchar IDL type to the IdlWriter.
193      *
194      * @param v The value of the wchar type.
195      */

196     public void write_wchar(String JavaDoc v) {
197         sb.append(indent() + "<wchar>" + v + "</wchar>\n");
198     }
199
200     /**
201      * Writes the value of a string IDL type to the IdlWriter.
202      *
203      * @param v The value of the string type.
204      */

205     public void write_string(String JavaDoc v) {
206         if (v == null) {
207             v = "";
208         }
209         sb.append(indent() + "<string>" + v + "</string>\n");
210     }
211
212     /**
213      * Writes the value of a wstring IDL type to the IdlWriter.
214      *
215      * @param v The value of the wstring type.
216      */

217     public void write_wstring(String JavaDoc v) {
218         if (v == null) {
219             v = "";
220         }
221         sb.append(indent() + "<wstring>" + v + "</wstring>\n");
222     }
223
224     /**
225      * Writes the value of a object IDL type to the IdlWriter.
226      *
227      * @param v The ior value of the object type.
228      */

229     public void write_Object(String JavaDoc v) {
230         sb.append(indent() + "<object>" + v + "</object>\n");
231     }
232
233     /**
234      * Writes the value of a sequence IDL type to the IdlWriter.
235      *
236      * A sequence is a nested type with zero or more elements.
237      * A write_start_sequence operation must be followed by zero or more other write
238      * operations which correspond with the sequence element types and must be closed by
239      * a write_end_sequence operation.
240      *
241      * @param id The repository id of the sequence or an empty string for anonymous sequences.
242      * @param length The number of sequence elements.
243      */

244     public void write_start_sequence(String JavaDoc id, int length) {
245         sb.append(indent() + "<sequence id=\"" + id + "\" length=\"" + length + "\">\n");
246         indent++;
247     }
248
249     /**
250      * Closes the nested sequence IDL type.
251      */

252     public void write_end_sequence() {
253         indent--;
254         sb.append(indent() + "</sequence>\n");
255     }
256
257     /**
258      * Writes the value of a array IDL type to the IdlWriter.
259      *
260      * An array is a nested type with zero or more elements.
261      * A write_start_array operation must be followed by zero or more other write
262      * operations which correspond with the array element types and must be closed by
263      * a write_end_array operation.
264      *
265      * @param id The repository id of the array or an empty string for anonymous arrays.
266      * @param length The number of array elements.
267      */

268     public void write_start_array(String JavaDoc id, int length) {
269         sb.append(indent() + "<array id=\"" + id + "\" length=\"" + length + "\">\n");
270         indent++;
271     }
272
273     /**
274      * Closes the nested array IDL type.
275      */

276     public void write_end_array() {
277         indent--;
278         sb.append(indent() + "</array>\n");
279     }
280
281     /**
282      * Writes the value of a struct IDL type to the IdlWriter.
283      *
284      * A struct is a nested type with one or more member elements.
285      * A write_start_struct operation must be followed by one or more other write_start_member
286      * and write_end_member operations for each struct member and must be closed by
287      * a write_end_struct operation.
288      *
289      * @param id The repository id of the struct.
290      */

291     public void write_start_struct(String JavaDoc id) {
292         sb.append(indent() + "<struct id=\"" + id + "\">\n");
293         indent++;
294     }
295
296     /**
297      * Writes the value of a valuetype IDL type to the IdlWriter.
298      *
299      * A valuetype is a nested type with one or more member elements.
300      * A write_start_value operation must be followed by one or more other write_start_member
301      * and write_end_member operations for each struct member and must be closed by
302      * a write_end_value operation.
303      *
304      * @param id The repository id of the struct.
305      */

306     public void write_start_value(String JavaDoc id) {
307         sb.append(indent() + "<valuetype id=\"" + id + "\">\n");
308         indent++;
309     }
310     
311     /**
312      * Writes the name of a struct or union member IDL type to the IdlWriter.
313      *
314      * The write_start_member operation must be followed by a write operation
315      * corresponding with the member type and must be closed by a write_end_member operation.
316      *
317      * @param name The name of the struct or union member.
318      */

319     public void write_start_member(String JavaDoc name) {
320         sb.append(indent() + "<member name=\"" + name + "\">\n");
321         indent++;
322     }
323
324     /**
325      * Closes the nested struct or union member IDL type.
326      */

327     public void write_end_member() {
328         indent--;
329         sb.append(indent() + "</member>\n");
330     }
331
332     /**
333      * Closes the nested struct or union IDL type.
334      */

335     public void write_end_struct() {
336         indent--;
337         sb.append(indent() + "</struct>\n");
338     }
339
340     /**
341      * Closes the nested valuetype IDL type.
342      */

343     public void write_end_value() {
344         indent--;
345         sb.append(indent() + "</valuetype>\n");
346     }
347
348     /**
349      * Writes the value of a union IDL type to the IdlWriter.
350      *
351      * A union is a nested type with one member element.
352      * A write_start_union operation must be followed by one write_union_tag operation and one write_start_member
353      * and write_end_member operation for the union member and must be closed by
354      * a write_end_union operation.
355      *
356      * @param id The repository id of the union.
357      */

358     public void write_start_union(String JavaDoc id) {
359         sb.append(indent() + "<union id=\"" + id + "\">\n");
360         indent++;
361     }
362
363     /**
364      * Writes the tag value of a union IDL type to the IdlWriter.
365      *
366      * @param v The tag value of the union.
367      */

368     public void write_union_tag(String JavaDoc v) {
369         sb.append(indent() + "<tag>" + v + "</tag>\n");
370     }
371
372     /**
373      * Closes the nested union IDL type.
374      */

375     public void write_end_union() {
376         indent--;
377         sb.append(indent() + "</union>\n");
378     }
379
380     /**
381      * Writes the value of a enum IDL type to the IdlWriter.
382      *
383      * @param id The repository id of the enum.
384      * @param v The value of the enum.
385      */

386     public void write_enum(String JavaDoc id, String JavaDoc v) {
387         sb.append(indent() + "<enum id=\"" + id + "\">" + v + "</enum>\n");
388     }
389
390     /**
391      * Writes the ior value of a interface IDL type to the IdlWriter.
392      *
393      * @param id The repository id of the interface.
394      * @param v The ior value of the interface reference.
395      */

396     public void write_interface(String JavaDoc id, String JavaDoc ior) {
397         sb.append(indent() + "<interface id=\"" + id + "\">" + ior + "</interface>\n");
398     }
399
400     /**
401      * Writes the ior value of a component IDL type to the IdlWriter.
402      *
403      * @param id The repository id of the CCM component.
404      * @param v The ior value of the component reference.
405      */

406     public void write_component(String JavaDoc id, String JavaDoc ior) {
407         sb.append(indent() + "<component id=\"" + id + "\">" + ior + "</component>\n");
408     }
409
410     /**
411      * Writes the name and type value of an IDL operation to the IdlWriter.
412      *
413      * The write_start_operation operation must be followed by a write operation corresponding
414      * to the operation in and inout parameters and must be closed with a write_end_operation operation.
415      *
416      * @param name The name of the operation.
417      * @param id The repository id of the interface to which the operation belongs.
418      */

419     public void write_start_operation(String JavaDoc name, String JavaDoc interfaceId) {
420         sb.append(indent() + "<operation name=\"" + name + "\" id=\"" + interfaceId + "\">\n");
421         indent++;
422     }
423
424     /**
425      * Writes the name of an 'in' operation parameter.
426      *
427      * The write_start_in operation must be followed by a write operation corresponding
428      * to the type of the parameter and must be closed with a write_end_inpar operation.
429      *
430      * @param name The name of the parameter.
431      */

432     public void write_start_inpar(String JavaDoc name) {
433         sb.append(indent() + "<in name=\"" + name + "\">\n");
434         indent++;
435     }
436
437     /**
438      * Closes the nested write_start_inpar operation.
439      */

440     public void write_end_inpar() {
441         indent--;
442         sb.append(indent() + "</in>\n");
443     }
444
445     /**
446      * Writes the name of an 'out' operation parameter.
447      *
448      * The write_start_out operation must be followed by a write operation corresponding
449      * to the type of the parameter and must be closed with a write_end_outpar operation.
450      *
451      * @param name The name of the parameter.
452      */

453     public void write_start_outpar(String JavaDoc name) {
454         sb.append(indent() + "<out name=\"" + name + "\">\n");
455         indent++;
456     }
457
458     /**
459      * Closes the nested write_start_outpar operation.
460      */

461     public void write_end_outpar() {
462         indent--;
463         sb.append(indent() + "</out>\n");
464     }
465
466     /**
467      * Writes the name of an 'inout' operation parameter.
468      *
469      * The write_start_inout operation must be followed by a write operation corresponding
470      * to the type of the parameter and must be closed with a write_end_inoutpar operation.
471      *
472      * @param name The name of the parameter.
473      */

474     public void write_start_inoutpar(String JavaDoc name) {
475         sb.append(indent() + "<inout name=\"" + name + "\">\n");
476         indent++;
477     }
478
479     /**
480      * Closes the nested write_start_inoutpar operation.
481      */

482     public void write_end_inoutpar() {
483         indent--;
484         sb.append(indent() + "</inout>\n");
485     }
486
487     /**
488      * Writes the return value of an operation.
489      *
490      * The write_start_return operation must be followed by a write operation corresponding
491      * to the return type of the operation and must be closed with a write_end_return operation.
492      */

493     public void write_start_return() {
494         sb.append(indent() + "<return>\n");
495         indent++;
496     }
497
498     /**
499      * Closes the nested write_start_return operation.
500      */

501     public void write_end_return() {
502         indent--;
503         sb.append(indent() + "</return>\n");
504     }
505
506     /**
507      * Writes the exception value of an operation.
508      *
509      * The write_start_exception operation can be followed by a start_write_member operations corresponding
510      * to the members of the exception and must be closed with a write_end_exception operation.
511      *
512      * @param id The repository id of the exception.
513      */

514     public void write_start_exception(String JavaDoc id) {
515         sb.append(indent() + "<exception id=\"" + id + "\">\n");
516         indent++;
517     }
518
519     /**
520      * Closes the nested write_start_exception operation.
521      */

522     public void write_end_exception() {
523         indent--;
524         sb.append(indent() + "</exception>\n");
525     }
526     
527     /**
528      * Closes the nested write_start_operation operation.
529      */

530     public void write_end_operation() {
531         indent--;
532         sb.append(indent() + "</operation>\n");
533     }
534
535     /**
536      * Writes the name and type value of the reply of an IDL operation to the IdlWriter.
537      *
538      * The write_start_reply operation must be followed by a write operation corresponding
539      * to the operation out and inout parameters and return value or exception value and
540      * must be closed with a write_end_operation operation.
541      *
542      * @param name The name of the operation.
543      * @param id The repository id of the interface to which the operation belongs.
544      */

545     public void write_start_reply(String JavaDoc name, String JavaDoc interfaceId) {
546         sb.append(indent() + "<reply name=\"" + name + "\" id=\"" + interfaceId + "\">\n");
547         indent++;
548     }
549
550     /**
551      * Closes the nested write_start_reply operation.
552      */

553     public void write_end_reply() {
554         indent--;
555         sb.append(indent() + "</reply>\n");
556     }
557
558     /**
559      * Returns the content of the IdlWriter buffer as a String value.
560      *
561      * @return The content of the IdlWriter buffer as a String value.
562      */

563     public String JavaDoc toString() {
564         indent--;
565         sb.append("</idlxml>\n");
566         return sb.toString();
567     }
568
569     /**
570      * Writes the value of a any IDL type to the IdlWriter.
571      *
572      * An any is a nested type with zero or one element.
573      * A write_start_any operation must be followed by zero or one other write
574      * operations which correspond with the content type of the any and must be closed by
575      * a write_end_any operation.
576      */

577     public void write_start_any() {
578         sb.append(indent() + "<any>\n");
579         indent++;
580     }
581
582     /**
583      * Closes the nested write_start_any operation.
584      */

585     public void write_end_any() {
586         indent--;
587         sb.append(indent() + "</any>\n");
588     }
589 }
Popular Tags