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.

MapPageOverlay.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.List;
  23. import org.apache.fop.afp.AFPConstants;
  24. import org.apache.fop.afp.util.BinaryUtils;
  25. /**
  26. * The Map Page Overlay structured field maps a Resource Local ID to the name of
  27. * a Begin Overlay structured field. A Map Page Overlay structured field may
  28. * contain from one to 254 repeating groups.
  29. */
  30. public class MapPageOverlay extends AbstractAFPObject {
  31. private static final int MAX_SIZE = 253;
  32. /**
  33. * The collection of overlays (maximum of 254 stored as byte[])
  34. */
  35. private List overLays;
  36. /**
  37. * Constructor for the Map Page Overlay
  38. */
  39. public MapPageOverlay() {
  40. }
  41. private List getOverlays() {
  42. if (overLays == null) {
  43. this.overLays = new java.util.ArrayList();
  44. }
  45. return this.overLays;
  46. }
  47. /**
  48. * Add an overlay to to the map page overlay object.
  49. *
  50. * @param name
  51. * The name of the overlay.
  52. * @throws MaximumSizeExceededException if the maximum size is reached
  53. */
  54. public void addOverlay(String name) throws MaximumSizeExceededException {
  55. if (getOverlays().size() > MAX_SIZE) {
  56. throw new MaximumSizeExceededException();
  57. }
  58. if (name.length() != 8) {
  59. throw new IllegalArgumentException("The name of overlay " + name
  60. + " must be 8 characters");
  61. }
  62. if (LOG.isDebugEnabled()) {
  63. LOG.debug("addOverlay():: adding overlay " + name);
  64. }
  65. try {
  66. byte[] data = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
  67. getOverlays().add(data);
  68. } catch (UnsupportedEncodingException usee) {
  69. LOG.error("addOverlay():: UnsupportedEncodingException translating the name "
  70. + name);
  71. }
  72. }
  73. /** {@inheritDoc} */
  74. public void writeToStream(OutputStream os) throws IOException {
  75. int oLayCount = getOverlays().size();
  76. int recordlength = oLayCount * 18;
  77. byte[] data = new byte[recordlength + 9];
  78. data[0] = 0x5A;
  79. // Set the total record length
  80. byte[] rl1 = BinaryUtils.convert(recordlength + 8, 2); //Ignore the
  81. // first byte in
  82. // the length
  83. data[1] = rl1[0];
  84. data[2] = rl1[1];
  85. // Structured field ID for a MPO
  86. data[3] = (byte) 0xD3;
  87. data[4] = Type.MAP;
  88. data[5] = Category.PAGE_OVERLAY;
  89. data[6] = 0x00; // Reserved
  90. data[7] = 0x00; // Reserved
  91. data[8] = 0x00; // Reserved
  92. int pos = 8;
  93. //For each overlay
  94. byte olayref = 0x00;
  95. for (int i = 0; i < oLayCount; i++) {
  96. olayref = (byte) (olayref + 1);
  97. data[++pos] = 0x00;
  98. data[++pos] = 0x12; //the length of repeating group
  99. data[++pos] = 0x0C; //Fully Qualified Name
  100. data[++pos] = 0x02;
  101. data[++pos] = (byte) 0x84;
  102. data[++pos] = 0x00;
  103. //now add the name
  104. byte[] name = (byte[]) overLays.get(i);
  105. for (int j = 0; j < name.length; j++) {
  106. data[++pos] = name[j];
  107. }
  108. data[++pos] = 0x04; //Resource Local Identifier (RLI)
  109. data[++pos] = 0x24;
  110. data[++pos] = 0x02;
  111. //now add the unique id to the RLI
  112. data[++pos] = olayref;
  113. }
  114. os.write(data);
  115. }
  116. }