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.

AbstractIFTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.XMLConstants;
  22. import javax.xml.transform.Result;
  23. import javax.xml.transform.Templates;
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.dom.DOMResult;
  26. import javax.xml.transform.dom.DOMSource;
  27. import javax.xml.transform.sax.SAXResult;
  28. import javax.xml.validation.Schema;
  29. import javax.xml.validation.SchemaFactory;
  30. import javax.xml.validation.Validator;
  31. import javax.xml.xpath.XPath;
  32. import javax.xml.xpath.XPathConstants;
  33. import javax.xml.xpath.XPathExpressionException;
  34. import javax.xml.xpath.XPathFactory;
  35. import org.w3c.dom.Document;
  36. import org.xml.sax.ErrorHandler;
  37. import org.xml.sax.SAXException;
  38. import org.xml.sax.SAXParseException;
  39. import org.apache.fop.apps.FOUserAgent;
  40. import org.apache.fop.apps.Fop;
  41. import org.apache.fop.render.intermediate.IFContext;
  42. import org.apache.fop.render.intermediate.IFDocumentHandler;
  43. import org.apache.fop.render.intermediate.IFSerializer;
  44. /**
  45. * A common super-class for intermediate format test cases.
  46. */
  47. abstract class AbstractIFTest extends AbstractIntermediateTest {
  48. private static final Schema IF_SCHEMA;
  49. static {
  50. Schema ifSchema = null;
  51. try {
  52. SchemaFactory sFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  53. sFactory.setErrorHandler(new ErrorHandler() {
  54. public void error(SAXParseException exception) throws SAXException {
  55. throw exception;
  56. }
  57. public void fatalError(SAXParseException exception) throws SAXException {
  58. throw exception;
  59. }
  60. public void warning(SAXParseException exception) throws SAXException {
  61. throw exception;
  62. }
  63. });
  64. File ifSchemaFile = new File(
  65. "src/documentation/intermediate-format-ng/fop-intermediate-format-ng.xsd");
  66. ifSchema = sFactory.newSchema(ifSchemaFile);
  67. } catch (IllegalArgumentException iae) {
  68. System.err.println("No suitable SchemaFactory for XML Schema validation found!");
  69. } catch (SAXException e) {
  70. throw new ExceptionInInitializerError(e);
  71. }
  72. IF_SCHEMA = ifSchema;
  73. }
  74. /**
  75. * Creates a new test case.
  76. *
  77. * @param testFile the file containing the document and the tests
  78. * @throws IOException if an I/O error occurs while loading the test case
  79. */
  80. public AbstractIFTest(File testFile) throws IOException {
  81. super(testFile);
  82. }
  83. @Override
  84. protected String getIntermediateFileExtension() {
  85. return ".if.xml";
  86. }
  87. @Override
  88. protected Document buildIntermediateDocument(Templates templates) throws Exception {
  89. Transformer transformer = templates.newTransformer();
  90. setErrorListener(transformer);
  91. //Set up XMLRenderer to render to a DOM
  92. DOMResult domResult = new DOMResult();
  93. FOUserAgent userAgent = createUserAgent();
  94. userAgent.setAccessibility(isAccessibility(testDoc));
  95. //Create an instance of the target renderer so the XMLRenderer can use its font setup
  96. IFDocumentHandler targetHandler = userAgent.getRendererFactory().createDocumentHandler(
  97. userAgent, getTargetMIME());
  98. //Setup painter
  99. IFSerializer serializer = new IFSerializer(new IFContext(userAgent));
  100. serializer.mimicDocumentHandler(targetHandler);
  101. serializer.setResult(domResult);
  102. userAgent.setDocumentHandlerOverride(serializer);
  103. Fop fop = fopFactory.newFop(userAgent);
  104. Result res = new SAXResult(fop.getDefaultHandler());
  105. transformer.transform(new DOMSource(testDoc), res);
  106. return (Document) domResult.getNode();
  107. }
  108. private boolean isAccessibility(Document testDoc) {
  109. try {
  110. String s = eval(testDoc, "/testcase/cfg/accessibility");
  111. return "true".equals(s);
  112. } catch (XPathExpressionException e) {
  113. throw new RuntimeException(e);
  114. }
  115. }
  116. private String eval(Document doc, String xpath) throws XPathExpressionException {
  117. XPath xPath = XPathFactory.newInstance().newXPath();
  118. return (String) xPath.compile(xpath).evaluate(doc, XPathConstants.STRING);
  119. }
  120. @Override
  121. protected void validate(Document doc) throws SAXException, IOException {
  122. if (IF_SCHEMA == null || isAccessibility(testDoc)) {
  123. return; //skip validation;
  124. }
  125. Validator validator = IF_SCHEMA.newValidator();
  126. validator.setErrorHandler(new ErrorHandler() {
  127. public void error(SAXParseException exception) throws SAXException {
  128. throw exception;
  129. }
  130. public void fatalError(SAXParseException exception) throws SAXException {
  131. throw exception;
  132. }
  133. public void warning(SAXParseException exception) throws SAXException {
  134. //ignore
  135. }
  136. });
  137. validator.validate(new DOMSource(doc));
  138. }
  139. }