コード例 #1
0
  protected void determineProjectName() throws ASExternalImporterException {
    String exceptionMessage = null;

    if (projectType == ProjectType.IPR) {
      // We are sure that determineProjectType() didn't throw exception, so iprFile is not null and
      // it represents *.ipr file
      projectName = iprFile.getName().replaceFirst("\\.ipr$", "");
    } else if (projectType == ProjectType.IDEA) {
      File nameFile = new File(ideaDir.getPath(), ".name");
      try {
        BufferedReader reader = new BufferedReader(new FileReader(nameFile));
        String name = reader.readLine();
        if (StringUtils.isWhitespace(name)) {
          exceptionMessage = ".name file in path " + nameFile.getPath() + " is empty";
        }
        projectName = name.trim();
      } catch (FileNotFoundException e) {
        exceptionMessage = "Cannot find .name file in path " + nameFile.getPath();
      } catch (IOException e) {
        exceptionMessage = "Cannot read .name file in path " + nameFile.getPath();
      }
    } else {
      // Actually unreachable (see setProject())
      exceptionMessage = "Project type was not determined";
    }

    if (exceptionMessage != null) {
      resetStateAndThrowException(exceptionMessage);
    }
  }
コード例 #2
0
 public static void copyChildNodes(SOAPElement target, Element source) throws SOAPException {
   // easy way out: no child nodes
   if (!source.hasChildNodes()) return;
   final boolean traceEnabled = log.isTraceEnabled();
   // traverse child nodes
   for (Node child = source.getFirstChild(); child != null; child = child.getNextSibling()) {
     switch (child.getNodeType()) {
       case Node.ELEMENT_NODE:
         {
           copyChildElement(target, (Element) child);
           break;
         }
       case Node.TEXT_NODE:
       case Node.CDATA_SECTION_NODE:
         {
           String text = child.getNodeValue();
           // drop whitespace-only text nodes
           if (!StringUtils.isWhitespace(text)) {
             target.addTextNode(text);
             if (traceEnabled) log.trace("added text node: " + text);
           }
           break;
         }
       default:
         log.debug("discarding child node: " + child);
     }
   }
 }
コード例 #3
0
 /** Assert string contains only whitespace. */
 protected void assertWhitespace(String str) {
   Assert.assertTrue(StringUtils.isWhitespace(str));
 }
コード例 #4
0
 public void setId(String id) {
   if (StringUtils.isWhitespace(id)) {
     throw new IllegalArgumentException("id was blank");
   }
   this.id = id;
 }
コード例 #5
0
  private void highlightActiveHostsFile(File hostsFile) throws IOException, ParseException {
    BufferedReader br = null;

    try {
      /* Converts the file to text */
      // StringReader fileReader = new StringReader(getLinuxHostsFile()); // For testing
      br = new BufferedReader(new FileReader(hostsFile));

      /* Character counter */
      int charCounter = 0;

      /* Line counter */
      int lineCounter = 1;

      /* Line */
      String line = null;

      while ((line = br.readLine()) != null) {
        line += "\n";
        activeHostsFileStyledText.append(line);

        /*
         * Remark line
         */
        if (line.startsWith(REM_LINE_CHAR)) {
          int prevCharCounter = charCounter;
          charCounter += line.length();

          formatRemark(prevCharCounter, charCounter);

          if (log.isTraceEnabled()) {
            log.trace("line  ='" + line + "'");
            // log.trace("remark='"+ getWindowsHostsFile().substring(prevCharCounter, charCounter)
            // +"' ("+ prevCharCounter +","+ charCounter +")");
          }
        } else if (StringUtils.isBlank(line)) // Empty line
        {
          charCounter += line.length();
        } else // Expects a host line
        {
          int localCharCounter = charCounter;
          charCounter += line.length();

          Scanner scanner = new Scanner(line);
          scanner.useDelimiter(Pattern.compile("(\\s)"));

          /* Output of the parsing code */
          String ipAddress = null;

          /* Verifies the number of tokens. At least two must exist (IP address and one name) */
          if (scanner.hasNext()) {
            /* The first token must be an IP address */
            {
              ipAddress = scanner.next();

              if (!NetworkUtils.isIpAddress(ipAddress))
                throw new ParseException(
                    "IP address expected. Token found: " + ipAddress, lineCounter);

              int prevCharCounter = localCharCounter;
              localCharCounter += ipAddress.length() + 1; // Sums 1 because of the lost space

              formatIpAddress(prevCharCounter, localCharCounter);
            }

            /* The remaining tokens are the host names associated to the IP address */
            {
              while (scanner.hasNext()) {
                String hostName = scanner.next();

                if (StringUtils.isWhitespace(hostName) || StringUtils.isBlank(hostName)) {
                  localCharCounter++;
                } else if (NetworkUtils.isHostName(hostName)) {
                  int prevCharCounter = localCharCounter;
                  localCharCounter += hostName.length() + 1; // 1 to compensate the space lost

                  //								if(log.isTraceEnabled())
                  //									log.trace("hostName='"+
                  // getWindowsHostsFile().substring(prevCharCounter, localCharCounter) +"' ("+
                  // prevCharCounter +","+ localCharCounter +")");

                  formatHostName(prevCharCounter, localCharCounter);
                } else
                  throw new ParseException(
                      "Host name expected at token " + localCharCounter + ". Found: " + hostName,
                      lineCounter);
              }
            }
          } else
            throw new ParseException(
                "At least 2 tokens are expected from a host line.", lineCounter);
        }

        lineCounter++;
      }
    } finally {
      if (br != null) br.close();
    }
  }
