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.

AFPResourceInfo.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.afp.extensions;
  19. import org.xml.sax.helpers.AttributesImpl;
  20. import org.xml.sax.ContentHandler;
  21. import org.xml.sax.SAXException;
  22. //import org.xml.sax.helpers.AttributesImpl;
  23. /**
  24. * An AFP resource group configuration definition for a document
  25. */
  26. public class AFPResourceInfo extends AFPExtensionAttachment {
  27. private static final long serialVersionUID = -7333967815112216396L;
  28. /** AFP resource groups are stored in an external resource file */
  29. private static final int LEVEL_INLINE = 0;
  30. /** AFP resource groups are stored at page level */
  31. private static final int LEVEL_PAGE = 1;
  32. /** AFP resource groups are stored at page group level */
  33. private static final int LEVEL_PAGE_GROUP = 2;
  34. /** AFP resource groups are stored within the document at document level */
  35. private static final int LEVEL_DOCUMENT = 3;
  36. /** AFP resource groups are stored outside the document at print file level */
  37. private static final int LEVEL_PRINT_FILE = 4;
  38. /** AFP resource groups are stored in an external resource file */
  39. private static final int LEVEL_EXTERNAL = 5;
  40. private static final String LEVEL_NAME_INLINE = "inline";
  41. private static final String LEVEL_NAME_PAGE = "page";
  42. private static final String LEVEL_NAME_PAGE_GROUP = "page-group";
  43. private static final String LEVEL_NAME_DOCUMENT = "document";
  44. private static final String LEVEL_NAME_PRINT_FILE = "print-file";
  45. private static final String LEVEL_NAME_EXTERNAL = "external";
  46. /**
  47. * level token/name mapping
  48. */
  49. private static final String[] LEVEL_NAME_MAP = {
  50. LEVEL_NAME_INLINE, LEVEL_NAME_PAGE, LEVEL_NAME_PAGE_GROUP,
  51. LEVEL_NAME_DOCUMENT, LEVEL_NAME_PRINT_FILE, LEVEL_NAME_EXTERNAL
  52. };
  53. /**
  54. * the <afp:resource-info/> element name
  55. */
  56. public static final String ELEMENT = "resource-info";
  57. /**
  58. * the level at which resource groups are placed
  59. */
  60. private int level = -1;
  61. /**
  62. * the destination filename for resource groups with level = "external"
  63. */
  64. private String dest;
  65. /**
  66. * Default constructor.
  67. */
  68. public AFPResourceInfo() {
  69. super(ELEMENT);
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public String toString() {
  75. return "AFPResourceInfo("
  76. + "name=" + name + ", "
  77. + (level > -1 ? "level=" + LEVEL_NAME_MAP[level] : "")
  78. + (dest != null ? ", dest=" + getDestination() : "" ) + ")";
  79. }
  80. /**
  81. * Sets the destination filename of where resources
  82. * are to be stored for this document
  83. * @param destination the location of the external resource group file
  84. */
  85. public void setExternalDestination(String destination) {
  86. this.dest = destination;
  87. }
  88. /**
  89. * Returns the destination filename of where external resources
  90. * are to be stored for this document.
  91. * @return the destination AFP external resource filename
  92. */
  93. public String getDestination() {
  94. return this.dest;
  95. }
  96. /**
  97. * Sets the level at which resource groups are stored
  98. * @param level the resource group level
  99. */
  100. public void setLevel(int level) {
  101. this.level = level;
  102. }
  103. /**
  104. * Sets the level at which resource groups are stored
  105. * @param name the level name
  106. */
  107. public void setLevel(String name) {
  108. if (name != null) {
  109. for (int i = 0; i < LEVEL_NAME_MAP.length; i++) {
  110. if (name.toLowerCase().equals(LEVEL_NAME_MAP[i])) {
  111. this.level = i;
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Returns the level at which resource groups are stored
  118. * @return the level at which resource groups are stored
  119. */
  120. public int getLevel() {
  121. return this.level;
  122. }
  123. private static final String ATT_LEVEL = "level";
  124. private static final String ATT_DEST = "dest";
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public void toSAX(ContentHandler handler) throws SAXException {
  129. AttributesImpl atts = new AttributesImpl();
  130. // name
  131. if (hasName()) {
  132. atts.addAttribute(null, ATT_NAME, ATT_NAME, "CDATA", super.getName());
  133. }
  134. // level
  135. if (level > 0) {
  136. atts.addAttribute(null, ATT_LEVEL, ATT_LEVEL, "CDATA", LEVEL_NAME_MAP[level]);
  137. // dest
  138. if (level == LEVEL_EXTERNAL && dest != null && dest.length() > 0) {
  139. atts.addAttribute(null, ATT_DEST, ATT_DEST, "CDATA", dest);
  140. }
  141. }
  142. handler.startElement(CATEGORY, elementName, elementName, atts);
  143. handler.endElement(CATEGORY, elementName, elementName);
  144. }
  145. /**
  146. * @return true if this resource group is to be stored externally
  147. */
  148. public boolean isExternalLevel() {
  149. return level == LEVEL_EXTERNAL;
  150. }
  151. }