/*
* Created on May 16, 2004
*
* This file is part of Thingamablog. ( http://thingamablog.sf.net )
*
* Copyright (c) 2004, Bob Tantlinger All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
package net.sf.thingamablog.xml;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.Vector;
//import java.util.logging.Logger;
import net.sf.thingamablog.PasswordUtil;
import net.sf.thingamablog.blog.ArchiveRange;
import net.sf.thingamablog.blog.PingService;
import net.sf.thingamablog.blog.TBWeblog;
import net.sf.thingamablog.blog.Weblog;
import net.sf.thingamablog.blog.WeblogBackend;
import net.sf.thingamablog.blog.WeblogList;
import net.sf.thingamablog.blog.WeblogsDotComPing;
import net.sf.thingamablog.feed.Feed;
import net.sf.thingamablog.feed.FeedBackend;
import net.sf.thingamablog.feed.FeedFolder;
import net.sf.thingamablog.generator.CustomTag;
import net.sf.thingamablog.generator.PageGenerator;
import net.sf.thingamablog.transport.EMailTransport;
import net.sf.thingamablog.transport.FTPTransport;
import net.sf.thingamablog.transport.LocalTransport;
import net.sf.thingamablog.transport.PublishTransport;
import net.sf.thingamablog.transport.RemotePublishTransport;
import net.sf.thingamablog.transport.RemoteTransport;
import net.sf.thingamablog.transport.SFTPTransport;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
/**
*
* Factory for persisting a Thingamablog XML "database"
*
* @author Bob Tantlinger
*
*/
public class TBPersistFactory
{
//private static final Logger logger = Logger.getLogger("net.sf.thingamablog.xml");
/**
* Saves the current data to an XML file
*
* @param blogList The list of weblogs
* @param feedRootFolder The root FeedFolder
* @param path The path to save the file to
* @throws IOException If an IO error occurs
* @throws JDOMException If a JDOM error occurs
*/
public synchronized static void save(WeblogList blogList, FeedFolder feedRootFolder, String path)
throws IOException, JDOMException
{
String mainRoot = "Thingamablog";
//Document document = XMLUtils.initDocument(mainRoot, mainRoot, new File(path));
Document document = new Document(new Element(mainRoot));
Comment comment =
new Comment("Generated by Thingamablog. DO NOT EDIT THIS FILE!!!");
document.getContent().add(0, comment);
Element weblogsRoot = new Element("Weblogs");
Element feedsRoot = new Element("Feeds");
saveWeblogsToXML(blogList, weblogsRoot);
saveFeedFolder(feedRootFolder, feedsRoot);
Element root = document.getRootElement();
root.addContent(weblogsRoot);
root.addContent(feedsRoot);
XMLUtils.writeXML(document, path, true);
}
public static void loadData(String path, WeblogList list, FeedFolder rootFolder, WeblogBackend b1, FeedBackend b2)
throws IOException, JDOMException
{
File f = new File(path);
Document doc = createDocument(f);
loadWeblogList(doc, f, list, b1);
loadFeedTree(doc, rootFolder, b2);
}
private static Document createDocument(File file)
throws IOException, JDOMException
{
SAXBuilder builder = new SAXBuilder(false);
builder.setEntityResolver(new TBEntityResolver());
Document document = builder.build(file);
return document;
}
private static void saveFeedFolder(FeedFolder folder, Element element)
{
FeedFolder sub[] = folder.getFolders();
for(int i = 0; i < sub.length; i++)
{
Element category = new Element("Folder");
category.setAttribute("name", sub[i].getName());
element.addContent(category);
saveFeedFolder(sub[i], category);
}
Feed feeds[] = folder.getFeeds();
for(int j = 0; j < feeds.length; j++)
{
Element link = new Element("Feed");
link.setText(feeds[j].getURL());
String title = feeds[j].getTitle();
if(title != null)
link.setAttribute("title", title);
else
link.setAttribute("title", "Untitled");
link.setAttribute("archive", feeds[j].isLimitItems() + "");
link.setAttribute("limit", feeds[j].getItemLimit() + "");
Date updateDate = feeds[j].getLastUpdated();
if(updateDate != null)
link.setAttribute("update_date", updateDate.getTime() + "");
//link.setAttribute("update_failed", feeds[j].isLastUpdateFailed() + "");
if(feeds[j].isLastUpdateFailed())
link.setAttribute("update_failed", feeds[j].getLastUpdateFailedReason());
element.addContent(link);
}
}
/**
* Loads the FeedFolder heirarchy from an XML file
*
* @param rootFolder The root FeedFolder
* @param path The path to the XML file
* @param backend The backend for the feeds
* @throws IOException If an IO error occurs
* @throws JDOMException If a JDOM error occurs
*/
public static void loadFeedTree(FeedFolder rootFolder, String path, FeedBackend backend)
throws IOException, JDOMException
{
Document document = createDocument(new File(path));
if(document == null)
return;
loadFeedTree(document, rootFolder, backend);
}
private static void loadFeedTree(Document document, FeedFolder rootFolder, FeedBackend backend)
{
Element root = document.getRootElement();
Element feedRoot = root.getChild("Feeds");
if(feedRoot != null)
{
loadFeeds(rootFolder, feedRoot, backend);
}
}
private static void loadFeeds(FeedFolder folder, Element element, FeedBackend backend)
{
List contents = element.getChildren();
Iterator contentsIt = contents.iterator();
while(contentsIt.hasNext())
{
Element ele = (Element) contentsIt.next();
/** The element is a category */
if(ele.getName().equals("Folder"))
{
FeedFolder subFolder = loadFeedFolder(ele);
folder.addFolder(subFolder);
loadFeeds(subFolder, ele, backend);
}
/** The element is a favorite */
else if (ele.getName().equals("Feed"))
{
folder.addFeed(loadFeed(ele, backend));
}
}
}
private static FeedFolder loadFeedFolder(Element element)
{
/** Name of the category */
String folderName = element.getAttributeValue("name");
/** Create a new category */
FeedFolder sub = new FeedFolder(folderName);
return sub;
}
private static Feed loadFeed(Element element, FeedBackend backend)
{
String url = "";
String title = "";
Date updated = null;
boolean /*lastUpdateFailed = false,*/ archive = true;
String failReason = null;
int limit = 50;
// Url of the feed
if (element.getText() != null)
url = element.getText();
// Title of the feed
if (element.getAttributeValue("title") != null)
title = element.getAttributeValue("title");
if(element.getAttributeValue("update_date") != null)
{
try
{
long epoch = Long.parseLong(element.getAttributeValue("update_date"));
updated = new Date(epoch);
}
catch(Exception ex){}
}
try
{
limit = Integer.parseInt(element.getAttributeValue("limit", "50"));
}catch(Exception ex){}
archive = element.getAttributeValue("archive", "true").equals("true");
if(element.getAttributeValue("update_failed") != null)
{
failReason = element.getAttributeValue("update_failed");
}
Feed f = new Feed(url);
f.setTitle(title);
f.setBackend(backend);
f.setLastUpdated(updated);
f.setLastUpdateFailed(failReason != null);
f.setLimitItems(archive);
f.setItemLimit(limit);
if(failReason != null)
f.setLastUpdateFailedReason(failReason);
return f;
}
private static void saveWeblogsToXML(WeblogList blogList, Element root)
throws IOException, JDOMException
{
for(int i = 0; i < blogList.getWeblogCount(); i++)
{
Weblog blog = blogList.getWeblogAt(i);
Element bElement = createWeblogElement(blog);
root.addContent(bElement);
}
}
private static Element createWeblogElement(Weblog blog)
{
Element element = new Element("Weblog");
element.setAttribute("title", blog.getTitle());
element.setAttribute("key", blog.getKey());
element.setAttribute("last_publish", blog.getLastPublishDate().getTime() + "");
element.setAttribute("publish_failed", blog.isPublishFailed() + "");
Element descr = new Element("Description");
descr.addContent(blog.getDescription());
element.addContent(descr);
element.addContent(createPingServicesElement(blog));
element.addContent(createPublishTransportElement(blog));
element.addContent(createMailSettingsElement(blog));
if(blog instanceof TBWeblog)
{
TBWeblog tbBlog = (TBWeblog)blog;
return createTBWeblogElement(tbBlog, element);
}
return element;
}
private static Element createPingServicesElement(Weblog blog)
{
Element element = new Element("PingServices");
PingService ps[] = blog.getPingServices();
for(int i = 0; i < ps.length; i++)
{
if(ps[i] instanceof WeblogsDotComPing)
{
Element ping = new Element("WeblogsDotComPing");
ping.setAttribute("name", ps[i].getServiceName());
ping.setAttribute("url", ps[i].getServiceUrl());
ping.setAttribute("enabled", ps[i].isEnabled() + "");
element.addContent(ping);
}
}
return element;
}
private static Element createTBWeblogElement(TBWeblog blog, Element element)
{
element.setName("TBWeblog");
element.setAttribute("url", blog.getBaseUrl());
element.setAttribute("arc_url", blog.getArchiveUrl());
element.setAttribute("media_url", blog.getMediaUrl());
element.setAttribute("base_path", blog.getBasePath());
element.setAttribute("base_date", blog.getArchiveBaseDate().getTime() + "");
element.setAttribute("arc_policy", blog.getArchivePolicy() + "");
element.setAttribute("day_interval", blog.getArchiveByDayInterval() + "");
element.setAttribute("arc_ext", blog.getArchivesExtension());
element.setAttribute("cat_ext", blog.getCategoriesExtension());
element.setAttribute("gen_cat_feed", blog.isGenerateCategoryFeeds() + "");
element.setAttribute("cat_feed_ext", blog.getCategoriesFeedExtension());
element.setAttribute("entry_ext", blog.getEntryPageExtension());
element.setAttribute("gen_rss", blog.isGenerateRssFeed() + "");
element.setAttribute("gen_arc_index", blog.isGenerateArchiveIndex() + "");
element.setAttribute("gen_entries", blog.isGenerateEntryPages() + "");
element.setAttribute("fp_filename", blog.getFrontPageFileName());
element.setAttribute("rss_filename", blog.getRssFileName());
element.setAttribute("arc_index_filename", blog.getArchiveIndexFileName());
element.setAttribute("locale", blog.getLocale().toString());
element.setAttribute("publish_all", blog.isPublishAll() + "");
Element outdatedArcs = new Element("OutdatedArchives");
ArchiveRange ar[] = blog.getOutdatedArchives();
for(int i = 0; i < ar.length; i++)
{
Element arcRange = new Element("Archive");
arcRange.setAttribute("start", ar[i].getStartDate().getTime() + "");
arcRange.setAttribute("end", ar[i].getExpirationDate().getTime() + "");
outdatedArcs.addContent(arcRange);
}
element.addContent(outdatedArcs);
Element outdatedCats = new Element("OutdatedCategories");
String cats[] = blog.getOutdatedCategories();
for(int i = 0; i < cats.length; i++)
{
Element category = new Element("Category");
category.setText(cats[i]);
outdatedCats.addContent(category);
}
element.addContent(outdatedCats);
Element outdatedEntries = new Element("OutdatedEntries");
long ids[] = blog.getOutdatedEntryIDs();
for(int i = 0; i < ids.length; i++)
{
Element ent = new Element("Entry");
ent.setText(ids[i] + "");
outdatedEntries.addContent(ent);
}
element.addContent(outdatedEntries);
Element genElement = new Element("Generator");
PageGenerator gen = blog.getPageGenerator();
genElement.setAttribute("charset", gen.getCharset());
genElement.setAttribute("range_format", gen.getArchiveRangeFormat());
genElement.setAttribute("span_range", gen.isSpanArcRange() + "");
genElement.setAttribute("date_format", gen.getDateFormat());
genElement.setAttribute("time_format", gen.getTimeFormat());
genElement.setAttribute("front_asc", gen.isFrontPageAscending() + "");
genElement.setAttribute("cat_asc", gen.isCategoryPageAscending() + "");
genElement.setAttribute("arc_asc", gen.isArchivePageAscending() + "");
genElement.setAttribute("limit_front", gen.isLimitFrontPage() + "");
genElement.setAttribute("front_page_limit", gen.getFrontPageLimit() + "");
genElement.setAttribute("limit_cat", gen.isLimitCategoryPage() + "");
genElement.setAttribute("cat_page_limit", gen.getCategoryPageLimit() + "");
genElement.setAttribute("limit_rss_body", gen.isLimitRssEntry() + "");
Element customTagsElement = new Element("CustomTags");
CustomTag tags[] = gen.getCustomTags();
for(int i = 0; i < tags.length; i++)
{
Element tag = new Element("Tag");
tag.setAttribute("name", tags[i].getName());
tag.setText(tags[i].getValue());
customTagsElement.addContent(tag);
}
genElement.addContent(customTagsElement);
element.addContent(genElement);
return element;
}
private static Element createMailSettingsElement(Weblog blog)
{
Element mailSettings = new Element("MailSettings");
mailSettings.setAttribute("enabled", blog.isImportFromEmailEnabled() + "");
if(blog.getLastEmailCheck() != null)
mailSettings.setAttribute("last_check", blog.getLastEmailCheck().getTime() + "");
mailSettings.setAttribute("check_minutes", blog.getOutdatedAfterMinutes() + "");
mailSettings.setAttribute("check_failed", blog.isMailCheckFailed() + "");
Element transport = new Element("MailTransport");
EMailTransport rpt = blog.getMailTransport();
transport.setAttribute("protocol", rpt.getProtocol());
transport.setAttribute("post_directive", rpt.getPostDirective());
transport.setAttribute("server", rpt.getAddress());
transport.setAttribute("port", rpt.getPort() + "");
transport.setAttribute("user", rpt.getUserName());
if(rpt.isSavePassword())
transport.setAttribute("password",
PasswordUtil.encrypt(rpt.getPassword(), PasswordUtil.KEY));
mailSettings.addContent(transport);
return mailSettings;
}
private static Element createPublishTransportElement(Weblog blog)
{
Element transport = new Element("Transport");
PublishTransport pt = blog.getPublishTransport();
if(pt instanceof RemoteTransport)
{
RemoteTransport rpt = (RemoteTransport)pt;
transport.setAttribute("type", "sftp");
transport.setAttribute("server", rpt.getAddress());
transport.setAttribute("port", rpt.getPort() + "");
transport.setAttribute("user", rpt.getUserName());
if(rpt.isSavePassword())
transport.setAttribute("password",
PasswordUtil.encrypt(rpt.getPassword(), PasswordUtil.KEY));
if(rpt instanceof FTPTransport)
{
FTPTransport ftp = (FTPTransport)rpt;
transport.setAttribute("type", "ftp");
transport.setAttribute("passive", ftp.isPassiveMode()+"");
Element e = new Element("ASCIITypes");
List exts = ftp.getASCIIExtensions();
for(Iterator it = exts.iterator(); it.hasNext();)
{
String ex = it.next().toString();
Element c = new Element("ext");
c.setText(ex);
e.addContent(c);
}
transport.addContent(e);
}
}
else
{
transport.setAttribute("type", "local");
}
return transport;
}
/**
* Creates a WeblogList from an XML file
*
* @param path The path to the XML file
* @param backend The backend to connect the weblogs to
* @param authStore The author store for the weblogs
* @param catStore The category store for the weblogs
* @return A list of weblogs
* @throws IOException If an IO error occurs
* @throws JDOMException If a JDOM error occurs
*/
public static WeblogList loadWeblogsFromXML(String path, WeblogBackend backend)
throws IOException, JDOMException
{
WeblogList list = new WeblogList();
File f = new File(path);
Document document = createDocument(f);
loadWeblogList(document, f, list, backend);
return list;
}
private static void loadWeblogList(Document document, File path, WeblogList list, WeblogBackend backend)
{
if(document == null)//couldn't load xml file
return;//return empty WeblogList
Element root = document.getRootElement();
List weblogs = root.getChild("Weblogs").getChildren();
Iterator weblogsIt = weblogs.iterator();
//children should all be weblogs
while(weblogsIt.hasNext())
{
Element blogElement = (Element)weblogsIt.next();
Weblog w = createWeblogFromElement(blogElement,path);
if(w != null)
{
w.setBackend(backend);
list.addWeblog(w);
}
}
list.sortList();
}
private static Weblog createWeblogFromElement(Element blogEle, File path)
{
Weblog weblog = null;
//different weblog types might be added later
//for now we're only dealing with TBWeblogs
if(blogEle.getName().equals("TBWeblog"))
weblog = createTBWeblogFromElement(blogEle, path);
if(weblog == null)
return null;
weblog.setPublishTransport(loadPublishTransportFromXML(blogEle));
loadPingServicesFromXML(blogEle, weblog);
loadEmailSettingsFromXML(blogEle, weblog);
weblog.setTitle(blogEle.getAttributeValue("title", "Untitled"));
String description = "";
Element desc = blogEle.getChild("Description");
if(desc != null)
description = desc.getText();
weblog.setDescription(description);
try
{
long time = Long.parseLong(
blogEle.getAttributeValue("last_publish",
System.currentTimeMillis() + ""));
weblog.setLastPublishDate(new Date(time));
}
catch(Exception ex){}
weblog.setPublishFailed(Boolean.parseBoolean(blogEle.getAttributeValue("publish_failed", "false")));
return weblog; //element wasn't a known type
}
private static TBWeblog createTBWeblogFromElement(Element element, File path)
{
String key = element.getAttributeValue("key");
if(key == null)
return null; //can't continue if this weblog doesnt have a unique key
TBWeblog tb = new TBWeblog(path.getParentFile(), key);
String genericUrl = "http://www.mysite.com";
String basePath = element.getAttributeValue("base_path", "/");
String url = element.getAttributeValue("url", genericUrl);
String arcUrl = element.getAttributeValue("arc_url", genericUrl);
String mediaUrl = element.getAttributeValue("media_url", genericUrl);
tb.setBlogUrls(basePath, url, arcUrl, mediaUrl);
tb.setCategoriesExtension(element.getAttributeValue("cat_ext", ".html"));
tb.setArchivesExtension(element.getAttributeValue("arc_ext", ".html"));
tb.setEntryPageExtension(element.getAttributeValue("entry_ext", ".html"));
tb.setFrontPageFileName(element.getAttributeValue("fp_filename", "blog.html"));
tb.setRssFileName(element.getAttributeValue("rss_filename", "rss.xml"));
tb.setArchiveIndexFileName(element.getAttributeValue("arc_index_filename", "list.html"));
tb.setGenerateCategoryFeeds(element.getAttributeValue("gen_cat_feed", "false").equals("true"));
tb.setCategoriesFeedExtension(element.getAttributeValue("cat_feed_ext", ".rss"));
tb.setGenerateRssFeed(element.getAttributeValue("gen_rss", "true").equals("true"));
tb.setGenerateArchiveIndex(element.getAttributeValue("gen_arc_index", "true").equals("true"));
tb.setGenerateEntryPages(element.getAttributeValue("gen_entries", "false").equals("true"));
tb.setLocale(createLocale(element.getAttributeValue("locale", Locale.getDefault().toString())));
tb.setPublishAll(element.getAttributeValue("publish_all", "true").equals("true"));
int arcPolicy = TBWeblog.ARCHIVE_MONTHLY;
int dayInterval = 5;
Date arcBaseDate = new Date(0);
try
{
arcPolicy = Integer.parseInt(
element.getAttributeValue("arc_policy", Integer.toString(TBWeblog.ARCHIVE_MONTHLY)));
dayInterval = Integer.parseInt(element.getAttributeValue("day_interval", "5"));
long time = Long.parseLong(element.getAttributeValue("base_date"));
arcBaseDate = new Date(time);
}
catch(Exception ex){}
tb.setArchivePolicy(arcPolicy);
tb.setArchiveByDayInterval(dayInterval);
tb.setArchiveBaseDate(arcBaseDate);
//load outdated archives from xml
Element outdatedArcs = element.getChild("OutdatedArchives");
if(outdatedArcs != null)
{
Iterator itr = (outdatedArcs.getChildren()).iterator();
while(itr.hasNext())
{
Date start = new Date();
Date exp = new Date();
Element e = (Element)itr.next();
if(e.getName().equals("Archive"))
{
try
{
long d1 = Long.parseLong(e.getAttributeValue("start"));
long d2 = Long.parseLong(e.getAttributeValue("end"));
start.setTime(d1);
exp.setTime(d2);
tb.addOutdatedArchive(new ArchiveRange(start, exp));
}
catch(Exception ex){}
}
}
}
//load outdated cats from xml
Element outdatedCats = element.getChild("OutdatedCategories");
if(outdatedCats != null)
{
Iterator itr = (outdatedCats.getChildren()).iterator();
while(itr.hasNext())
{
Element e = (Element)itr.next();
if(e.getName().equals("Category"))
{
tb.addOutdatedCategory(e.getText());
}
}
}
//load outdated entries from xml
Element outdatedEnts = element.getChild("OutdatedEntries");
if(outdatedEnts != null)
{
Iterator itr = (outdatedEnts.getChildren()).iterator();
while(itr.hasNext())
{
Element e = (Element)itr.next();
if(e.getName().equals("Entry"))
{
try{
long id = Long.parseLong(e.getText());
tb.addOutdatedEntryID(new Long(id));
}catch(Exception ex){}
}
}
}
loadGeneratorSettingsFromXML(tb, element);
return tb;
}
private static void loadGeneratorSettingsFromXML(TBWeblog tbw, Element element)
{
//set up the page generator defaults
PageGenerator gen = tbw.getPageGenerator();
Element genElement = element.getChild("Generator");
if(genElement == null)
return;
gen.setCharset(genElement.getAttributeValue("charset", "UTF-8"));
gen.setDateFormat(genElement.getAttributeValue("date_format"));
gen.setTimeFormat(genElement.getAttributeValue("time_format"));
String s = genElement.getAttributeValue("span_range");
boolean span = s != null && s.equals("true");
gen.setArchiveRangeFormat(genElement.getAttributeValue("range_format"), span);
s = genElement.getAttributeValue("front_asc");
gen.setFrontPageAscending(s != null && s.equals("true"));
s = genElement.getAttributeValue("arc_asc");
gen.setArchivePageAscending(s != null && s.equals("true"));
s = genElement.getAttributeValue("cat_asc");
gen.setCategoryPageAscending(s != null && s.equals("true"));
int fpLimit = 10;
int catLimit = 20;
try
{
fpLimit = Integer.parseInt(genElement.getAttributeValue("front_page_limit"));
catLimit = Integer.parseInt(genElement.getAttributeValue("cat_page_limit"));
}
catch(Exception ex){}
gen.setLimitFrontPage(genElement.getAttributeValue("limit_front", "true").equals("true"));
gen.setFrontPageLimit(fpLimit);
gen.setLimitCategoryPage(genElement.getAttributeValue("limit_cat", "true").equals("true"));
gen.setCategoryPageLimit(catLimit);
s = genElement.getAttributeValue("limit_rss_body");
gen.setLimitRssEntry(s != null && s.equals("true"));
Element custTags = genElement.getChild("CustomTags");
if(custTags == null)return;
Iterator itr = custTags.getChildren().iterator();
while(itr.hasNext())
{
Element e = (Element)itr.next();
if(e.getName().equals("Tag"))
{
String name = e.getAttributeValue("name");
if(name == null)
continue;
String value = e.getText();
CustomTag tag = new CustomTag(name, value);
gen.addCustomTag(tag);
}
}
}
private static void loadPingServicesFromXML(Element parent, Weblog blog)
{
Element services = parent.getChild("PingServices");
if(services == null)
services = new Element("PingServices");
Iterator itr = (services.getChildren()).iterator();
while(itr.hasNext())
{
Element service = (Element)itr.next();
if(service.getName().equals("WeblogsDotComPing"))
{
WeblogsDotComPing ping = new WeblogsDotComPing();
ping.setServiceUrl(service.getAttributeValue("url"));
ping.setServiceName(service.getAttributeValue("name"));
ping.setEnabled(service.getAttributeValue("enabled", "true").equals("true"));
blog.addPingService(ping);
}
}
}
private static void loadEmailSettingsFromXML(Element parent, Weblog blog)
{
Element mailSettings = parent.getChild("MailSettings");
if(mailSettings == null)
return;
blog.setImportFromEmailEnabled(Boolean.parseBoolean(mailSettings.getAttributeValue("enabled", "true")));
blog.setLastEmailCheck(parseDate(mailSettings.getAttributeValue("last_check", "0")));
blog.setOutdatedAfterMinutes(parseInt(mailSettings.getAttributeValue("check_minutes", "30"), 30));
blog.setMailCheckFailed(Boolean.parseBoolean(mailSettings.getAttributeValue("check_failed", "false")));
Element transport = mailSettings.getChild("MailTransport");
if(transport == null)
return;
blog.getMailTransport().setProtocol(transport.getAttributeValue("protocol", "POP3"));
blog.getMailTransport().setPostDirective(transport.getAttributeValue("post_directive", "POST"));
configureRemoteTransport(blog.getMailTransport(), transport, 110);
}
private static int parseInt(String intStr, int defaultVal)
{
try
{
defaultVal = Integer.parseInt(intStr);
}
catch(Exception ex){}
return defaultVal;
}
private static Date parseDate(String epocString)
{
long d = 0;
try
{
d = Long.parseLong(epocString);
}
catch(Exception ex){}
return new Date(d);
}
private static Locale createLocale(String str)
{
Vector v = new Vector();
if(str != null)
{
StringTokenizer st = new StringTokenizer(str, "_");
while (st.hasMoreTokens())
{
v.add(st.nextToken());
}
}
Locale loc = Locale.getDefault();
if(v.size() == 3)
loc = new Locale(v.elementAt(0).toString(),
v.elementAt(1).toString(), v.elementAt(2).toString());
else if(v.size() == 2)
loc = new Locale(v.elementAt(0).toString(),
v.elementAt(1).toString());
else if(v.size() == 1)
loc = new Locale(v.elementAt(0).toString());
return loc;
}
private static PublishTransport loadPublishTransportFromXML(Element parent)
{
Element transport = parent.getChild("Transport");
if(transport == null)
transport = new Element("Transport");
PublishTransport pubTransport = null;
String type = transport.getAttributeValue("type", "local");
if(type.equals("ftp") || type.equals("sftp"))
{
RemotePublishTransport rtp = null;
int port = 22;
if(type.equals("ftp"))
{
FTPTransport ftp = new FTPTransport();
ftp.setPassiveMode(transport.getAttributeValue("passive", "true").equals("true"));
Element asciiElem = transport.getChild("ASCIITypes");
ArrayList lst = new ArrayList();
if(asciiElem == null)
{
lst.add("cgi");
lst.add("pl");
lst.add("php");
lst.add("asp");
}
else
{
List ch = asciiElem.getChildren();
for(int i = 0; i < ch.size(); i++)
{
Element e = (Element)ch.get(i);
if(e.getName() == "ext");
lst.add(e.getText());
}
}
ftp.setASCIIExtentions(lst);
rtp = ftp;
port = 21;
}
else
rtp = new SFTPTransport();
configureRemoteTransport(rtp, transport, port);
pubTransport = rtp;
}
else
pubTransport = new LocalTransport();
return pubTransport;
}
private static void configureRemoteTransport(RemoteTransport rtp, Element transport, int defaultPort)
{
try
{
defaultPort = Integer.parseInt(transport.getAttributeValue("port"));
}catch(Exception ex){}
rtp.setPort(defaultPort);
rtp.setAddress(transport.getAttributeValue("server"));
rtp.setUserName(transport.getAttributeValue("user"));
String password = transport.getAttributeValue("password");
rtp.setSavePassword(password != null);
if(rtp.isSavePassword())
{
try
{
password = PasswordUtil.decrypt(password, PasswordUtil.KEY);
}
catch(Exception ex)
{
password = "";
}
rtp.setPassword(password);
}
}
}