001: package org.jacorb.ir;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.io.*;
024:
025: import org.apache.avalon.framework.logger.Logger;
026: import org.jacorb.orb.ORB;
027: import org.jacorb.orb.TypeCode;
028: import org.omg.CORBA.INTERNAL;
029: import org.omg.DynamicAny.*;
030:
031: /**
032: * This class prints IDL from IR-Descriptions to PrintStreams
033: *
034: * @author (c) Gerald Brose, FU Berlin 2000
035: * @version $Id: IdlWriter.java,v 1.13 2006/05/17 12:57:44 alphonse.bendt Exp $
036: */
037:
038: public class IdlWriter {
039: private final PrintStream printStream;
040: private final org.omg.CORBA.Repository ir;
041: private final DynAnyFactory factory;
042: private final Logger logger;
043: private int indent = 0;
044:
045: /**
046: * create a new IdlWriter for the default JacORB IR
047: * which writes to a specific PrintStream
048: *
049: * @param orb
050: * @param ps a PrintStream
051: * @param logger
052: */
053:
054: public IdlWriter(ORB orb, PrintStream ps, Logger logger) {
055: printStream = ps;
056: this .logger = logger;
057:
058: try {
059: ir = org.omg.CORBA.RepositoryHelper.narrow(orb
060: .resolve_initial_references("InterfaceRepository"));
061: factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow(orb
062: .resolve_initial_references("DynAnyFactory"));
063: } catch (org.omg.CORBA.ORBPackage.InvalidName e) {
064: throw new INTERNAL(e.toString());
065: }
066:
067: if (ir == null) {
068: logger.fatalError("No IR configured! Exiting..");
069: System.exit(1);
070: }
071: }
072:
073: public void close() {
074: printStream.flush();
075: printStream.close();
076: }
077:
078: private void indent(int indentation) {
079: indent = indentation;
080: }
081:
082: private void print(String s) {
083: for (int i = 0; i < indent; i++)
084: printStream.print(" ");
085: printStream.print(s);
086: }
087:
088: /**
089: * print the IDL definition for a contained objec
090: *
091: * @param c the contained object
092: * @param indentation how many spaces to use for indentation
093: */
094:
095: public void printContained(org.omg.CORBA.Contained c,
096: int indentation) {
097: org.omg.CORBA.ContainedPackage.Description descr = c.describe();
098:
099: switch (descr.kind.value()) {
100: case org.omg.CORBA.DefinitionKind._dk_Module: {
101: printModule(org.omg.CORBA.ModuleDescriptionHelper
102: .extract(descr.value), indentation + 3);
103: break;
104: }
105: case org.omg.CORBA.DefinitionKind._dk_Interface: {
106: org.omg.CORBA.InterfaceDef idef = org.omg.CORBA.InterfaceDefHelper
107: .narrow(ir
108: .lookup_id(org.omg.CORBA.InterfaceDescriptionHelper
109: .extract(descr.value).id));
110: printInterface(idef, indentation + 3);
111: break;
112: }
113: case org.omg.CORBA.DefinitionKind._dk_Attribute: {
114: printAttribute(org.omg.CORBA.AttributeDescriptionHelper
115: .extract(descr.value), indentation + 3);
116: break;
117: }
118: case org.omg.CORBA.DefinitionKind._dk_Operation: {
119: printOperation(org.omg.CORBA.OperationDescriptionHelper
120: .extract(descr.value), indentation + 3);
121: break;
122: }
123: case org.omg.CORBA.DefinitionKind._dk_Exception: {
124: printException(org.omg.CORBA.ExceptionDescriptionHelper
125: .extract(descr.value), indentation + 3);
126: break;
127: }
128: case org.omg.CORBA.DefinitionKind._dk_Constant: {
129: printConstant(org.omg.CORBA.ConstantDescriptionHelper
130: .extract(descr.value), indentation + 3);
131: break;
132: }
133: case org.omg.CORBA.DefinitionKind._dk_Struct: {
134: printStruct(org.omg.CORBA.TypeDescriptionHelper
135: .extract(descr.value), indentation + 3);
136: break;
137: }
138: case org.omg.CORBA.DefinitionKind._dk_Enum: {
139: printEnum(org.omg.CORBA.TypeDescriptionHelper
140: .extract(descr.value), indentation + 3);
141: break;
142: }
143: case org.omg.CORBA.DefinitionKind._dk_Union: {
144: printUnion(org.omg.CORBA.TypeDescriptionHelper
145: .extract(descr.value), indentation + 3);
146: break;
147: }
148: case org.omg.CORBA.DefinitionKind._dk_Alias: {
149: printAlias(org.omg.CORBA.TypeDescriptionHelper
150: .extract(descr.value), indentation + 3);
151: break;
152: }
153: }
154: }
155:
156: /**
157: * print the IDL definition for a module
158: *
159: * @param mdes the module description
160: * @param indentation how many spaces to use for indentation
161: */
162:
163: public void printModule(org.omg.CORBA.ModuleDescription mdes,
164: int indentation) {
165: indent(indentation);
166:
167: org.omg.CORBA.ModuleDef mdef = org.omg.CORBA.ModuleDefHelper
168: .narrow(ir.lookup_id(mdes.id));
169: print("module " + mdef.name() + "\n");
170: print("{\n");
171: org.omg.CORBA.Contained[] contents = mdef.contents(
172: org.omg.CORBA.DefinitionKind.dk_all, true);
173:
174: for (int x = 0; x < contents.length; x++) {
175: printContained(contents[x], indentation);
176: }
177:
178: indent(indentation);
179: print("};" + "\n\n");
180: }
181:
182: /**
183: * print an IDL interface
184: */
185:
186: public void printInterface(org.omg.CORBA.InterfaceDef idef,
187: int indentation) {
188: org.omg.CORBA.InterfaceDefPackage.FullInterfaceDescription idfid = idef
189: .describe_interface();
190: org.omg.CORBA.Contained[] contents = idef.contents(
191: org.omg.CORBA.DefinitionKind.dk_all, true);
192:
193: indent(indentation);
194:
195: StringBuffer inheritanceSb = new StringBuffer();
196:
197: if (idfid.base_interfaces.length > 0)
198: inheritanceSb.append(" : " + idfid.base_interfaces[0]);
199:
200: for (int b = 1; b < idfid.base_interfaces.length; b++) {
201: inheritanceSb.append(", " + idfid.base_interfaces[b]);
202: }
203:
204: print("interface " + idfid.name + inheritanceSb.toString()
205: + "\n");
206: print("{" + "\n");
207:
208: for (int x = 0; x < contents.length; x++) {
209: printContained(contents[x], indentation);
210: }
211:
212: indent(indentation);
213: print("};" + "\n\n");
214: }
215:
216: /** print an IDL exception def
217: */
218:
219: public void printException(org.omg.CORBA.ExceptionDescription e,
220: int indentation) {
221: org.omg.CORBA.ExceptionDef e_def = org.omg.CORBA.ExceptionDefHelper
222: .narrow(ir.lookup_id(e.id));
223:
224: if (e_def != null) {
225: org.omg.CORBA.StructMember[] members = e_def.members();
226: indent(indentation);
227: print("exception " + e.name + " {" + "\n");
228: indent(indentation + 3);
229: for (int i = 0; i < members.length; i++) {
230: print(TypeCode.idlTypeName(members[i].type) + " "
231: + members[i].name + ";" + "\n");
232: }
233: indent(indentation);
234: print("};" + "\n\n");
235: } else {
236: logger.error("Error, could not find exception " + e.id
237: + " in IR ");
238: }
239: }
240:
241: /** print an IDL struct def
242: */
243:
244: public void printStruct(org.omg.CORBA.TypeDescription t,
245: int indentation) {
246: org.omg.CORBA.StructDef s_def = org.omg.CORBA.StructDefHelper
247: .narrow(ir.lookup_id(t.id));
248:
249: if (s_def != null) {
250: org.omg.CORBA.StructMember[] members = s_def.members();
251: org.omg.CORBA.Contained[] contents = s_def.contents(
252: org.omg.CORBA.DefinitionKind.dk_all, false);
253:
254: indent(indentation);
255: print("struct " + s_def.name() + " {" + "\n");
256: indent(indentation + 3);
257:
258: for (int i = 0; i < members.length; i++) {
259: print(TypeCode.idlTypeName(members[i].type) + " "
260: + members[i].name + ";" + "\n");
261: }
262:
263: for (int i = 0; i < contents.length; i++) {
264: printContained(contents[i], indentation);
265: }
266:
267: indent(indentation);
268: print("};" + "\n\n");
269: } else {
270: logger.error("Error, could not find struct " + t.id
271: + " in IR ");
272: }
273: }
274:
275: /** print an IDL const
276: */
277:
278: public void printConstant(org.omg.CORBA.ConstantDescription c,
279: int indentation) {
280: indent(indentation);
281: StringBuffer sb = new StringBuffer("const "
282: + TypeCode.idlTypeName(c.type) + " " + c.name + " = ");
283: switch (c.type.kind().value()) {
284: case org.omg.CORBA.TCKind._tk_string:
285: sb.append("\"" + c.value.extract_string() + "\"");
286: break;
287: case org.omg.CORBA.TCKind._tk_wstring:
288: sb.append("\"" + c.value.extract_wstring() + "\"");
289: break;
290: case org.omg.CORBA.TCKind._tk_boolean:
291: sb.append(c.value.extract_boolean());
292: break;
293: case org.omg.CORBA.TCKind._tk_long:
294: sb.append(c.value.extract_long());
295: break;
296: case org.omg.CORBA.TCKind._tk_ulong:
297: sb.append(c.value.extract_ulong());
298: break;
299: case org.omg.CORBA.TCKind._tk_longlong:
300: sb.append(c.value.extract_longlong());
301: break;
302: case org.omg.CORBA.TCKind._tk_ulonglong:
303: sb.append(c.value.extract_ulonglong());
304: break;
305: case org.omg.CORBA.TCKind._tk_short:
306: sb.append(c.value.extract_short());
307: break;
308: case org.omg.CORBA.TCKind._tk_ushort:
309: sb.append(c.value.extract_ushort());
310: break;
311: case org.omg.CORBA.TCKind._tk_float:
312: sb.append(c.value.extract_float());
313: break;
314: case org.omg.CORBA.TCKind._tk_octet:
315: sb.append(c.value.extract_octet());
316: break;
317: case org.omg.CORBA.TCKind._tk_char:
318: sb.append("\'" + c.value.extract_char() + "\'");
319: break;
320: case org.omg.CORBA.TCKind._tk_wchar:
321: sb.append("\'" + c.value.extract_wchar() + "\'");
322: break;
323: case org.omg.CORBA.TCKind._tk_fixed:
324: sb.append(c.value.extract_fixed());
325: break;
326: }
327:
328: print(sb.toString() + ";\n\n");
329: }
330:
331: /** print an IDL attribute
332: */
333:
334: public void printAttribute(org.omg.CORBA.AttributeDescription a,
335: int indentation) {
336: indent(indentation);
337: String mode = "";
338: if (a.mode.equals(org.omg.CORBA.AttributeMode.ATTR_READONLY))
339: mode = "readonly ";
340: print(mode + "attribute " + TypeCode.idlTypeName(a.type) + " "
341: + a.name + ";" + "\n");
342: }
343:
344: /** print an IDL Enum
345: */
346:
347: public void printEnum(org.omg.CORBA.TypeDescription t,
348: int indentation) {
349: org.omg.CORBA.EnumDef e_def = org.omg.CORBA.EnumDefHelper
350: .narrow(ir.lookup_id(t.id));
351: if (e_def != null) {
352: String[] members = e_def.members();
353: indent(indentation);
354: StringBuffer vals = new StringBuffer();
355: if (members.length > 0)
356: vals.append(members[0]);
357: for (int i = 1; i < members.length; i++) {
358: vals.append("," + members[i]);
359: }
360: print("enum " + e_def.name() + " {" + vals + "};" + "\n\n");
361: } else {
362: logger.error("Error, could not find enum " + t.id
363: + " in IR ");
364: }
365:
366: }
367:
368: /** print an IDL Union
369: */
370:
371: public void printUnion(org.omg.CORBA.TypeDescription t,
372: int indentation) {
373: org.omg.CORBA.UnionDef u_def = org.omg.CORBA.UnionDefHelper
374: .narrow(ir.lookup_id(t.id));
375: if (u_def != null) {
376: org.omg.CORBA.UnionMember[] members = u_def.members();
377: indent(indentation);
378: print("union " + u_def.name() + " switch ( "
379: + TypeCode.idlTypeName(u_def.discriminator_type())
380: + " )\n");
381: print("{\n");
382: indent(indentation + 4);
383: int def_idx = -1;
384: for (int i = 0; i < members.length; i++) {
385: if (members[i].label.type().kind() == org.omg.CORBA.TCKind.tk_octet
386: && (members[i].label.extract_octet() == (byte) 0)) {
387: def_idx = i;
388: } else if (members[i].label.type().kind() == org.omg.CORBA.TCKind.tk_char) {
389: print("case \'" + members[i].label.extract_char()
390: + "\' : "
391: + TypeCode.idlTypeName(members[i].type)
392: + " " + members[i].name + ";" + "\n");
393: } else if (members[i].label.type().kind() == org.omg.CORBA.TCKind.tk_enum) {
394: // int val = members[i].label.extract_long();
395: try {
396: DynEnum dEnum = DynEnumHelper.narrow(factory
397: .create_dyn_any(members[i].label));
398:
399: // print("case " + members[i].label.type().member_name(val) + " : " +
400: print("case " + dEnum.get_as_string() + " : "
401: + TypeCode.idlTypeName(members[i].type)
402: + " " + members[i].name + ";" + "\n");
403: } catch (Exception bk) {
404: logger.error("unexpected exception", bk);
405: }
406: } else
407: print("case " + members[i].label.type() + " : "
408: + TypeCode.idlTypeName(members[i].type)
409: + " " + members[i].name + ";" + "\n");
410: }
411: if (def_idx != -1) {
412: print("default : "
413: + TypeCode.idlTypeName(members[def_idx].type)
414: + " " + members[def_idx].name + ";" + "\n");
415: }
416: indent(indentation);
417: print("};" + "\n\n");
418: } else {
419: logger.error("Error, could not find union " + t.id
420: + " in IR ");
421: }
422: }
423:
424: /**
425: * print an IDL alias
426: */
427:
428: public void printAlias(org.omg.CORBA.TypeDescription t,
429: int indentation) {
430: org.omg.CORBA.AliasDef adef = org.omg.CORBA.AliasDefHelper
431: .narrow(ir.lookup_id(t.id));
432: indent(indentation);
433:
434: String originalTypeName = TypeCode.idlTypeName(adef
435: .original_type_def().type());
436:
437: print("typedef " + originalTypeName + " " + adef.name()
438: + ";\n\n");
439:
440: }
441:
442: /**
443: * print an IDL operation
444: */
445:
446: public void printOperation(org.omg.CORBA.OperationDescription op,
447: int indentation) {
448: indent(indentation);
449:
450: String mode = "";
451: if (op.mode.equals(org.omg.CORBA.OperationMode.OP_ONEWAY))
452: mode = "oneway ";
453: print(mode + TypeCode.idlTypeName(op.result) + " " + op.name
454: + "(");
455:
456: indent(0);
457:
458: for (int i = 0; i < op.parameters.length - 1; i++) {
459: printParameter(op.parameters[i], ",");
460: }
461:
462: if (op.parameters.length > 0)
463: printParameter(op.parameters[op.parameters.length - 1], "");
464: print(")");
465:
466: if (op.exceptions.length > 0) {
467: print(" raises (");
468: print(TypeCode.idlTypeName(op.exceptions[0].type));
469: for (int i = 1; i < op.exceptions.length; i++) {
470: print(TypeCode.idlTypeName(op.exceptions[0].type) + ",");
471: }
472: print(")");
473: }
474: print(";" + "\n");
475: indent(indentation);
476: }
477:
478: public void printParameter(org.omg.CORBA.ParameterDescription p,
479: String separator) {
480: if (p.mode.equals(org.omg.CORBA.ParameterMode.PARAM_OUT))
481: print("out ");
482: else if (p.mode.equals(org.omg.CORBA.ParameterMode.PARAM_INOUT))
483: print("inout ");
484: else
485: print("in ");
486: print(TypeCode.idlTypeName(p.type) + " " + p.name);
487: print(separator);
488: }
489:
490: }
|