| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.util.importer; |
| 33 | |
|
| 34 | |
import net.sourceforge.pebble.dao.DAOFactory; |
| 35 | |
import net.sourceforge.pebble.dao.file.FileDAOFactory; |
| 36 | |
import net.sourceforge.pebble.domain.Blog; |
| 37 | |
import net.sourceforge.pebble.domain.BlogEntry; |
| 38 | |
import net.sourceforge.pebble.domain.BlogService; |
| 39 | |
import org.w3c.dom.Document; |
| 40 | |
import org.w3c.dom.Node; |
| 41 | |
import org.w3c.dom.NodeList; |
| 42 | |
import org.xml.sax.ErrorHandler; |
| 43 | |
import org.xml.sax.SAXException; |
| 44 | |
import org.xml.sax.SAXParseException; |
| 45 | |
|
| 46 | |
import javax.xml.parsers.DocumentBuilder; |
| 47 | |
import javax.xml.parsers.DocumentBuilderFactory; |
| 48 | |
import java.io.File; |
| 49 | |
import java.text.SimpleDateFormat; |
| 50 | |
import java.util.Date; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 0 | public class RadioUserlandImporter { |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public static void main(String[] args) throws Exception { |
| 63 | 0 | File root = new File(args[0]); |
| 64 | 0 | File sources[] = root.listFiles(); |
| 65 | 0 | DAOFactory.setConfiguredFactory(new FileDAOFactory()); |
| 66 | 0 | Blog blog = new Blog(args[1]); |
| 67 | 0 | blog.setProperty(Blog.TIMEZONE_KEY, args[2]); |
| 68 | |
|
| 69 | 0 | for (int i = 0; i < sources.length; i++) { |
| 70 | 0 | importFile(blog, sources[i]); |
| 71 | |
} |
| 72 | 0 | } |
| 73 | |
|
| 74 | |
private static void importFile(Blog blog, File source) throws Exception { |
| 75 | 0 | System.out.println("Importing " + source.getName()); |
| 76 | |
|
| 77 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 78 | 0 | factory.setValidating(false); |
| 79 | 0 | factory.setNamespaceAware(true); |
| 80 | 0 | factory.setIgnoringElementContentWhitespace(true); |
| 81 | 0 | factory.setIgnoringComments(true); |
| 82 | 0 | DocumentBuilder builder = factory.newDocumentBuilder(); |
| 83 | 0 | builder.setErrorHandler(new ErrorHandler() { |
| 84 | |
public void warning(SAXParseException e) throws SAXException { |
| 85 | 0 | System.out.println("Warning : " + e.getMessage()); |
| 86 | 0 | throw e; |
| 87 | |
} |
| 88 | |
|
| 89 | |
public void error(SAXParseException e) throws SAXException { |
| 90 | 0 | System.out.println("Error : " + e.getMessage()); |
| 91 | 0 | throw e; |
| 92 | |
} |
| 93 | |
|
| 94 | |
public void fatalError(SAXParseException e) throws SAXException { |
| 95 | 0 | System.out.println("Fatal : " + e.getMessage()); |
| 96 | 0 | throw e; |
| 97 | |
} |
| 98 | |
}); |
| 99 | |
|
| 100 | 0 | SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss"); |
| 101 | 0 | String title = "No title"; |
| 102 | 0 | String body = ""; |
| 103 | 0 | Date date = null; |
| 104 | 0 | Document doc = builder.parse(source); |
| 105 | 0 | Node root = doc.getDocumentElement(); |
| 106 | 0 | NodeList nodes = root.getChildNodes(); |
| 107 | 0 | System.out.println(nodes.getLength()); |
| 108 | 0 | for (int i = 0; i < nodes.getLength(); i++) { |
| 109 | 0 | Node n = nodes.item(i); |
| 110 | 0 | System.out.println(n.getNodeName()); |
| 111 | |
|
| 112 | 0 | if (n.getNodeName().equals("string")) { |
| 113 | 0 | System.out.println(n.getAttributes().getNamedItem("name")); |
| 114 | 0 | System.out.println(n.getAttributes().getNamedItem("name").getNodeValue()); |
| 115 | 0 | if (n.getAttributes().getNamedItem("name").getNodeValue().equals("title")) { |
| 116 | 0 | title = n.getAttributes().getNamedItem("value").getNodeValue(); |
| 117 | 0 | System.out.println("Title : " + title); |
| 118 | 0 | } else if (n.getAttributes().getNamedItem("name").getNodeValue().equals("text")) { |
| 119 | 0 | body = n.getAttributes().getNamedItem("value").getNodeValue(); |
| 120 | 0 | System.out.println("Body : " + body); |
| 121 | |
} |
| 122 | |
} |
| 123 | |
|
| 124 | 0 | if (n.getNodeName().equals("date") && n.getAttributes().getNamedItem("name").getNodeValue().equals("when")) { |
| 125 | 0 | date = sdf.parse((n.getAttributes().getNamedItem("value").getNodeValue()).substring(4)); |
| 126 | 0 | System.out.println("Date : " + date); |
| 127 | |
} |
| 128 | |
} |
| 129 | |
|
| 130 | 0 | BlogEntry entry = new BlogEntry(blog); |
| 131 | 0 | entry.setTitle(title); |
| 132 | 0 | entry.setBody(body); |
| 133 | 0 | entry.setDate(date); |
| 134 | |
|
| 135 | 0 | BlogService service = new BlogService(); |
| 136 | 0 | service.putBlogEntry(entry); |
| 137 | 0 | } |
| 138 | |
|
| 139 | |
} |