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.

ChunkFactory.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hdgf.chunks;
  16. import java.io.BufferedReader;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.InputStreamReader;
  20. import java.util.ArrayList;
  21. import java.util.Hashtable;
  22. import java.util.StringTokenizer;
  23. import org.apache.poi.util.POILogFactory;
  24. import org.apache.poi.util.POILogger;
  25. /**
  26. * Factor class to create the appropriate chunks, which
  27. * needs the version of the file to process the chunk header
  28. * and trailer areas.
  29. * Makes use of chunks_parse_cmds.tbl from vsdump to be able
  30. * to process the chunk value area
  31. */
  32. public class ChunkFactory {
  33. /** The version of the currently open document */
  34. private int version;
  35. /**
  36. * Key is a Chunk's type, value is an array of its CommandDefinitions
  37. */
  38. private Hashtable chunkCommandDefinitions = new Hashtable();
  39. /**
  40. * What the name is of the chunk table definitions file?
  41. * This file comes from the scratchpad resources directory.
  42. */
  43. private static String chunkTableName =
  44. "/org/apache/poi/hdgf/chunks_parse_cmds.tbl";
  45. /** For logging problems we spot with the file */
  46. private POILogger logger = POILogFactory.getLogger(ChunkFactory.class);
  47. public ChunkFactory(int version) throws IOException {
  48. this.version = version;
  49. processChunkParseCommands();
  50. }
  51. /**
  52. * Open chunks_parse_cmds.tbl and process it, to get the definitions
  53. * of all the different possible chunk commands.
  54. */
  55. private void processChunkParseCommands() throws IOException {
  56. String line;
  57. InputStream cpd = ChunkFactory.class.getResourceAsStream(chunkTableName);
  58. BufferedReader inp = new BufferedReader(new InputStreamReader(cpd));
  59. while( (line = inp.readLine()) != null ) {
  60. if(line.startsWith("#")) continue;
  61. if(line.startsWith(" ")) continue;
  62. if(line.startsWith("\t")) continue;
  63. if(line.length() == 0) continue;
  64. // Start xxx
  65. if(!line.startsWith("start")) {
  66. throw new IllegalStateException("Expecting start xxx, found " + line);
  67. }
  68. int chunkType = Integer.parseInt(line.substring(6));
  69. ArrayList defsL = new ArrayList();
  70. // Data entries
  71. while( ! (line = inp.readLine()).startsWith("end") ) {
  72. StringTokenizer st = new StringTokenizer(line, " ");
  73. int defType = Integer.parseInt(st.nextToken());
  74. int offset = Integer.parseInt(st.nextToken());
  75. String name = st.nextToken("\uffff").substring(1);
  76. CommandDefinition def = new CommandDefinition(defType,offset,name);
  77. defsL.add(def);
  78. }
  79. CommandDefinition[] defs = (CommandDefinition[])
  80. defsL.toArray(new CommandDefinition[defsL.size()]);
  81. // Add to the hashtable
  82. chunkCommandDefinitions.put(new Integer(chunkType), defs);
  83. }
  84. inp.close();
  85. cpd.close();
  86. }
  87. public int getVersion() { return version; }
  88. /**
  89. * Creates the appropriate chunk at the given location.
  90. * @param data
  91. * @param offset
  92. */
  93. public Chunk createChunk(byte[] data, int offset) {
  94. // Create the header
  95. ChunkHeader header =
  96. ChunkHeader.createChunkHeader(version, data, offset);
  97. // Sanity check
  98. if(header.length < 0) {
  99. throw new IllegalArgumentException("Found a chunk with a negative length, which isn't allowed");
  100. }
  101. // How far up to look
  102. int endOfDataPos = offset + header.getLength() + header.getSizeInBytes();
  103. // Check we have enough data, and tweak the header size
  104. // as required
  105. if(endOfDataPos > data.length) {
  106. logger.log(POILogger.WARN,
  107. "Header called for " + header.getLength() +" bytes, but that would take us passed the end of the data!");
  108. endOfDataPos = data.length;
  109. header.length = data.length - offset - header.getSizeInBytes();
  110. if(header.hasTrailer()) {
  111. header.length -= 8;
  112. endOfDataPos -= 8;
  113. }
  114. if(header.hasSeparator()) {
  115. header.length -= 4;
  116. endOfDataPos -= 4;
  117. }
  118. }
  119. // Create the trailer and separator, if required
  120. ChunkTrailer trailer = null;
  121. ChunkSeparator separator = null;
  122. if(header.hasTrailer()) {
  123. if(endOfDataPos <= data.length-8) {
  124. trailer = new ChunkTrailer(
  125. data, endOfDataPos);
  126. endOfDataPos += 8;
  127. } else {
  128. System.err.println("Header claims a length to " + endOfDataPos + " there's then no space for the trailer in the data (" + data.length + ")");
  129. }
  130. }
  131. if(header.hasSeparator()) {
  132. if(endOfDataPos <= data.length-4) {
  133. separator = new ChunkSeparator(
  134. data, endOfDataPos);
  135. } else {
  136. System.err.println("Header claims a length to " + endOfDataPos + " there's then no space for the separator in the data (" + data.length + ")");
  137. }
  138. }
  139. // Now, create the chunk
  140. byte[] contents = new byte[header.getLength()];
  141. System.arraycopy(data, offset+header.getSizeInBytes(), contents, 0, contents.length);
  142. Chunk chunk = new Chunk(header, trailer, separator, contents);
  143. // Feed in the stuff from chunks_parse_cmds.tbl
  144. CommandDefinition[] defs = (CommandDefinition[])
  145. chunkCommandDefinitions.get(new Integer(header.getType()));
  146. if(defs == null) defs = new CommandDefinition[0];
  147. chunk.commandDefinitions = defs;
  148. // Now get the chunk to process its commands
  149. chunk.processCommands();
  150. // All done
  151. return chunk;
  152. }
  153. /**
  154. * The definition of a Command, which a chunk may hold.
  155. * The Command holds the value, this describes it.
  156. */
  157. public class CommandDefinition {
  158. private int type;
  159. private int offset;
  160. private String name;
  161. public CommandDefinition(int type, int offset, String name) {
  162. this.type = type;
  163. this.offset = offset;
  164. this.name = name;
  165. }
  166. public String getName() {
  167. return name;
  168. }
  169. public int getOffset() {
  170. return offset;
  171. }
  172. public int getType() {
  173. return type;
  174. }
  175. }
  176. }