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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. import org.apache.fop.util.XMLConstants;
  29. /**
  30. * Converts structure tree events to SAX events.
  31. */
  32. public final class StructureTree2SAXEventAdapter implements StructureTreeEventHandler {
  33. private final ContentHandler contentHandler;
  34. private StructureTree2SAXEventAdapter(ContentHandler currentContentHandler) {
  35. this.contentHandler = currentContentHandler;
  36. }
  37. /**
  38. * Factory method that creates a new instance.
  39. * @param contentHandler The handler that receives SAX events
  40. * @return -
  41. */
  42. public static StructureTreeEventHandler newInstance(ContentHandler contentHandler) {
  43. return new StructureTree2SAXEventAdapter(contentHandler);
  44. }
  45. /** {@inheritDoc} */
  46. public void startPageSequence(Locale locale, String role) {
  47. try {
  48. AttributesImpl attributes = new AttributesImpl();
  49. if (role != null) {
  50. attributes.addAttribute("", "type", "type", XMLConstants.CDATA, role);
  51. }
  52. contentHandler.startPrefixMapping(
  53. InternalElementMapping.STANDARD_PREFIX, InternalElementMapping.URI);
  54. contentHandler.startPrefixMapping(
  55. ExtensionElementMapping.STANDARD_PREFIX, ExtensionElementMapping.URI);
  56. contentHandler.startElement(IFConstants.NAMESPACE,
  57. IFConstants.EL_STRUCTURE_TREE, IFConstants.EL_STRUCTURE_TREE,
  58. attributes);
  59. } catch (SAXException e) {
  60. throw new RuntimeException(e);
  61. }
  62. }
  63. /** {@inheritDoc} */
  64. public void endPageSequence() {
  65. try {
  66. contentHandler.endElement(IFConstants.NAMESPACE, IFConstants.EL_STRUCTURE_TREE,
  67. IFConstants.EL_STRUCTURE_TREE);
  68. contentHandler.endPrefixMapping(
  69. ExtensionElementMapping.STANDARD_PREFIX);
  70. contentHandler.endPrefixMapping(
  71. InternalElementMapping.STANDARD_PREFIX);
  72. } catch (SAXException e) {
  73. throw new RuntimeException(e);
  74. }
  75. }
  76. /** {@inheritDoc} */
  77. public StructureTreeElement startNode(String name, Attributes attributes, StructureTreeElement parent) {
  78. try {
  79. if (name.equals("#PCDATA")) {
  80. name = "marked-content";
  81. contentHandler.startElement(IFConstants.NAMESPACE, name,
  82. name, attributes);
  83. } else {
  84. contentHandler.startElement(FOElementMapping.URI, name,
  85. FOElementMapping.STANDARD_PREFIX + ":" + name,
  86. attributes);
  87. }
  88. return null;
  89. } catch (SAXException e) {
  90. throw new RuntimeException(e);
  91. }
  92. }
  93. /** {@inheritDoc} */
  94. public void endNode(String name) {
  95. try {
  96. contentHandler.endElement(FOElementMapping.URI, name,
  97. FOElementMapping.STANDARD_PREFIX + ":" + name);
  98. } catch (SAXException e) {
  99. throw new RuntimeException(e);
  100. }
  101. }
  102. /** {@inheritDoc} */
  103. public StructureTreeElement startImageNode(String name, Attributes attributes, StructureTreeElement parent) {
  104. return startNode(name, attributes, null);
  105. }
  106. /** {@inheritDoc} */
  107. public StructureTreeElement startReferencedNode(String name, Attributes attributes, StructureTreeElement parent) {
  108. return startNode(name, attributes, null);
  109. }
  110. }