コード例 #6
0
  private void validateType(
      final DataValidatorBuilder baseDataValidator, final JsonElement column) {
    final String type = fromApiJsonHelper.extractStringNamed("type", column);
    baseDataValidator
        .reset()
        .parameter("type")
        .value(type)
        .notBlank()
        .isOneOfTheseValues(supportedColumnTypes);

    if (type != null && type.equals("String")) {
      if (fromApiJsonHelper.parameterExists("length", column)) {
        final String lengthStr = fromApiJsonHelper.extractStringNamed("length", column);
        if (lengthStr != null
            && !StringUtils.isWhitespace(lengthStr)
            && StringUtils.isNumeric(lengthStr)
            && StringUtils.isNotBlank(lengthStr)) {
          Integer length = Integer.parseInt(lengthStr);
          baseDataValidator.reset().parameter("length").value(length).positiveAmount();
        } else if (StringUtils.isBlank(lengthStr) || StringUtils.isWhitespace(lengthStr)) {
          baseDataValidator
              .reset()
              .parameter("length")
              .failWithCode("must.be.provided.when.type.is.String");
        } else if (!StringUtils.isNumeric(lengthStr)) {
          baseDataValidator.reset().parameter("length").failWithCode("not.greater.than.zero");
        }
      } else {
        baseDataValidator
            .reset()
            .parameter("length")
            .failWithCode("must.be.provided.when.type.is.String");
      }
    } else {
      baseDataValidator
          .reset()
          .parameter("length")
          .mustBeBlankWhenParameterProvidedIs("type", type);
    }

    final String code = fromApiJsonHelper.extractStringNamed("code", column);
    if (type != null && type.equals("Dropdown")) {
      if (code != null) {
        baseDataValidator
            .reset()
            .parameter("code")
            .value(code)
            .notBlank()
            .matchesRegularExpression(DATATABLE_NAME_REGEX_PATTERN);
      } else {
        baseDataValidator
            .reset()
            .parameter("code")
            .value(code)
            .cantBeBlankWhenParameterProvidedIs("type", type);
      }
    } else {
      baseDataValidator
          .reset()
          .parameter("code")
          .value(code)
          .mustBeBlankWhenParameterProvided("type", type);
    }
  }
