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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.accessibility;
  19. import javax.xml.transform.Source;
  20. import javax.xml.transform.Templates;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.TransformerConfigurationException;
  23. import javax.xml.transform.sax.SAXTransformerFactory;
  24. import javax.xml.transform.sax.TransformerHandler;
  25. import javax.xml.transform.stream.StreamSource;
  26. import org.xml.sax.helpers.DefaultHandler;
  27. import org.apache.fop.apps.FOPException;
  28. import org.apache.fop.apps.FOUserAgent;
  29. /**
  30. * Helper class for FOP's accessibility features.
  31. */
  32. public final class Accessibility {
  33. /** Constant string for the rendering options key to enable accessibility features. */
  34. public static final String ACCESSIBILITY = "accessibility";
  35. // TODO what if the default factory is not a SAXTransformerFactory?
  36. private static SAXTransformerFactory tfactory
  37. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  38. private static Templates addPtrTemplates;
  39. private static Templates reduceFOTreeTemplates;
  40. private Accessibility() { }
  41. /**
  42. * Decorates the given handler so the structure tree used for accessibility
  43. * features can be branched off the main content stream.
  44. * @param handler the handler to decorate
  45. * @param userAgent the user agent
  46. * @return the decorated handler
  47. * @throws FOPException if an error occurs setting up the decoration
  48. */
  49. public static DefaultHandler decorateDefaultHandler(DefaultHandler handler,
  50. FOUserAgent userAgent) throws FOPException {
  51. try {
  52. setupTemplates();
  53. TransformerHandler addPtr = tfactory.newTransformerHandler(addPtrTemplates);
  54. Transformer reduceFOTree = reduceFOTreeTemplates.newTransformer();
  55. return new AccessibilityPreprocessor(addPtr, reduceFOTree, userAgent, handler);
  56. } catch (TransformerConfigurationException e) {
  57. throw new FOPException(e);
  58. }
  59. }
  60. private static synchronized void setupTemplates() throws TransformerConfigurationException {
  61. if (addPtrTemplates == null) {
  62. addPtrTemplates = loadTemplates("addPtr.xsl");
  63. }
  64. if (reduceFOTreeTemplates == null) {
  65. reduceFOTreeTemplates = loadTemplates("reduceFOTree.xsl");
  66. }
  67. }
  68. private static Templates loadTemplates(String source) throws TransformerConfigurationException {
  69. Source src = new StreamSource(Accessibility.class.getResource(source).toExternalForm());
  70. return tfactory.newTemplates(src);
  71. }
  72. }