1 package org.catacomb.act;
2
3 import org.catacomb.report.E;
4
5
6 public class FunctionSignature implements BlockSignature {
7
8 public int type;
9
10 public int sourceType;
11
12 public String info;
13 public String functionName;
14 public Object[] argTypes;
15
16
17 public FunctionSignature(String fn) {
18 this(fn, UNKNOWN);
19 }
20
21
22 public FunctionSignature(String fn, int typ) {
23 functionName = fn;
24 type = typ;
25 sourceType = USER_SOURCE;
26 }
27
28 public void setUserSource() {
29 sourceType = USER_SOURCE;
30 }
31
32 public void setSystemSource() {
33 sourceType = SYSTEM_SOURCE;
34 }
35
36
37 public int getTypeCode() {
38 return type;
39 }
40
41
42 public static String getTypeInfo(int itc) {
43 String ret = "";
44 if (itc == RECEIVE) {
45 ret = "Handlers: functions that are called when an event occurs in a connected component.";
46
47 } else if (itc == SEND) {
48 ret = "Senders: these send an event to any connected components. The handler \n" +
49 "on the receiving component will be called.";
50
51
52 } else if (itc == SETTER) {
53 ret = "Setters: these set a value for use later, but have no other effect: \n" +
54 "the value is available to connected components if they ask for it";
55
56 } else if (itc == GETTER) {
57 ret = "Getters: give access to quantities in connected components.";
58 }
59 return ret;
60 }
61
62
63 public void setInfo(String s) {
64 info = s;
65 }
66
67
68 public String getName() {
69 return functionName;
70 }
71
72
73 public String toJavaScriptStub() {
74 String ret = null;
75 if (sourceType == SYSTEM_SOURCE) {
76 ret = writeJsDoc();
77
78 } else {
79 ret = writeEmptyJsFunction();
80
81 }
82 return ret;
83 }
84
85
86 private String writeJsDoc() {
87 StringBuffer sb = new StringBuffer();
88
89 if (info != null) {
90 if (info.indexOf("\n") > 0) {
91 sb.append("/* Pre-defined function \n " + info + "\n*/\n");
92 } else {
93 sb.append("// Pre-defined function: " + info + "\n");
94 }
95 } else {
96 sb.append("// Pre-defined \n");
97 }
98 sb.append("// ");
99 sb.append(functionName);
100 sb.append("(");
101 if (argTypes != null) {
102 E.missing();
103 }
104 sb.append(");\n");
105 return sb.toString();
106 }
107
108
109 private String writeEmptyJsFunction() {
110 StringBuffer sb = new StringBuffer();
111
112 if (info != null) {
113 if (info.length() > 60 || info.indexOf("\n") > 0) {
114 sb.append("/*\n");
115 sb.append(info);
116 sb.append("\n");
117
118 } else {
119 sb.append("//");
120 sb.append(info);
121 sb.append("\n");
122 }
123 }
124 sb.append("function ");
125 sb.append(functionName);
126 sb.append("(");
127 if (argTypes != null) {
128 E.missing();
129 }
130 sb.append(") {\n");
131 sb.append("}\n");
132 return sb.toString();
133 }
134
135 }