org.yaml.snakeyaml.reader.StreamReader Java Examples

The following examples show how to use org.yaml.snakeyaml.reader.StreamReader. 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: 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 #2
Source File: Yaml.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
/**
 * Parse all YAML documents in a stream and produce corresponding
 * representation trees.
 * 
 * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
 * @param yaml
 *            stream of YAML documents
 * @return parsed root Nodes for all the specified YAML documents
 */
public Iterable<Node> composeAll(Reader yaml) {
    final Composer composer = new Composer(new ParserImpl(new StreamReader(yaml)), resolver);
    constructor.setComposer(composer);
    Iterator<Node> result = new Iterator<Node>() {
        public boolean hasNext() {
            return composer.checkNode();
        }

        public Node next() {
            return composer.getNode();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new NodeIterable(result);
}
 
Example #3
Source File: Yaml.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
/**
 * Parse all YAML documents in a String and produce corresponding Java
 * objects. The documents are parsed only when the iterator is invoked.
 * 
 * @param yaml
 *            YAML data to load from (BOM must not be present)
 * @return an iterator over the parsed Java objects in this String in proper
 *         sequence
 */
public Iterable<Object> loadAll(Reader yaml) {
    Composer composer = new Composer(new ParserImpl(new StreamReader(yaml)), resolver);
    constructor.setComposer(composer);
    Iterator<Object> result = new Iterator<Object>() {
        public boolean hasNext() {
            return constructor.checkData();
        }

        public Object next() {
            return constructor.getData();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new YamlIterable(result);
}
 
Example #4
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 #5
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 #6
Source File: ParserImplTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testGetEvent2() {
    String data = "american:\n  - Boston Red Sox";
    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.TRUE));
    etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "american",
            dummyMark, dummyMark, (char) 0));
    etalonEvents.add(new SequenceStartEvent(null, null, true, dummyMark, dummyMark,
            Boolean.FALSE));
    etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false),
            "Boston Red Sox", dummyMark, dummyMark, (char) 0));
    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));
    check(etalonEvents, parser);
}
 
