示例#1
0
 @Override
 protected NodeTuple representJavaBeanProperty(
     Object javaBean, Property property, Object propertyValue, Tag customTag) {
   NodeTuple tuple =
       super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
   Node valueNode = tuple.getValueNode();
   if (Tag.NULL.equals(valueNode.getTag())) {
     return null; // skip 'null' values
   }
   if (valueNode instanceof CollectionNode) {
     if (Tag.SEQ.equals(valueNode.getTag())) {
       SequenceNode seq = (SequenceNode) valueNode;
       if (seq.getValue().isEmpty()) {
         return null; // skip empty lists
       }
     }
     if (Tag.MAP.equals(valueNode.getTag())) {
       MappingNode seq = (MappingNode) valueNode;
       if (seq.getValue().isEmpty()) {
         return null; // skip empty maps
       }
     }
   }
   return tuple;
 }
 private SequenceNode cloneSequenceNode(SequenceNode node, Map<String, String> parameters) {
   List<Node> nodes = new ArrayList<Node>();
   for (Node item : node.getValue()) {
     nodes.add(cloneNode(item, parameters));
   }
   return new SequenceNode(node.getTag(), nodes, node.getFlowStyle());
 }
  private void loopTemplateSequence(
      SequenceNode templateSequence,
      String templateType,
      List<ValidationResult> validationResults) {
    List<Node> prunedTemplates = new ArrayList<Node>();
    for (int j = 0; j < templateSequence.getValue().size(); j++) {
      Node template = templateSequence.getValue().get(j);

      if (template.getNodeId() == scalar) {
        if (!template.getTag().equals(INCLUDE_TAG)) {
          validationResults.add(
              createErrorResult(
                  "Mapping or !include expected",
                  templateSequence.getStartMark(),
                  templateSequence.getEndMark()));
        }
        template = includeResolver.resolve(template, resourceLoader, nodeNandler);
      }
      for (NodeTuple tuple : ((MappingNode) template).getValue()) {
        if (tuple.getKeyNode().getNodeId() != scalar) {
          validationResults.add(createErrorResult(NON_SCALAR_KEY_MESSAGE, tuple.getKeyNode()));
          continue;
        }
        String templateKey = ((ScalarNode) tuple.getKeyNode()).getValue();
        Node templateValue = tuple.getValueNode();
        if (templateValue.getNodeId() != mapping) {
          validationResults.add(
              createErrorResult(
                  "Mapping expected", templateValue.getStartMark(), templateValue.getEndMark()));
          continue;
        }
        if (templateType.equals("resourceTypes")) {

          resourceTypesMap.put(templateKey, (MappingNode) templateValue);
        }
        if (templateType.equals("traits")) {
          traitsMap.put(templateKey, (MappingNode) templateValue);
        }
        prunedTemplates.add(getFakeTemplateNode(tuple.getKeyNode()));
      }
    }
    templateSequence.getValue().clear();
    templateSequence.getValue().addAll(prunedTemplates);
  }
 private void applyTraitsToActions(
     SequenceNode traits, Map<String, Node> actionNodes, String actionName) {
   for (Node ref : traits.getValue()) {
     if (actionName == null) {
       for (Map.Entry<String, Node> actionEntry : actionNodes.entrySet()) {
         currentAction = actionEntry.getKey();
         MappingNode templateNode = cloneTemplate(ref, TemplateType.TRAIT);
         if (templateNode != null) {
           mergeNodes(actionEntry.getValue(), templateNode, Action.class);
         }
       }
     } else {
       currentAction = actionName;
       MappingNode templateNode = cloneTemplate(ref, TemplateType.TRAIT);
       if (templateNode != null) {
         mergeNodes(actionNodes.get(actionName), templateNode, Action.class);
       }
     }
   }
 }
 protected NodeTuple representJavaBeanProperty(
     Object javaBean, Property property, Object propertyValue, Tag customTag) {
   final NodeTuple tuple =
       super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
   final Node valueNode = tuple.getValueNode();
   if (valueNode instanceof CollectionNode) {
     // Removed null check
     if (Tag.SEQ.equals(valueNode.getTag())) {
       final SequenceNode seq = (SequenceNode) valueNode;
       if (seq.getValue().isEmpty()) {
         return null; // skip empty lists
       }
     }
     if (Tag.MAP.equals(valueNode.getTag())) {
       final MappingNode seq = (MappingNode) valueNode;
       if (seq.getValue().isEmpty()) {
         return null; // skip empty maps
       }
     }
   }
   return tuple;
 }
示例#6
0
    @SuppressWarnings("unchecked")
    public Object construct(Node node) {
      SequenceNode snode = (SequenceNode) node;
      if (Set.class.isAssignableFrom(node.getType())) {
        if (node.isTwoStepsConstruction()) {
          throw new YAMLException("Set cannot be recursive.");
        } else {
          return constructSet(snode);
        }
      } else if (Collection.class.isAssignableFrom(node.getType())) {
        if (node.isTwoStepsConstruction()) {
          return createDefaultList(snode.getValue().size());
        } else {
          return constructSequence(snode);
        }
      } else if (node.getType().isArray()) {
        if (node.isTwoStepsConstruction()) {
          return createArray(node.getType(), snode.getValue().size());
        } else {
          return constructArray(snode);
        }
      } else {
        // create immutable object
        List<java.lang.reflect.Constructor<?>> possibleConstructors =
            new ArrayList<java.lang.reflect.Constructor<?>>(snode.getValue().size());
        for (java.lang.reflect.Constructor<?> constructor : node.getType().getConstructors()) {
          if (snode.getValue().size() == constructor.getParameterTypes().length) {
            possibleConstructors.add(constructor);
          }
        }
        if (!possibleConstructors.isEmpty()) {
          if (possibleConstructors.size() == 1) {
            Object[] argumentList = new Object[snode.getValue().size()];
            java.lang.reflect.Constructor<?> c = possibleConstructors.get(0);
            int index = 0;
            for (Node argumentNode : snode.getValue()) {
              Class<?> type = c.getParameterTypes()[index];
              // set runtime classes for arguments
              argumentNode.setType(type);
              argumentList[index++] = constructObject(argumentNode);
            }

            try {
              return c.newInstance(argumentList);
            } catch (Exception e) {
              throw new YAMLException(e);
            }
          }

          // use BaseConstructor
          List<Object> argumentList = (List<Object>) constructSequence(snode);
          Class<?>[] parameterTypes = new Class[argumentList.size()];
          int index = 0;
          for (Object parameter : argumentList) {
            parameterTypes[index] = parameter.getClass();
            index++;
          }

          for (java.lang.reflect.Constructor<?> c : possibleConstructors) {
            Class<?>[] argTypes = c.getParameterTypes();
            boolean foundConstructor = true;
            for (int i = 0; i < argTypes.length; i++) {
              if (!wrapIfPrimitive(argTypes[i]).isAssignableFrom(parameterTypes[i])) {
                foundConstructor = false;
                break;
              }
            }
            if (foundConstructor) {
              try {
                return c.newInstance(argumentList.toArray());
              } catch (Exception e) {
                throw new YAMLException(e);
              }
            }
          }
        }
        throw new YAMLException(
            "No suitable constructor with "
                + String.valueOf(snode.getValue().size())
                + " arguments found for "
                + node.getType());
      }
    }