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.

PageGroup.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.afp.modca;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.io.UnsupportedEncodingException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. /**
  24. * A page group is used in the data stream to define a named, logical grouping
  25. * of sequential pages. Page groups are delimited by begin-end structured fields
  26. * that carry the name of the page group. Page groups are defined so that the
  27. * pages that comprise the group can be referenced or processed as a single
  28. * entity. Page groups are often processed in stand-alone fashion; that is, they
  29. * are indexed, retrieved, and presented outside the context of the containing
  30. * document.
  31. *
  32. * @author <a href="mailto:pete@townsend.uk.com">Pete Townsend </a>
  33. */
  34. public class PageGroup extends AbstractNamedAFPObject {
  35. /**
  36. * The pages contained within this group
  37. */
  38. private List _objects = new ArrayList();
  39. /**
  40. * The tag logical elements contained within this group
  41. */
  42. private List _tagLogicalElements = new ArrayList();
  43. /**
  44. * The page state
  45. */
  46. private boolean _complete = false;
  47. /**
  48. * Constructor for the PageGroup.
  49. *
  50. * @param name
  51. * the name of the page group
  52. */
  53. public PageGroup(String name) {
  54. super(name);
  55. }
  56. /**
  57. * Adds a page object to the group.
  58. *
  59. * @param page
  60. * the page object to add
  61. */
  62. public void addPage(PageObject page) {
  63. if (!_objects.contains(page)) {
  64. _objects.add(page);
  65. }
  66. }
  67. /**
  68. * @return the name of the page group
  69. */
  70. public String getName() {
  71. return _name;
  72. }
  73. /**
  74. * Creates a TagLogicalElement on the page.
  75. *
  76. * @param name
  77. * the name of the tag
  78. * @param value
  79. * the value of the tag
  80. */
  81. public void createTagLogicalElement(String name, String value) {
  82. TagLogicalElement tle = new TagLogicalElement(name, value);
  83. _tagLogicalElements.add(tle);
  84. }
  85. /**
  86. * Creates an InvokeMediaMap on the page.
  87. *
  88. * @param name
  89. * the name of the media map
  90. */
  91. public void createInvokeMediumMap(String name) {
  92. InvokeMediumMap imm = new InvokeMediumMap(name);
  93. _objects.add(imm);
  94. }
  95. /**
  96. * Method to mark the end of the page group.
  97. */
  98. public void endPageGroup() {
  99. _complete = true;
  100. }
  101. /**
  102. * Returns an indication if the page group is complete
  103. */
  104. public boolean isComplete() {
  105. return _complete;
  106. }
  107. /**
  108. * Accessor method to write the AFP datastream for the page group.
  109. * @param os The stream to write to
  110. * @throws java.io.IOException
  111. */
  112. public void writeDataStream(OutputStream os)
  113. throws IOException {
  114. writeStart(os);
  115. writeObjectList(_tagLogicalElements, os);
  116. writeObjectList(_objects, os);
  117. writeEnd(os);
  118. }
  119. /**
  120. * Helper method to write the start of the page group.
  121. * @param os The stream to write to
  122. */
  123. private void writeStart(OutputStream os)
  124. throws IOException {
  125. byte[] data = new byte[17];
  126. data[0] = 0x5A; // Structured field identifier
  127. data[1] = 0x00; // Length byte 1
  128. data[2] = 0x10; // Length byte 2
  129. data[3] = (byte) 0xD3; // Structured field id byte 1
  130. data[4] = (byte) 0xA8; // Structured field id byte 2
  131. data[5] = (byte) 0xAD; // Structured field id byte 3
  132. data[6] = 0x00; // Flags
  133. data[7] = 0x00; // Reserved
  134. data[8] = 0x00; // Reserved
  135. for (int i = 0; i < _nameBytes.length; i++) {
  136. data[9 + i] = _nameBytes[i];
  137. }
  138. os.write(data);
  139. }
  140. /**
  141. * Helper method to write the end of the page group.
  142. * @param os The stream to write to
  143. */
  144. private void writeEnd(OutputStream os)
  145. throws IOException {
  146. byte[] data = new byte[17];
  147. data[0] = 0x5A; // Structured field identifier
  148. data[1] = 0x00; // Length byte 1
  149. data[2] = 0x10; // Length byte 2
  150. data[3] = (byte) 0xD3; // Structured field id byte 1
  151. data[4] = (byte) 0xA9; // Structured field id byte 2
  152. data[5] = (byte) 0xAD; // Structured field id byte 3
  153. data[6] = 0x00; // Flags
  154. data[7] = 0x00; // Reserved
  155. data[8] = 0x00; // Reserved
  156. for (int i = 0; i < _nameBytes.length; i++) {
  157. data[9 + i] = _nameBytes[i];
  158. }
  159. os.write(data);
  160. }
  161. }