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

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