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.

MapPageSegment.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.modca;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.io.UnsupportedEncodingException;
  22. import java.util.Iterator;
  23. import java.util.Set;
  24. import org.apache.fop.afp.AFPConstants;
  25. import org.apache.fop.afp.util.BinaryUtils;
  26. /**
  27. * The Map Page Segment structured field identifies page segments that are required to present
  28. * a page on a physical medium.
  29. */
  30. public class MapPageSegment extends AbstractAFPObject {
  31. private static final int MAX_SIZE = 127;
  32. /**
  33. * The collection of page segments (maximum of 127 stored as String)
  34. */
  35. private Set pageSegments;
  36. /**
  37. * Constructor for the Map Page Overlay
  38. */
  39. public MapPageSegment() {
  40. }
  41. private Set getPageSegments() {
  42. if (pageSegments == null) {
  43. this.pageSegments = new java.util.HashSet();
  44. }
  45. return this.pageSegments;
  46. }
  47. /**
  48. * Add a page segment to to the map page segment object.
  49. * @param name the name of the page segment.
  50. * @throws MaximumSizeExceededException if the maximum size is reached
  51. */
  52. public void addPageSegment(String name) throws MaximumSizeExceededException {
  53. if (getPageSegments().size() > MAX_SIZE) {
  54. throw new MaximumSizeExceededException();
  55. }
  56. if (name.length() > 8) {
  57. throw new IllegalArgumentException("The name of page segment " + name
  58. + " must not be longer than 8 characters");
  59. }
  60. if (LOG.isDebugEnabled()) {
  61. LOG.debug("addPageSegment():: adding page segment " + name);
  62. }
  63. getPageSegments().add(name);
  64. }
  65. /**
  66. * Indicates whether this object already contains the maximum number of
  67. * page segments.
  68. * @return true if the object is full
  69. */
  70. public boolean isFull() {
  71. return this.pageSegments.size() >= MAX_SIZE;
  72. }
  73. /** {@inheritDoc} */
  74. public void writeToStream(OutputStream os) throws IOException {
  75. int count = getPageSegments().size();
  76. byte groupLength = 0x0C;
  77. int groupsLength = count * groupLength;
  78. byte[] data = new byte[groupsLength + 12 + 1];
  79. data[0] = 0x5A;
  80. // Set the total record length
  81. byte[] rl1 = BinaryUtils.convert(data.length - 1, 2); //Ignore the
  82. // first byte in
  83. // the length
  84. data[1] = rl1[0];
  85. data[2] = rl1[1];
  86. // Structured field ID for a MPS
  87. data[3] = (byte) 0xD3;
  88. data[4] = Type.MIGRATION;
  89. data[5] = Category.PAGE_SEGMENT;
  90. data[6] = 0x00; // Flags
  91. data[7] = 0x00; // Reserved
  92. data[8] = 0x00; // Reserved
  93. data[9] = groupLength;
  94. data[10] = 0x00; // Reserved
  95. data[11] = 0x00; // Reserved
  96. data[12] = 0x00; // Reserved
  97. int pos = 13;
  98. Iterator iter = this.pageSegments.iterator();
  99. while (iter.hasNext()) {
  100. pos += 4;
  101. String name = (String)iter.next();
  102. try {
  103. byte[] nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
  104. System.arraycopy(nameBytes, 0, data, pos, nameBytes.length);
  105. } catch (UnsupportedEncodingException usee) {
  106. LOG.error("UnsupportedEncodingException translating the name "
  107. + name);
  108. }
  109. pos += 8;
  110. }
  111. os.write(data);
  112. }
  113. }