1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.doxia.sink.impl;
20
21 import javax.swing.text.AttributeSet;
22 import javax.swing.text.MutableAttributeSet;
23
24 import java.util.Arrays;
25 import java.util.Enumeration;
26
27 import org.apache.maven.doxia.markup.Markup;
28 import org.apache.maven.doxia.sink.SinkEventAttributes;
29
30
31
32
33
34
35
36 public class SinkUtils {
37
38
39 private SinkUtils() {
40
41 }
42
43
44
45
46 public static final String[] SINK_BASE_ATTRIBUTES = {
47 SinkEventAttributes.CLASS,
48 SinkEventAttributes.ID,
49 SinkEventAttributes.LANG,
50 SinkEventAttributes.STYLE,
51 SinkEventAttributes.TITLE
52 };
53
54
55
56
57 public static final String[] SINK_BR_ATTRIBUTES = {
58 SinkEventAttributes.CLASS, SinkEventAttributes.ID,
59 SinkEventAttributes.STYLE, SinkEventAttributes.TITLE
60 };
61
62
63
64
65 public static final String[] SINK_IMG_ATTRIBUTES;
66
67
68
69
70 public static final String[] SINK_SECTION_ATTRIBUTES;
71
72
73
74
75 public static final String[] SINK_VERBATIM_ATTRIBUTES;
76
77
78
79
80 public static final String[] SINK_HR_ATTRIBUTES;
81
82
83
84
85 public static final String[] SINK_LINK_ATTRIBUTES;
86
87
88
89
90 public static final String[] SINK_TABLE_ATTRIBUTES;
91
92
93
94
95 public static final String[] SINK_TD_ATTRIBUTES;
96
97
98
99
100 public static final String[] SINK_TR_ATTRIBUTES;
101
102 private static final String[] IMG_ATTRIBUTES = {
103 SinkEventAttributes.ALT,
104 SinkEventAttributes.HEIGHT,
105 SinkEventAttributes.ISMAP,
106 SinkEventAttributes.SRC,
107 SinkEventAttributes.USEMAP,
108 SinkEventAttributes.WIDTH
109 };
110
111 private static final String[] HR_ATTRIBUTES = {};
112
113 private static final String[] LINK_ATTRIBUTES = {
114 SinkEventAttributes.HREF,
115 SinkEventAttributes.HREFLANG,
116 SinkEventAttributes.REL,
117 SinkEventAttributes.TARGET,
118 SinkEventAttributes.TYPE
119 };
120
121 private static final String[] TABLE_ATTRIBUTES = {};
122
123 private static final String[] TABLE_CELL_ATTRIBUTES = {
124 SinkEventAttributes.COLSPAN, SinkEventAttributes.HEADERS, SinkEventAttributes.ROWSPAN
125 };
126
127 static {
128 SINK_IMG_ATTRIBUTES = join(SINK_BASE_ATTRIBUTES, IMG_ATTRIBUTES);
129 SINK_SECTION_ATTRIBUTES = join(SINK_BASE_ATTRIBUTES, new String[0]);
130 SINK_VERBATIM_ATTRIBUTES = join(SINK_BASE_ATTRIBUTES, new String[] {SinkEventAttributes.DECORATION});
131 SINK_HR_ATTRIBUTES = join(SINK_BASE_ATTRIBUTES, HR_ATTRIBUTES);
132 SINK_LINK_ATTRIBUTES = join(SINK_BASE_ATTRIBUTES, LINK_ATTRIBUTES);
133 SINK_TABLE_ATTRIBUTES = join(SINK_BASE_ATTRIBUTES, TABLE_ATTRIBUTES);
134 SINK_TR_ATTRIBUTES = join(SINK_BASE_ATTRIBUTES, new String[0]);
135 SINK_TD_ATTRIBUTES = join(SINK_BASE_ATTRIBUTES, TABLE_CELL_ATTRIBUTES);
136 }
137
138 private static String[] join(String[] a, String[] b) {
139 String[] temp = new String[a.length + b.length];
140 System.arraycopy(a, 0, temp, 0, a.length);
141 System.arraycopy(b, 0, temp, a.length, b.length);
142
143 Arrays.sort(temp);
144
145 return temp;
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160 public static String getAttributeString(AttributeSet att) {
161 if (att == null) {
162 return "";
163 }
164
165 StringBuilder sb = new StringBuilder();
166
167 Enumeration<?> names = att.getAttributeNames();
168
169 while (names.hasMoreElements()) {
170 Object key = names.nextElement();
171 Object value = att.getAttribute(key);
172
173 if (value instanceof AttributeSet) {
174
175 if (SinkEventAttributes.STYLE.equals(key.toString())) {
176 sb.append(Markup.SPACE)
177 .append(key.toString())
178 .append(Markup.EQUAL)
179 .append(Markup.QUOTE)
180 .append(asCssString((AttributeSet) value))
181 .append(Markup.QUOTE);
182 }
183 } else {
184 sb.append(Markup.SPACE)
185 .append(key.toString())
186 .append(Markup.EQUAL)
187 .append(Markup.QUOTE)
188 .append(value.toString())
189 .append(Markup.QUOTE);
190 }
191 }
192
193 return sb.toString();
194 }
195
196 private static String asCssString(AttributeSet att) {
197 StringBuilder sb = new StringBuilder();
198
199 Enumeration<?> names = att.getAttributeNames();
200
201 while (names.hasMoreElements()) {
202 Object key = names.nextElement();
203 Object value = att.getAttribute(key);
204
205
206 if (!(value instanceof AttributeSet)) {
207 sb.append(key.toString())
208 .append(Markup.COLON)
209 .append(Markup.SPACE)
210 .append(value.toString());
211
212 if (names.hasMoreElements()) {
213 sb.append(Markup.SEMICOLON).append(Markup.SPACE);
214 }
215 }
216 }
217
218 return sb.toString();
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232 public static MutableAttributeSet filterAttributes(AttributeSet attributes, String[] valids) {
233 if (attributes == null) {
234 return null;
235 }
236
237 if (valids == null || valids.length == 0) {
238 return new SinkEventAttributeSet(0);
239 }
240
241 MutableAttributeSet atts = new SinkEventAttributeSet(attributes.getAttributeCount());
242
243 Enumeration<?> names = attributes.getAttributeNames();
244
245 while (names.hasMoreElements()) {
246 String key = names.nextElement().toString();
247
248 if (Arrays.binarySearch(valids, key) >= 0) {
249 atts.addAttribute(key, attributes.getAttribute(key));
250 }
251 }
252
253 return atts;
254 }
255 }