You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AssertElement.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.fotreetest.ext;
  19. import org.xml.sax.Attributes;
  20. import org.xml.sax.Locator;
  21. import org.apache.fop.apps.FOPException;
  22. import org.apache.fop.fo.FONode;
  23. import org.apache.fop.fo.FOPropertyMapping;
  24. import org.apache.fop.fo.PropertyList;
  25. import org.apache.fop.fo.properties.KeepProperty;
  26. import org.apache.fop.fo.properties.LengthPairProperty;
  27. import org.apache.fop.fo.properties.LengthRangeProperty;
  28. import org.apache.fop.fo.properties.PercentLength;
  29. import org.apache.fop.fo.properties.Property;
  30. import org.apache.fop.fo.properties.SpaceProperty;
  31. import org.apache.fop.fotreetest.ResultCollector;
  32. /**
  33. * Defines the assert element for the FOP Test extension.
  34. */
  35. public class AssertElement extends TestObj {
  36. /**
  37. * Creates a new AssertElement instance that is a child
  38. * of the given {@link FONode}
  39. *
  40. * @param parent the parent {@link FONode}
  41. */
  42. public AssertElement(FONode parent) {
  43. super(parent);
  44. }
  45. /**
  46. * @see org.apache.fop.fo.FONode#processNode
  47. */
  48. public void processNode(String elementName,
  49. Locator locator,
  50. Attributes attlist,
  51. PropertyList propertyList) throws FOPException {
  52. //super.processNode(elementName, locator, attlist, propertyList);
  53. ResultCollector collector = ResultCollector.getInstance();
  54. String propName = attlist.getValue("property");
  55. String expected = attlist.getValue("expected");
  56. String component = null;
  57. int dotIndex = propName.indexOf('.');
  58. if (dotIndex >= 0) {
  59. component = propName.substring(dotIndex + 1);
  60. propName = propName.substring(0, dotIndex);
  61. }
  62. int propID = FOPropertyMapping.getPropertyId(propName);
  63. if (propID < 0) {
  64. collector.notifyAssertionFailure("Property not found: " + propName);
  65. } else {
  66. Property prop;
  67. prop = propertyList.getParentPropertyList().get(propID);
  68. if (component != null) {
  69. //Access subcomponent
  70. Property mainProp = prop;
  71. prop = null;
  72. LengthPairProperty lpp = mainProp.getLengthPair();
  73. if (lpp != null) {
  74. prop = lpp.getComponent(FOPropertyMapping.getSubPropertyId(component));
  75. }
  76. LengthRangeProperty lrp = mainProp.getLengthRange();
  77. if (lrp != null) {
  78. prop = lrp.getComponent(FOPropertyMapping.getSubPropertyId(component));
  79. }
  80. KeepProperty kp = mainProp.getKeep();
  81. if (kp != null) {
  82. prop = kp.getComponent(FOPropertyMapping.getSubPropertyId(component));
  83. }
  84. SpaceProperty sp = mainProp.getSpace();
  85. if (sp != null) {
  86. prop = sp.getComponent(FOPropertyMapping.getSubPropertyId(component));
  87. }
  88. }
  89. String s;
  90. if (prop instanceof PercentLength) {
  91. s = prop.getString();
  92. } else {
  93. s = String.valueOf(prop);
  94. }
  95. if (!expected.equals(s)) {
  96. collector.notifyAssertionFailure(
  97. locator.getSystemId()
  98. + "\nProperty '" + propName
  99. + "' expected to evaluate to '" + expected
  100. + "' but got '" + s
  101. + "'\n(test:assert in "
  102. + propertyList.getParentFObj().getName()
  103. + " at line #" + locator.getLineNumber()
  104. + ", column #" + locator.getColumnNumber() + ")\n");
  105. }
  106. }
  107. }
  108. /** @see org.apache.fop.fo.FONode#getLocalName() */
  109. public String getLocalName() {
  110. return "assert";
  111. }
  112. }