radiouserlandimporter.java

### File Source: radiouserlandimporter.java

             /*
        P/P   *  Method: net.sourceforge.pebble.util.importer.RadioUserlandImporter__static_init
              */
         1  /*
         2   * Copyright (c) 2003-2006, 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   */
             /*
        P/P   *  Method: void net.sourceforge.pebble.util.importer.RadioUserlandImporter()
              */
        57  public class RadioUserlandImporter {
        58
        59    /**
        60     * Starts the importer.
        61     */
        62    public static void main(String[] args) throws Exception {
                 /*
        P/P       *  Method: void main(String[])
                  *
                  *  Preconditions:
                  *    args != null
                  *    args.length >= 3
                  *    init'ed(args[0])
                  *    init'ed(args[1])
                  *    init'ed(args[2])
                  *
                  *  Presumptions:
                  *    java.io.File:listFiles(...)@64 != null
                  *    sources.length@64 <= 232-1
                  *    sources[i]@64 != null
                  */
        63      File root = new File(args[0]);
        64      File sources[] = root.listFiles();
        65      DAOFactory.setConfiguredFactory(new FileDAOFactory());
        66      Blog blog = new Blog(args[1]);
        67      blog.setProperty(Blog.TIMEZONE_KEY, args[2]);
        68
        69      for (int i = 0; i < sources.length; i++) {
        70        importFile(blog, sources[i]);
        71      }
        72    }
        73
        74    private static void importFile(Blog blog, File source) throws Exception {
                 /*
        P/P       *  Method: void importFile(Blog, File)
                  *
                  *  Preconditions:
                  *    source != null
                  *
                  *  Presumptions:
                  *    java.lang.System.out != null
                  *    javax.xml.parsers.DocumentBuilder:parse(...)@104 != null
                  *    javax.xml.parsers.DocumentBuilderFactory:newDocumentBuilder(...)@82 != null
                  *    javax.xml.parsers.DocumentBuilderFactory:newInstance(...)@77 != null
                  *    org.w3c.dom.Document:getDocumentElement(...)@105 != null
                  *    ...
                  *
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@112: {0}, {1}
                  *    java.lang.String:equals(...)@115: {0}, {1}
                  *    java.lang.String:equals(...)@118: {0}, {1}
                  *    java.lang.String:equals(...)@124: {0}, {1}
                  */
        75      System.out.println("Importing " + source.getName());
        76      // create a factory and builder - an abstraction for an XML parser
        77      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        78      factory.setValidating(false);
        79      factory.setNamespaceAware(true);
        80      factory.setIgnoringElementContentWhitespace(true);
        81      factory.setIgnoringComments(true);
        82      DocumentBuilder builder = factory.newDocumentBuilder();
                 /*
        P/P       *  Method: void net.sourceforge.pebble.util.importer.RadioUserlandImporter$1()
                  */
        83      builder.setErrorHandler(new ErrorHandler() {
        84        public void warning(SAXParseException e) throws SAXException {
                     /*
        P/P           *  Method: void warning(SAXParseException)
                      *    warning fails for all possible inputs
                      *
                      *  Preconditions:
                      *    (soft) e != null
                      */
        85          System.out.println("Warning : " + e.getMessage());
        86          throw e;
        87        }
        88
        89        public void error(SAXParseException e) throws SAXException {
                     /*
        P/P           *  Method: void error(SAXParseException)
                      *    error fails for all possible inputs
                      *
                      *  Preconditions:
                      *    (soft) e != null
                      */
        90          System.out.println("Error : " + e.getMessage());
        91          throw e;
        92        }
        93
        94        public void fatalError(SAXParseException e) throws SAXException {
                     /*
        P/P           *  Method: void fatalError(SAXParseException)
                      *    fatalError fails for all possible inputs
                      *
                      *  Preconditions:
                      *    (soft) e != null
                      */
        95          System.out.println("Fatal : " + e.getMessage());
        96          throw e;
        97        }
        98      });
        99
       100      SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
       101      String title = "No title";
       102      String body = "";
       103      Date date = null;
       104      Document doc = builder.parse(source);
       105      Node root = doc.getDocumentElement();
       106      NodeList nodes = root.getChildNodes();
       107      System.out.println(nodes.getLength());
       108      for (int i = 0; i < nodes.getLength(); i++) {
       109        Node n = nodes.item(i);
       110        System.out.println(n.getNodeName());
       111
       112        if (n.getNodeName().equals("string")) {
       113          System.out.println(n.getAttributes().getNamedItem("name"));
       114          System.out.println(n.getAttributes().getNamedItem("name").getNodeValue());
       115          if (n.getAttributes().getNamedItem("name").getNodeValue().equals("title")) {
       116            title = n.getAttributes().getNamedItem("value").getNodeValue();
       117            System.out.println("Title : " + title);
       118          } else if (n.getAttributes().getNamedItem("name").getNodeValue().equals("text")) {
       119            body = n.getAttributes().getNamedItem("value").getNodeValue();
       120            System.out.println("Body : " + body);
       121          }
       122        }
       123
       124        if (n.getNodeName().equals("date") && n.getAttributes().getNamedItem("name").getNodeValue().equals("when")) {
       125          date = sdf.parse((n.getAttributes().getNamedItem("value").getNodeValue()).substring(4));
       126          System.out.println("Date : " + date);
       127        }
       128      }
       129
       130      BlogEntry entry = new BlogEntry(blog);
       131      entry.setTitle(title);
       132      entry.setBody(body);
       133      entry.setDate(date);
       134
       135      BlogService service = new BlogService();
       136      service.putBlogEntry(entry);
       137    }
       138
       139  }

SofCheck Inspector Build Version : 2.22510

radiouserlandimporter.java | 2010-Jun-25 19:40:32
radiouserlandimporter.class | 2010-Jul-19 20:23:38
radiouserlandimporter$1.class | 2010-Jul-19 20:23:38