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.

AFPResourceLevel.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.afp;
  19. import java.net.URI;
  20. import static org.apache.fop.afp.AFPResourceLevel.ResourceType.DOCUMENT;
  21. import static org.apache.fop.afp.AFPResourceLevel.ResourceType.EXTERNAL;
  22. import static org.apache.fop.afp.AFPResourceLevel.ResourceType.INLINE;
  23. import static org.apache.fop.afp.AFPResourceLevel.ResourceType.PAGE;
  24. import static org.apache.fop.afp.AFPResourceLevel.ResourceType.PAGE_GROUP;
  25. import static org.apache.fop.afp.AFPResourceLevel.ResourceType.PRINT_FILE;
  26. /**
  27. * A resource level
  28. */
  29. public class AFPResourceLevel {
  30. public enum ResourceType {
  31. /** directly in page **/
  32. INLINE("inline"),
  33. /** page level **/
  34. PAGE("page"),
  35. /** page group level **/
  36. PAGE_GROUP("page-group"),
  37. /** document level **/
  38. DOCUMENT("document"),
  39. /** print file level **/
  40. PRINT_FILE("print-file"),
  41. /** external level **/
  42. EXTERNAL("external");
  43. private final String name;
  44. private ResourceType(String name) {
  45. this.name = name;
  46. }
  47. public static ResourceType getValueOf(String levelString) {
  48. for (ResourceType resType : ResourceType.values()) {
  49. if (resType.name.equalsIgnoreCase(levelString)) {
  50. return resType;
  51. }
  52. }
  53. return null;
  54. }
  55. public String getName() {
  56. return name;
  57. }
  58. }
  59. private URI extUri;
  60. private final ResourceType resourceType;
  61. /**
  62. * Sets the resource placement level within the AFP output
  63. *
  64. * @param levelString the resource level (page, page-group, document, print-file or external)
  65. * @return true if the resource level was successfully set
  66. */
  67. public static AFPResourceLevel valueOf(String levelString) {
  68. ResourceType resType = ResourceType.getValueOf(levelString);
  69. return resType != null ? new AFPResourceLevel(resType) : null;
  70. }
  71. /**
  72. * Main constructor
  73. *
  74. * @param resourceType the resource type
  75. */
  76. public AFPResourceLevel(ResourceType resourceType) {
  77. this.resourceType = resourceType;
  78. }
  79. /**
  80. * Returns true if this is at page level
  81. *
  82. * @return true if this is at page level
  83. */
  84. public boolean isPage() {
  85. return resourceType == PAGE;
  86. }
  87. /**
  88. * Returns true if this is at page group level
  89. *
  90. * @return true if this is at page group level
  91. */
  92. public boolean isPageGroup() {
  93. return resourceType == PAGE_GROUP;
  94. }
  95. /**
  96. * Returns true if this is at document level
  97. *
  98. * @return true if this is at document level
  99. */
  100. public boolean isDocument() {
  101. return resourceType == DOCUMENT;
  102. }
  103. /**
  104. * Returns true if this is at external level
  105. *
  106. * @return true if this is at external level
  107. */
  108. public boolean isExternal() {
  109. return resourceType == EXTERNAL;
  110. }
  111. /**
  112. * Returns true if this is at print-file level
  113. *
  114. * @return true if this is at print-file level
  115. */
  116. public boolean isPrintFile() {
  117. return resourceType == PRINT_FILE;
  118. }
  119. /**
  120. * Returns true if this resource level is inline
  121. *
  122. * @return true if this resource level is inline
  123. */
  124. public boolean isInline() {
  125. return resourceType == INLINE;
  126. }
  127. /**
  128. * Returns the URI of the external resource group.
  129. *
  130. * @return the destination URI of the external resource group
  131. */
  132. public URI getExternalURI() {
  133. return this.extUri;
  134. }
  135. /**
  136. * Sets the URI of the external resource group.
  137. *
  138. * @param uri the URI of the external resource group
  139. */
  140. public void setExternalUri(URI uri) {
  141. this.extUri = uri;
  142. }
  143. /** {@inheritDoc} */
  144. public String toString() {
  145. return resourceType + (isExternal() ? ", uri=" + extUri : "");
  146. }
  147. /** {@inheritDoc} */
  148. public boolean equals(Object obj) {
  149. if (this == obj) {
  150. return true;
  151. }
  152. if ((obj == null) || !(obj instanceof AFPResourceLevel)) {
  153. return false;
  154. }
  155. AFPResourceLevel rl = (AFPResourceLevel)obj;
  156. return (resourceType == rl.resourceType)
  157. && (extUri == rl.extUri
  158. || extUri != null && extUri.equals(rl.extUri));
  159. }
  160. /** {@inheritDoc} */
  161. public int hashCode() {
  162. int hash = 7;
  163. hash = 31 * hash + resourceType.hashCode();
  164. hash = 31 * hash + (null == extUri ? 0 : extUri.hashCode());
  165. return hash;
  166. }
  167. }