001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id$ */
019:
020: package ps;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.OutputStream;
025: import java.io.InputStream;
026:
027: import org.apache.commons.io.IOUtils;
028: import org.apache.xmlgraphics.ps.dsc.DSCException;
029: import org.apache.xmlgraphics.ps.dsc.tools.PageExtractor;
030:
031: /**
032: * Demonstrates how the DSC parser can be used to extract a series of pages from a DSC-compliant
033: * PostScript file. For details how this works, please look into the PageExtractor class
034: * where the actual functionality is located. This sample class only calls the code there.
035: */
036: public class DSCProcessingExample1 {
037:
038: /**
039: * Extracts a series of pages from a DSC-compliant PostScript file.
040: * @param srcFile the source PostScript file
041: * @param tgtFile the target file to write the extracted pages to
042: * @param from the starting page number
043: * @param to the ending page number
044: * @throws IOException In case of an I/O error
045: */
046: public void extractPages(File srcFile, File tgtFile, int from,
047: int to) throws IOException {
048: InputStream in = new java.io.FileInputStream(srcFile);
049: in = new java.io.BufferedInputStream(in);
050: try {
051: OutputStream out = new java.io.FileOutputStream(tgtFile);
052: out = new java.io.BufferedOutputStream(out);
053: try {
054: PageExtractor.extractPages(in, out, from, to);
055: } catch (DSCException e) {
056: throw new RuntimeException(e.getMessage());
057: } finally {
058: IOUtils.closeQuietly(out);
059: }
060: } finally {
061: IOUtils.closeQuietly(in);
062: }
063: }
064:
065: private static void showInfo() {
066: System.out
067: .println("Call: DSCProcessingExample1 <source-file> <target-file> <from-page> <to-page>");
068: }
069:
070: /**
071: * Command-line interface
072: * @param args command-line arguments
073: */
074: public static void main(String[] args) {
075: try {
076: File srcFile, tgtFile;
077: int from, to;
078: if (args.length >= 4) {
079: srcFile = new File(args[0]);
080: tgtFile = new File(args[1]);
081: from = Integer.parseInt(args[2]);
082: to = Integer.parseInt(args[3]);
083: } else {
084: throw new IllegalArgumentException(
085: "Invalid number of parameters!");
086: }
087: if (!srcFile.exists()) {
088: throw new IllegalArgumentException(
089: "Source file not found: " + srcFile);
090: }
091: DSCProcessingExample1 app = new DSCProcessingExample1();
092: app.extractPages(srcFile, tgtFile, from, to);
093: System.out.println("File written: "
094: + tgtFile.getCanonicalPath());
095: } catch (IllegalArgumentException iae) {
096: System.err.println(iae.getMessage());
097: showInfo();
098: System.exit(1);
099: } catch (Exception e) {
100: e.printStackTrace();
101: System.exit(-1);
102: }
103: }
104:
105: }
|