Package net.sf.jpluck.apps.cmdline

Source Code of net.sf.jpluck.apps.cmdline.Convert

package net.sf.jpluck.apps.cmdline;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;

import javax.imageio.ImageIO;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;

import net.sf.jpluck.VersionInfo;
import net.sf.jpluck.apps.ExitCodes;
import net.sf.jpluck.apps.OptionsUtil;
import net.sf.jpluck.jxl.URIRewriter;
import net.sf.jpluck.palm.bitmap.CompositeBitmap;
import net.sf.jpluck.plucker.CompositeImageRecord;
import net.sf.jpluck.plucker.Document;
import net.sf.jpluck.plucker.ImageRecord;
import net.sf.jpluck.plucker.TextRecord;
import net.sf.jpluck.spider.ContentType;
import net.sf.jpluck.spider.Resource;
import net.sf.jpluck.util.LogUtil;
import net.sf.jpluck.xml.TextRecordResult;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;


public class Convert {
    private static List SUPPORTED_MIME_TYPES = new ArrayList();

    static {
        SUPPORTED_MIME_TYPES.add("text/html");
        SUPPORTED_MIME_TYPES.add("image/gif");
        SUPPORTED_MIME_TYPES.add("image/jpeg");
        SUPPORTED_MIME_TYPES.add("image/png");
        Collections.sort(SUPPORTED_MIME_TYPES);
    }

    public static void main(String[] args) throws Exception {
        System.setProperty("java.awt.headless", "true");

        Options options = OptionsUtil.createGeneric();
        options.addOption("bpp", true, "Image bit depth. Required when converting an image. 1|2|4|8|16");
        options.addOption("mimetype", true,
                          "Content MIME type. Supported MIME types: " + SUPPORTED_MIME_TYPES + " Default: text/html.");
        CommandLine cl = new GnuParser().parse(options, args);

        if (cl.hasOption("help")) {
            OptionsUtil.printHelp("java -jar convert.jar [options] <document name>",
                                  "Parses content through stdin and outputs a Plucker document to stdout.", options);
            System.exit(ExitCodes.OK);
        }
        if (cl.hasOption("version")) {
            OptionsUtil.printVersion("Convert", VersionInfo.VERSION, VersionInfo.RELEASE_DATE);
            System.exit(ExitCodes.OK);
        }

        args = cl.getArgs();
        if (args.length != 1) {
            System.err.println("ERROR: Invalid number of arguments " + args.length);
            System.exit(ExitCodes.ERROR_INVALID_NUMBER_OF_ARGUMENTS);
        }
        String name = args[0];
        if (name.length() > 31) {
            System.err.println("ERROR: Name exceeds maximum length of 31 characters.");
            System.exit(ExitCodes.ERROR_INVALID_OPTION);
        }
        String mimeType = "text/html";
        if (cl.hasOption("mimetype")) {
            mimeType = cl.getOptionValue("mimetype");
        }
        if (!SUPPORTED_MIME_TYPES.contains(mimeType)) {
            System.err.println("ERROR: Unsupported MIME type: " + mimeType);
            System.exit(ExitCodes.ERROR_INVALID_OPTION);
        }
        if (mimeType.startsWith("image/") && !cl.hasOption("bpp")) {
          System.err.println("ERROR: Bitdepth not specified for image.");
          System.exit(ExitCodes.ERROR_INVALID_OPTION);
        }
    int bpp = 0;
    if (cl.hasOption("bpp")) {
      bpp = Integer.parseInt(cl.getOptionValue("bpp"));
    }

        byte[] buf = new byte[4096];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        for (int bytesRead; (bytesRead = System.in.read(buf)) != -1; ) {
          baos.write(buf, 0, bytesRead);
        }
        byte[] data = baos.toByteArray();
        Document document = new Document();
        if (mimeType.equals("text/html")) {
      Resource resource = new Resource("http://127.0.0.1/", null, new ContentType(mimeType), data, 0, false);
      org.w3c.dom.Document dom = resource.parseHTML();
      TextRecord textRecord = new TextRecord("http://127.0.0.1/");
      Transformer transformer = TransformerFactory.newInstance().newTransformer();
      TextRecordResult result = new TextRecordResult(textRecord, new URIRewriter(), 100, false, document);
      transformer.transform(new DOMSource(dom), result);
      document.addRecord(textRecord);
        } else if (mimeType.startsWith("image/")) {
      BufferedImage image = ImageIO.read(new ByteArrayInputStream(data));
      CompositeBitmap compositeBitmap = CompositeBitmap.create(image, bpp);
      String[] uris = new String[compositeBitmap.getRows()];
      for (int j = 0, n = compositeBitmap.getRows(); j < n; j++) {
        uris[j] = "http://127.0.0.1/-" + j;
        document.addRecord(new ImageRecord(uris[j], compositeBitmap.getBitmapAt(j, 0)));
      }
      document.addRecord(new CompositeImageRecord("http://127.0.0.1/", uris, 1));
        }

        if (name.length() > 31) {
            name = name.substring(0, 31).trim();
        }
        LogUtil logUtil = new LogUtil(LogUtil.DEFAULT_LOGGER_NAMES);
        logUtil.setLevel(Level.OFF);
        document.setName(name);
        document.setHome("http://127.0.0.1/");
        document.setCompression(Document.ZLIB_COMPRESSION);
        document.setIncludeURIInfo(false);
        document.write(System.out);
    }
}
TOP

Related Classes of net.sf.jpluck.apps.cmdline.Convert

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.