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.

PDFStructureTreeBuilder.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.render.pdf;
  19. import java.util.LinkedList;
  20. import java.util.Locale;
  21. import org.xml.sax.Attributes;
  22. import org.apache.fop.accessibility.StructureTreeEventHandler;
  23. import org.apache.fop.events.EventBroadcaster;
  24. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  25. import org.apache.fop.fo.extensions.InternalElementMapping;
  26. import org.apache.fop.pdf.PDFFactory;
  27. import org.apache.fop.pdf.PDFStructElem;
  28. class PDFStructureTreeBuilder implements StructureTreeEventHandler {
  29. private PDFFactory pdfFactory;
  30. private PDFLogicalStructureHandler logicalStructureHandler;
  31. private EventBroadcaster eventBroadcaster;
  32. private LinkedList<PDFStructElem> ancestors = new LinkedList<PDFStructElem>();
  33. void setPdfFactory(PDFFactory pdfFactory) {
  34. this.pdfFactory = pdfFactory;
  35. }
  36. void setLogicalStructureHandler(PDFLogicalStructureHandler logicalStructureHandler) {
  37. this.logicalStructureHandler = logicalStructureHandler;
  38. }
  39. void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
  40. this.eventBroadcaster = eventBroadcaster;
  41. }
  42. public void startPageSequence(Locale locale) {
  43. ancestors = new LinkedList<PDFStructElem>();
  44. ancestors.add(logicalStructureHandler.createPageSequence(locale));
  45. }
  46. public void endPageSequence() {
  47. }
  48. public void startNode(String name, Attributes attributes) {
  49. PDFStructElem parent = ancestors.getFirst();
  50. String role = attributes.getValue("role");
  51. PDFStructElem created = pdfFactory.makeStructureElement(
  52. FOToPDFRoleMap.mapFormattingObject(name, role, parent,
  53. eventBroadcaster), parent);
  54. if (ancestors.size() <= 2) { // TODO remove
  55. parent.addKid(created);
  56. }
  57. String ptr = attributes.getValue(InternalElementMapping.URI, "ptr");
  58. if (ptr != null) {
  59. logicalStructureHandler.addStructurePointer(ptr, created);
  60. }
  61. if (name.equals("external-graphic") || name.equals("instream-foreign-object")) {
  62. String altTextNode = attributes.getValue(ExtensionElementMapping.URI, "alt-text");
  63. if (altTextNode != null) {
  64. created.put("Alt", altTextNode);
  65. } else {
  66. created.put("Alt", "No alternate text specified");
  67. }
  68. }
  69. ancestors.addFirst(created);
  70. }
  71. public void endNode(String name) {
  72. ancestors.removeFirst();
  73. }
  74. }