コード例 #7
0
  public void validateForUpdate(final String json) {
    if (StringUtils.isBlank(json)) {
      throw new InvalidJsonException();
    }
    // Because all parameters are optional, a check to see if at least one parameter
    // has been specified is necessary in order to avoid JSON requests with no parameters
    if (!json.matches("(?s)\\A\\{.*?(\\\".*?\\\"\\s*?:\\s*?)+.*?\\}\\z")) {
      throw new PlatformDataIntegrityException(
          "error.msg.invalid.request.body.no.parameters",
          "Provided JSON request body does not have any parameters.");
    }

    final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
    fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParametersForUpdate);

    final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
    final DataValidatorBuilder baseDataValidator =
        new DataValidatorBuilder(dataValidationErrors).resource("datatable");

    final JsonElement element = fromApiJsonHelper.parse(json);
    final String apptableName = fromApiJsonHelper.extractStringNamed("apptableName", element);
    baseDataValidator
        .reset()
        .parameter("apptableName")
        .value(apptableName)
        .ignoreIfNull()
        .notBlank()
        .isOneOfTheseValues(supportedApptableNames);
    final String fkColumnName = (apptableName != null) ? apptableName.substring(2) + "_id" : "";

    final JsonArray changeColumns =
        fromApiJsonHelper.extractJsonArrayNamed("changeColumns", element);
    baseDataValidator
        .reset()
        .parameter("changeColumns")
        .value(changeColumns)
        .ignoreIfNull()
        .jsonArrayNotEmpty();

    if (changeColumns != null) {
      for (JsonElement column : changeColumns) {
        fromApiJsonHelper.checkForUnsupportedParameters(
            column.getAsJsonObject(), supportedParametersForChangeColumns);

        final String name = fromApiJsonHelper.extractStringNamed("name", column);
        baseDataValidator
            .reset()
            .parameter("name")
            .value(name)
            .notBlank()
            .isNotOneOfTheseValues("id", fkColumnName)
            .matchesRegularExpression(DATATABLE_COLUMN_NAME_REGEX_PATTERN);

        final String newName = fromApiJsonHelper.extractStringNamed("newName", column);
        baseDataValidator
            .reset()
            .parameter("newName")
            .value(newName)
            .ignoreIfNull()
            .notBlank()
            .notExceedingLengthOf(50)
            .isNotOneOfTheseValues("id", fkColumnName)
            .matchesRegularExpression(DATATABLE_NAME_REGEX_PATTERN);

        if (fromApiJsonHelper.parameterExists("length", column)) {
          final String lengthStr = fromApiJsonHelper.extractStringNamed("length", column);
          if (StringUtils.isWhitespace(lengthStr)
              || !StringUtils.isNumeric(lengthStr)
              || StringUtils.isBlank(lengthStr)) {
            baseDataValidator.reset().parameter("length").failWithCode("not.greater.than.zero");
          } else {
            Integer length = Integer.parseInt(lengthStr);
            baseDataValidator
                .reset()
                .parameter("length")
                .value(length)
                .ignoreIfNull()
                .notBlank()
                .positiveAmount();
          }
        }

        final String code = fromApiJsonHelper.extractStringNamed("code", column);
        baseDataValidator
            .reset()
            .parameter("code")
            .value(code)
            .ignoreIfNull()
            .notBlank()
            .notExceedingLengthOf(100)
            .matchesRegularExpression(DATATABLE_COLUMN_NAME_REGEX_PATTERN);

        final String newCode = fromApiJsonHelper.extractStringNamed("newCode", column);
        baseDataValidator
            .reset()
            .parameter("newCode")
            .value(newCode)
            .ignoreIfNull()
            .notBlank()
            .notExceedingLengthOf(100)
            .matchesRegularExpression(DATATABLE_COLUMN_NAME_REGEX_PATTERN);

        if (StringUtils.isBlank(code) && StringUtils.isNotBlank(newCode)) {
          baseDataValidator
              .reset()
              .parameter("code")
              .value(code)
              .cantBeBlankWhenParameterProvidedIs("newCode", newCode);
        }

        final Boolean mandatory = fromApiJsonHelper.extractBooleanNamed("mandatory", column);
        baseDataValidator
            .reset()
            .parameter("mandatory")
            .value(mandatory)
            .ignoreIfNull()
            .notBlank()
            .isOneOfTheseValues(true, false);

        final Boolean after = fromApiJsonHelper.extractBooleanNamed("after", column);
        baseDataValidator
            .reset()
            .parameter("after")
            .value(after)
            .ignoreIfNull()
            .notBlank()
            .isOneOfTheseValues(true, false);
      }
    }

    final JsonArray addColumns = fromApiJsonHelper.extractJsonArrayNamed("addColumns", element);
    baseDataValidator
        .reset()
        .parameter("addColumns")
        .value(addColumns)
        .ignoreIfNull()
        .jsonArrayNotEmpty();

    if (addColumns != null) {
      for (JsonElement column : addColumns) {
        fromApiJsonHelper.checkForUnsupportedParameters(
            column.getAsJsonObject(), supportedParametersForAddColumns);

        final String name = fromApiJsonHelper.extractStringNamed("name", column);
        baseDataValidator
            .reset()
            .parameter("name")
            .value(name)
            .notBlank()
            .isNotOneOfTheseValues("id", fkColumnName)
            .matchesRegularExpression(DATATABLE_COLUMN_NAME_REGEX_PATTERN);

        validateType(baseDataValidator, column);

        final Boolean mandatory = fromApiJsonHelper.extractBooleanNamed("mandatory", column);
        baseDataValidator
            .reset()
            .parameter("mandatory")
            .value(mandatory)
            .ignoreIfNull()
            .notBlank()
            .isOneOfTheseValues(true, false);

        final Boolean after = fromApiJsonHelper.extractBooleanNamed("after", column);
        baseDataValidator
            .reset()
            .parameter("after")
            .value(after)
            .ignoreIfNull()
            .notBlank()
            .isOneOfTheseValues(true, false);
      }
    }

    final JsonArray dropColumns = fromApiJsonHelper.extractJsonArrayNamed("dropColumns", element);
    baseDataValidator
        .reset()
        .parameter("dropColumns")
        .value(dropColumns)
        .ignoreIfNull()
        .jsonArrayNotEmpty();

    if (dropColumns != null) {
      for (JsonElement column : dropColumns) {
        fromApiJsonHelper.checkForUnsupportedParameters(
            column.getAsJsonObject(), supportedParametersForDropColumns);

        final String name = fromApiJsonHelper.extractStringNamed("name", column);
        baseDataValidator
            .reset()
            .parameter("name")
            .value(name)
            .notBlank()
            .isNotOneOfTheseValues("id", fkColumnName)
            .matchesRegularExpression(DATATABLE_COLUMN_NAME_REGEX_PATTERN);
      }
    }

    throwExceptionIfValidationWarningsExist(dataValidationErrors);
  }