⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filestaticpagedao.java

📁 pebble-blog 博客源码博客源码博客源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * @param destination the File pointing to the destination   * @throws net.sourceforge.pebble.dao.PersistenceException if something goes wrong storing the entry   */  private void storeBlogEntry(BlogEntry blogEntry, File destination) throws PersistenceException {    File backupFile = new File(destination.getParentFile(), destination.getName() + ".bak");    try {      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();      factory.setValidating(false);      factory.setNamespaceAware(true);      factory.setIgnoringElementContentWhitespace(true);      factory.setIgnoringComments(true);      DocumentBuilder builder = factory.newDocumentBuilder();      Document doc = builder.newDocument();      Element root = doc.createElement("blogEntry");      doc.appendChild(root);      Element titleNode = doc.createElement("title");      Element subtitleNode = doc.createElement("subtitle");      Element excerptNode = doc.createElement("excerpt");      Element bodyNode = doc.createElement("body");      Element categoryNode;      Element tagsNode = doc.createElement("tags");      Element dateNode = doc.createElement("date");      Element stateNode = doc.createElement("state");      Element authorNode = doc.createElement("author");      Element staticNameNode = doc.createElement("staticName");      Element commentsEnabledNode = doc.createElement("commentsEnabled");      Element trackBacksEnabledNode = doc.createElement("trackBacksEnabled");      Element attachmentNode = doc.createElement("attachment");      root.appendChild(titleNode);      root.appendChild(subtitleNode);      root.appendChild(excerptNode);      root.appendChild(bodyNode);      root.appendChild(dateNode);      root.appendChild(stateNode);      root.appendChild(authorNode);      root.appendChild(staticNameNode);      if (blogEntry.isAggregated()) {        Element permalinkNode = doc.createElement("originalPermalink");        permalinkNode.appendChild(doc.createTextNode(blogEntry.getOriginalPermalink()));        root.appendChild(permalinkNode);      }      titleNode.appendChild(doc.createTextNode(blogEntry.getTitle()));      subtitleNode.appendChild(doc.createTextNode(blogEntry.getSubtitle()));      bodyNode.appendChild(doc.createCDATASection(blogEntry.getBody()));      if (blogEntry.getExcerpt() != null) {        excerptNode.appendChild(doc.createCDATASection(blogEntry.getExcerpt()));      }      root.appendChild(commentsEnabledNode);      commentsEnabledNode.appendChild(doc.createTextNode("" + blogEntry.isCommentsEnabled()));      root.appendChild(trackBacksEnabledNode);      trackBacksEnabledNode.appendChild(doc.createTextNode("" + blogEntry.isTrackBacksEnabled()));      Iterator it = blogEntry.getCategories().iterator();      Category category;      while (it.hasNext()) {        category = (Category) it.next();        categoryNode = doc.createElement("category");        categoryNode.appendChild(doc.createTextNode(category.getId()));        root.appendChild(categoryNode);      }      if (blogEntry.getTags() != null) {        root.appendChild(tagsNode);        tagsNode.appendChild(doc.createTextNode(blogEntry.getTags()));      }      if (blogEntry.getAuthor() != null) {        authorNode.appendChild(doc.createTextNode(blogEntry.getAuthor()));      }//      if (blogEntry.getStaticName() != null) {//        staticNameNode.appendChild(doc.createTextNode(blogEntry.getStaticName()));//      }      SimpleDateFormat sdf = new SimpleDateFormat(NEW_PERSISTENT_DATETIME_FORMAT, Locale.ENGLISH);      sdf.setTimeZone(blogEntry.getBlog().getTimeZone());      dateNode.appendChild(doc.createTextNode(sdf.format(blogEntry.getDate())));      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 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");    commentNode.appendChild(titleNode);    commentNode.appendChild(bodyNode);    commentNode.appendChild(authorNode);    commentNode.appendChild(emailNode);    commentNode.appendChild(websiteNode);    commentNode.appendChild(ipAddressNode);    commentNode.appendChild(dateNode);    commentNode.appendChild(stateNode);    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(comment.getBlogEntry().getBlog().getTimeZone());    dateNode.appendChild(createTextNode(doc, sdf.format(comment.getDate())));    stateNode.appendChild(createTextNode(doc, comment.getState().getName()));    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 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(trackBack.getBlogEntry().getBlog().getTimeZone());    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 net.sourceforge.pebble.dao.PersistenceException if something goes wrong removing the entry   */  public void removeBlogEntry(BlogEntry blogEntry) throws PersistenceException {    File path = new File(getPath(blogEntry.getBlog(), blogEntry.getId()));    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   */  private String getPath(Blog blog, String blogEntryId) {    StringBuffer buf = new StringBuffer();    buf.append(blog.getRoot());    buf.append(File.separator);    buf.append("pages");    return buf.toString();  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -