Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.layoutengine;
  19. import javax.xml.transform.TransformerException;
  20. import org.w3c.dom.Document;
  21. import org.w3c.dom.Node;
  22. import org.apache.xml.utils.PrefixResolver;
  23. import org.apache.xml.utils.PrefixResolverDefault;
  24. import org.apache.xpath.XPathAPI;
  25. import org.apache.xpath.objects.XBoolean;
  26. import org.apache.xpath.objects.XObject;
  27. import org.apache.fop.intermediate.IFCheck;
  28. /**
  29. * Simple check that requires an XPath expression to evaluate to true.
  30. */
  31. public class TrueCheck implements LayoutEngineCheck, IFCheck {
  32. private String xpath;
  33. private String failureMessage;
  34. private PrefixResolver prefixResolver;
  35. /**
  36. * Creates a new instance from a DOM node.
  37. * @param node DOM node that defines this check
  38. */
  39. public TrueCheck(Node node) {
  40. this.xpath = node.getAttributes().getNamedItem("xpath").getNodeValue();
  41. Node nd = node.getAttributes().getNamedItem("fail-msg");
  42. if (nd != null) {
  43. this.failureMessage = nd.getNodeValue();
  44. }
  45. this.prefixResolver = new PrefixResolverDefault(node);
  46. }
  47. /** {@inheritDoc} */
  48. public void check(LayoutResult result) {
  49. doCheck(result.getAreaTree());
  50. }
  51. /** {@inheritDoc} */
  52. public void check(Document intermediate) {
  53. doCheck(intermediate);
  54. }
  55. private void doCheck(Document doc) {
  56. XObject res;
  57. try {
  58. res = XPathAPI.eval(doc, xpath, prefixResolver);
  59. } catch (TransformerException e) {
  60. throw new RuntimeException("XPath evaluation failed: " + e.getMessage());
  61. }
  62. if (!XBoolean.S_TRUE.equals(res)) {
  63. if (failureMessage != null) {
  64. throw new RuntimeException(failureMessage);
  65. } else {
  66. throw new RuntimeException(
  67. "Expected XPath expression to evaluate to 'true', but got '"
  68. + res + "' (" + this + ")");
  69. }
  70. }
  71. }
  72. /** {@inheritDoc} */
  73. public String toString() {
  74. return "XPath: " + xpath;
  75. }
  76. }