1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.utils;
17
18 import java.util.Arrays;
19 import java.util.HashSet;
20 import java.util.Set;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.apache.commons.lang.StringUtils;
25
26
27
28
29 public final class JavadocCommentHelper
30 {
31
32
33
34
35
36
37
38
39
40
41 private static final Pattern INLINE_PATTERN = Pattern.compile(
42 "\\{@[\\w]+\\s*([#\\.\\w]+(\\([^)]*\\))?)\\s*([^}]+)?\\}",
43 Pattern.MULTILINE);
44
45
46
47
48 private static final Set<String> BLOCK_ELEMENTS = new HashSet<String>(
49 Arrays.asList(new String[] { "address", "blockquote", "center", "del",
50 "dir", "div", "dl", "fieldset", "form", "h1",
51 "h2", "h3", "h4", "h5", "h5", "hr", "ins",
52 "isindex", "menu", "noframes", "noscript",
53 "ol", "p", "pre", "table", "ul" }));
54
55
56
57
58
59
60 private final String prefix1;
61
62
63
64
65 private final String suffix1;
66
67
68
69
70 private final String prefix2;
71
72
73
74
75 private final String suffix2;
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public JavadocCommentHelper(final String prefix1, final String suffix1,
90 final String prefix2, final String suffix2)
91 {
92 this.prefix1 = prefix1;
93 this.suffix1 = suffix1;
94 this.prefix2 = prefix2;
95 this.suffix2 = suffix2;
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public static JavadocCommentHelper createHtml()
112 {
113 return new JavadocCommentHelper("<tt>", "</tt>", "<i>", "</i>");
114 }
115
116
117
118
119
120
121 public static JavadocCommentHelper createText()
122 {
123 return new JavadocCommentHelper("'", "'", "", "");
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public String expandFirstBlock(final String fragment)
139 {
140 if (StringUtils.isBlank(fragment))
141 {
142 return fragment;
143 }
144
145 final String norm = fragment.trim();
146 if (isStartingWithTag(norm))
147 {
148 final String elementName = calcElementName(norm);
149 if (isBlockElement(elementName))
150 {
151 final int index = norm.indexOf("</" + elementName);
152 return createBlock(norm, index);
153 }
154 }
155 else
156 {
157 final int index = calcIndexOfFirstBlockElement(norm);
158 return createBlock(norm, index);
159 }
160
161 return fragment;
162 }
163
164 private static int calcIndexOfFirstBlockElement(final String fragment)
165 {
166 int index = Integer.MAX_VALUE;
167 for (final String elementName : BLOCK_ELEMENTS)
168 {
169 final int currentIndex = fragment.indexOf('<' + elementName);
170 if (currentIndex != -1)
171 {
172 index = Math.min(currentIndex, index);
173 }
174 }
175
176 return index != Integer.MAX_VALUE ? index : -1;
177 }
178
179 private static String createBlock(final String fragment, final int index)
180 {
181 if (index != -1)
182 {
183 final String firstBlock = fragment.substring(0, index);
184 final String rest = fragment.substring(index);
185 return "<p>" + firstBlock + "</p>\n" + rest;
186 }
187 else
188 {
189 return "<p>" + fragment + "</p>";
190 }
191 }
192
193 private static boolean isStartingWithTag(final String fragment)
194 {
195 return fragment.startsWith("<");
196 }
197
198 private static String calcElementName(final String fragment)
199 {
200 final StringBuilder buffer = new StringBuilder();
201 final int end = fragment.length();
202 int i = 1;
203 while (i < end)
204 {
205 final char ch = fragment.charAt(i++);
206 if (Character.isLetterOrDigit(ch))
207 {
208 buffer.append(ch);
209 }
210 }
211
212 final String name = buffer.toString();
213 return name;
214 }
215
216 private static boolean isBlockElement(final String elementName)
217 {
218 return BLOCK_ELEMENTS.contains(elementName);
219 }
220
221
222
223
224
225
226
227 public String replaceJavadocInlines(final String fragment)
228 {
229 if (StringUtils.isBlank(fragment))
230 {
231 return fragment;
232 }
233
234 final StringBuffer buffer = new StringBuffer(fragment.length());
235 final Matcher matcher = INLINE_PATTERN.matcher(fragment);
236
237 while (matcher.find())
238 {
239 final String group2 = matcher.group(3);
240 if (group2 != null)
241 {
242 final String replacement = prefix2 + group2 + suffix2;
243 matcher.appendReplacement(buffer, replacement);
244 }
245 else
246 {
247 String group1 = matcher.group(1);
248 if (group1.length() > 1 && group1.charAt(0) == '#')
249 {
250 group1 = group1.substring(1);
251 }
252 final String replacement = prefix1 + group1 + suffix1;
253 matcher.appendReplacement(buffer, replacement);
254 }
255 }
256 matcher.appendTail(buffer);
257
258 return buffer.toString();
259 }
260
261
262
263 }