1 |
|
package net.sourceforge.pebble.dao.file; |
2 |
|
|
3 |
|
import net.sourceforge.pebble.domain.*; |
4 |
|
import org.apache.commons.logging.Log; |
5 |
|
import org.apache.commons.logging.LogFactory; |
6 |
|
import org.xml.sax.Attributes; |
7 |
|
import org.xml.sax.SAXException; |
8 |
|
import org.xml.sax.SAXParseException; |
9 |
|
import org.xml.sax.helpers.DefaultHandler; |
10 |
|
|
11 |
|
import java.text.ParseException; |
12 |
|
import java.text.SimpleDateFormat; |
13 |
|
import java.util.Date; |
14 |
|
import java.util.Locale; |
15 |
|
|
|
|
| 93% |
Uncovered Elements: 22 (315) |
Complexity: 81 |
Complexity Density: 0,34 |
|
16 |
|
public class BlogEntryHandler extends DefaultHandler { |
17 |
|
|
18 |
|
|
19 |
|
private static Log log = LogFactory.getLog(BlogEntryHandler.class); |
20 |
|
|
21 |
|
private static final int NOT_DEFINED = -1; |
22 |
|
private static final int TITLE = 0; |
23 |
|
private static final int EXCERPT = 1; |
24 |
|
private static final int BODY = 2; |
25 |
|
private static final int DATE = 3; |
26 |
|
private static final int AUTHOR = 4; |
27 |
|
private static final int ORIGINAL_PERMALINK = 5; |
28 |
|
private static final int STATIC_NAME = 6; |
29 |
|
private static final int CATEGORY = 7; |
30 |
|
private static final int COMMENTS_ENABLED = 8; |
31 |
|
private static final int TRACKBACKS_ENABLED = 9; |
32 |
|
private static final int EMAIL = 10; |
33 |
|
private static final int WEBSITE = 11; |
34 |
|
private static final int BLOG_NAME = 12; |
35 |
|
private static final int URL = 13; |
36 |
|
private static final int PARENT = 14; |
37 |
|
private static final int IP_ADDRESS = 15; |
38 |
|
private static final int SIZE = 16; |
39 |
|
private static final int TYPE = 17; |
40 |
|
private static final int STATE = 18; |
41 |
|
private static final int TAGS = 19; |
42 |
|
private static final int SUBTITLE = 20; |
43 |
|
private static final int TIME_ZONE = 21; |
44 |
|
private static final int AUTHENTICATED = 22; |
45 |
|
|
46 |
|
private static final int IN_BLOG_ENTRY = 100; |
47 |
|
private static final int IN_COMMENT = 101; |
48 |
|
private static final int IN_TRACKBACK = 102; |
49 |
|
private static final int IN_ATTACHMENT = 103; |
50 |
|
|
51 |
|
private BlogEntry blogEntry; |
52 |
|
private int groupStatus = IN_BLOG_ENTRY; |
53 |
|
private int elementStatus = NOT_DEFINED; |
54 |
|
private SimpleDateFormat dateTimeFormats[]; |
55 |
|
|
56 |
|
private StringBuffer elementContent; |
57 |
|
|
58 |
|
private String attachmentUrl; |
59 |
|
private String attachmentSize; |
60 |
|
private String attachmentType; |
61 |
|
|
62 |
|
private String commentTitle; |
63 |
|
private String commentBody; |
64 |
|
private String commentAuthor; |
65 |
|
private String commentWebsite; |
66 |
|
private String commentIpAddress; |
67 |
|
private String commentEmail; |
68 |
|
private Date commentDate; |
69 |
|
private long commentParent = -1; |
70 |
|
private State commentState = State.APPROVED; |
71 |
|
private boolean commentAuthenticated = false; |
72 |
|
|
73 |
|
private String trackBackTitle; |
74 |
|
private String trackBackExcerpt; |
75 |
|
private String trackBackBlogName; |
76 |
|
private String trackBackUrl; |
77 |
|
private String trackBackIpAddress; |
78 |
|
private Date trackBackDate; |
79 |
|
private State trackBackState = State.APPROVED; |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 1 |
Complexity Density: 0,05 |
|
81 |
52
|
public BlogEntryHandler(BlogEntry blogEntry) {... |
82 |
52
|
this.blogEntry = blogEntry; |
83 |
|
|
84 |
|
|
85 |
52
|
SimpleDateFormat format; |
86 |
52
|
dateTimeFormats = new SimpleDateFormat[6]; |
87 |
|
|
88 |
52
|
format = new SimpleDateFormat(FileBlogEntryDAO.NEW_PERSISTENT_DATETIME_FORMAT, Locale.ENGLISH); |
89 |
52
|
format.setTimeZone(blogEntry.getBlog().getTimeZone()); |
90 |
52
|
dateTimeFormats[0] = format; |
91 |
|
|
92 |
52
|
format = new SimpleDateFormat(FileBlogEntryDAO.NEW_PERSISTENT_DATETIME_FORMAT, blogEntry.getBlog().getLocale()); |
93 |
52
|
format.setTimeZone(blogEntry.getBlog().getTimeZone()); |
94 |
52
|
dateTimeFormats[1] = format; |
95 |
|
|
96 |
52
|
format = new SimpleDateFormat(FileBlogEntryDAO.NEW_PERSISTENT_DATETIME_FORMAT); |
97 |
52
|
format.setTimeZone(blogEntry.getBlog().getTimeZone()); |
98 |
52
|
dateTimeFormats[2] = format; |
99 |
|
|
100 |
52
|
format = new SimpleDateFormat(FileBlogEntryDAO.OLD_PERSISTENT_DATETIME_FORMAT, Locale.ENGLISH); |
101 |
52
|
format.setTimeZone(blogEntry.getBlog().getTimeZone()); |
102 |
52
|
dateTimeFormats[3] = format; |
103 |
|
|
104 |
52
|
format = new SimpleDateFormat(FileBlogEntryDAO.OLD_PERSISTENT_DATETIME_FORMAT, blogEntry.getBlog().getLocale()); |
105 |
52
|
format.setTimeZone(blogEntry.getBlog().getTimeZone()); |
106 |
52
|
dateTimeFormats[4] = format; |
107 |
|
|
108 |
52
|
format = new SimpleDateFormat(FileBlogEntryDAO.OLD_PERSISTENT_DATETIME_FORMAT); |
109 |
52
|
format.setTimeZone(blogEntry.getBlog().getTimeZone()); |
110 |
52
|
dateTimeFormats[5] = format; |
111 |
|
} |
112 |
|
|
|
|
| 96,3% |
Uncovered Elements: 4 (109) |
Complexity: 27 |
Complexity Density: 0,47 |
|
113 |
902
|
public void startElement(String uri, String name, String qName, Attributes attributes) throws SAXException {... |
114 |
|
|
115 |
902
|
elementContent = new StringBuffer(); |
116 |
902
|
if (name.equals("title")) { |
117 |
68
|
elementStatus = TITLE; |
118 |
834
|
} else if (name.equals("subtitle")) { |
119 |
52
|
elementStatus = SUBTITLE; |
120 |
782
|
} else if (name.equals("excerpt")) { |
121 |
56
|
elementStatus = EXCERPT; |
122 |
726
|
} else if (name.equals("body")) { |
123 |
66
|
elementStatus = BODY; |
124 |
660
|
} else if (name.equals("date")) { |
125 |
70
|
elementStatus = DATE; |
126 |
590
|
} else if (name.equals("timeZone")) { |
127 |
52
|
elementStatus = TIME_ZONE; |
128 |
538
|
} else if (name.equals("author")) { |
129 |
66
|
elementStatus = AUTHOR; |
130 |
472
|
} else if (name.equals("originalPermalink")) { |
131 |
0
|
elementStatus = ORIGINAL_PERMALINK; |
132 |
472
|
} else if (name.equals("staticName")) { |
133 |
50
|
elementStatus = STATIC_NAME; |
134 |
422
|
} else if (name.equals("category")) { |
135 |
56
|
elementStatus = CATEGORY; |
136 |
366
|
} else if (name.equals("tags")) { |
137 |
52
|
elementStatus = TAGS; |
138 |
314
|
} else if (name.equals("commentsEnabled")) { |
139 |
52
|
elementStatus = COMMENTS_ENABLED; |
140 |
262
|
} else if (name.equals("trackBacksEnabled")) { |
141 |
52
|
elementStatus = TRACKBACKS_ENABLED; |
142 |
210
|
} else if (name.equals("email")) { |
143 |
14
|
elementStatus = EMAIL; |
144 |
196
|
} else if (name.equals("website")) { |
145 |
14
|
elementStatus = WEBSITE; |
146 |
182
|
} else if (name.equals("ipAddress")) { |
147 |
18
|
elementStatus = IP_ADDRESS; |
148 |
164
|
} else if (name.equals("authenticated")) { |
149 |
12
|
elementStatus = AUTHENTICATED; |
150 |
152
|
} else if (name.equals("blogName")) { |
151 |
4
|
elementStatus = BLOG_NAME; |
152 |
148
|
} else if (name.equals("url")) { |
153 |
6
|
elementStatus = URL; |
154 |
142
|
} else if (name.equals("parent")) { |
155 |
0
|
elementStatus = PARENT; |
156 |
142
|
} else if (name.equals("state")) { |
157 |
66
|
elementStatus = STATE; |
158 |
76
|
} else if (name.equals("size")) { |
159 |
2
|
elementStatus = SIZE; |
160 |
74
|
} else if (name.equals("type")) { |
161 |
2
|
elementStatus = TYPE; |
162 |
72
|
} else if (name.equals("attachment")) { |
163 |
2
|
groupStatus = IN_ATTACHMENT; |
164 |
2
|
elementStatus = NOT_DEFINED; |
165 |
70
|
} else if (name.equals("comment")) { |
166 |
14
|
groupStatus = IN_COMMENT; |
167 |
14
|
elementStatus = NOT_DEFINED; |
168 |
56
|
} else if (name.equals("trackback")) { |
169 |
4
|
groupStatus = IN_TRACKBACK; |
170 |
4
|
elementStatus = NOT_DEFINED; |
171 |
|
} else { |
172 |
52
|
elementStatus = NOT_DEFINED; |
173 |
|
} |
174 |
|
} |
175 |
|
|
|
|
| 94,6% |
Uncovered Elements: 9 (166) |
Complexity: 46 |
Complexity Density: 0,31 |
|
176 |
902
|
public void endElement(String uri, String name, String qName) throws SAXException {... |
177 |
|
|
178 |
902
|
if (groupStatus == IN_BLOG_ENTRY) { |
179 |
730
|
switch (elementStatus) { |
180 |
52
|
case TITLE : |
181 |
52
|
blogEntry.setTitle(elementContent.toString()); |
182 |
52
|
break; |
183 |
52
|
case SUBTITLE : |
184 |
52
|
blogEntry.setSubtitle(elementContent.toString()); |
185 |
52
|
break; |
186 |
52
|
case EXCERPT : |
187 |
52
|
blogEntry.setExcerpt(elementContent.toString()); |
188 |
52
|
break; |
189 |
52
|
case BODY : |
190 |
52
|
blogEntry.setBody(elementContent.toString()); |
191 |
52
|
break; |
192 |
52
|
case DATE : |
193 |
52
|
blogEntry.setDate(getDate(elementContent.toString())); |
194 |
52
|
break; |
195 |
52
|
case TIME_ZONE : |
196 |
52
|
blogEntry.setTimeZoneId(elementContent.toString()); |
197 |
52
|
break; |
198 |
52
|
case STATE : |
199 |
52
|
if (elementContent.toString().equals(State.UNPUBLISHED.getName())) { |
200 |
2
|
blogEntry.setPublished(false); |
201 |
|
} else { |
202 |
50
|
blogEntry.setPublished(true); |
203 |
|
} |
204 |
52
|
break; |
205 |
52
|
case AUTHOR : |
206 |
52
|
blogEntry.setAuthor(elementContent.toString()); |
207 |
52
|
break; |
208 |
0
|
case ORIGINAL_PERMALINK : |
209 |
0
|
blogEntry.setOriginalPermalink(elementContent.toString()); |
210 |
0
|
break; |
211 |
56
|
case CATEGORY : |
212 |
56
|
blogEntry.addCategory(blogEntry.getBlog().getCategory(elementContent.toString())); |
213 |
56
|
break; |
214 |
52
|
case TAGS : |
215 |
52
|
blogEntry.setTags(elementContent.toString()); |
216 |
52
|
break; |
217 |
52
|
case COMMENTS_ENABLED : |
218 |
52
|
blogEntry.setCommentsEnabled(Boolean.valueOf(elementContent.toString()).booleanValue()); |
219 |
52
|
break; |
220 |
52
|
case TRACKBACKS_ENABLED : |
221 |
52
|
blogEntry.setTrackBacksEnabled(Boolean.valueOf(elementContent.toString()).booleanValue()); |
222 |
52
|
break; |
223 |
|
} |
224 |
172
|
} else if (groupStatus == IN_ATTACHMENT && name.equals("attachment")) { |
225 |
2
|
Attachment attachment = new Attachment(); |
226 |
2
|
attachment.setUrl(attachmentUrl); |
227 |
2
|
attachment.setSize(Long.parseLong(attachmentSize)); |
228 |
2
|
attachment.setType(attachmentType); |
229 |
2
|
blogEntry.setAttachment(attachment); |
230 |
2
|
groupStatus = IN_BLOG_ENTRY; |
231 |
|
|
232 |
2
|
attachmentUrl = null; |
233 |
2
|
attachmentSize = null; |
234 |
2
|
attachmentType = null; |
235 |
170
|
} else if (groupStatus == IN_ATTACHMENT) { |
236 |
6
|
switch (elementStatus) { |
237 |
2
|
case URL : |
238 |
2
|
attachmentUrl = elementContent.toString(); |
239 |
2
|
break; |
240 |
2
|
case SIZE : |
241 |
2
|
attachmentSize = elementContent.toString(); |
242 |
2
|
break; |
243 |
2
|
case TYPE : |
244 |
2
|
attachmentType = elementContent.toString(); |
245 |
2
|
break; |
246 |
|
} |
247 |
164
|
} else if (groupStatus == IN_COMMENT && name.equals("comment")) { |
248 |
14
|
Comment comment = blogEntry.createComment(commentTitle, commentBody, commentAuthor, commentEmail, commentWebsite, commentIpAddress, commentDate, commentState); |
249 |
14
|
if (commentParent != -1) { |
250 |
0
|
comment.setParent(blogEntry.getComment(commentParent)); |
251 |
|
} |
252 |
14
|
comment.setAuthenticated(commentAuthenticated); |
253 |
14
|
blogEntry.addComment(comment); |
254 |
14
|
groupStatus = IN_BLOG_ENTRY; |
255 |
|
|
256 |
|
|
257 |
14
|
commentTitle = null; |
258 |
14
|
commentBody = null; |
259 |
14
|
commentAuthor = null; |
260 |
14
|
commentWebsite = null; |
261 |
14
|
commentIpAddress = null; |
262 |
14
|
commentEmail = null; |
263 |
14
|
commentDate = null; |
264 |
14
|
commentParent = -1; |
265 |
14
|
commentState = State.APPROVED; |
266 |
14
|
commentAuthenticated = false; |
267 |
150
|
} else if (groupStatus == IN_COMMENT) { |
268 |
120
|
switch (elementStatus) { |
269 |
12
|
case TITLE : |
270 |
12
|
commentTitle = elementContent.toString(); |
271 |
12
|
break; |
272 |
14
|
case BODY : |
273 |
14
|
commentBody = elementContent.toString(); |
274 |
14
|
break; |
275 |
14
|
case DATE : |
276 |
14
|
commentDate = getDate(elementContent.toString()); |
277 |
14
|
break; |
278 |
14
|
case AUTHOR : |
279 |
14
|
commentAuthor = elementContent.toString(); |
280 |
14
|
break; |
281 |
14
|
case EMAIL : |
282 |
14
|
commentEmail = elementContent.toString(); |
283 |
14
|
break; |
284 |
14
|
case WEBSITE : |
285 |
14
|
commentWebsite = elementContent.toString(); |
286 |
14
|
break; |
287 |
14
|
case IP_ADDRESS : |
288 |
14
|
commentIpAddress = elementContent.toString(); |
289 |
14
|
break; |
290 |
0
|
case PARENT : |
291 |
0
|
commentParent = Long.parseLong(elementContent.toString()); |
292 |
0
|
break; |
293 |
12
|
case STATE : |
294 |
12
|
commentState = State.getState(elementContent.toString()); |
295 |
12
|
break; |
296 |
12
|
case AUTHENTICATED : |
297 |
12
|
commentAuthenticated = Boolean.parseBoolean(elementContent.toString()); |
298 |
12
|
break; |
299 |
|
} |
300 |
30
|
} else if (groupStatus == IN_TRACKBACK && name.equals("trackback")) { |
301 |
4
|
TrackBack trackBack = blogEntry.createTrackBack(trackBackTitle, trackBackExcerpt, trackBackUrl, trackBackBlogName, trackBackIpAddress, trackBackDate, trackBackState); |
302 |
4
|
blogEntry.addTrackBack(trackBack); |
303 |
4
|
groupStatus = IN_BLOG_ENTRY; |
304 |
|
|
305 |
|
|
306 |
4
|
trackBackTitle = null; |
307 |
4
|
trackBackExcerpt = null; |
308 |
4
|
trackBackBlogName = null; |
309 |
4
|
trackBackUrl = null; |
310 |
4
|
trackBackIpAddress = null; |
311 |
4
|
trackBackDate = null; |
312 |
4
|
trackBackState = State.APPROVED; |
313 |
26
|
} else if (groupStatus == IN_TRACKBACK) { |
314 |
26
|
switch (elementStatus) { |
315 |
4
|
case TITLE : |
316 |
4
|
trackBackTitle = elementContent.toString(); |
317 |
4
|
break; |
318 |
4
|
case EXCERPT : |
319 |
4
|
trackBackExcerpt = elementContent.toString(); |
320 |
4
|
break; |
321 |
4
|
case DATE : |
322 |
4
|
trackBackDate = getDate(elementContent.toString()); |
323 |
4
|
break; |
324 |
4
|
case BLOG_NAME : |
325 |
4
|
trackBackBlogName = elementContent.toString(); |
326 |
4
|
break; |
327 |
4
|
case URL : |
328 |
4
|
trackBackUrl = elementContent.toString(); |
329 |
4
|
break; |
330 |
4
|
case IP_ADDRESS : |
331 |
4
|
trackBackIpAddress = elementContent.toString(); |
332 |
4
|
break; |
333 |
2
|
case STATE : |
334 |
2
|
trackBackState = State.getState(elementContent.toString()); |
335 |
2
|
break; |
336 |
|
} |
337 |
|
} |
338 |
|
|
339 |
902
|
elementStatus = NOT_DEFINED; |
340 |
|
} |
341 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
342 |
1576
|
public void characters(char ch[], int start, int length) throws SAXException {... |
343 |
1576
|
elementContent.append(new String(ch, start, length)); |
344 |
|
|
345 |
|
} |
346 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
347 |
0
|
public void warning(SAXParseException e) throws SAXException {... |
348 |
0
|
log.warn(e); |
349 |
|
} |
350 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
351 |
0
|
public void error(SAXParseException e) throws SAXException {... |
352 |
0
|
log.error(e); |
353 |
|
} |
354 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
355 |
0
|
public void fatalError(SAXParseException e) throws SAXException {... |
356 |
0
|
log.fatal(e); |
357 |
|
} |
358 |
|
|
|
|
| 57,1% |
Uncovered Elements: 3 (7) |
Complexity: 3 |
Complexity Density: 0,6 |
|
359 |
70
|
private Date getDate(String s) {... |
360 |
100
|
for (int i = 0; i < dateTimeFormats.length; i++) { |
361 |
100
|
try { |
362 |
100
|
return dateTimeFormats[i].parse(s); |
363 |
|
} catch (ParseException pe) { |
364 |
|
} |
365 |
|
} |
366 |
|
|
367 |
0
|
log.error("Could not parse date of " + s); |
368 |
0
|
return null; |
369 |
|
} |
370 |
|
|
371 |
|
} |