Example #7
Source File: SafeRepresenter.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public Node representData(Object data) {
    Tag tag = Tag.STR;
    Character style = null;
    String value = data.toString();
    if (StreamReader.NON_PRINTABLE.matcher(value).find()) {
        tag = Tag.BINARY;
        char[] binary;
        try {
            binary = Base64Coder.encode(value.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new YAMLException(e);
        }
        value = String.valueOf(binary);
        style = '|';
    }
    // if no other scalar style is explicitly set, use literal style for
    // multiline scalars
    if (defaultScalarStyle == null && MULTILINE_PATTERN.matcher(value).find()) {
        style = '|';
    }
    return representScalar(tag, value, style);
}
 
Example #8
Source File: ScannerImplTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testGetToken() {
    String data = "string: abcd";
    StreamReader reader = new StreamReader(data);
    Scanner scanner = new ScannerImpl(reader);
    Mark dummy = new Mark("dummy", 0, 0, 0, "", 0);
    LinkedList<Token> etalonTokens = new LinkedList<Token>();
    etalonTokens.add(new StreamStartToken(dummy, dummy));
    etalonTokens.add(new BlockMappingStartToken(dummy, dummy));
    etalonTokens.add(new KeyToken(dummy, dummy));
    etalonTokens.add(new ScalarToken("string", true, dummy, dummy, (char) 0));
    etalonTokens.add(new ValueToken(dummy, dummy));
    etalonTokens.add(new ScalarToken("abcd", true, dummy, dummy, (char) 0));
    etalonTokens.add(new BlockEndToken(dummy, dummy));
    etalonTokens.add(new StreamEndToken(dummy, dummy));
    while (!etalonTokens.isEmpty() && scanner.checkToken(etalonTokens.get(0).getTokenId())) {
        assertEquals(etalonTokens.removeFirst(), scanner.getToken());
    }
    assertFalse("Must contain no more tokens: " + scanner.getToken(),
            scanner.checkToken(new Token.ID[0]));
}
 
Example #9
Source File: Yaml.java    From orion.server with Eclipse Public License 1.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 #10
Source File: Yaml.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parse all YAML documents in a stream and produce corresponding
 * representation trees.
 * 
 * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
 * @param yaml
 *            stream of YAML documents
 * @return parsed root Nodes for all the specified YAML documents
 */
public Iterable<Node> composeAll(Reader yaml) {
    final Composer composer = new Composer(new ParserImpl(new StreamReader(yaml)), resolver);
    constructor.setComposer(composer);
    Iterator<Node> result = new Iterator<Node>() {
        public boolean hasNext() {
            return composer.checkNode();
        }

        public Node next() {
            return composer.getNode();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new NodeIterable(result);
}
 
Example #11
Source File: Yaml.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parse all YAML documents in a String and produce corresponding Java
 * objects. The documents are parsed only when the iterator is invoked.
 * 
 * @param yaml
 *            YAML data to load from (BOM must not be present)
 * @return an iterator over the parsed Java objects in this String in proper
 *         sequence
 */
public Iterable<Object> loadAll(Reader yaml) {
    Composer composer = new Composer(new ParserImpl(new StreamReader(yaml)), resolver);
    constructor.setComposer(composer);
    Iterator<Object> result = new Iterator<Object>() {
        public boolean hasNext() {
            return constructor.checkData();
        }

        public Object next() {
            return constructor.getData();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new YamlIterable(result);
}
 
Example #12
Source File: PyStructureTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<Object> loadAll(Reader yaml) {
    StreamReader reader = new StreamReader(yaml);
    StringBuilder buffer = new StringBuilder();
    while (reader.peek() != '\0') {
        buffer.append(reader.peek());
        reader.forward();
    }
    CanonicalParser parser = new CanonicalParser(buffer.toString());
    Composer composer = new Composer(parser, resolver);
    this.constructor.setComposer(composer);
    Iterator<Object> result = new Iterator<Object>() {
        public boolean hasNext() {
            return constructor.checkData();
        }

        public Object next() {
            return constructor.getData();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new YamlIterable(result);
}
 
Example #13
Source File: SafeRepresenter.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
public Node representData(Object data) {
    Tag tag = Tag.STR;
    Character style = null;
    String value = data.toString();
    if (StreamReader.NON_PRINTABLE.matcher(value).find()) {
        tag = Tag.BINARY;
        char[] binary;
        try {
            binary = Base64Coder.encode(value.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new YAMLException(e);
        }
        value = String.valueOf(binary);
        style = '|';
    }
    // if no other scalar style is explicitly set, use literal style for
    // multiline scalars
    if (defaultScalarStyle == null && MULTILINE_PATTERN.matcher(value).find()) {
        style = '|';
    }
    return representScalar(tag, value, style);
}
 
Example #14
Source File: Yaml.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parse all YAML documents in a stream and produce corresponding
 * representation trees.
 *
 * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
 * @param yaml
 *            stream of YAML documents
 * @return parsed root Nodes for all the specified YAML documents
 */
public Iterable<Node> composeAll(Reader yaml) {
    final Composer composer = new Composer(new ParserImpl(new StreamReader(yaml)), resolver);
    constructor.setComposer(composer);
    Iterator<Node> result = new Iterator<Node>() {
        public boolean hasNext() {
            return composer.checkNode();
        }

        public Node next() {
            return composer.getNode();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new NodeIterable(result);
}
 
Example #15
Source File: Yaml.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parse all YAML documents in a String and produce corresponding Java
 * objects. The documents are parsed only when the iterator is invoked.
 *
 * @param yaml
 *            YAML data to load from (BOM must not be present)
 * @return an iterator over the parsed Java objects in this String in proper
 *         sequence
 */
public Iterable<Object> loadAll(Reader yaml) {
    Composer composer = new Composer(new ParserImpl(new StreamReader(yaml)), resolver);
    constructor.setComposer(composer);
    Iterator<Object> result = new Iterator<Object>() {
        public boolean hasNext() {
            return constructor.checkData();
        }

        public Object next() {
            return constructor.getData();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new YamlIterable(result);
}
 
Example #16
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 #17
Source File: ScannerImpl.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public ScannerImpl(StreamReader reader) {
    this.reader = reader;
    this.tokens = new ArrayList<Token>(100);
    this.indents = new ArrayStack<Integer>(10);
    // The order in possibleSimpleKeys is kept for nextPossibleSimpleKey()
    this.possibleSimpleKeys = new LinkedHashMap<Integer, SimpleKey>();
    fetchStreamStart();// Add the STREAM-START token.
}
 
Example #18
Source File: ParserImpl.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public ParserImpl(StreamReader reader) {
    this.scanner = new ScannerImpl(reader);
    currentEvent = null;
    directives = new VersionTagsTuple(null, new HashMap<String, String>(DEFAULT_TAGS));
    states = new ArrayStack<Production>(100);
    marks = new ArrayStack<Mark>(10);
    state = new ParseStreamStart();
}
 
Example #19
Source File: ConstructorMappingTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private Object construct(Constructor constructor, String data) {
    StreamReader reader = new StreamReader(data);
    Parser parser = new ParserImpl(reader);
    Resolver resolver = new Resolver();
    Composer composer = new Composer(parser, resolver);
    constructor.setComposer(composer);
    return constructor.getSingleData(Object.class);
}
 
Example #20
Source File: ScannerImpl.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public ScannerImpl(StreamReader reader) {
    this.reader = reader;
    this.tokens = new ArrayList<Token>(100);
    this.indents = new ArrayStack<Integer>(10);
    // The order in possibleSimpleKeys is kept for nextPossibleSimpleKey()
    this.possibleSimpleKeys = new LinkedHashMap<Integer, SimpleKey>();
    fetchStreamStart();// Add the STREAM-START token.
}
 
Example #21
Source File: AbstractRepresent.java    From Diorite with MIT License 5 votes vote down vote up
public final Node representString(String data)
{
    Tag tag = Tag.STR;
    Character style = null;
    if (StreamReader.NON_PRINTABLE.matcher(data).find())
    {
        tag = Tag.BINARY;
        char[] binary;
        try
        {
            binary = Base64Coder.encode(data.getBytes("UTF-8"));
        }
        catch (UnsupportedEncodingException e)
        {
            throw new YAMLException(e);
        }
        data = String.valueOf(binary);
        style = '|';
    }
    // if no other scalar style is explicitly set, use literal style for
    // multiline scalars
    if ((this.getDefaultScalarStyle() == null) && Representer.MULTILINE_PATTERN.matcher(data).find())
    {
        style = '|';
    }
    return this.representer.representScalar(tag, data, style);
}
 
Example #22
Source File: FragmentComposerTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testFragment() {
    String document = "foo:  blargle\n"
            + "developer:  { name: \"Bjarne Stroustrup\", language: \"C++\"}\n"
            + "gee:  [ \"whiz\", \"bang\"]\n";//

    StreamReader reader = new StreamReader(document);
    Composer composer = new FragmentComposer(new ParserImpl(reader), new Resolver(),
            "developer");
    Constructor constructor = new Constructor();
    constructor.setComposer(composer);
    DeveloperBean developer = (DeveloperBean) constructor.getSingleData(DeveloperBean.class);
    assertEquals("Bjarne Stroustrup", developer.name);
    assertEquals("C++", developer.language);
}
 
Example #23
Source File: StreamReaderWithSource.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
public StreamReaderWithSource(YamlSource source, Reader reader) throws IOException {
    super(reader);
    try {
        final Field f = StreamReader.class.getDeclaredField("name");
        f.setAccessible(true);
        f.set(this, source.source());
    } catch (NoSuchFieldException | IllegalAccessException e) {
        // Can't track origin, maybe due to SecurityManager ?
        // never mind
    }
}
 
Example #24
Source File: ConstructorSequenceTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private List<Object> construct(Constructor constructor, String data) {
    StreamReader reader = new StreamReader(data);
    Parser parser = new ParserImpl(reader);
    Resolver resolver = new Resolver();
    Composer composer = new Composer(parser, resolver);
    constructor.setComposer(composer);
    List<Object> result = (List<Object>) constructor.getSingleData(Object.class);
    return result;
}
 
Example #25
Source File: PyEmitterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private List<Event> parse(String data) {
    ParserImpl parser = new ParserImpl(new StreamReader(data));
    List<Event> newEvents = new ArrayList<Event>();
    while (parser.peekEvent() != null) {
        newEvents.add(parser.getEvent());
    }
    return newEvents;
}
 
Example #26
Source File: PyImportTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
protected List<Event> canonicalParse(InputStream input2) throws IOException {
    StreamReader reader = new StreamReader(new UnicodeReader(input2));
    StringBuilder buffer = new StringBuilder();
    while (reader.peek() != '\0') {
        buffer.append(reader.peek());
        reader.forward();
    }
    CanonicalParser parser = new CanonicalParser(buffer.toString());
    List<Event> result = new ArrayList<Event>();
    while (parser.peekEvent() != null) {
        result.add(parser.getEvent());
    }
    input2.close();
    return result;
}
 
Example #27
Source File: PyStructureTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private List<Node> canonical_compose_all(InputStream file) {
    StreamReader reader = new StreamReader(new UnicodeReader(file));
    StringBuilder buffer = new StringBuilder();
    while (reader.peek() != '\0') {
        buffer.append(reader.peek());
        reader.forward();
    }
    CanonicalParser parser = new CanonicalParser(buffer.toString());
    Composer composer = new Composer(parser, new Resolver());
    List<Node> documents = new ArrayList<Node>();
    while (composer.checkNode()) {
        documents.add(composer.getNode());
    }
    return documents;
}
 
Example #28
Source File: PyStructureTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private List<Node> compose_all(InputStream file) {
    Composer composer = new Composer(new ParserImpl(new StreamReader(new UnicodeReader(file))),
            new Resolver());
    List<Node> documents = new ArrayList<Node>();
    while (composer.checkNode()) {
        documents.add(composer.getNode());
    }
    return documents;
}
 
Example #29
Source File: Yaml.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
private Object loadFromReader(StreamReader sreader, Class<?> type) {
    Composer composer = new Composer(new ParserImpl(sreader), resolver);
    constructor.setComposer(composer);
    return constructor.getSingleData(type);
}
 
Example #30
Source File: ParserImpl.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
public ParserImpl(StreamReader reader) {
    this(new ScannerImpl(reader));
}