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.

AbstractIFTestCase.java 5.2KB

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