Ejemplo n.º 1
0
  protected AbstractParsedCommand validateArguments(CmdLineHandler cmdLineHandler)
      throws ConsoleException {

    UnpackParsedCommand parsedCommandDTO = new UnpackParsedCommand();

    if (cmdLineHandler != null) {
      // -o
      FileParam oOption = (FileParam) cmdLineHandler.getOption(UnpackParsedCommand.O_ARG);
      if ((oOption.isSet())) {
        File outFile = oOption.getFile();
        ValidationUtility.assertValidDirectory(outFile);
        parsedCommandDTO.setOutputFile(outFile);
      } else {
        throw new ParseException(ParseException.ERR_NO_O);
      }

      // -f and -d
      PdfFileParam fOption = (PdfFileParam) cmdLineHandler.getOption(UnpackParsedCommand.F_ARG);
      FileParam dOption = (FileParam) cmdLineHandler.getOption(UnpackParsedCommand.D_ARG);
      if (fOption.isSet() || dOption.isSet()) {
        // -f
        if (fOption.isSet()) {
          // validate file extensions
          for (Iterator fIterator = fOption.getPdfFiles().iterator(); fIterator.hasNext(); ) {
            PdfFile currentFile = (PdfFile) fIterator.next();
            ValidationUtility.assertValidPdfExtension(currentFile.getFile().getName());
          }
          parsedCommandDTO.setInputFileList(FileUtility.getPdfFiles(fOption.getPdfFiles()));
        }
        // -d
        if ((dOption.isSet())) {
          File inputDir = dOption.getFile();
          ValidationUtility.assertValidDirectory(inputDir);
          parsedCommandDTO.setInputDirectory(inputDir);
        }
      } else {
        throw new ParseException(ParseException.ERR_NO_F_OR_D);
      }
    } else {
      throw new ConsoleException(ConsoleException.CMD_LINE_HANDLER_NULL);
    }
    return parsedCommandDTO;
  }
Ejemplo n.º 2
0
  public AbstractParsedCommand validateArguments(CmdLineHandler cmdLineHandler)
      throws ConsoleException {

    EncryptParsedCommand parsedCommandDTO = new EncryptParsedCommand();

    if (cmdLineHandler != null) {
      // -o
      FileParam oOption = (FileParam) cmdLineHandler.getOption(EncryptParsedCommand.O_ARG);
      if ((oOption.isSet())) {
        File outFile = oOption.getFile();
        ValidationUtility.checkValidDirectory(outFile);
        parsedCommandDTO.setOutputFile(outFile);
      } else {
        throw new ParseException(ParseException.ERR_NO_O);
      }

      // -p
      StringParam pOption = (StringParam) cmdLineHandler.getOption(EncryptParsedCommand.P_ARG);
      if (pOption.isSet()) {
        parsedCommandDTO.setOutputFilesPrefix(pOption.getValue());
      }

      // -apwd
      StringParam apwdOption =
          (StringParam) cmdLineHandler.getOption(EncryptParsedCommand.APWD_ARG);
      if (apwdOption.isSet()) {
        parsedCommandDTO.setOwnerPwd(apwdOption.getValue());
      }

      // -upwd
      StringParam upwdOption =
          (StringParam) cmdLineHandler.getOption(EncryptParsedCommand.UPWD_ARG);
      if (upwdOption.isSet()) {
        parsedCommandDTO.setUserPwd(upwdOption.getValue());
      }

      // -etype
      StringParam etypeOption =
          (StringParam) cmdLineHandler.getOption(EncryptParsedCommand.ETYPE_ARG);
      if (etypeOption.isSet()) {
        parsedCommandDTO.setEncryptionType(etypeOption.getValue());
      }

      // -f - d
      PdfFileParam fOption = (PdfFileParam) cmdLineHandler.getOption(EncryptParsedCommand.F_ARG);
      FileParam dOption = (FileParam) cmdLineHandler.getOption(EncryptParsedCommand.D_ARG);
      if (fOption.isSet() || dOption.isSet()) {
        // -f
        if (fOption.isSet()) {
          // validate file extensions
          for (Iterator fIterator = fOption.getPdfFiles().iterator(); fIterator.hasNext(); ) {
            PdfFile currentFile = (PdfFile) fIterator.next();
            ValidationUtility.checkValidPdfExtension(currentFile.getFile().getName());
          }
          parsedCommandDTO.setInputFileList(FileUtility.getPdfFiles(fOption.getPdfFiles()));
        }
        // -d
        if ((dOption.isSet())) {
          File inputDir = dOption.getFile();
          ValidationUtility.checkValidDirectory(inputDir);
          parsedCommandDTO.setInputDirectory(inputDir);
        }
      } else {
        throw new ParseException(ParseException.ERR_NO_F_OR_D);
      }

      // -allow
      StringParam allowOption =
          (StringParam) cmdLineHandler.getOption(EncryptParsedCommand.ALLOW_ARG);
      if (allowOption.isSet()) {
        Hashtable permissionsMap = getPermissionsMap(parsedCommandDTO.getEncryptionType());
        int permissions = 0;
        if (!permissionsMap.isEmpty()) {
          for (Iterator permIterator = allowOption.getValues().iterator();
              permIterator.hasNext(); ) {
            String currentPermission = (String) permIterator.next();
            Object value = permissionsMap.get(currentPermission);
            if (value != null) {
              permissions |= ((Integer) value).intValue();
            }
          }
        }
        permissionsMap = null;
        parsedCommandDTO.setPermissions(permissions);
      }
    } else {
      throw new ConsoleException(ConsoleException.CMD_LINE_HANDLER_NULL);
    }
    return parsedCommandDTO;
  }
