88
99import javax .xml .parsers .DocumentBuilder ;
1010import javax .xml .parsers .DocumentBuilderFactory ;
11+ import javax .xml .parsers .ParserConfigurationException ;
1112import javax .xml .transform .OutputKeys ;
1213import javax .xml .transform .Transformer ;
1314import javax .xml .transform .TransformerFactory ;
1415import javax .xml .transform .dom .DOMSource ;
1516import javax .xml .transform .stream .StreamResult ;
1617
18+ import org .xml .sax .SAXException ;
19+
1720import edu .stanford .nlp .util .StringUtils ;
1821import edu .stanford .nlp .util .logging .RedwoodConfiguration ;
1922import org .w3c .dom .Document ;
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
0 commit comments