Package org.yaml.snakeyaml.events

Examples of org.yaml.snakeyaml.events.Event


        } else {
            reader = new StreamReader(new StringReader(target.convertToString().asJavaString()));
        }
        Parser parser = new ParserImpl(reader);
        IRubyObject handler = getInstanceVariable("@handler");
        Event event;

        while (true) {
            try {
                event = parser.getEvent();

                // FIXME: Event should expose a getID, so it can be switched
                if (event.is(ID.StreamStart)) {
                    invoke(
                            context,
                            handler,
                            "start_stream",
                            runtime.newFixnum(YAML_ANY_ENCODING));
                } else if (event.is(ID.DocumentStart)) {
                    DocumentStartEvent dse = (DocumentStartEvent)event;

                    Integer[] versionInts = dse.getVersion();
                    IRubyObject version = versionInts == null ?
                        runtime.getNil() :
                        RubyArray.newArray(runtime, runtime.newFixnum(versionInts[0]), runtime.newFixnum(versionInts[1]));
                   
                    Map<String, String> tagsMap = dse.getTags();
                    RubyArray tags = RubyArray.newArray(runtime);
                    if (tags.size() > 0) {
                        for (Map.Entry<String, String> tag : tagsMap.entrySet()) {
                            tags.append(RubyArray.newArray(
                                    runtime,
                                    RubyString.newString(runtime, tag.getKey()),
                                    RubyString.newString(runtime, tag.getValue())));
                        }
                    }

                    invoke(
                            context,
                            handler,
                            "start_document",
                            version,
                            tags,
                            runtime.newBoolean(dse.getExplicit()));
                } else if (event.is(ID.DocumentEnd)) {
                    DocumentEndEvent dee = (DocumentEndEvent)event;
                    invoke(
                            context,
                            handler,
                            "end_document",
                            runtime.newBoolean(dee.getExplicit()));
                } else if (event.is(ID.Alias)) {
                    AliasEvent ae = (AliasEvent)event;
                    IRubyObject alias = runtime.getNil();
                    if (ae.getAnchor() != null) {
                        alias = RubyString.newString(runtime, ae.getAnchor());
                    }

                    invoke(
                            context,
                            handler,
                            "alias",
                            alias);
                } else if (event.is(ID.Scalar)) {
                    ScalarEvent se = (ScalarEvent)event;
                    IRubyObject anchor = se.getAnchor() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, se.getAnchor());
                    IRubyObject tag = se.getTag() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, se.getTag());
                    IRubyObject plain_implicit = runtime.newBoolean(se.getImplicit().isFirst());
                    IRubyObject quoted_implicit = runtime.newBoolean(se.getImplicit().isSecond());
                    IRubyObject style = runtime.newFixnum(se.getStyle());
                    IRubyObject val = RubyString.newString(runtime, se.getValue());

                    invoke(
                            context,
                            handler,
                            "scalar",
                            val,
                            anchor,
                            tag,
                            plain_implicit,
                            quoted_implicit,
                            style);
                } else if (event.is(ID.SequenceStart)) {
                    SequenceStartEvent sse = (SequenceStartEvent)event;
                    IRubyObject anchor = sse.getAnchor() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, sse.getAnchor());
                    IRubyObject tag = sse.getTag() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, sse.getTag());
                    IRubyObject implicit = runtime.newBoolean(sse.getImplicit());
                    IRubyObject style = runtime.newFixnum(sse.getFlowStyle() ? 1 : 0);

                    invoke(
                            context,
                            handler,
                            "start_sequence",
                            anchor,
                            tag,
                            implicit,
                            style);
                } else if (event.is(ID.SequenceEnd)) {
                    invoke(
                            context,
                            handler,
                            "end_sequence");
                } else if (event.is(ID.MappingStart)) {
                    MappingStartEvent mse = (MappingStartEvent)event;
                    IRubyObject anchor = mse.getAnchor() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, mse.getAnchor());
                    IRubyObject tag = mse.getTag() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, mse.getTag());
                    IRubyObject implicit = runtime.newBoolean(mse.getImplicit());
                    IRubyObject style = runtime.newFixnum(mse.getFlowStyle() ? 1 : 0);

                    invoke(
                            context,
                            handler,
                            "start_mapping",
                            anchor,
                            tag,
                            implicit,
                            style);
                } else if (event.is(ID.MappingEnd)) {
                    invoke(
                            context,
                            handler,
                            "end_mapping");
                } else if (event.is(ID.StreamEnd)) {
                    invoke(
                            context,
                            handler,
                            "end_stream");
                    break;
View Full Code Here


    @JRubyMethod
    public IRubyObject mark(ThreadContext context) {
        Ruby runtime = context.runtime;

        Event event = null;

        if (parser != null) {
            event = parser.peekEvent();

            if (event == null) event = this.event;
        }

        if (event == null) {
            return ((RubyClass)context.runtime.getClassFromPath("Psych::Parser::Mark")).newInstance(
                    context,
                    runtime.newFixnum(0),
                    runtime.newFixnum(0),
                    runtime.newFixnum(0),
                    Block.NULL_BLOCK
            );
        }

        Mark mark = event.getStartMark();

        return ((RubyClass)context.runtime.getClassFromPath("Psych::Parser::Mark")).newInstance(
                context,
                runtime.newFixnum(mark.getIndex()),
                runtime.newFixnum(mark.getLine()),
View Full Code Here

    private void compareEvents(List<Event> events1, List<Event> events2, boolean full) {
        assertEquals(events1.size(), events2.size());
        Iterator<Event> iter1 = events1.iterator();
        Iterator<Event> iter2 = events2.iterator();
        while (iter1.hasNext()) {
            Event event1 = iter1.next();
            Event event2 = iter2.next();
            assertEquals(event1.getClass(), event2.getClass());
            if (event1 instanceof AliasEvent && full) {
                assertEquals(((AliasEvent) event1).getAnchor(), ((AliasEvent) event2).getAnchor());
            }
            if (event1 instanceof CollectionStartEvent) {
                String tag1 = ((CollectionStartEvent) event1).getTag();
View Full Code Here

                yaml.compose(new StringReader(etalon)).toString(), node.toString());
        //
        List<Event> events = yaml.serialize(node);
        int i = 0;
        for (Event etalonEvent : yaml.parse(new StringReader(etalon))) {
            Event ev1 = events.get(i++);
            assertEquals(etalonEvent.getClass(), ev1.getClass());
            if (etalonEvent instanceof ScalarEvent) {
                ScalarEvent scalar1 = (ScalarEvent) etalonEvent;
                ScalarEvent scalar2 = (ScalarEvent) ev1;
                assertEquals(scalar1.getAnchor(), scalar2.getAnchor());
                assertEquals(scalar1.getValue(), scalar2.getValue());
View Full Code Here

                mapping = new HashMap<Object, Object>();
            } else {
                mapping = constructMapping((MappingNode) node);
            }
            String className = node.getTag().getValue().substring(1) + "Event";
            Event value;
            if (className.equals("AliasEvent")) {
                value = new AliasEvent((String) mapping.get("anchor"), null, null);
            } else if (className.equals("ScalarEvent")) {
                String tag = (String) mapping.get("tag");
                String v = (String) mapping.get("value");
View Full Code Here

public class YamlParseTest extends TestCase {

    public void testParse() {
        Yaml yaml = new Yaml();
        Event e = null;
        int counter = 0;
        for (Event event : yaml.parse(new StringReader("abc: 56"))) {
            if (e == null) {
                assertTrue(event instanceof StreamStartEvent);
            }
View Full Code Here

        assertEquals(8, counter);
    }

    public void testParseManyDocuments() {
        Yaml yaml = new Yaml();
        Event e = null;
        int counter = 0;
        for (Event event : yaml.parse(new StringReader("abc: 56\n---\n4\n---\nqwe\n"))) {
            if (e == null) {
                assertTrue(event instanceof StreamStartEvent);
            }
View Full Code Here

                dummyMark, dummyMark, (char) 0));
        etalonEvents.add(new MappingEndEvent(dummyMark, dummyMark));
        etalonEvents.add(new DocumentEndEvent(dummyMark, dummyMark, false));
        etalonEvents.add(new StreamEndEvent(dummyMark, dummyMark));
        while (parser.checkEvent(null)) {
            Event event = parser.getEvent();
            if (etalonEvents.isEmpty()) {
                fail("unexpected event: " + event);
            }
            assertEquals(etalonEvents.removeFirst(), event);
        }
View Full Code Here

        etalonEvents.add(new SequenceEndEvent(dummyMark, dummyMark));
        etalonEvents.add(new MappingEndEvent(dummyMark, dummyMark));
        etalonEvents.add(new DocumentEndEvent(dummyMark, dummyMark, false));
        etalonEvents.add(new StreamEndEvent(dummyMark, dummyMark));
        while (parser.checkEvent(null)) {
            Event event = parser.getEvent();
            if (etalonEvents.isEmpty()) {
                fail("unexpected event: " + event);
            }
            assertEquals(etalonEvents.removeFirst(), event);
        }
View Full Code Here

                String data = stream.toString();
                List<Event> newEvents = new ArrayList<Event>();
                StreamReader reader = new StreamReader(data);
                Parser parser = new ParserImpl(reader);
                while (parser.peekEvent() != null) {
                    Event event = parser.getEvent();
                    newEvents.add(event);
                }
                // check
                assertEquals(events.size(), newEvents.size());
                Iterator<Event> iter1 = events.iterator();
                Iterator<Event> iter2 = newEvents.iterator();
                while (iter1.hasNext()) {
                    Event event = iter1.next();
                    Event newEvent = iter2.next();
                    assertEquals(event.getClass().getName(), newEvent.getClass().getName());
                    if (event instanceof NodeEvent) {
                        NodeEvent e1 = (NodeEvent) event;
                        NodeEvent e2 = (NodeEvent) newEvent;
                        assertEquals(e1.getAnchor(), e2.getAnchor());
                    }
View Full Code Here

TOP

Related Classes of org.yaml.snakeyaml.events.Event

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.