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.

FOTreeTester.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2005-2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.fotreetest;
  18. import java.io.File;
  19. import java.util.List;
  20. import javax.xml.parsers.SAXParser;
  21. import javax.xml.parsers.SAXParserFactory;
  22. import org.apache.fop.apps.FOUserAgent;
  23. import org.apache.fop.apps.Fop;
  24. import org.apache.fop.apps.FopFactory;
  25. import org.apache.fop.fotreetest.ext.TestElementMapping;
  26. import org.xml.sax.SAXException;
  27. import org.xml.sax.XMLReader;
  28. import org.xml.sax.helpers.XMLFilterImpl;
  29. /**
  30. * Test driver class for FO tree tests.
  31. */
  32. public class FOTreeTester {
  33. /**
  34. * Runs a test.
  35. * @param testFile the test file.
  36. * @throws Exception if a test or FOP itself fails
  37. */
  38. public void runTest(File testFile) throws Exception {
  39. ResultCollector collector = ResultCollector.getInstance();
  40. collector.reset();
  41. SAXParserFactory spf = SAXParserFactory.newInstance();
  42. spf.setNamespaceAware(true);
  43. spf.setValidating(false);
  44. SAXParser parser = spf.newSAXParser();
  45. XMLReader reader = parser.getXMLReader();
  46. //Setup FOP for area tree rendering
  47. FopFactory fopFactory = FopFactory.newInstance();
  48. fopFactory.addElementMapping(new TestElementMapping());
  49. FOUserAgent ua = fopFactory.newFOUserAgent();
  50. ua.setBaseURL(testFile.getParentFile().toURL().toString());
  51. ua.setFOEventHandlerOverride(new DummyFOEventHandler(ua));
  52. //Used to set values in the user agent through processing instructions
  53. reader = new PIListener(reader, ua);
  54. Fop fop = fopFactory.newFop(ua);
  55. reader.setContentHandler(fop.getDefaultHandler());
  56. reader.setDTDHandler(fop.getDefaultHandler());
  57. reader.setErrorHandler(fop.getDefaultHandler());
  58. reader.setEntityResolver(fop.getDefaultHandler());
  59. reader.parse(testFile.toURL().toExternalForm());
  60. List results = collector.getResults();
  61. if (results.size() > 0) {
  62. for (int i = 0; i < results.size(); i++) {
  63. System.out.println(((Exception)results.get(i)).getMessage());
  64. }
  65. throw (Exception)results.get(0);
  66. }
  67. }
  68. private class PIListener extends XMLFilterImpl {
  69. private FOUserAgent userAgent;
  70. public PIListener(XMLReader parent, FOUserAgent userAgent) {
  71. super(parent);
  72. this.userAgent = userAgent;
  73. }
  74. /** @see org.xml.sax.helpers.XMLFilterImpl */
  75. public void processingInstruction(String target, String data) throws SAXException {
  76. if ("fop-useragent-break-indent-inheritance".equals(target)) {
  77. userAgent.getFactory().setBreakIndentInheritanceOnReferenceAreaBoundary(
  78. Boolean.valueOf(data).booleanValue());
  79. }
  80. super.processingInstruction(target, data);
  81. }
  82. }
  83. }