org.yaml.snakeyaml.events.Event Java Examples

The following examples show how to use org.yaml.snakeyaml.events.Event. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParserImpl.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public Event produce() {
    // Parse an implicit document.
    if (!scanner.checkToken(Token.ID.Directive, Token.ID.DocumentStart, Token.ID.StreamEnd)) {
        directives = new VersionTagsTuple(null, DEFAULT_TAGS);
        Token token = scanner.peekToken();
        Mark startMark = token.getStartMark();
        Mark endMark = startMark;
        Event event = new DocumentStartEvent(startMark, endMark, false, null, null);
        // Prepare the next state.
        states.push(new ParseDocumentEnd());
        state = new ParseBlockNode();
        return event;
    } else {
        Production p = new ParseDocumentStart();
        return p.produce();
    }
}
 
Example #2
Source File: PyStructureTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testParser() {
    File[] files = getStreamsByExtension(".data", true);
    assertTrue("No test files found.", files.length > 0);
    for (File file : files) {
        if (!file.getName().contains("scan-line-b")) {
            continue;
        }
        try {
            InputStream input = new FileInputStream(file);
            List<Event> events1 = parse(input);
            input.close();
            assertFalse(events1.isEmpty());
            int index = file.getAbsolutePath().lastIndexOf('.');
            String canonicalName = file.getAbsolutePath().substring(0, index) + ".canonical";
            File canonical = new File(canonicalName);
            List<Event> events2 = canonicalParse(new FileInputStream(canonical));
            assertFalse(events2.isEmpty());
            compareEvents(events1, events2, false);
        } catch (Exception e) {
            System.out.println("Failed File: " + file);
            // fail("Failed File: " + file + "; " + e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
 
Example #3
Source File: ParserImplTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testGetEvent() {
    String data = "string: abcd";
    StreamReader reader = new StreamReader(data);
    Parser parser = new ParserImpl(reader);
    Mark dummyMark = new Mark("dummy", 0, 0, 0, "", 0);
    LinkedList<Event> etalonEvents = new LinkedList<Event>();
    etalonEvents.add(new StreamStartEvent(dummyMark, dummyMark));
    etalonEvents.add(new DocumentStartEvent(dummyMark, dummyMark, false, null, null));
    etalonEvents.add(new MappingStartEvent(null, null, true, dummyMark, dummyMark,
            Boolean.FALSE));
    etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "string",
            dummyMark, dummyMark, (char) 0));
    etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "abcd",
            dummyMark, dummyMark, (char) 0));
    etalonEvents.add(new MappingEndEvent(dummyMark, dummyMark));
    etalonEvents.add(new DocumentEndEvent(dummyMark, dummyMark, false));
    etalonEvents.add(new StreamEndEvent(dummyMark, dummyMark));
    check(etalonEvents, parser);
}
 
Example #4
Source File: ParserImpl.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public Event produce() {
    // Parse the document end.
    Token token = scanner.peekToken();
    Mark startMark = token.getStartMark();
    Mark endMark = startMark;
    boolean explicit = false;
    if (scanner.checkToken(Token.ID.DocumentEnd)) {
        token = scanner.getToken();
        endMark = token.getEndMark();
        explicit = true;
    }
    Event event = new DocumentEndEvent(startMark, endMark, explicit);
    // Prepare the next state.
    state = new ParseDocumentStart();
    return event;
}
 
Example #5
Source File: Emitter.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
private boolean needEvents(int count) {
    int level = 0;
    Iterator<Event> iter = events.iterator();
    iter.next();
    while (iter.hasNext()) {
        Event event = iter.next();
        if (event instanceof DocumentStartEvent || event instanceof CollectionStartEvent) {
            level++;
        } else if (event instanceof DocumentEndEvent || event instanceof CollectionEndEvent) {
            level--;
        } else if (event instanceof StreamEndEvent) {
            level = -1;
        }
        if (level < 0) {
            return false;
        }
    }
    return events.size() < count + 1;
}
 
