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.

MODCAParser.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.parser;
  19. import java.io.DataInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.afp.parser.UnparsedStructuredField.Introducer;
  25. /**
  26. * An simple MO:DCA/AFP parser.
  27. */
  28. public class MODCAParser {
  29. private static final Log LOG = LogFactory.getLog(MODCAParser.class);
  30. private static final int INTRODUCER_LENGTH = 8;
  31. /** The carriage control character (0x5A) used to indicate the start of a structured field. */
  32. public static final byte CARRIAGE_CONTROL_CHAR = (byte)(0x5A & 0xFF);
  33. private DataInputStream din;
  34. /**
  35. * Main constructor
  36. * @param in the {@link InputStream} to read the AFP file from.
  37. */
  38. public MODCAParser(InputStream in) {
  39. this.din = new DataInputStream(in);
  40. }
  41. /**
  42. * Reads the next structured field from the input stream.
  43. * <p>
  44. * No structure validation of the MO:DCA file is performed.
  45. * @return a new unparsed structured field (or null when parsing is finished).
  46. * @throws IOException if an I/O error occurs
  47. */
  48. public UnparsedStructuredField readNextStructuredField() throws IOException {
  49. //Find the SF delimiter
  50. do {
  51. //Exhausted streams and so no next SF
  52. // - null return represents this case
  53. // TODO should this happen?
  54. if (din.available() == 0) {
  55. return null;
  56. }
  57. } while (din.readByte() != CARRIAGE_CONTROL_CHAR);
  58. //Read introducer as byte array to preserve any data not parsed below
  59. byte[] introducerData = new byte[INTRODUCER_LENGTH]; //Length of introducer
  60. din.readFully(introducerData);
  61. Introducer introducer = new Introducer(introducerData);
  62. int dataLength = introducer.getLength() - INTRODUCER_LENGTH;
  63. //Handle optional extension
  64. byte[] extData = null;
  65. if (introducer.isExtensionPresent()) {
  66. short extLength = 0;
  67. extLength = (short)((din.readByte()) & 0xFF);
  68. if (extLength > 0) {
  69. extData = new byte[extLength - 1];
  70. din.readFully(extData);
  71. dataLength -= extLength;
  72. }
  73. }
  74. //Read payload
  75. byte[] data = new byte[dataLength];
  76. din.readFully(data);
  77. UnparsedStructuredField sf = new UnparsedStructuredField(introducer, data, extData);
  78. if (LOG.isTraceEnabled()) {
  79. LOG.trace(sf);
  80. }
  81. return sf;
  82. }
  83. }