📄 fileblogentrydao.java
字号:
package net.sourceforge.pebble.dao.file;import net.sourceforge.pebble.dao.BlogEntryDAO;import net.sourceforge.pebble.dao.PersistenceException;import net.sourceforge.pebble.domain.*;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.xml.sax.helpers.DefaultHandler;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import java.io.*;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.*;public class FileBlogEntryDAO implements BlogEntryDAO { /** timezone to use for calculating paths on disk, etc */ private static final TimeZone GMT = TimeZone.getTimeZone("GMT"); /** * the log used by this class */ private static Log log = LogFactory.getLog(FileBlogEntryDAO.class); public FileBlogEntryDAO() { } /** the date/time format used when persisting dates */ static final String OLD_PERSISTENT_DATETIME_FORMAT = "dd MMM yyyy HH:mm:ss z"; static final String NEW_PERSISTENT_DATETIME_FORMAT = "dd MMM yyyy HH:mm:ss:S Z"; static final String REGEX_FOR_YEAR = "\\d\\d\\d\\d"; /** * Loads a specific blog entry. * * @param blogEntryId the blog entry ID * @return a BlogEntry instance * @throws net.sourceforge.pebble.dao.PersistenceException * if the specified blog entry cannot be loaded */ public BlogEntry loadBlogEntry(Blog blog, String blogEntryId) throws PersistenceException { File path = new File(getPath(blog, blogEntryId, GMT)); File file = new File(path, blogEntryId + ".xml"); return loadBlogEntry(blog, file); } /** * Loads a blog entry from the specified file. * * @param source the File pointing to the source * @throws net.sourceforge.pebble.dao.PersistenceException * if the blog entry can't be loaded */ private BlogEntry loadBlogEntry(Blog blog, File source) throws PersistenceException { if (source.exists()) { log.debug("Loading " + source.getAbsolutePath()); BlogEntry blogEntry = new BlogEntry(blog); try { DefaultHandler handler = new BlogEntryHandler(blogEntry); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); saxFactory.setValidating(false); saxFactory.setNamespaceAware(true); SAXParser parser = saxFactory.newSAXParser(); parser.parse(source, handler); } catch (Exception e) { log.error(e.getMessage() + " while loading blog enty from " + source.getAbsolutePath(), e); e.printStackTrace(); throw new PersistenceException(e.getMessage()); } return blogEntry; } else { return null; } } /** * Loads all blog entries. * * @param blog the Blog to load all entries for * @return a List of BlogEntry objects * @throws net.sourceforge.pebble.dao.PersistenceException * if the blog entries cannot be loaded */ public List<BlogEntry> loadBlogEntries(Blog blog) throws PersistenceException { List<BlogEntry> list = new ArrayList<BlogEntry>(); File root = new File(blog.getRoot()); File years[] = root.listFiles(new FourDigitFilenameFilter()); for (File year : years) { File months[] = year.listFiles(new TwoDigitFilenameFilter()); for (File month : months) { File days[] = month.listFiles(new TwoDigitFilenameFilter()); for (File day : days) { File blogEntryFiles[] = day.listFiles(new BlogEntryFilenameFilter()); for (File blogEntryFile : blogEntryFiles) { list.add(loadBlogEntry(blog, blogEntryFile)); } } } } return list; } /** * Stores the specified blog entry. * * @param blogEntry the blog entry to store * @throws PersistenceException if something goes wrong storing the entry */ public void storeBlogEntry(BlogEntry blogEntry) throws PersistenceException { File outputDir = new File(getPath(blogEntry.getBlog(), blogEntry.getId(), GMT)); if (!outputDir.exists()) { outputDir.mkdirs(); } File outputFile = new File(outputDir, blogEntry.getId() + ".xml"); storeBlogEntry(blogEntry, outputFile); } /** * Stores a blog entry to the specified file. * * @param blogEntry the BlogEntry that is being stored * @param destination the File pointing to the destination * @throws 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 timeZoneNode = doc.createElement("timeZone"); 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(timeZoneNode); 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())); } SimpleDateFormat sdf = new SimpleDateFormat(NEW_PERSISTENT_DATETIME_FORMAT, Locale.ENGLISH); sdf.setTimeZone(GMT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -