Changeset 99 for java/main/src/main/java/com/framsticks/core/Path.java
- Timestamp:
- 07/10/13 22:41:02 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/core/Path.java
r98 r99 6 6 import com.framsticks.util.FramsticksException; 7 7 import com.framsticks.util.dispatching.Dispatching; 8 import com.framsticks.util.dispatching.ExceptionResultHandler; 9 import com.framsticks.util.dispatching.IgnoreExceptionHandler; 10 import com.framsticks.util.lang.Pair; 8 11 9 12 import java.util.Iterator; 10 13 import java.util.LinkedList; 11 14 import java.util.List; 15 import java.util.regex.Pattern; 12 16 13 17 import javax.annotation.Nonnull; … … 27 31 final LinkedList<Node> nodes; 28 32 29 protected static Object getKnownChild(Tree tree, AccessInterface access, CompositeParam param ) {33 protected static Object getKnownChild(Tree tree, AccessInterface access, CompositeParam param, ExceptionResultHandler handler) { 30 34 Object child = access.get(param, Object.class); 31 35 if (child == null) { … … 36 40 return child; 37 41 } catch (FramsticksException e) { 42 handler.handle(e); 38 43 } 39 44 return null; … … 130 135 } 131 136 132 133 137 public PathBuilder resolve(@Nonnull Tree tree, String textual) { 138 return resolve(tree, textual, IgnoreExceptionHandler.getInstance()); 139 } 140 141 public PathBuilder resolve(@Nonnull Tree tree, String textual, ExceptionResultHandler handler) { 134 142 135 143 assert nodes.isEmpty(); … … 146 154 String e = i.next(); 147 155 Param p = access.getParam(e); 156 if (p == null) { 157 break; 158 } 148 159 if (!(p instanceof CompositeParam)) { 149 //entries.add(new Entry()); 150 break; 151 } 152 CompositeParam c = (CompositeParam)p; 160 throw new FramsticksException().msg("param is not a composite").arg("param", p).arg("tree", tree).arg("textual", textual).arg("access", access); 161 } 162 CompositeParam c = (CompositeParam) p; 153 163 b.append("/").append(e); 154 164 access.select(current.getObject()); 155 current = new Node(current.getTree(), c, getKnownChild(tree, access, c ));165 current = new Node(current.getTree(), c, getKnownChild(tree, access, c, handler)); 156 166 nodes.add(current); 157 167 } … … 245 255 return Path.build().resolve(tree, "/").finish(); 246 256 } 247 Object child = getKnownChild(tree, TreeOperations.bindAccess(getUnder()), getTop().getParam() );257 Object child = getKnownChild(tree, TreeOperations.bindAccess(getUnder()), getTop().getParam(), IgnoreExceptionHandler.getInstance()); 248 258 if (child == null) { 249 259 return this; … … 274 284 } 275 285 286 public static Path tryTo(@Nonnull Tree tree, String textual) { 287 return Path.build().resolve(tree, textual).finish(); 288 } 289 276 290 public static Path to(@Nonnull Tree tree, String textual) { 277 return Path.build().resolve(tree, textual).finish(); 291 Path path = tryTo(tree, textual); 292 if (path.getTextual().equals(textual)) { 293 return path; 294 } 295 throw new FramsticksException().msg("failed to create path").arg("textual", textual).arg("result", path).arg("tree", tree); 278 296 } 279 297 … … 299 317 } 300 318 319 public static final Pattern pathPattern = Pattern.compile("(\\/)|((\\/[^/]+)+)"); 320 321 public static boolean isValidString(String path) { 322 return pathPattern.matcher(path).matches(); 323 } 324 325 public static String appendString(String path, String element) { 326 if (path.equals("/")) { 327 return path + element; 328 } 329 return path + "/" + element; 330 } 331 332 public static Pair<String, String> removeLastElement(String path) { 333 assert isValidString(path); 334 if (path.equals("/")) { 335 throw new FramsticksException().msg("cannot remove last element from root path"); 336 } 337 int index = path.lastIndexOf('/'); 338 assert index != -1; 339 if (index == 0) { 340 return new Pair<String, String>("/", path.substring(1)); 341 } 342 return new Pair<String, String>(path.substring(0, index), path.substring(index + 1)); 343 } 344 345 public static String validateString(String path) { 346 if (!isValidString(path)) { 347 throw new FramsticksException().msg("path string validation failured").arg("path", path); 348 } 349 return path; 350 } 351 301 352 302 353 // public boolean isEmpty() {
Note: See TracChangeset
for help on using the changeset viewer.