Skip to content

Commit

Permalink
Use SAXException for parse method
Browse files Browse the repository at this point in the history
Signed-off-by: Jarno Elovirta <jarno@elovirta.com>
  • Loading branch information
jelovirt committed Nov 3, 2023
1 parent e0e17ae commit 29c269d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/elovirta/dita/markdown/MarkdownParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.URI;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;

/**
* Interface for Markdown parser that produces SAX events for a DITA document.
Expand All @@ -16,7 +17,7 @@ public interface MarkdownParser {
* @param input Markdown document URI.
* @throws ParseException if parsing failed
*/
void convert(BasedSequence sequence, URI input) throws ParseException;
void convert(BasedSequence sequence, URI input) throws SAXException;

/**
* Set the content event handler.
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/elovirta/dita/markdown/MarkdownParserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.DataSet;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
Expand All @@ -33,15 +34,20 @@ public MarkdownParserImpl(DataSet options) {
}

@Override
public void convert(BasedSequence sequence, URI input) throws ParseException {
public void convert(BasedSequence sequence, URI input) throws SAXException {
final Parser parser = Parser.builder(options).build();
final Document root = parser.parse(sequence);
try {
final Document cleaned = preprocess(root, input);
validate(cleaned);
render(cleaned);
} catch (SAXException e) {
throw new ParseException(e);
} catch (ParseException e) {
final Throwable cause = e.getCause();
if (cause != null && cause instanceof SAXException) {
throw (SAXException) cause;
} else {
throw new SAXException(e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public void parse(final InputSource input) throws IOException, SAXException {
try {
markdownParser.convert(sequence, Optional.ofNullable(input.getSystemId()).map(URI::create).orElse(null));
} catch (ParseException e) {
throw new SAXException(e);
throw new SAXException("Failed to parse: " + e.getMessage(), e);
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/com/elovirta/dita/markdown/ParseException.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package com.elovirta.dita.markdown;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
* Markdown parsing exception. Extends RuntimeException because underlying
* parser framework doesn't allow throwing checked exceptions.
*/
public class ParseException extends RuntimeException {

ParseException() {
super();
}

public ParseException(final String msg) {
super(msg);
super(new SAXException(msg));
}

ParseException(final Throwable cause) {
ParseException(final SAXException cause) {
super(cause);
}

public ParseException(String msg, Throwable cause) {
super(msg, cause);
public ParseException(String msg, Exception cause) {
super(new SAXException(msg, cause));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.elovirta.dita.markdown;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

import com.elovirta.dita.utils.AbstractReaderTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.opentest4j.AssertionFailedError;
import org.xml.sax.SAXException;

public class MDitaReaderCoreTest extends AbstractReaderTest {

Expand Down Expand Up @@ -79,6 +80,11 @@ public void test_unsupported(String file) {
@ParameterizedTest
@ValueSource(strings = { "header.md", "invalid_header.md", "invalid_header_third.md" })
public void test_fail(String file) {
assertThrows(ParseException.class, () -> run(file));
try {
run(file);
fail();
} catch (Exception e) {
assertEquals(SAXException.class, e.getCause().getClass());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.elovirta.dita.markdown;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

import com.elovirta.dita.utils.AbstractReaderTest;
import java.io.IOException;
Expand Down Expand Up @@ -79,7 +79,12 @@ public void test(String file) throws Exception {
@ParameterizedTest
@ValueSource(strings = { "header.md", "invalid_header.md", "invalid_header_third.md" })
public void test_fail(String file) {
assertThrows(ParseException.class, () -> run(file));
try {
run(file);
fail();
} catch (Exception e) {
assertEquals(SAXException.class, e.getCause().getClass());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,12 @@ public void test_schemaParseFailure_withoutErrorHandler() throws Exception {
@ParameterizedTest
@ValueSource(strings = { "invalid_header.md" })
public void test_fail(String file) {
assertThrows(RuntimeException.class, () -> run(file));
try {
run(file);
fail();
} catch (Exception e) {
assertEquals(SAXException.class, e.getCause().getClass());
}
}

@Test
Expand Down

0 comments on commit 29c269d

Please sign in to comment.