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