1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.exolab.castor.builder.printing;
17
18 import java.lang.reflect.Array;
19 import java.util.ArrayList;
20
21 import org.exolab.javasource.JAnnotation;
22 import org.exolab.javasource.JClass;
23 import org.exolab.javasource.JEnum;
24
25
26
27
28
29 public class TemplateHelper {
30
31
32
33
34
35
36
37 public String removeLineBreaks(String string) {
38 return string.replaceAll("\n", "");
39 }
40
41
42
43
44
45
46
47
48 public ArrayList<String> getLines(String comment) {
49 comment = removeLineBreaks(comment);
50 ArrayList<String> list = new ArrayList<String>();
51 do {
52 comment = computeLine(comment, list);
53 } while (comment.length() > 0);
54 return list;
55 }
56
57
58
59
60
61
62
63
64
65 private String computeLine(String comment, ArrayList<String> list) {
66 if (comment == null) {
67 return "";
68 }
69
70 if (comment.length() <=70) {
71 list.add(comment);
72 return "";
73 }
74
75 int pos = comment.indexOf(' ', 70);
76 if (pos == -1) {
77 list.add(comment);
78 return "";
79 }
80
81 String line = comment.substring(0,pos);
82 list.add(line);
83 return comment.substring(pos + 1);
84
85 }
86
87
88
89
90
91
92
93 public boolean isEnum(final JClass jClass) {
94 return (jClass instanceof JEnum);
95 }
96
97
98
99
100
101
102
103
104 public String printAnnotation(final JAnnotation annotation, String shift) {
105 StringBuilder stringBuffer = new StringBuilder(32);
106 stringBuffer.append(shift);
107 stringBuffer.append("@");
108 stringBuffer.append(annotation.getAnnotationType().getLocalName());
109 stringBuffer.append("(");
110
111 String[] elementNames = annotation.getElementNames();
112 if (elementNames.length == 1 && elementNames[0].equals(JAnnotation.VALUE)) {
113
114 stringBuffer.append(printAnnotationValue(annotation.getElementValueObject(JAnnotation.VALUE),shift));
115 } else if (elementNames.length > 0) {
116
117 int maxLength = 0;
118 for (int i = 0; i < elementNames.length; i++) {
119 int elementNameLength = elementNames[i].length();
120 if (elementNameLength > maxLength) { maxLength = elementNameLength; }
121 }
122
123 stringBuffer.append("\n");
124 stringBuffer.append(shift + " ");
125 for (int i = 0; i < elementNames.length; i++) {
126 int elementNameLength = elementNames[i].length();
127
128 stringBuffer.append(elementNames[i]);
129 for (int p = 0; p < maxLength - elementNameLength; p++) {
130 stringBuffer.append(" ");
131 }
132
133 stringBuffer.append(" = ");
134
135 stringBuffer.append(printAnnotationValue(
136 annotation.getElementValueObject(elementNames[i]), shift));
137 if (i < elementNames.length - 1) {
138 stringBuffer.append(",");
139 stringBuffer.append("\n");
140 stringBuffer.append(shift + " ");
141 }
142 }
143 }
144 stringBuffer.append(")");
145 return stringBuffer.toString();
146 }
147
148
149
150
151
152
153
154
155 private String printAnnotationValue(final Object elementValue, final String shift) {
156
157 if (elementValue instanceof String) {
158 return (String) elementValue;
159 } else if (elementValue instanceof JAnnotation) {
160 JAnnotation annotation = (JAnnotation) elementValue;
161 return printAnnotation(annotation, shift);
162 } else if (elementValue.getClass().isArray()) {
163
164 int listLength = Array.getLength(elementValue);
165 if (listLength == 1) {
166 return printAnnotationValue(Array.get(elementValue, 0), shift);
167 }
168
169 StringBuilder stringBuffer = new StringBuilder();
170 stringBuffer.append("\n");
171 stringBuffer.append("{");
172 stringBuffer.append("\n");
173 stringBuffer.append(shift);
174 for (int i = 0; i < listLength; i++) {
175 stringBuffer.append(shift);
176 stringBuffer.append(printAnnotationValue(Array.get(elementValue, i), shift));
177 if (i < listLength - 1) { stringBuffer.append(","); }
178 stringBuffer.append("\n");
179 }
180 stringBuffer.append("}");
181 return stringBuffer.toString();
182 }
183 throw new IllegalArgumentException("'" + elementValue + "' was not expected.");
184 }
185
186 }