Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java @ 4737:718adea968e2
Add <dc:variable name="variable" expr="xpath"/> construct to datacage language.
Each time during traversal of the template this element is found a variable
called 'variable' is set to the result of the the evaluation of the
XPpath 'xpath'.
Example:
<dc:variable name="x" expr="$x + 1"/> <!-- Assign x+1 to x -->
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Wed, 02 Jan 2013 14:05:21 +0100 |
parents | 3a077fc360c2 |
children | fb135e1dfa35 |
comparison
equal
deleted
inserted
replaced
4736:b195fede1c3b | 4737:718adea968e2 |
---|---|
344 if (test.length() == 0) { | 344 if (test.length() == 0) { |
345 log.warn("missing 'test' attribute in 'if'"); | 345 log.warn("missing 'test' attribute in 'if'"); |
346 return; | 346 return; |
347 } | 347 } |
348 | 348 |
349 Boolean result = evaluateXPath(test); | 349 Boolean result = evaluateXPathToBoolean(test); |
350 | 350 |
351 if (result != null && result.booleanValue()) { | 351 if (result != null && result.booleanValue()) { |
352 NodeList subs = current.getChildNodes(); | 352 NodeList subs = current.getChildNodes(); |
353 for (int i = 0, N = subs.getLength(); i < N; ++i) { | 353 for (int i = 0, N = subs.getLength(); i < N; ++i) { |
354 build(parent, subs.item(i)); | 354 build(parent, subs.item(i)); |
378 if (test.length() == 0) { | 378 if (test.length() == 0) { |
379 log.warn("no 'test' attribute found for when"); | 379 log.warn("no 'test' attribute found for when"); |
380 continue; | 380 continue; |
381 } | 381 } |
382 | 382 |
383 Boolean result = evaluateXPath(test); | 383 Boolean result = evaluateXPathToBoolean(test); |
384 if (result != null && result.booleanValue()) { | 384 if (result != null && result.booleanValue()) { |
385 branch = child; | 385 branch = child; |
386 break; | 386 break; |
387 } | 387 } |
388 | 388 |
400 build(parent, subs.item(i)); | 400 build(parent, subs.item(i)); |
401 } | 401 } |
402 } | 402 } |
403 } | 403 } |
404 | 404 |
405 protected Boolean evaluateXPath(String expr) { | 405 protected Object evaluateXPath(String expr) { |
406 | 406 |
407 if (log.isDebugEnabled()) { | 407 if (log.isDebugEnabled()) { |
408 log.debug("evaluate: '" + expr + "'"); | 408 log.debug("evaluate: '" + expr + "'"); |
409 } | 409 } |
410 | 410 |
411 try { | 411 try { |
412 XPath xpath = XPATH_FACTORY.newXPath(); | 412 XPath xpath = XPATH_FACTORY.newXPath(); |
413 xpath.setXPathVariableResolver(frames); | 413 xpath.setXPathVariableResolver(frames); |
414 xpath.setXPathFunctionResolver(FunctionResolver.FUNCTIONS); | 414 xpath.setXPathFunctionResolver(FunctionResolver.FUNCTIONS); |
415 Object result = xpath.evaluate( | 415 return xpath.evaluate(expr, EVAL_DOCUMENT, XPathConstants.BOOLEAN); |
416 expr, EVAL_DOCUMENT, XPathConstants.BOOLEAN); | |
417 | |
418 return result instanceof Boolean | |
419 ? (Boolean)result | |
420 : null; | |
421 } | 416 } |
422 catch (XPathExpressionException xfce) { | 417 catch (XPathExpressionException xfce) { |
423 log.error("expression: " + expr, xfce); | 418 log.error("expression: " + expr, xfce); |
424 } | 419 } |
425 return null; | 420 return null; |
426 } | 421 } |
427 | 422 |
428 protected void convert(Node parent, Element current) { | 423 protected Boolean evaluateXPathToBoolean(String expr) { |
424 | |
425 Object result = evaluateXPath(expr); | |
426 | |
427 return result instanceof Boolean | |
428 ? (Boolean)result | |
429 : null; | |
430 } | |
431 | |
432 protected void convert(Element current) { | |
429 | 433 |
430 String variable = expand(current.getAttribute("var")); | 434 String variable = expand(current.getAttribute("var")); |
431 String type = expand(current.getAttribute("type")); | 435 String type = expand(current.getAttribute("type")); |
432 | 436 |
433 Object [] result = new Object[1]; | 437 Object [] result = new Object[1]; |
434 | 438 |
435 if (frames.getStore(variable, result)) { | 439 if (frames.getStore(variable, result)) { |
436 Object object = TypeConverter.convert(result[0], type); | 440 Object object = TypeConverter.convert(result[0], type); |
437 frames.put(variable.toUpperCase(), object); | 441 frames.put(variable.toUpperCase(), object); |
442 } | |
443 } | |
444 | |
445 protected void variable(Element current) { | |
446 | |
447 String varName = expand(current.getAttribute("name")); | |
448 String expr = current.getAttribute("expr"); | |
449 | |
450 if (varName.length() == 0 || expr.length() == 0) { | |
451 log.error("dc:variable 'name' or 'expr' empty."); | |
452 } | |
453 else { | |
454 frames.put(varName.toUpperCase(), evaluateXPath(expr)); | |
438 } | 455 } |
439 } | 456 } |
440 | 457 |
441 protected String expand(String s) { | 458 protected String expand(String s) { |
442 Matcher m = CompiledStatement.VAR.matcher(s); | 459 Matcher m = CompiledStatement.VAR.matcher(s); |
494 elements(parent, (Element)current); | 511 elements(parent, (Element)current); |
495 } | 512 } |
496 else if ("text".equals(localName)) { | 513 else if ("text".equals(localName)) { |
497 text(parent, (Element)current); | 514 text(parent, (Element)current); |
498 } | 515 } |
516 else if ("variable".equals(localName)) { | |
517 variable((Element)current); | |
518 } | |
499 else if ("comment".equals(localName) | 519 else if ("comment".equals(localName) |
500 || "statement".equals(localName)) { | 520 || "statement".equals(localName)) { |
501 // ignore comments and statements in output | 521 // ignore comments and statements in output |
502 } | 522 } |
503 else if ("convert".equals(localName)) { | 523 else if ("convert".equals(localName)) { |
504 convert(parent, (Element)current); | 524 convert((Element)current); |
505 } | 525 } |
506 else { | 526 else { |
507 log.warn("unknown '" + localName + "' -> ignore"); | 527 log.warn("unknown '" + localName + "' -> ignore"); |
508 } | 528 } |
509 } | 529 } |