1 /****************************************************************************
2 * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the LGPL License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 ****************************************************************************/
8 package it.imolinfo.jbi4corba.webservice.generator.bcm;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 /**
14 * Parses the parameter types from a methond internal description. *
15 *
16 * The descriptors of the primitive types are single characters: Z for boolean, C
17 * for char, B for byte, S for short, I for int, F for float, J for long and D
18 * for double. The descriptor of a class type is the internal name of this class,
19 * preceded by L and followed by a semicolon. For instance the type descriptor
20 * of String is Ljava/lang/String;. Finally the descriptor of an array type is
21 * a square bracket followed by the descriptor of the array element type.
22 * Examples are: <br/>
23 * <li/> I -> int
24 * <li/> [I ->int[]
25 * <li/> Ljava/lang/String; -> String
26 * <li/> [Ljava/lang/String -> String[]
27 *
28 * A method description contains the parameter list with the return type, for
29 * example: <br/>
30 * ([Lit/imolinfo/jbi4corba/test/webservice/generator/EchoStruct;[ILorg/omg/CORBA/StringHolder;)Ljava/lang/String;
31 *
32 * This first implementation parses only the method paramter form the wholde description.
33 *
34 * @author marco
35 *
36 */
37 public class InternalMethodDescriptionParser {
38
39 private String description;
40
41 private String methodDescriptionTail;
42
43 public InternalMethodDescriptionParser(String desc) {
44 this.description = desc;
45 }
46
47 /**
48 * Parses the description string, separating the method "tail" (with the return type) and returns the array of
49 * parameters (in internal form).
50 * @return
51 */
52 public List<String> parse() {
53 List<String> params = new ArrayList<String>();
54 int firstBrPos = description.indexOf('(');
55 int lastBrPos = description.indexOf(')');
56 String parameterDesc = description.substring(firstBrPos + 1, lastBrPos);
57 methodDescriptionTail = description.substring(lastBrPos +1);
58 String descToConsume = parameterDesc;
59 while (!("".equals(descToConsume))) {
60 String nextParameter = getNextParam(descToConsume);
61 params.add(nextParameter);
62 // consumes th description
63 descToConsume = descToConsume.substring(nextParameter.length(), descToConsume.length());
64 }
65 return params;
66 }
67
68 /**
69 * Gets the next internal parameter description
70 *
71 * @param param
72 *
73 * @return the next param
74 */
75 private String getNextParam(String param) {
76
77 if ((param == null) || (param.equals(""))) {
78 return "";
79 }
80
81 // Symple types
82 if ((param.charAt(0) == 'Z') ||
83 (param.charAt(0) == 'C') ||
84 (param.charAt(0) == 'B') ||
85 (param.charAt(0) == 'S') ||
86 (param.charAt(0) == 'I') ||
87 (param.charAt(0) == 'F') ||
88 (param.charAt(0) == 'J') ||
89 (param.charAt(0) == 'D')) {
90 return new Character(param.charAt(0)).toString();
91 } else if (param.charAt(0) == '[') {
92 // Recursive call
93 String paramArray = param.charAt(0) + getNextParam(param.substring(1, param.length()));
94 return paramArray;
95 } else if (param.charAt(0) == 'L') {
96 // Object:recursive call
97 int pos = param.indexOf(';');
98 String paramArray = param.substring(0, pos + 1);
99 return paramArray;
100 } else {
101 return "";
102 }
103 }
104
105 public String getMethodDescriptionTail() {
106 return methodDescriptionTail;
107 }
108
109 public void setMethodDescriptionTail(String methodDescriptionTail) {
110 this.methodDescriptionTail = methodDescriptionTail;
111 }
112
113
114 }