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.

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