From e41a4fdd7537656c045eaf0f998c6b67af88136d Mon Sep 17 00:00:00 2001 From: Vhati Date: Tue, 3 Sep 2013 16:19:21 -0400 Subject: [PATCH] Added panic= arg to find tags --- skel_common/readme_modders.txt | 7 ++++--- .../net/vhati/modmanager/core/XMLPatcher.java | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/skel_common/readme_modders.txt b/skel_common/readme_modders.txt index 5a7f065..47daa51 100644 --- a/skel_common/readme_modders.txt +++ b/skel_common/readme_modders.txt @@ -105,7 +105,7 @@ Advanced XML They take the form: - + @@ -114,8 +114,9 @@ Advanced XML Some identify existing tags, using each result as context for commands. Unless stated otherwise, these all accept optional reverse, start, - and limit args: defaulting to search forward, skip 0 matched - candidates, and return up to an unlimited number of results. + limit, and panic args: defaulting to search forward, skip 0 matched + candidates, return up to an unlimited number of results, and not cause + an error when no results are found. Sometimes the may have an auxiliary tag just to hold more args. diff --git a/src/main/java/net/vhati/modmanager/core/XMLPatcher.java b/src/main/java/net/vhati/modmanager/core/XMLPatcher.java index 3e51c9b..59674c6 100644 --- a/src/main/java/net/vhati/modmanager/core/XMLPatcher.java +++ b/src/main/java/net/vhati/modmanager/core/XMLPatcher.java @@ -10,6 +10,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import java.util.Set; import java.util.TreeMap; import java.util.regex.Matcher; @@ -98,6 +99,7 @@ public class XMLPatcher { boolean searchReverse = getAttributeBooleanValue( node, "reverse", true ); int searchStart = getAttributeIntValue( node, "start", 0 ); int searchLimit = getAttributeIntValue( node, "limit", 1 ); + boolean panic = getAttributeBooleanValue( node, "panic", false ); if ( searchName == null || searchName.length() == 0 ) throw new IllegalArgumentException( String.format( "<%s> requires a name attribute (%s).", node.getName(), getPathToRoot(node) ) ); @@ -122,7 +124,9 @@ public class XMLPatcher { matchedNodes = matchedNodes.subList( searchStart, matchedNodes.size() ); } } - + if ( matchedNodes.isEmpty() ) + throw new NoSuchElementException( String.format( "<%s> was set to require results but found none (%s).", node.getName(), getPathToRoot(node) ) ); + result = matchedNodes; } else if ( node.getName().equals( "findLike" ) ) { @@ -131,6 +135,7 @@ public class XMLPatcher { boolean searchReverse = getAttributeBooleanValue( node, "reverse", false ); int searchStart = getAttributeIntValue( node, "start", 0 ); int searchLimit = getAttributeIntValue( node, "limit", -1 ); + boolean panic = getAttributeBooleanValue( node, "panic", false ); if ( searchType != null && searchType.length() == 0 ) throw new IllegalArgumentException( String.format( "<%s> type attribute, when present, can't be empty (%s).", node.getName(), getPathToRoot(node) ) ); @@ -170,6 +175,8 @@ public class XMLPatcher { matchedNodes = matchedNodes.subList( searchStart, matchedNodes.size() ); } } + if ( matchedNodes.isEmpty() ) + throw new NoSuchElementException( String.format( "<%s> was set to require results but found none (%s).", node.getName(), getPathToRoot(node) ) ); result = matchedNodes; } @@ -180,6 +187,7 @@ public class XMLPatcher { boolean searchReverse = getAttributeBooleanValue( node, "reverse", false ); int searchStart = getAttributeIntValue( node, "start", 0 ); int searchLimit = getAttributeIntValue( node, "limit", -1 ); + boolean panic = getAttributeBooleanValue( node, "panic", false ); if ( searchType != null && searchType.length() == 0 ) throw new IllegalArgumentException( String.format( "<%s> type attribute, when present, can't be empty (%s).", node.getName(), getPathToRoot(node) ) ); @@ -218,6 +226,8 @@ public class XMLPatcher { matchedNodes = matchedNodes.subList( searchStart, matchedNodes.size() ); } } + if ( matchedNodes.isEmpty() ) + throw new NoSuchElementException( String.format( "<%s> was set to require results but found none (%s).", node.getName(), getPathToRoot(node) ) ); result = matchedNodes; } @@ -226,6 +236,7 @@ public class XMLPatcher { boolean searchReverse = getAttributeBooleanValue( node, "reverse", false ); int searchStart = getAttributeIntValue( node, "start", 0 ); int searchLimit = getAttributeIntValue( node, "limit", -1 ); + boolean panic = getAttributeBooleanValue( node, "panic", false ); if ( searchStart < 0 ) throw new IllegalArgumentException( String.format( "<%s> 'start' attribute is not >= 0 (%s).", node.getName(), getPathToRoot(node) ) ); @@ -246,6 +257,8 @@ public class XMLPatcher { matchedNodes = matchedNodes.subList( searchStart, matchedNodes.size() ); } } + if ( matchedNodes.isEmpty() ) + throw new NoSuchElementException( String.format( "<%s> was set to require results but found none (%s).", node.getName(), getPathToRoot(node) ) ); result = matchedNodes; }