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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 AbstractIFTest extends AbstractIntermediateTest {
  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. File ifSchemaFile = new File(
  61. "src/documentation/intermediate-format-ng/fop-intermediate-format-ng.xsd");
  62. ifSchema = sFactory.newSchema(ifSchemaFile);
  63. } catch (IllegalArgumentException iae) {
  64. System.err.println("No suitable SchemaFactory for XML Schema validation found!");
  65. } catch (SAXException e) {
  66. throw new ExceptionInInitializerError(e);
  67. }
  68. IF_SCHEMA = ifSchema;
  69. }
  70. /**
  71. * Creates a new test case.
  72. *
  73. * @param testFile the file containing the document and the tests
  74. * @throws IOException if an I/O error occurs while loading the test case
  75. */
  76. public AbstractIFTest(File testFile) throws IOException {
  77. super(testFile);
  78. }
  79. @Override
  80. protected String getIntermediateFileExtension() {
  81. return ".if.xml";
  82. }
  83. @Override
  84. protected Document buildIntermediateDocument(Templates templates) throws Exception {
  85. Transformer transformer = templates.newTransformer();
  86. setErrorListener(transformer);
  87. //Set up XMLRenderer to render to a DOM
  88. DOMResult domResult = new DOMResult();
  89. FOUserAgent userAgent = createUserAgent();
  90. //Create an instance of the target renderer so the XMLRenderer can use its font setup
  91. IFDocumentHandler targetHandler = userAgent.getRendererFactory().createDocumentHandler(
  92. userAgent, getTargetMIME());
  93. //Setup painter
  94. IFSerializer serializer = new IFSerializer(new IFContext(userAgent));
  95. serializer.mimicDocumentHandler(targetHandler);
  96. serializer.setResult(domResult);
  97. userAgent.setDocumentHandlerOverride(serializer);
  98. Fop fop = fopFactory.newFop(userAgent);
  99. Result res = new SAXResult(fop.getDefaultHandler());
  100. transformer.transform(new DOMSource(testDoc), res);
  101. return (Document) domResult.getNode();
  102. }
  103. @Override
  104. protected void validate(Document doc) throws SAXException, IOException {
  105. if (IF_SCHEMA == null) {
  106. return; //skip validation;
  107. }
  108. Validator validator = IF_SCHEMA.newValidator();
  109. validator.setErrorHandler(new ErrorHandler() {
  110. public void error(SAXParseException exception) throws SAXException {
  111. throw exception;
  112. }
  113. public void fatalError(SAXParseException exception) throws SAXException {
  114. throw exception;
  115. }
  116. public void warning(SAXParseException exception) throws SAXException {
  117. //ignore
  118. }
  119. });
  120. validator.validate(new DOMSource(doc));
  121. }
  122. }