Skip to content

Commit 59af3e5

Browse files
committed
Simplify exception handling in Ssurgeon
1 parent ddb0c90 commit 59af3e5

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/Ssurgeon.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
import javax.xml.parsers.DocumentBuilder;
1010
import javax.xml.parsers.DocumentBuilderFactory;
11+
import javax.xml.parsers.ParserConfigurationException;
1112
import javax.xml.transform.OutputKeys;
1213
import javax.xml.transform.Transformer;
1314
import javax.xml.transform.TransformerFactory;
1415
import javax.xml.transform.dom.DOMSource;
1516
import javax.xml.transform.stream.StreamResult;
1617

18+
import org.xml.sax.SAXException;
19+
1720
import edu.stanford.nlp.util.StringUtils;
1821
import edu.stanford.nlp.util.logging.RedwoodConfiguration;
1922
import org.w3c.dom.Document;
@@ -35,6 +38,8 @@
3538
* This is the primary class for loading and saving out Ssurgeon patterns.
3639
* This is also the class that maintains the current list of resources loaded into Ssurgeon: any pattern
3740
* loaded can reference these resources.
41+
*<br>
42+
* An Ssurgeon can be built from an XML pattern or by assembling the pieces by hand.
3843
*
3944
* @author Eric Yeh
4045
*/
@@ -260,7 +265,7 @@ private static String[] parseArgs(String argsString) {
260265
else
261266
retList.add(matched);
262267
} else
263-
throw new IllegalArgumentException("Unmatched quote in string to parse");
268+
throw new SsurgeonParseException("Unmatched quote in string to parse");
264269
}
265270
return retList.toArray(StringUtils.EMPTY_STRING_ARRAY);
266271
}
@@ -272,7 +277,7 @@ public static SsurgeonEdit parseEditLine(String editLine) {
272277
// Extract the operation name first
273278
String[] tuples1 = editLine.split("\\s+", 2);
274279
if (tuples1.length < 2) {
275-
throw new IllegalArgumentException("Error in SsurgeonEdit.parseEditLine: invalid number of arguments");
280+
throw new SsurgeonParseException("Error in SsurgeonEdit.parseEditLine: invalid number of arguments");
276281
}
277282
String command = tuples1[0];
278283
String[] argsArray = parseArgs(tuples1[1]);
@@ -313,7 +318,7 @@ public static SsurgeonEdit parseEditLine(String editLine) {
313318
argIndex += 2;
314319
break;
315320
default:
316-
throw new IllegalArgumentException("Parsing Ssurgeon args: unknown flag " + argsArray[argIndex]);
321+
throw new SsurgeonParseException("Parsing Ssurgeon args: unknown flag " + argsArray[argIndex]);
317322
}
318323
}
319324

@@ -343,7 +348,7 @@ public static SsurgeonEdit parseEditLine(String editLine) {
343348
} else if (command.equalsIgnoreCase(KillAllIncomingEdges.LABEL)) {
344349
retEdit = new KillAllIncomingEdges(argsBox.node);
345350
} else {
346-
throw new IllegalArgumentException("Error in SsurgeonEdit.parseEditLine: command '"+command+"' is not supported");
351+
throw new SsurgeonParseException("Error in SsurgeonEdit.parseEditLine: command '"+command+"' is not supported");
347352
}
348353
return retEdit;
349354
}
@@ -443,20 +448,35 @@ private static Document createPatternXMLDoc(List<SsurgeonPattern> patterns) {
443448
}
444449
}
445450

451+
public List<SsurgeonPattern> readFromString(String text) {
452+
try {
453+
Document doc = XMLUtils.readDocumentFromString(text);
454+
return readFromDocument(doc);
455+
} catch (ParserConfigurationException | SAXException e) {
456+
throw new SsurgeonParseException("XML failure while reading string", e);
457+
}
458+
}
446459