Ejemplo n.º 3
0
  public AbstractParsedCommand validateArguments(CmdLineHandler cmdLineHandler)
      throws ConsoleException {
    ConcatParsedCommand parsedCommandDTO = new ConcatParsedCommand();

    if (cmdLineHandler != null) {
      // -o
      FileParam oOption = (FileParam) cmdLineHandler.getOption("o");
      if ((oOption.isSet())) {
        File outFile = oOption.getFile();
        // checking extension
        if ((outFile.getName().toLowerCase().endsWith(PDF_EXTENSION))
            && (outFile.getName().length() > PDF_EXTENSION.length())) {
          parsedCommandDTO.setOutputFile(outFile);
        } else {
          throw new ParseException(
              ParseException.ERR_OUT_NOT_PDF, new String[] {outFile.getPath()});
        }
      } else {
        throw new ParseException(ParseException.ERR_NO_O);
      }

      // -f -l
      FileParam lOption = (FileParam) cmdLineHandler.getOption("l");
      PdfFileParam fOption = (PdfFileParam) cmdLineHandler.getOption("f");
      if (lOption.isSet() || fOption.isSet()) {
        if (!(lOption.isSet() && fOption.isSet())) {
          if (fOption.isSet()) {
            // validate file extensions
            for (Iterator fIterator = fOption.getPdfFiles().iterator(); fIterator.hasNext(); ) {
              PdfFile currentFile = (PdfFile) fIterator.next();
              if (!((currentFile.getFile().getName().toLowerCase().endsWith(PDF_EXTENSION))
                  && (currentFile.getFile().getName().length() > PDF_EXTENSION.length()))) {
                throw new ParseException(
                    ParseException.ERR_IN_NOT_PDF, new String[] {currentFile.getFile().getPath()});
              }
            }
            parsedCommandDTO.setInputFileList(FileUtility.getPdfFiles(fOption.getPdfFiles()));
          } else {
            if (lOption.getFile().getPath().toLowerCase().endsWith(CSV_EXTENSION)
                || lOption.getFile().getPath().toLowerCase().endsWith(XML_EXTENSION)) {
              parsedCommandDTO.setInputCvsOrXmlFile(lOption.getFile());
            } else {
              throw new ParseException(ParseException.ERR_NOT_CSV_OR_XML);
            }
          }
        } else {
          throw new ParseException(ParseException.ERR_BOTH_F_OR_L);
        }
      } else {
        throw new ParseException(ParseException.ERR_NO_F_OR_L);
      }

      // -u
      StringParam uOption = (StringParam) cmdLineHandler.getOption("u");
      // if it's set we proceed with validation
      if (uOption.isSet()) {
        Pattern p = Pattern.compile("(([0-9]*[-][0-9]*[:])|(all:))+", Pattern.CASE_INSENSITIVE);
        if ((p.matcher(uOption.getValue()).matches())) {
          parsedCommandDTO.setPageSelection(uOption.getValue());
        } else {
          throw new ParseException(ParseException.ERR_ILLEGAL_U);
        }
      }

      // -copyfields
      parsedCommandDTO.setCopyFields(
          ((BooleanParam) cmdLineHandler.getOption("copyfields")).isTrue());
    } else {
      throw new ParseException(ParseException.CMD_LINE_HANDLER_NULL);
    }
    return parsedCommandDTO;
  }