Example #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;
 }
Example #2
0
  private Node composeSequenceNode(String anchor) {
    SequenceStartEvent startEvent = (SequenceStartEvent) this.parser.getEvent();
    String tag = startEvent.getTag();

    boolean resolved = false;
    Tag nodeTag;
    if ((tag == null) || (tag.equals("!"))) {
      Tag nodeTag = this.resolver.resolve(NodeId.sequence, null, startEvent.getImplicit());
      resolved = true;
    } else {
      nodeTag = new Tag(tag);
    }
    ArrayList<Node> children = new ArrayList();
    SequenceNode node =
        new SequenceNode(
            nodeTag,
            resolved,
            children,
            startEvent.getStartMark(),
            null,
            startEvent.getFlowStyle());
    if (anchor != null) {
      this.anchors.put(anchor, node);
    }
    while (!this.parser.checkEvent(Event.ID.SequenceEnd)) {
      children.add(composeNode(node));
    }
    Event endEvent = this.parser.getEvent();
    node.setEndMark(endEvent.getEndMark());
    return node;
  }
 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);
  }
 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;
 }
 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);
       }
     }
   }
 }
Example #7
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());
      }
    }
Example #8
0
 protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
   flattenMapping(node);
   Class<? extends Object> beanType = node.getType();
   List<NodeTuple> nodeValue = node.getValue();
   for (NodeTuple tuple : nodeValue) {
     ScalarNode keyNode;
     if (tuple.getKeyNode() instanceof ScalarNode) {
       // key must be scalar
       keyNode = (ScalarNode) tuple.getKeyNode();
     } else {
       throw new YAMLException("Keys must be scalars but found: " + tuple.getKeyNode());
     }
     Node valueNode = tuple.getValueNode();
     // keys can only be Strings
     keyNode.setType(String.class);
     String key = (String) constructObject(keyNode);
     try {
       Property property = getProperty(beanType, key);
       valueNode.setType(property.getType());
       TypeDescription memberDescription = typeDefinitions.get(beanType);
       boolean typeDetected = false;
       if (memberDescription != null) {
         switch (valueNode.getNodeId()) {
           case sequence:
             SequenceNode snode = (SequenceNode) valueNode;
             Class<? extends Object> memberType = memberDescription.getListPropertyType(key);
             if (memberType != null) {
               snode.setListType(memberType);
               typeDetected = true;
             } else if (property.getType().isArray()) {
               snode.setListType(property.getType().getComponentType());
               typeDetected = true;
             }
             break;
           case mapping:
             MappingNode mnode = (MappingNode) valueNode;
             Class<? extends Object> keyType = memberDescription.getMapKeyType(key);
             if (keyType != null) {
               mnode.setTypes(keyType, memberDescription.getMapValueType(key));
               typeDetected = true;
             }
             break;
           default: // scalar
         }
       }
       if (!typeDetected && valueNode.getNodeId() != NodeId.scalar) {
         // only if there is no explicit TypeDescription
         Class<?>[] arguments = property.getActualTypeArguments();
         if (arguments != null) {
           // type safe (generic) collection may contain the
           // proper class
           if (valueNode.getNodeId() == NodeId.sequence) {
             Class<?> t = arguments[0];
             SequenceNode snode = (SequenceNode) valueNode;
             snode.setListType(t);
           } else if (valueNode.getTag().equals(Tag.SET)) {
             Class<?> t = arguments[0];
             MappingNode mnode = (MappingNode) valueNode;
             mnode.setOnlyKeyType(t);
             mnode.setUseClassConstructor(true);
           } else if (property.getType().isAssignableFrom(Map.class)) {
             Class<?> ketType = arguments[0];
             Class<?> valueType = arguments[1];
             MappingNode mnode = (MappingNode) valueNode;
             mnode.setTypes(ketType, valueType);
             mnode.setUseClassConstructor(true);
           } else {
             // the type for collection entries cannot be
             // detected
           }
         }
       }
       Object value = constructObject(valueNode);
       property.set(object, value);
     } catch (Exception e) {
       throw new YAMLException(
           "Cannot create property=" + key + " for JavaBean=" + object + "; " + e.getMessage(),
           e);
     }
   }
   return object;
 }