Package net.sf.jpluck.test

Source Code of net.sf.jpluck.test.AtomTest

package net.sf.jpluck.test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import net.sf.jpluck.feed.AtomFeed;
import net.sf.jpluck.feed.FeedFormatter;
import net.sf.jpluck.jxl.Feed;
import net.sf.jpluck.jxl.JXL;
import net.sf.jpluck.plucker.parsing.XHTMLEntityResolver;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class AtomTest {
  public static void main(String[] args) throws Exception {
    JXL jxl = new JXL(new File(args[0]));
    File file = new File(args[1]);
    byte[] data = new byte[(int)file.length()];
    FileInputStream in = new FileInputStream(file);
    in.read(data);
    in.close();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(false);

    DocumentBuilder builder = dbf.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {
        public InputSource resolveEntity(String publicId, String systemId)
          throws SAXException, IOException {
          return XHTMLEntityResolver.getTransitionalDTD();
        }
      });

    org.w3c.dom.Document document = null;
    try {
      document = builder.parse(new InputSource(new ByteArrayInputStream(data)));
    } catch (Exception e) {
      InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(data), "ISO-8859-1");
      document = builder.parse(new InputSource(reader));
    }
    AtomFeed feed = new AtomFeed(document);
    FeedFormatter formatter = new FeedFormatter(feed, (Feed)jxl.getDocuments()[0]);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(formatter.getIndexDocument()),
      new StreamResult(new File(file.getParentFile(),"index.html")));
    for (int i = 0, n = feed.getItems().length; i < n; i++) {
      transformer.transform(new DOMSource(formatter.getItemDocuments()[i]),
        new StreamResult(new File(file.getParentFile(),"item" + (i+1) + ".html")));
    }   
  }
}
TOP

Related Classes of net.sf.jpluck.test.AtomTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.