guru.nidi.graphviz.parse.Parser Maven / Gradle / Ivy
/*
* Copyright © 2015 Stefan Niederhauser ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package guru.nidi.graphviz.parse;
import guru.nidi.graphviz.attribute.validate.*;
import guru.nidi.graphviz.model.MutableGraph;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;
import static guru.nidi.graphviz.attribute.validate.ValidatorEngine.UNKNOWN_ENGINE;
import static guru.nidi.graphviz.attribute.validate.ValidatorFormat.UNKNOWN_FORMAT;
import static guru.nidi.graphviz.attribute.validate.ValidatorMessage.LINE_LOGGING_CONSUMER;
public final class Parser {
private final ValidatorEngine engine;
private final ValidatorFormat format;
private final Consumer messageConsumer;
public Parser() {
this(UNKNOWN_ENGINE, UNKNOWN_FORMAT, LINE_LOGGING_CONSUMER);
}
private Parser(ValidatorEngine engine, ValidatorFormat format, Consumer messageConsumer) {
this.engine = engine;
this.format = format;
this.messageConsumer = messageConsumer;
}
public Parser forEngine(ValidatorEngine engine) {
return new Parser(engine, format, messageConsumer);
}
public Parser forFormat(ValidatorFormat format) {
return new Parser(engine, format, messageConsumer);
}
public Parser messageConsumer(Consumer messageConsumer) {
return new Parser(engine, format, messageConsumer);
}
public MutableGraph read(File file) throws IOException {
return read(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8), file.getName());
}
public MutableGraph read(InputStream is) throws IOException {
return read(new InputStreamReader(is, StandardCharsets.UTF_8), "");
}
public MutableGraph read(String dot) throws IOException {
return read(new StringReader(dot), "");
}
public MutableGraph read(Reader dot, String name) throws IOException {
final AttributeValidator validator = new AttributeValidator().forEngine(engine).forFormat(format);
return new ParserImpl(new Lexer(dot, name), validator, messageConsumer).parse();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy