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.

StructureTree2SAXEventAdapter.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 java.util.Locale;
  20. import org.xml.sax.Attributes;
  21. import org.xml.sax.ContentHandler;
  22. import org.xml.sax.SAXException;
  23. import org.xml.sax.helpers.AttributesImpl;
  24. import org.apache.fop.fo.FOElementMapping;
  25. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  26. import org.apache.fop.fo.extensions.InternalElementMapping;
  27. import org.apache.fop.render.intermediate.IFConstants;
  28. /**
  29. * Converts structure tree events to SAX events.
  30. */
  31. public final class StructureTree2SAXEventAdapter implements StructureTreeEventHandler {
  32. private final ContentHandler contentHandler;
  33. private StructureTree2SAXEventAdapter(ContentHandler currentContentHandler) {
  34. this.contentHandler = currentContentHandler;
  35. }
  36. /**
  37. * Factory method that creates a new instance.
  38. * @param contentHandler The handler that receives SAX events
  39. */
  40. public static StructureTreeEventHandler newInstance(ContentHandler contentHandler) {
  41. return new StructureTree2SAXEventAdapter(contentHandler);
  42. }
  43. /** {@inheritDoc} */
  44. public void startPageSequence(Locale locale) {
  45. try {
  46. contentHandler.startPrefixMapping(
  47. InternalElementMapping.STANDARD_PREFIX, InternalElementMapping.URI);
  48. contentHandler.startPrefixMapping(
  49. ExtensionElementMapping.STANDARD_PREFIX, ExtensionElementMapping.URI);
  50. contentHandler.startElement(IFConstants.NAMESPACE,
  51. IFConstants.EL_STRUCTURE_TREE, IFConstants.EL_STRUCTURE_TREE,
  52. new AttributesImpl());
  53. } catch (SAXException e) {
  54. throw new RuntimeException(e);
  55. }
  56. }
  57. /** {@inheritDoc} */
  58. public void endPageSequence() {
  59. try {
  60. contentHandler.endElement(IFConstants.NAMESPACE, IFConstants.EL_STRUCTURE_TREE,
  61. IFConstants.EL_STRUCTURE_TREE);
  62. contentHandler.endPrefixMapping(
  63. ExtensionElementMapping.STANDARD_PREFIX);
  64. contentHandler.endPrefixMapping(
  65. InternalElementMapping.STANDARD_PREFIX);
  66. } catch (SAXException e) {
  67. throw new RuntimeException(e);
  68. }
  69. }
  70. /** {@inheritDoc} */
  71. public void startNode(String name, Attributes attributes) {
  72. try {
  73. contentHandler.startElement(FOElementMapping.URI, name,
  74. FOElementMapping.STANDARD_PREFIX + ":" + name,
  75. attributes);
  76. } catch (SAXException e) {
  77. throw new RuntimeException(e);
  78. }
  79. }
  80. /** {@inheritDoc} */
  81. public void endNode(String name) {
  82. try {
  83. contentHandler.endElement(FOElementMapping.URI, name,
  84. FOElementMapping.STANDARD_PREFIX + ":" + name);
  85. } catch (SAXException e) {
  86. throw new RuntimeException(e);
  87. }
  88. }
  89. }