Example #6
Source File: Yaml.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parse a YAML stream and produce parsing events.
 *
 * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
 * @param yaml
 *            YAML document(s)
 * @return parsed events
 */
public Iterable<Event> parse(Reader yaml) {
    final Parser parser = new ParserImpl(new StreamReader(yaml));
    Iterator<Event> result = new Iterator<Event>() {
        public boolean hasNext() {
            return parser.peekEvent() != null;
        }

        public Event next() {
            return parser.getEvent();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new EventIterable(result);
}
 
Example #7
Source File: Yaml.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
/**
 * Parse a YAML stream and produce parsing events.
 * 
 * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
 * @param yaml
 *            YAML document(s)
 * @return parsed events
 */
public Iterable<Event> parse(Reader yaml) {
    final Parser parser = new ParserImpl(new StreamReader(yaml));
    Iterator<Event> result = new Iterator<Event>() {
        public boolean hasNext() {
            return parser.peekEvent() != null;
        }

        public Event next() {
            return parser.getEvent();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new EventIterable(result);
}
 
Example #8
Source File: Composer.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Reads a document from a source that contains only one document.
 * <p>
 * If the stream contains more than one document an exception is thrown.
 * </p>
 * 
 * @return The root node of the document or <code>null</code> if no document
 *         is available.
 */
public Node getSingleNode() {
    // Drop the STREAM-START event.
    parser.getEvent();
    // Compose a document if the stream is not empty.
    Node document = null;
    if (!parser.checkEvent(Event.ID.StreamEnd)) {
        document = composeDocument();
    }
    // Ensure that the stream contains no more documents.
    if (!parser.checkEvent(Event.ID.StreamEnd)) {
        Event event = parser.getEvent();
        throw new ComposerException("expected a single document in the stream",
                document.getStartMark(), "but found another document", event.getStartMark());
    }
    // Drop the STREAM-END event.
    parser.getEvent();
    return document;
}
 
Example #9
Source File: Composer.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a document from a source that contains only one document.
 * <p>
 * If the stream contains more than one document an exception is thrown.
 * </p>
 * 
 * @return The root node of the document or <code>null</code> if no document
 *         is available.
 */
public Node getSingleNode() {
    // Drop the STREAM-START event.
    parser.getEvent();
    // Compose a document if the stream is not empty.
    Node document = null;
    if (!parser.checkEvent(Event.ID.StreamEnd)) {
        document = composeDocument();
    }
    // Ensure that the stream contains no more documents.
    if (!parser.checkEvent(Event.ID.StreamEnd)) {
        Event event = parser.getEvent();
        throw new ComposerException("expected a single document in the stream",
                document.getStartMark(), "but found another document", event.getStartMark());
    }
    // Drop the STREAM-END event.
    parser.getEvent();
    return document;
}
 
Example #10
Source File: Yaml.java    From Diorite with MIT License 6 votes vote down vote up
/**
 * Serialize the representation tree into Events.
 *
 * @param data
 *         representation tree
 *
 * @return Event list
 *
 * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
 */
public List<Event> serialize(Node data)
{
    YamlSilentEmitter emitter = new YamlSilentEmitter();
    Serializer serializer = new Serializer(this.serialization, emitter, this.resolver, this.dumperOptions, null);
    try
    {
        serializer.open();
        serializer.serialize(data);
        serializer.close();
    }
    catch (IOException e)
    {
        throw new YAMLException(e);
    }
    return emitter.getEvents();
}
 
Example #11
Source File: Emitter.java    From Diorite with MIT License 5 votes vote down vote up
private boolean needEvents(int count)
{
    int level = 0;
    Iterator<Event> iter = this.events.iterator();
    iter.next();
    while (iter.hasNext())
    {
        Event event = iter.next();
        if ((event instanceof DocumentStartEvent) || (event instanceof CollectionStartEvent))
        {
            level++;
        }
        else if ((event instanceof DocumentEndEvent) || (event instanceof CollectionEndEvent))
        {
            level--;
        }
        else if (event instanceof StreamEndEvent)
        {
            level = - 1;
        }
        if (level < 0)
        {
            return false;
        }
    }
    return this.events.size() < (count + 1);
}
 
Example #12
Source File: ParserImpl.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Event produce() {
    Event event;
    if (scanner.checkToken(Token.ID.Directive, Token.ID.DocumentStart,
            Token.ID.DocumentEnd, Token.ID.StreamEnd)) {
        event = processEmptyScalar(scanner.peekToken().getStartMark());
        state = states.pop();
        return event;
    } else {
        Production p = new ParseBlockNode();
        return p.produce();
    }
}
 
Example #13
Source File: Emitter.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private boolean needMoreEvents() {
    if (events.isEmpty()) {
        return true;
    }
    Event event = events.peek();
    if (event instanceof DocumentStartEvent) {
        return needEvents(1);
    } else if (event instanceof SequenceStartEvent) {
        return needEvents(2);
    } else if (event instanceof MappingStartEvent) {
        return needEvents(3);
    } else {
        return false;
    }
}
 
Example #14
Source File: CanonicalParser.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
/**
 * Get the next event.
 */
public Event peekEvent() {
    if (!parsed) {
        parse();
    }
    if (events.isEmpty()) {
        return null;
    } else {
        return events.get(0);
    }
}
 
Example #15
Source File: PyImportTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
protected List<Event> parse(InputStream input) throws IOException {
    StreamReader reader = new StreamReader(new UnicodeReader(input));
    Parser parser = new ParserImpl(reader);
    List<Event> result = new ArrayList<Event>();
    while (parser.peekEvent() != null) {
        result.add(parser.getEvent());
    }
    input.close();
    return result;
}
 
Example #16
Source File: CanonicalParser.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
/**
 * Check the type of the next event.
 */
public boolean checkEvent(Event.ID choice) {
    if (!parsed) {
        parse();
    }
    if (!events.isEmpty()) {
        if (events.get(0).is(choice)) {
            return true;
        }
    }
    return false;
}
 
Example #17
Source File: ParserImpl.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public Event produce() {
    Token token = scanner.getToken();
    if (!scanner.checkToken(Token.ID.Value, Token.ID.FlowEntry, Token.ID.FlowSequenceEnd)) {
        states.push(new ParseFlowSequenceEntryMappingValue());
        return parseFlowNode();
    } else {
        state = new ParseFlowSequenceEntryMappingValue();
        return processEmptyScalar(token.getEndMark());
    }
}
 
Example #18
Source File: PyEmitterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private String emit(List<Event> events) throws IOException {
    StringWriter writer = new StringWriter();
    Emitter emitter = new Emitter(writer, new DumperOptions());
    for (Event event : events) {
        emitter.emit(event);
    }
    return writer.toString();
}
 
Example #19
Source File: PyCanonicalTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testCanonicalParser() throws IOException {
    File[] files = getStreamsByExtension(".canonical");
    assertTrue("No test files found.", files.length > 0);
    for (int i = 0; i < files.length; i++) {
        InputStream input = new FileInputStream(files[i]);
        List<Event> tokens = canonicalParse(input);
        input.close();
        assertFalse(tokens.isEmpty());
    }
}
 
Example #20
Source File: ParserImpl.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
/**
 * Get the next event and proceed further.
 */
public Event getEvent() {
    peekEvent();
    Event value = currentEvent;
    currentEvent = null;
    return value;
}
 
Example #21
Source File: ParserImpl.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Event produce() {
    // Parse the stream start.
    StreamStartToken token = (StreamStartToken) scanner.getToken();
    Event event = new StreamStartEvent(token.getStartMark(), token.getEndMark());
    // Prepare the next state.
    state = new ParseImplicitDocumentStart();
    return event;
}
 
Example #22
Source File: ParserImpl.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Check the type of the next event.
 */
public boolean checkEvent(Event.ID choices) {
    peekEvent();
    if (currentEvent != null) {
        if (currentEvent.is(choices)) {
            return true;
        }
    }
    return false;
}
 
Example #23
Source File: Emitter.java    From Diorite with MIT License 5 votes vote down vote up
@Override
public void emit(Event event) throws IOException
{
    this.events.add(event);
    while (! this.needMoreEvents())
    {
        this.event = this.events.poll();
        this.state.expect(this);
        this.event = null;
    }
}
 
Example #24
Source File: ParserImpl.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Event produce() {
    Token token = scanner.getToken();
    if (!scanner.checkToken(Token.ID.Value, Token.ID.FlowEntry, Token.ID.FlowSequenceEnd)) {
        states.push(new ParseFlowSequenceEntryMappingValue());
        return parseFlowNode();
    } else {
        state = new ParseFlowSequenceEntryMappingValue();
        return processEmptyScalar(token.getEndMark());
    }
}
 
Example #25
Source File: Yaml.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Serialize the representation tree into Events.
 *
 * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
 * @param data
 *            representation tree
 * @return Event list
 */
public List<Event> serialize(Node data) {
    SilentEmitter emitter = new SilentEmitter();
    Serializer serializer = new Serializer(emitter, resolver, dumperOptions, null);
    try {
        serializer.open();
        serializer.serialize(data);
        serializer.close();
    } catch (IOException e) {
        throw new YAMLException(e);
    }
    return emitter.getEvents();
}
 
Example #26
Source File: Yaml.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize the representation tree into Events.
 * 
 * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
 * @param data
 *            representation tree
 * @return Event list
 */
public List<Event> serialize(Node data) {
    SilentEmitter emitter = new SilentEmitter();
    Serializer serializer = new Serializer(emitter, resolver, dumperOptions, null);
    try {
        serializer.open();
        serializer.serialize(data);
        serializer.close();
    } catch (IOException e) {
        throw new YAMLException(e);
    }
    return emitter.getEvents();
}
 
Example #27
Source File: Emitter.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void emit(Event event) throws IOException {
    this.events.add(event);
    while (!needMoreEvents()) {
        this.event = this.events.poll();
        this.state.expect();
        this.event = null;
    }
}
 
Example #28
Source File: ParserImplTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private void check(LinkedList<Event> etalonEvents, Parser parser) {
    while (parser.checkEvent(null)) {
        Event event = parser.getEvent();
        if (etalonEvents.isEmpty()) {
            fail("unexpected event: " + event);
        }
        assertEquals(etalonEvents.removeFirst(), event);
    }
    assertFalse("Must contain no more events: " + parser.getEvent(), parser.checkEvent(null));
}
 
Example #29
Source File: LowLevelApiTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testLowLevel() {
    List<Object> list = new ArrayList<Object>();
    list.add(1);
    list.add("abc");
    Map<String, String> map = new HashMap<String, String>();
    map.put("name", "Tolstoy");
    map.put("book", "War and People");
    list.add(map);
    Yaml yaml = new Yaml();
    String etalon = yaml.dump(list);
    // System.out.println(etalon);
    //
    Node node = yaml.represent(list);
    // System.out.println(node);
    assertEquals(
            "Representation tree from an object and from its YAML document must be the same.",
            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());
        }
    }
    assertEquals(i, events.size());
}
 
Example #30
Source File: ScalarEventTagTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testLoad() {
    Yaml yaml = new Yaml();
    Iterable<Event> parsed = yaml.parse(new StringReader("5"));
    List<Event> events = new ArrayList<Event>(5);
    for (Event event : parsed) {
        events.add(event);
        // System.out.println(event);
    }
    String tag = ((ScalarEvent) events.get(2)).getTag();
    assertNull("The tag should not be specified: " + tag, tag);
}