447460
/**
448461
* Given a path to a file containing a list of SsurgeonPatterns, returns
449462
*
450463
* TODO: deal with resources
451-
* @throws Exception
452464
*/
453-
@SuppressWarnings("unchecked")
454-
public List<SsurgeonPattern> readFromFile(File file) throws Exception {
455-
List<SsurgeonPattern> retList = new ArrayList<>();
456-
Document doc = XMLUtils.safeDocumentBuilderFactory().newDocumentBuilder().parse(file);
465+
public List<SsurgeonPattern> readFromFile(File file) {
466+
try {
467+
Document doc = XMLUtils.readDocumentFromFile(file.getPath());
457468

458-
if (VERBOSE)
459-
System.out.println("Reading ssurgeon file="+file.getAbsolutePath());
469+
if (VERBOSE)
470+
System.out.println("Reading ssurgeon file="+file.getAbsolutePath());
471+
472+
return readFromDocument(doc);
473+
} catch (ParserConfigurationException | SAXException e) {
474+
throw new SsurgeonParseException("XML failure while reading " + file, e);
475+
}
476+
}
477+
478+
public List<SsurgeonPattern> readFromDocument(Document doc) {
479+
List<SsurgeonPattern> retList = new ArrayList<>();
460480

461481
NodeList patternNodes = doc.getElementsByTagName(SsurgeonPattern.SSURGEON_ELEM_TAG);
462482
for (int i=0; i<patternNodes.getLength(); i++) {
@@ -507,7 +527,7 @@ public List<SsurgeonPattern> readFromDirectory(File dir) throws Exception {
507527
* @throws Exception
508528
*/
509529
@SuppressWarnings("unchecked")
510-
public static SsurgeonPattern ssurgeonPatternFromXML(Element elt) throws Exception {
530+
public static SsurgeonPattern ssurgeonPatternFromXML(Element elt) {
511531
String uid = getTagText(elt, SsurgeonPattern.UID_ELEM_TAG);
512532
String notes = getTagText(elt, SsurgeonPattern.NOTES_ELEM_TAG);
513533
String semgrexString = getTagText(elt, SsurgeonPattern.SEMGREX_ELEM_TAG);
@@ -538,7 +558,7 @@ public static SsurgeonPattern ssurgeonPatternFromXML(Element elt) throws Excepti
538558
* Constructs a {@code SsurgPred} structure from file, given the root element.
539559
* @throws Exception
540560
*/
541-
public static SsurgPred assemblePredFromXML(Element elt) throws Exception {
561+
public static SsurgPred assemblePredFromXML(Element elt) {
542562
String eltName = elt.getTagName();
543563
switch (eltName) {
544564
case SsurgeonPattern.PREDICATE_AND_TAG:
@@ -564,16 +584,16 @@ public static SsurgPred assemblePredFromXML(Element elt) throws Exception {
564584
String matchName = getEltText(elt).trim(); // node name to match on
565585

566586
if (matchName == null) {
567-
throw new Exception("Could not find match name for " + elt);
587+
throw new SsurgeonParseException("Could not find match name for " + elt);
568588
}
569589
if (id == null) {
570-
throw new Exception("No ID attribute for element = " + elt);
590+
throw new SsurgeonParseException("No ID attribute for element = " + elt);
571591
}
572592
return new WordlistTest(id, resourceID, typeStr, matchName);
573593
}
574594

575595
// Not a valid node, error out!
576-
throw new Exception("Invalid node encountered during Ssurgeon predicate processing, node name="+eltName);
596+
throw new SsurgeonParseException("Invalid node encountered during Ssurgeon predicate processing, node name="+eltName);
577597
}
578598

579599

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package edu.stanford.nlp.semgraph.semgrex.ssurgeon;
2+
3+
/**
4+
* A runtime exception that indicates something went wrong parsing a
5+
* Ssurgeon expression.
6+
*
7+
* @author John Bauer
8+
*/
9+
public class SsurgeonParseException extends RuntimeException {
10+
11+
private static final long serialVersionUID = -278683457698L;
12+
13+
public SsurgeonParseException(String message) {
14+
super(message);
15+
}
16+
17+
public SsurgeonParseException(String message, Throwable cause) {
18+
super(message, cause);
19+
}
20+
21+
}

src/edu/stanford/nlp/util/XMLUtils.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.xml.sax.SAXParseException;
2323

2424
import edu.stanford.nlp.io.IOUtils;
25+
import edu.stanford.nlp.io.RuntimeIOException;
2526
import edu.stanford.nlp.util.logging.Redwood;
2627

2728

@@ -1221,14 +1222,19 @@ public static XMLTag parseTag(String tagString) {
12211222
return new XMLTag(tagString);
12221223
}
12231224

1224-
public static Document readDocumentFromFile(String filename) throws Exception {
1225-
InputSource in = new InputSource(new FileReader(filename));
1226-
DocumentBuilderFactory factory = safeDocumentBuilderFactory();
1225+
public static Document readDocumentFromFile(String filename) throws ParserConfigurationException, SAXException {
1226+
try {
1227+
InputSource in = new InputSource(new FileReader(filename));
1228+
DocumentBuilderFactory factory = safeDocumentBuilderFactory();
12271229

1228-
factory.setNamespaceAware(false);
1229-
DocumentBuilder db = factory.newDocumentBuilder();
1230-
db.setErrorHandler(new SAXErrorHandler());
1231-
return db.parse(in);
1230+
factory.setNamespaceAware(false);
1231+
DocumentBuilder db = factory.newDocumentBuilder();
1232+
db.setErrorHandler(new SAXErrorHandler());
1233+
1234+
return db.parse(in);
1235+
} catch(IOException e) {
1236+
throw new RuntimeIOException(e);
1237+
}
12321238
}
12331239

12341240
private static class SAXErrorHandler implements ErrorHandler {
@@ -1272,11 +1278,15 @@ public void fatalError(SAXParseException ex) throws SAXParseException {
12721278

12731279
} // end class SAXErrorHandler
12741280

1275-
public static Document readDocumentFromString(String s) throws Exception {
1281+
public static Document readDocumentFromString(String s) throws ParserConfigurationException, SAXException {
12761282
InputSource in = new InputSource(new StringReader(s));
12771283
DocumentBuilderFactory factory = safeDocumentBuilderFactory();
12781284
factory.setNamespaceAware(false);
1279-
return factory.newDocumentBuilder().parse(in);
1285+
try {
1286+
return factory.newDocumentBuilder().parse(in);
1287+
} catch(IOException e) {
1288+
throw new RuntimeIOException(e);
1289+
}
12801290
}
12811291

12821292
/** Tests a few methods.

0 commit comments

Comments
 (0)