Coverage Report - net.sourceforge.pebble.util.importer.RadioUserlandImporter
 
Classes in this File Line Coverage Branch Coverage Complexity
RadioUserlandImporter
0%
0/47
0%
0/14
3
RadioUserlandImporter$1
0%
0/7
N/A
3
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 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  
  * Simple utility to import posts Radio Userland into Pebble.
 54  
  *
 55  
  * @author    Simon Brown
 56  
  */
 57  0
 public class RadioUserlandImporter {
 58  
 
 59  
   /**
 60  
    * Starts the importer.
 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  
     // create a factory and builder - an abstraction for an XML parser
 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  
 }