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.0KB

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