Пример #1
1
  /**
   * Create job arguments that are used by CrowdClient to build a POST request for a new job on
   * Crowdflower
   */
  void createArgumentMaps() {
    argumentMap = new LinkedMultiValueMap<String, String>();
    includedCountries = new Vector<String>();
    excludedCountries = new Vector<String>();

    Iterator<Map.Entry<String, JsonNode>> jsonRootIt = template.fields();

    for (Map.Entry<String, JsonNode> elt; jsonRootIt.hasNext(); ) {
      elt = jsonRootIt.next();

      JsonNode currentNode = elt.getValue();
      String currentKey = elt.getKey();

      if (currentNode.isContainerNode()) {
        // special processing for these arrays:
        if (currentKey.equals(includedCountriesKey) || currentKey.equals(excludedCountriesKey)) {
          Iterator<JsonNode> jsonSubNodeIt = currentNode.elements();
          for (JsonNode subElt; jsonSubNodeIt.hasNext(); ) {
            subElt = jsonSubNodeIt.next();
            (currentKey.equals(includedCountriesKey) ? includedCountries : excludedCountries)
                .addElement(subElt.path(countryCodeKey).asText());
          }
        }
      } else if (!currentNode.isNull() && argumentFilterSet.contains(currentKey)) {
        argumentMap.add(jobKey + "[" + currentKey + "]", currentNode.asText());
      }
      if (currentKey == idKey) {
        this.id = currentNode.asText();
      }
    }
  }
  @Override
  public void validate(final ValidationReport report, final JsonNode instance) {
    final JsonNode oldParent = report.getSchema();

    report.setSchema(parent);

    for (final KeywordValidator validator : validators)
      validator.validateInstance(report, instance);

    if (!(instance.isContainerNode() || report.isSuccess())) {
      report.setSchema(oldParent);
      return;
    }

    final JsonPointer ptr = report.getPath();

    JsonPointer current;

    if (instance.isArray()) {
      JsonNode subSchema;
      int i = 0;
      for (final JsonNode element : instance) {
        current = ptr.append(i);
        report.setPath(current);
        subSchema = arrayPath(i);
        JsonSchema.fromNode(parent, subSchema).validate(report, element);
        i++;
      }
      report.setSchema(oldParent);
      report.setPath(ptr);
      return;
    }

    // Can only be an object now
    final Map<String, JsonNode> map = CollectionUtils.toMap(instance.fields());

    String key;
    JsonNode value;

    for (final Map.Entry<String, JsonNode> entry : map.entrySet()) {
      key = entry.getKey();
      value = entry.getValue();
      current = ptr.append(key);
      report.setPath(current);
      for (final JsonNode subSchema : objectPath(key))
        JsonSchema.fromNode(parent, subSchema).validate(report, value);
    }

    report.setSchema(oldParent);
    report.setPath(ptr);
  }
Пример #3
0
 private String nodeAsString(JsonNode node) {
   if (isNull(node)) return null;
   return node.isContainerNode() ? node.toString() : node.asText();
 }