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.

LinkResolver.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.area;
  19. import java.io.Serializable;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. /**
  23. * Link resolving for resolving internal links.
  24. */
  25. public class LinkResolver implements Resolvable, Serializable {
  26. private static final long serialVersionUID = -7102134165192960718L;
  27. private boolean resolved;
  28. private String idRef;
  29. private Area area;
  30. private transient List<Resolvable> dependents;
  31. /**
  32. * Create a new link resolver.
  33. *
  34. * @param id the id to resolve
  35. * @param a the area that will have the link attribute
  36. */
  37. public LinkResolver(String id, Area a) {
  38. idRef = id;
  39. area = a;
  40. }
  41. /**
  42. * @return true if this link is resolved
  43. */
  44. public boolean isResolved() {
  45. return resolved;
  46. }
  47. /**
  48. * Get the references for this link.
  49. *
  50. * @return the String array of references.
  51. */
  52. public String[] getIDRefs() {
  53. return new String[] {idRef};
  54. }
  55. /**
  56. * Resolve by adding an internal link to the first PageViewport in the list.
  57. *
  58. * {@inheritDoc}
  59. */
  60. public void resolveIDRef(String id, List<PageViewport> pages) {
  61. resolveIDRef(id, pages.get(0));
  62. }
  63. /**
  64. * Resolve by adding an InternalLink trait to the area
  65. *
  66. * @param id the target id (should be equal to the object's idRef)
  67. * @param pv the PageViewport containing the first area with the given id
  68. */
  69. public void resolveIDRef(String id, PageViewport pv) {
  70. if (idRef.equals(id) && pv != null) {
  71. resolved = true;
  72. if (area != null) {
  73. Trait.InternalLink iLink = new Trait.InternalLink(pv.getKey(), idRef);
  74. area.addTrait(Trait.INTERNAL_LINK, iLink);
  75. area = null; // break circular reference from basic link area to this resolver
  76. }
  77. resolveDependents(id, pv);
  78. }
  79. }
  80. /**
  81. * Add dependent resolvable. Used to resolve second-order resolvables that
  82. * depend on resolution of this resolver.
  83. * @param dependent resolvable
  84. */
  85. public void addDependent(Resolvable dependent) {
  86. if (dependents == null) {
  87. dependents = new ArrayList<Resolvable>();
  88. }
  89. dependents.add(dependent);
  90. }
  91. private void resolveDependents(String id, PageViewport pv) {
  92. if (dependents != null) {
  93. List<PageViewport> pages = new ArrayList<PageViewport>();
  94. pages.add(pv);
  95. for (Resolvable r : dependents) {
  96. r.resolveIDRef(id, pages);
  97. }
  98. }
  99. }
  100. }