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.

TestAssistant.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.intermediate;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import javax.xml.parsers.DocumentBuilder;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import javax.xml.transform.Result;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.Templates;
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.TransformerConfigurationException;
  28. import javax.xml.transform.TransformerException;
  29. import javax.xml.transform.dom.DOMResult;
  30. import javax.xml.transform.dom.DOMSource;
  31. import javax.xml.transform.sax.SAXTransformerFactory;
  32. import javax.xml.transform.stream.StreamResult;
  33. import javax.xml.transform.stream.StreamSource;
  34. import org.w3c.dom.Document;
  35. import org.w3c.dom.Element;
  36. import org.apache.xpath.XPathAPI;
  37. import org.apache.xpath.objects.XObject;
  38. import org.apache.fop.apps.EnvironmentProfile;
  39. import org.apache.fop.apps.EnvironmentalProfileFactory;
  40. import org.apache.fop.apps.FopFactory;
  41. import org.apache.fop.apps.FopFactoryBuilder;
  42. import org.apache.fop.apps.io.ResourceResolverFactory;
  43. /**
  44. * Helper class for running FOP tests.
  45. */
  46. public class TestAssistant {
  47. // configure fopFactory as desired
  48. protected final File testDir = new File("test/layoutengine/standard-testcases");
  49. private SAXTransformerFactory tfactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
  50. private DocumentBuilderFactory domBuilderFactory;
  51. private Templates testcase2fo;
  52. private Templates testcase2checks;
  53. /**
  54. * Main constructor.
  55. */
  56. public TestAssistant() {
  57. domBuilderFactory = DocumentBuilderFactory.newInstance();
  58. domBuilderFactory.setNamespaceAware(true);
  59. domBuilderFactory.setValidating(false);
  60. }
  61. /**
  62. * Returns the stylesheet for convert extracting the XSL-FO part from the test case.
  63. * @return the stylesheet
  64. * @throws TransformerConfigurationException if an error occurs loading the stylesheet
  65. */
  66. public Templates getTestcase2FOStylesheet() throws TransformerConfigurationException {
  67. if (testcase2fo == null) {
  68. //Load and cache stylesheet
  69. Source src = new StreamSource(new File("test/layoutengine/testcase2fo.xsl"));
  70. testcase2fo = tfactory.newTemplates(src);
  71. }
  72. return testcase2fo;
  73. }
  74. /**
  75. * Returns the stylesheet for convert extracting the checks from the test case.
  76. * @return the stylesheet
  77. * @throws TransformerConfigurationException if an error occurs loading the stylesheet
  78. */
  79. private Templates getTestcase2ChecksStylesheet() throws TransformerConfigurationException {
  80. if (testcase2checks == null) {
  81. //Load and cache stylesheet
  82. Source src = new StreamSource(new File("test/layoutengine/testcase2checks.xsl"));
  83. testcase2checks = tfactory.newTemplates(src);
  84. }
  85. return testcase2checks;
  86. }
  87. /**
  88. * Returns the element from the given XML file that encloses the tests.
  89. *
  90. * @param testFile a test case
  91. * @return the parent element of the group(s) of checks
  92. * @throws TransformerException if an error occurs while extracting the test element
  93. */
  94. public Element getTestRoot(File testFile) throws TransformerException {
  95. Transformer transformer = getTestcase2ChecksStylesheet().newTransformer();
  96. DOMResult res = new DOMResult();
  97. transformer.transform(new StreamSource(testFile), res);
  98. Document doc = (Document) res.getNode();
  99. return doc.getDocumentElement();
  100. }
  101. public FopFactory getFopFactory(Document testDoc) {
  102. boolean base14KerningEnabled = isBase14KerningEnabled(testDoc);
  103. boolean strictValidation = isStrictValidation(testDoc);
  104. EnvironmentProfile envProfile = EnvironmentalProfileFactory.createRestrictedIO(
  105. testDir.getParentFile().toURI(),
  106. ResourceResolverFactory.createDefaultResourceResolver());
  107. FopFactoryBuilder builder = new FopFactoryBuilder(envProfile);
  108. builder.setStrictFOValidation(strictValidation);
  109. builder.getFontManager().setBase14KerningEnabled(base14KerningEnabled);
  110. return builder.build();
  111. }
  112. private boolean isBase14KerningEnabled(Document testDoc) {
  113. try {
  114. XObject xo = XPathAPI.eval(testDoc, "/testcase/cfg/base14kerning");
  115. String s = xo.str();
  116. return ("true".equalsIgnoreCase(s));
  117. } catch (TransformerException e) {
  118. throw new RuntimeException("Error while evaluating XPath expression", e);
  119. }
  120. }
  121. private boolean isStrictValidation(Document testDoc) {
  122. try {
  123. XObject xo = XPathAPI.eval(testDoc, "/testcase/cfg/strict-validation");
  124. return !("false".equalsIgnoreCase(xo.str()));
  125. } catch (TransformerException e) {
  126. throw new RuntimeException("Error while evaluating XPath expression", e);
  127. }
  128. }
  129. /**
  130. * Loads a test case into a DOM document.
  131. * @param testFile the test file
  132. * @return the loaded test case
  133. * @throws IOException if an I/O error occurs loading the test case
  134. */
  135. public Document loadTestCase(File testFile)
  136. throws IOException {
  137. try {
  138. DocumentBuilder builder = domBuilderFactory.newDocumentBuilder();
  139. Document testDoc = builder.parse(testFile);
  140. return testDoc;
  141. } catch (Exception e) {
  142. throw new IOException("Error while loading test case: " + e.getMessage());
  143. }
  144. }
  145. /**
  146. * Serialize the DOM for later inspection.
  147. * @param doc the DOM document
  148. * @param target target file
  149. * @throws TransformerException if a problem occurs during serialization
  150. */
  151. public void saveDOM(Document doc, File target) throws TransformerException {
  152. Transformer transformer = getTransformerFactory().newTransformer();
  153. Source src = new DOMSource(doc);
  154. Result res = new StreamResult(target);
  155. transformer.transform(src, res);
  156. }
  157. /**
  158. * Returns the SAXTransformerFactory.
  159. * @return the SAXTransformerFactory
  160. */
  161. public SAXTransformerFactory getTransformerFactory() {
  162. return tfactory;
  163. }
  164. }