Package org.yaml.snakeyaml.nodes

Examples of org.yaml.snakeyaml.nodes.ScalarNode


        }

        @Override
        public Node resolve(Node valueNode, ResourceLoader resourceLoader, NodeHandler nodeHandler)
        {
            return new ScalarNode(Tag.STR, "custom tag resolved", valueNode.getStartMark(), valueNode.getEndMark(), ((ScalarNode) valueNode).getStyle());
        }
View Full Code Here


        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);
View Full Code Here

     * Construct scalar instance when the runtime class is known. Recursive
     * structures are not supported.
     */
    protected class ConstructScalar extends AbstractConstruct {
        public Object construct(Node nnode) {
            ScalarNode node = (ScalarNode) nnode;
            Class<?> type = node.getType();
            Object result;
            if (type.isPrimitive() || type == String.class || Number.class.isAssignableFrom(type)
                    || type == Boolean.class || Date.class.isAssignableFrom(type)
                    || type == Character.class || type == BigInteger.class
                    || type == BigDecimal.class || Enum.class.isAssignableFrom(type)
                    || Tag.BINARY.equals(node.getTag()) || Calendar.class.isAssignableFrom(type)) {
                // standard classes created directly
                result = constructStandardJavaInstance(type, node);
            } else {
                // there must be only 1 constructor with 1 argument
                java.lang.reflect.Constructor<?>[] javaConstructors = type.getConstructors();
                int oneArgCount = 0;
                java.lang.reflect.Constructor<?> javaConstructor = null;
                for (java.lang.reflect.Constructor<?> c : javaConstructors) {
                    if (c.getParameterTypes().length == 1) {
                        oneArgCount++;
                        javaConstructor = c;
                    }
                }
                Object argument;
                if (javaConstructor == null) {
                    throw new YAMLException("No single argument constructor found for " + type);
                } else if (oneArgCount == 1) {
                    argument = constructStandardJavaInstance(
                            javaConstructor.getParameterTypes()[0], node);
                } else {
                    // TODO it should be possible to use implicit types instead
                    // of forcing String. Resolver must be available here to
                    // obtain the implicit tag. Then we can set the tag and call
                    // callConstructor(node) to create the argument instance.
                    // On the other hand it may be safer to require a custom
                    // constructor to avoid guessing the argument class
                    argument = constructScalar(node);
                    try {
                        javaConstructor = type.getConstructor(String.class);
                    } catch (Exception e) {
                        throw new YAMLException("Can't construct a java object for scalar "
                                + node.getTag() + "; No String constructor found. Exception="
                                + e.getMessage(), e);
                    }
                }
                try {
                    result = javaConstructor.newInstance(argument);
                } catch (Exception e) {
                    throw new ConstructorException(null, null,
                            "Can't construct a java object for scalar " + node.getTag()
                                    + "; exception=" + e.getMessage(), node.getStartMark(), e);
                }
            }
            return result;
        }
View Full Code Here

            this.emitter.emit(new AliasEvent(tAlias, null, null));
        } else {
            this.serializedNodes.add(node);
            switch (node.getNodeId()) {
            case scalar:
                ScalarNode scalarNode = (ScalarNode) node;
                Tag detectedTag = this.resolver.resolve(NodeId.scalar, scalarNode.getValue(), true);
                Tag defaultTag = this.resolver.resolve(NodeId.scalar, scalarNode.getValue(), false);
                ImplicitTuple tuple = new ImplicitTuple(node.getTag().equals(detectedTag), node
                        .getTag().equals(defaultTag));
                ScalarEvent event = new ScalarEvent(tAlias, node.getTag().getValue(), tuple,
                        scalarNode.getValue(), null, null, scalarNode.getStyle());
                this.emitter.emit(event);
                break;
            case sequence:
                SequenceNode seqNode = (SequenceNode) node;
                boolean implicitS = (node.getTag().equals(this.resolver.resolve(NodeId.sequence,
View Full Code Here

     * @return NodeTuple to be used in a MappingNode. Return null to skip the
     *         property
     */
    protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
            Object propertyValue, Tag customTag) {
        ScalarNode nodeKey = (ScalarNode) representData(property.getName());
        // the first occurrence of the node must keep the tag
        boolean hasAlias = this.representedObjects.containsKey(propertyValue);

        Node nodeValue = representData(propertyValue);

View Full Code Here

                    .canOmitTagInPlainScalar());
            resolved = true;
        } else {
            nodeTag = new Tag(tag);
        }
        Node node = new ScalarNode(nodeTag, resolved, ev.getValue(), ev.getStartMark(),
                ev.getEndMark(), ev.getStyle());
        if (anchor != null) {
            anchors.put(anchor, node);
        }
        return node;
View Full Code Here

        public Calendar getCalendar() {
            return calendar;
        }

        public Object construct(Node node) {
            ScalarNode scalar = (ScalarNode) node;
            String nodeValue = scalar.getValue();
            Matcher match = YMD_REGEXP.matcher(nodeValue);
            if (match.matches()) {
                String year_s = match.group(1);
                String month_s = match.group(2);
                String day_s = match.group(3);
View Full Code Here

            List<NodeTuple> list = mnode.getValue();
            if (list.size() == 1) {
                NodeTuple tuple = list.get(0);
                Node key = tuple.getKeyNode();
                if (key instanceof ScalarNode) {
                    ScalarNode scalar = (ScalarNode) key;
                    if (GUESS_COMPACT.matcher(scalar.getValue()).matches()) {
                        return getCompactConstruct();
                    }
                }
            }
        } else if (node instanceof ScalarNode) {
            ScalarNode scalar = (ScalarNode) node;
            if (GUESS_COMPACT.matcher(scalar.getValue()).matches()) {
                return getCompactConstruct();
            }
        }
        return super.getConstructor(node);
    }
View Full Code Here

        /*
         * MappingNode and ScalarNode end up here only they assumed to be a
         * compact object's representation (@see getConstructor(Node) above)
         */
        public Object construct(Node node) {
            ScalarNode tmpNode = null;
            if (node instanceof MappingNode) {
                // Compact Object Notation may contain only one entry
                MappingNode mnode = (MappingNode) node;
                NodeTuple nodeTuple = mnode.getValue().iterator().next();
                node.setTwoStepsConstruction(true);
                tmpNode = (ScalarNode) nodeTuple.getKeyNode();
                // return constructScalar((ScalarNode) keyNode);
            } else {
                tmpNode = (ScalarNode) node;
            }

            CompactData data = getCompactData(tmpNode.getValue());
            if (data == null) { // TODO: Should we throw an exception here ?
                return constructScalar(tmpNode);
            }
            return constructCompactFormat(tmpNode, data);
        }
View Full Code Here

        MappingNode node = new MappingNode( tag, value, null );
        representedObjects.put( objectToRepresent, node );
        boolean bestStyle = true;
        for ( Property property : properties )
        {
            ScalarNode nodeKey = (ScalarNode) representData( property.getName() );
            Object memberValue = property.get( javaBean );
            boolean hasAlias = false;
            if ( representedObjects.containsKey( memberValue ) )
            {
                // the first occurrence of the node must keep the tag
                hasAlias = true;
            }
            Node nodeValue = representData( memberValue );

            // TODO: The impl seems not to allow to skip certain values
            if ( nodeValue == null )
            {
                continue;
            }

            // if possible try to avoid a global tag with a class name
            if ( nodeValue instanceof MappingNode && !hasAlias )
            {
                // the node is a map, set or JavaBean
                if ( !Map.class.isAssignableFrom( memberValue.getClass() ) )
                {
                    // the node is set or JavaBean
                    if ( property.getType() == memberValue.getClass() )
                    {
                        // we do not need global tag because the property
                        // Class is the same as the runtime class
                        nodeValue.setTag( Tags.MAP );
                    }
                }
            }
            else if ( memberValue != null && Enum.class.isAssignableFrom( memberValue.getClass() ) )
            {
                nodeValue.setTag( Tags.STR );
            }
            if ( nodeKey.getStyle() != null )
            {
                bestStyle = false;
            }
            if ( !( ( nodeValue instanceof ScalarNode && ( (ScalarNode) nodeValue ).getStyle() == null ) ) )
            {
View Full Code Here

TOP

Related Classes of org.yaml.snakeyaml.nodes.ScalarNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.