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 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2005 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.transform.Source;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.sax.SAXResult;
  23. import javax.xml.transform.sax.SAXTransformerFactory;
  24. import javax.xml.transform.stream.StreamSource;
  25. import org.apache.fop.apps.FOUserAgent;
  26. import org.apache.fop.apps.Fop;
  27. import org.apache.fop.apps.MimeConstants;
  28. import org.apache.fop.fotreetest.ext.TestElementMapping;
  29. /**
  30. * Test driver class for FO tree tests.
  31. */
  32. public class FOTreeTester {
  33. private SAXTransformerFactory tfactory
  34. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  35. /**
  36. * Runs a test.
  37. * @param testFile the test file.
  38. * @throws Exception if a test or FOP itself fails
  39. */
  40. public void runTest(File testFile) throws Exception {
  41. ResultCollector collector = ResultCollector.getInstance();
  42. collector.reset();
  43. //Setup identity Transformer
  44. Transformer transformer = tfactory.newTransformer();
  45. Source src = new StreamSource(testFile);
  46. //Setup FOP for area tree rendering
  47. FOUserAgent ua = new FOUserAgent();
  48. ua.setBaseURL(testFile.getParentFile().toURL().toString());
  49. ua.setFOEventHandlerOverride(new DummyFOEventHandler(ua));
  50. ua.addElementMapping(new TestElementMapping());
  51. Fop fop = new Fop(MimeConstants.MIME_FOP_AREA_TREE, ua);
  52. SAXResult fores = new SAXResult(fop.getDefaultHandler());
  53. transformer.transform(src, fores);
  54. List results = collector.getResults();
  55. if (results.size() > 0) {
  56. for (int i = 0; i < results.size(); i++) {
  57. System.out.println(((Exception)results.get(i)).getMessage());
  58. }
  59. throw (Exception)results.get(0);
  60. }
  61. }
  62. }