diff --git a/commonmark/src/main/java/org/commonmark/parser/Parser.java b/commonmark/src/main/java/org/commonmark/parser/Parser.java index 8faac789b..2cac1c5a3 100644 --- a/commonmark/src/main/java/org/commonmark/parser/Parser.java +++ b/commonmark/src/main/java/org/commonmark/parser/Parser.java @@ -66,6 +66,10 @@ public static Builder builder() { return new Builder(); } + private Node processParsedDocument(Node document) { + return postProcess(document); + } + /** * Parse the specified input text into a tree of nodes. *
@@ -74,11 +78,12 @@ public static Builder builder() { * @param input the text to parse - must not be null * @return the root node */ + public Node parse(String input) { - Objects.requireNonNull(input, "input must not be null"); - DocumentParser documentParser = createDocumentParser(); - Node document = documentParser.parse(input); - return postProcess(document); + Objects.requireNonNull(input); + DocumentParser documentParser = createDocumentParser(); + + return processParsedDocument(documentParser.parse(input)); } /** @@ -100,10 +105,10 @@ public Node parse(String input) { * @throws IOException when reading throws an exception */ public Node parseReader(Reader input) throws IOException { - Objects.requireNonNull(input, "input must not be null"); + Objects.requireNonNull(input); DocumentParser documentParser = createDocumentParser(); - Node document = documentParser.parse(input); - return postProcess(document); + return processParsedDocument(documentParser.parse(input)); + } private DocumentParser createDocumentParser() { @@ -147,13 +152,18 @@ public Parser build() { public Builder extensions(Iterable extends Extension> extensions) { Objects.requireNonNull(extensions, "extensions must not be null"); for (Extension extension : extensions) { + applyExtension(extension); + } + return this; + } + private void applyExtension(Extension extension){ if (extension instanceof ParserExtension) { ParserExtension parserExtension = (ParserExtension) extension; parserExtension.extend(this); } } - return this; - } + + /** * Describe the list of markdown features the parser will recognize and parse. @@ -217,12 +227,15 @@ public Builder includeSourceSpans(IncludeSourceSpans includeSourceSpans) { * @return {@code this} */ public Builder maxOpenBlockParsers(int maxOpenBlockParsers) { - if (maxOpenBlockParsers < 0) { - throw new IllegalArgumentException("maxOpenBlockParsers must be >= 0"); - } + validateMaxOpenBlockParsers(maxOpenBlockParsers); this.maxOpenBlockParsers = maxOpenBlockParsers; return this; } + private void validateMaxOpenBlockParsers(int value){ + if (value < 0) { + throw new IllegalArgumentException("maxOpenBlockParsers must be >= 0"); + } + } /** * Add a custom block parser factory. @@ -335,11 +348,10 @@ public Builder inlineParserFactory(InlineParserFactory inlineParserFactory) { } private InlineParserFactory getInlineParserFactory() { - if (inlineParserFactory != null) { - return inlineParserFactory; - } else { - return InlineParserImpl::new; - } + return inlineParserFactory != null + ? inlineParserFactory : + InlineParserImpl::new; + } }