📄 fileblogentrydao.java
字号:
dateNode.appendChild(doc.createTextNode(sdf.format(blogEntry.getDate()))); timeZoneNode.appendChild(doc.createTextNode(blogEntry.getTimeZoneId())); stateNode.appendChild(createTextNode(doc, blogEntry.getState().getName())); Attachment attachment = blogEntry.getAttachment(); if (attachment != null) { root.appendChild(attachmentNode); Element attachmentUrlNode = doc.createElement("url"); attachmentUrlNode.appendChild(createTextNode(doc, attachment.getUrl())); attachmentNode.appendChild(attachmentUrlNode); Element attachmentSizeNode = doc.createElement("size"); attachmentSizeNode.appendChild(createTextNode(doc, "" + attachment.getSize())); attachmentNode.appendChild(attachmentSizeNode); Element attachmentTypeNode = doc.createElement("type"); attachmentTypeNode.appendChild(createTextNode(doc, attachment.getType())); attachmentNode.appendChild(attachmentTypeNode); } // and now store the comments it = blogEntry.getComments().iterator(); while (it.hasNext()) { Comment comment = (Comment) it.next(); storeComment(comment, doc, root); } // and finally the trackbacks it = blogEntry.getTrackBacks().iterator(); while (it.hasNext()) { TrackBack trackBack = (TrackBack) it.next(); storeTrackBack(trackBack, doc, root); } // write the XMl to a String, and then write this string to a file // (if the XML format fails, we don't corrupt the file) StringWriter sw = new StringWriter(); Source source = new DOMSource(doc); Result result = new StreamResult(sw); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.METHOD, "xml"); xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); xformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); xformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "body"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result); // now take a backup of the correct file if (destination.exists() && destination.length() > 0) { log.debug("Backing up to " + backupFile.getAbsolutePath()); destination.renameTo(backupFile); } log.debug("Saving to " + destination.getAbsolutePath()); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destination), "UTF-8")); bw.write(sw.getBuffer().toString()); bw.flush(); bw.close(); } catch (Exception e) { log.error(e.getMessage(), e); e.printStackTrace(); throw new PersistenceException(e.getMessage()); } } /** * Helper method to store an individual comment. * * @param comment the Comment being stored * @param doc the Document into which the comment is to be inserted * @param root the root Node for the comment * @throws java.lang.Exception if something goes wrong */ private void storeComment(Comment comment, Document doc, Node root) throws Exception { Element commentNode = doc.createElement("comment"); root.appendChild(commentNode); Element titleNode = doc.createElement("title"); Element bodyNode = doc.createElement("body"); Element authorNode = doc.createElement("author"); Element emailNode = doc.createElement("email"); Element websiteNode = doc.createElement("website"); Element ipAddressNode = doc.createElement("ipAddress"); Element dateNode = doc.createElement("date"); Element parentNode = doc.createElement("parent"); Element stateNode = doc.createElement("state"); Element authenticatedNode = doc.createElement("authenticated"); commentNode.appendChild(titleNode); commentNode.appendChild(bodyNode); commentNode.appendChild(authorNode); commentNode.appendChild(emailNode); commentNode.appendChild(websiteNode); commentNode.appendChild(ipAddressNode); commentNode.appendChild(dateNode); commentNode.appendChild(stateNode); commentNode.appendChild(authenticatedNode); titleNode.appendChild(createTextNode(doc, comment.getTitle())); bodyNode.appendChild(createCDATASection(doc, comment.getBody())); authorNode.appendChild(createTextNode(doc, comment.getAuthor())); emailNode.appendChild(createTextNode(doc, comment.getEmail())); websiteNode.appendChild(createTextNode(doc, comment.getWebsite())); ipAddressNode.appendChild(createTextNode(doc, comment.getIpAddress())); SimpleDateFormat sdf = new SimpleDateFormat(NEW_PERSISTENT_DATETIME_FORMAT, Locale.ENGLISH); sdf.setTimeZone(GMT); dateNode.appendChild(createTextNode(doc, sdf.format(comment.getDate()))); stateNode.appendChild(createTextNode(doc, comment.getState().getName())); authenticatedNode.appendChild(createTextNode(doc, "" + comment.isAuthenticated())); if (comment.getParent() != null) { commentNode.appendChild(parentNode); parentNode.appendChild(createTextNode(doc, "" + comment.getParent().getId())); } } private Node createCDATASection(Document doc, String text) { if (text != null) { return doc.createCDATASection(text); } else { return doc.createCDATASection(""); } } private Node createTextNode(Document doc, String text) { if (text != null) { return doc.createTextNode(text); } else { return doc.createTextNode(""); } } /** * Helper method to store an individual trackback. * * @param trackBack the TrackBack being stored * @param doc the Document into which the trackback is to be inserted * @param root the root Node for the comment * @throws java.lang.Exception if something goes wrong */ private void storeTrackBack(TrackBack trackBack, Document doc, Node root) throws Exception { Element commentNode = doc.createElement("trackback"); root.appendChild(commentNode); Element titleNode = doc.createElement("title"); Element excerptNode = doc.createElement("excerpt"); Element urlNode = doc.createElement("url"); Element blogNameNode = doc.createElement("blogName"); Element ipAddressNode = doc.createElement("ipAddress"); Element dateNode = doc.createElement("date"); Element stateNode = doc.createElement("state"); commentNode.appendChild(titleNode); commentNode.appendChild(excerptNode); commentNode.appendChild(urlNode); commentNode.appendChild(blogNameNode); commentNode.appendChild(ipAddressNode); commentNode.appendChild(dateNode); commentNode.appendChild(stateNode); titleNode.appendChild(createTextNode(doc, trackBack.getTitle())); excerptNode.appendChild(createCDATASection(doc, trackBack.getExcerpt())); urlNode.appendChild(createTextNode(doc, trackBack.getUrl())); blogNameNode.appendChild(createTextNode(doc, trackBack.getBlogName())); ipAddressNode.appendChild(createTextNode(doc, trackBack.getIpAddress())); SimpleDateFormat sdf = new SimpleDateFormat(NEW_PERSISTENT_DATETIME_FORMAT, Locale.ENGLISH); sdf.setTimeZone(GMT); dateNode.appendChild(doc.createTextNode(sdf.format(trackBack.getDate()))); stateNode.appendChild(createTextNode(doc, trackBack.getState().getName())); } /** * Removes the specified blog entry. * * @param blogEntry the blog entry to remove * @throws PersistenceException if something goes wrong removing the entry */ public void removeBlogEntry(BlogEntry blogEntry) throws PersistenceException { File path = new File(getPath(blogEntry.getBlog(), blogEntry.getId(), GMT)); File file = new File(path, blogEntry.getId() + ".xml"); log.debug("Removing " + blogEntry.getGuid()); boolean success = file.delete(); if (!success) { throw new PersistenceException("Deletion of blog entry " + blogEntry.getGuid() + " failed"); } } /** * Given a blog and blog entry ID, this method determines the path where * that blog entry is stored. * * @param blog the owning Blog * @param blogEntryId the ID of the blog entry * @return a String of the form blogroot/yyyy/MM/dd */ public String getPath(Blog blog, String blogEntryId, TimeZone timeZone) { DateFormat year = new SimpleDateFormat("yyyy"); year.setTimeZone(timeZone); DateFormat month = new SimpleDateFormat("MM"); month.setTimeZone(timeZone); DateFormat day = new SimpleDateFormat("dd"); day.setTimeZone(timeZone); long dateInMillis = Long.parseLong(blogEntryId); Date date = new Date(dateInMillis); StringBuffer buf = new StringBuffer(); buf.append(blog.getRoot()); buf.append(File.separator); buf.append(year.format(date)); buf.append(File.separator); buf.append(month.format(date)); buf.append(File.separator); buf.append(day.format(date)); return buf.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -