Skip to content

Commit

Permalink
Do not throw Metamorph specific exception in DomLoader
Browse files Browse the repository at this point in the history
Changes the `DomLoader` class to throw a generic `MetafactureException`
instead of a `MorphDefException`. The `DomLoader` may not only be used
for loading Metamorph files but also for other xml files. Throwing a
Metamorph specific exception is misleading.
  • Loading branch information
cboehme committed Jul 10, 2016
1 parent 39f094e commit 5043295
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
23 changes: 9 additions & 14 deletions src/main/java/org/culturegraph/mf/util/xml/DomLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.culturegraph.mf.exceptions.MorphDefException;
import org.culturegraph.mf.exceptions.MetafactureException;
import org.culturegraph.mf.util.ResourceUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -66,12 +65,13 @@ private DomLoader() {
// No instances allowed
}

public static Document parse(final String schemaFile, final InputSource inputSource) {
public static Document parse(final String schemaFile,
final InputSource inputSource) {
final URL schemaUrl;
try {
schemaUrl = ResourceUtil.getUrl(schemaFile);
} catch (final MalformedURLException e) {
throw new MorphDefException("'" + schemaFile + "' not found:", e);
throw new MetafactureException("'" + schemaFile + "' not found:", e);
}

try {
Expand Down Expand Up @@ -123,14 +123,9 @@ public static Document parse(final String schemaFile, final InputSource inputSou

return document;

} catch (final ParserConfigurationException e) {
throw new MorphDefException(e);
} catch (final SAXException e) {
throw new MorphDefException(e);
} catch (final TransformerConfigurationException e) {
throw new MorphDefException(e);
} catch (final TransformerException e) {
throw new MorphDefException(e);
} catch (final ParserConfigurationException | TransformerException |
SAXException e) {
throw new MetafactureException(e);
}
}

Expand Down Expand Up @@ -177,7 +172,7 @@ public void error(final SAXParseException exception) throws SAXException {
}

private void handle(final SAXParseException exception) {
throw new MorphDefException("Error parsing xml: " +
throw new MetafactureException("Error parsing xml: " +
exception.getMessage(), exception);
}

Expand Down Expand Up @@ -212,7 +207,7 @@ public void fatalError(final TransformerException exception)
}

private void handle(final TransformerException exception) {
throw new MorphDefException("Error during DOM creation: " +
throw new MetafactureException("Error during DOM creation: " +
exception.getMessage(), exception);
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/culturegraph/mf/util/xml/DomLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.net.MalformedURLException;
import java.net.URL;

import org.culturegraph.mf.exceptions.MorphDefException;
import org.culturegraph.mf.exceptions.MetafactureException;
import org.culturegraph.mf.util.ResourceUtil;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void shouldCreateDOM() throws FileNotFoundException, MalformedURLExceptio
assertEquals("test-schema", rootNode.getNodeName());
}

@Test(expected=MorphDefException.class)
@Test(expected=MetafactureException.class)
public void shouldValidateInputAgainstSchema()
throws FileNotFoundException, MalformedURLException {

Expand Down

0 comments on commit 5043295

Please sign in to comment.