Examples of MixParsedCommand


Examples of org.pdfsam.console.business.dto.commands.MixParsedCommand

*/
public class MixCmdValidator extends AbstractCmdValidator {

  public AbstractParsedCommand validateArguments(CmdLineHandler cmdLineHandler) throws ConsoleException {
   
    MixParsedCommand parsedCommandDTO = new MixParsedCommand();
   
    if(cmdLineHandler != null){
      //-o
      FileParam oOption = (FileParam) cmdLineHandler.getOption(MixParsedCommand.O_ARG);
          if ((oOption.isSet())){
              File outFile = oOption.getFile();
              //checking extension
              ValidationUtility.assertValidPdfExtension(outFile.getName());
              parsedCommandDTO.setOutputFile(outFile)
         
          }else{
            throw new ParseException(ParseException.ERR_NO_O);
          }
         
          //-f1
      PdfFileParam f1Option = (PdfFileParam) cmdLineHandler.getOption(MixParsedCommand.F1_ARG);          
          if(f1Option.isSet()){
            PdfFile firstFile = f1Option.getPdfFile();
             //checking extension
              ValidationUtility.assertValidPdfExtension(firstFile.getFile().getName());
              parsedCommandDTO.setFirstInputFile(FileUtility.getPdfFile(firstFile));                               
          }else{
            throw new ParseException(ParseException.ERR_NO_F1)
          }
         
          //-f2
          PdfFileParam f2Option = (PdfFileParam) cmdLineHandler.getOption(MixParsedCommand.F2_ARG);          
          if(f2Option.isSet()){
            PdfFile secondFile = f2Option.getPdfFile();
             //checking extension
              ValidationUtility.assertValidPdfExtension(secondFile.getFile().getName());
               parsedCommandDTO.setSecondInputFile(FileUtility.getPdfFile(secondFile));
          }else{
            throw new ParseException(ParseException.ERR_NO_F2)
          }          
 
          //-step
          IntParam stepOption = (IntParam) cmdLineHandler.getOption(MixParsedCommand.STEP_ARG);                   
          if(stepOption.isSet()){   
            int step = stepOption.intValue();
            if(step>0){
              parsedCommandDTO.setStep(stepOption.intValue());
            }else{
                throw new ParseException(ParseException.ERR_STEP_ZERO_OR_NEGATIVE);
              }
          }else{
            parsedCommandDTO.setStep(MixParsedCommand.DEFAULT_STEP);
          }
         
          //-secondstep
          IntParam secondStepOption = (IntParam) cmdLineHandler.getOption(MixParsedCommand.SECOND_STEP_ARG);                   
          if(secondStepOption.isSet()){   
            int step = secondStepOption.intValue();
            if(step>0){
              parsedCommandDTO.setSecondStep(secondStepOption.intValue());
            }else{
                throw new ParseException(ParseException.ERR_STEP_ZERO_OR_NEGATIVE);
              }
          }else{
            parsedCommandDTO.setSecondStep(parsedCommandDTO.getStep());
          }
                  
          //-reversefirst
          parsedCommandDTO.setReverseFirst(((BooleanParam) cmdLineHandler.getOption(MixParsedCommand.REVERSE_FIRST_ARG)).isTrue());
          //-reversesecond
          parsedCommandDTO.setReverseSecond(((BooleanParam) cmdLineHandler.getOption(MixParsedCommand.REVERSE_SECOND_ARG)).isTrue());               
    }else{
      throw new ConsoleException(ConsoleException.CMD_LINE_HANDLER_NULL);
    }
        return parsedCommandDTO;
  }   
View Full Code Here

Examples of org.pdfsam.console.business.dto.commands.MixParsedCommand

    private static final Logger LOG = Logger.getLogger(AlternateMixCmdExecutor.class.getPackage().getName());

    public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException {
        if ((parsedCommand != null) && (parsedCommand instanceof MixParsedCommand)) {
            MixParsedCommand inputCommand = (MixParsedCommand) parsedCommand;
            setWorkIndeterminate();
            Document pdfDocument = null;

            int[] limits1 = { 1, 1 };
            int[] limits2 = { 1, 1 };
            try {
                File tmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile());

                pdfReader1 = new PdfReader(new RandomAccessFileOrArray(inputCommand.getFirstInputFile().getFile()
                        .getAbsolutePath()), inputCommand.getFirstInputFile().getPasswordBytes());
                pdfReader1.removeUnusedObjects();
                pdfReader1.consolidateNamedDestinations();
                limits1[1] = pdfReader1.getNumberOfPages();

                pdfReader2 = new PdfReader(new RandomAccessFileOrArray(inputCommand.getSecondInputFile().getFile()
                        .getAbsolutePath()), inputCommand.getSecondInputFile().getPasswordBytes());
                pdfReader2.removeUnusedObjects();
                pdfReader2.consolidateNamedDestinations();
                limits2[1] = pdfReader2.getNumberOfPages();

                pdfDocument = new Document(pdfReader1.getPageSizeWithRotation(1));
                LOG.debug("Creating a new document.");
                pdfWriter = new PdfCopy(pdfDocument, new FileOutputStream(tmpFile));

                setPdfVersionSettingOnWriter(inputCommand, pdfWriter);
                setCompressionSettingOnWriter(inputCommand, pdfWriter);

                pdfDocument.addCreator(ConsoleServicesFacade.CREATOR);
                pdfDocument.open();

                PdfImportedPage page;

                boolean finished1 = false;
                boolean finished2 = false;
                int current1 = (inputCommand.isReverseFirst()) ? limits1[1] : limits1[0];
                int current2 = (inputCommand.isReverseSecond()) ? limits2[1] : limits2[0];
                while (!finished1 || !finished2) {
                    if (!finished1) {
                        for (int i = 0; (i < inputCommand.getStep() && !finished1); i++) {
                            if (current1 >= limits1[0] && current1 <= limits1[1]) {
                                page = pdfWriter.getImportedPage(pdfReader1, current1);
                                pdfWriter.addPage(page);
                                current1 = (inputCommand.isReverseFirst()) ? (current1 - 1) : (current1 + 1);
                            } else {
                                LOG.info("First file processed.");
                                pdfReader1.close();
                                finished1 = true;
                            }
                        }
                    }
                    if (!finished2) {
                        for (int i = 0; (i < inputCommand.getSecondStep() && !finished2); i++) {
                            if (current2 >= limits2[0] && current2 <= limits2[1] && !finished2) {
                                page = pdfWriter.getImportedPage(pdfReader2, current2);
                                pdfWriter.addPage(page);
                                current2 = (inputCommand.isReverseSecond()) ? (current2 - 1) : (current2 + 1);
                            } else {
                                LOG.info("Second file processed.");
                                pdfReader2.close();
                                finished2 = true;
                            }
                        }
                    }

                }

                pdfWriter.freeReader(pdfReader1);
                pdfWriter.freeReader(pdfReader2);

                pdfDocument.close();
                FileUtility.renameTemporaryFile(tmpFile, inputCommand.getOutputFile(), inputCommand.isOverwrite());
                LOG.debug("File " + inputCommand.getOutputFile().getCanonicalPath() + " created.");
                LOG.info("Alternate mix with step first document " + inputCommand.getStep()
                        + " and step second document " + inputCommand.getSecondStep() + " completed.");
            } catch (Exception e) {
                throw new MixException(e);
            } finally {
                setWorkCompleted();
            }
View Full Code Here
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.