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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * @return -
  40. */
  41. public static StructureTreeEventHandler newInstance(ContentHandler contentHandler) {
  42. return new StructureTree2SAXEventAdapter(contentHandler);
  43. }
  44. /** {@inheritDoc} */
  45. public void startPageSequence(Locale locale) {
  46. try {
  47. contentHandler.startPrefixMapping(
  48. InternalElementMapping.STANDARD_PREFIX, InternalElementMapping.URI);
  49. contentHandler.startPrefixMapping(
  50. ExtensionElementMapping.STANDARD_PREFIX, ExtensionElementMapping.URI);
  51. contentHandler.startElement(IFConstants.NAMESPACE,
  52. IFConstants.EL_STRUCTURE_TREE, IFConstants.EL_STRUCTURE_TREE,
  53. new AttributesImpl());
  54. } catch (SAXException e) {
  55. throw new RuntimeException(e);
  56. }
  57. }
  58. /** {@inheritDoc} */
  59. public void endPageSequence() {
  60. try {
  61. contentHandler.endElement(IFConstants.NAMESPACE, IFConstants.EL_STRUCTURE_TREE,
  62. IFConstants.EL_STRUCTURE_TREE);
  63. contentHandler.endPrefixMapping(
  64. ExtensionElementMapping.STANDARD_PREFIX);
  65. contentHandler.endPrefixMapping(
  66. InternalElementMapping.STANDARD_PREFIX);
  67. } catch (SAXException e) {
  68. throw new RuntimeException(e);
  69. }
  70. }
  71. /** {@inheritDoc} */
  72. public StructureTreeElement startNode(String name, Attributes attributes) {
  73. try {
  74. if (name.equals("#PCDATA")) {
  75. name = "marked-content";
  76. contentHandler.startElement(IFConstants.NAMESPACE, name,
  77. name, attributes);
  78. } else {
  79. contentHandler.startElement(FOElementMapping.URI, name,
  80. FOElementMapping.STANDARD_PREFIX + ":" + name,
  81. attributes);
  82. }
  83. return null;
  84. } catch (SAXException e) {
  85. throw new RuntimeException(e);
  86. }
  87. }
  88. /** {@inheritDoc} */
  89. public void endNode(String name) {
  90. try {
  91. contentHandler.endElement(FOElementMapping.URI, name,
  92. FOElementMapping.STANDARD_PREFIX + ":" + name);
  93. } catch (SAXException e) {
  94. throw new RuntimeException(e);
  95. }
  96. }
  97. /** {@inheritDoc} */
  98. public StructureTreeElement startImageNode(String name, Attributes attributes) {
  99. return startNode(name, attributes);
  100. }
  101. /** {@inheritDoc} */
  102. public StructureTreeElement startReferencedNode(String name, Attributes attributes) {
  103. return startNode(name, attributes);
  104. }
  105. }