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.

URIProperty.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.fo.properties;
  19. import java.net.URI;
  20. import java.net.URISyntaxException;
  21. import org.apache.fop.datatypes.URISpecification;
  22. import org.apache.fop.fo.FObj;
  23. import org.apache.fop.fo.PropertyList;
  24. import org.apache.fop.fo.expr.PropertyException;
  25. import org.apache.fop.util.CompareUtil;
  26. import static org.apache.fop.fo.Constants.PR_X_XML_BASE;
  27. /**
  28. * Class modeling a property that has a value of type <uri-specification>.
  29. * The purpose is mainly to support resolution of a specified
  30. * relative URI against a specified or inherited <code>xml:base</code>
  31. * during the property refinement stage.
  32. * If no <code>xml:base</code> has been specified, only the original URI, as
  33. * it appears in the source document, is stored as the property's specified
  34. * value.
  35. */
  36. public class URIProperty extends Property {
  37. /** will be null if the URI is not resolved against an xml:base */
  38. private URI resolvedURI;
  39. /**
  40. * Default constructor, to create a {@link URIProperty} from a
  41. * {@code java.net.URI} directly.
  42. * @param uri a resolved {@code java.net.URI}
  43. */
  44. protected URIProperty(URI uri) {
  45. this.resolvedURI = uri;
  46. }
  47. /**
  48. * Alternate constructor, to create a {@link URIProperty} from a
  49. * string representation.
  50. * @param uri a {@code java.lang.String} representing the URI
  51. * @param resolve flag indicating whether this URI was the result of resolution
  52. * @throws IllegalArgumentException if the URI should be resolved, but is not valid.
  53. */
  54. private URIProperty(String uri, boolean resolve) {
  55. if (resolve && !(uri == null || "".equals(uri))) {
  56. this.resolvedURI = URI.create(uri);
  57. } else {
  58. setSpecifiedValue(uri);
  59. }
  60. }
  61. /**
  62. * Return a string representing the resolved URI, or the
  63. * specified value if the URI is not resolved against
  64. * an <code>xml:base</code>
  65. * @return a string representing the URI
  66. */
  67. @Override
  68. public String getString() {
  69. if (resolvedURI == null) {
  70. return getSpecifiedValue();
  71. } else {
  72. return resolvedURI.toString();
  73. }
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public String toString() {
  78. return this.getString();
  79. }
  80. /**
  81. * Inner {@link PropertyMaker} subclass responsible
  82. * for making instances of this type.
  83. */
  84. public static class Maker extends PropertyMaker {
  85. /**
  86. * Create a maker for the given property id
  87. *
  88. * @param propId the id of the property for which a Maker should be created
  89. */
  90. public Maker(int propId) {
  91. super(propId);
  92. }
  93. /**
  94. * {@inheritDoc}
  95. * Check if {@code xml:base} has been specified and whether the
  96. * given {@code value} represents a relative URI. If so, create
  97. * a property representing the resolved URI.
  98. */
  99. @Override
  100. public Property make(PropertyList propertyList, String value,
  101. FObj fo) throws PropertyException {
  102. Property p = null;
  103. //special treament for data: URIs
  104. if (value.matches("(?s)^(url\\(('|\")?)?data:.*$")) {
  105. p = new URIProperty(value, false);
  106. } else {
  107. try {
  108. URI specifiedURI = new URI(URISpecification.escapeURI(value));
  109. URIProperty xmlBase = (URIProperty)propertyList.get(PR_X_XML_BASE, true, false);
  110. if (xmlBase == null) {
  111. //xml:base undefined
  112. if (this.propId == PR_X_XML_BASE) {
  113. //if current property is xml:base, define a new one
  114. p = new URIProperty(specifiedURI);
  115. p.setSpecifiedValue(value);
  116. } else {
  117. //otherwise, just store the specified value (for backward compatibility)
  118. p = new URIProperty(value, false);
  119. }
  120. } else {
  121. //xml:base defined, so resolve
  122. p = new URIProperty(xmlBase.resolvedURI.resolve(specifiedURI));
  123. p.setSpecifiedValue(value);
  124. }
  125. } catch (URISyntaxException use) {
  126. // Let PropertyList propagate the exception
  127. throw new PropertyException("Invalid URI specified");
  128. }
  129. }
  130. return p;
  131. }
  132. }
  133. @Override
  134. public int hashCode() {
  135. final int prime = 31;
  136. int result = 1;
  137. result = prime * result + CompareUtil.getHashCode(getSpecifiedValue());
  138. result = prime * result + CompareUtil.getHashCode(resolvedURI);
  139. return result;
  140. }
  141. @Override
  142. public boolean equals(Object obj) {
  143. if (obj == null) {
  144. return false;
  145. }
  146. if (!(obj instanceof URIProperty)) {
  147. return false;
  148. }
  149. URIProperty other = (URIProperty) obj;
  150. return CompareUtil.equal(getSpecifiedValue(), other.getSpecifiedValue())
  151. && CompareUtil.equal(resolvedURI, other.resolvedURI);
  152. }
  153. }