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.

HDGFDiagram.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import org.apache.poi.POIReadOnlyDocument;
  19. import org.apache.poi.hdgf.chunks.ChunkFactory;
  20. import org.apache.poi.hdgf.pointers.Pointer;
  21. import org.apache.poi.hdgf.pointers.PointerFactory;
  22. import org.apache.poi.hdgf.streams.PointerContainingStream;
  23. import org.apache.poi.hdgf.streams.Stream;
  24. import org.apache.poi.hdgf.streams.StringsStream;
  25. import org.apache.poi.hdgf.streams.TrailerStream;
  26. import org.apache.poi.poifs.filesystem.DirectoryNode;
  27. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  28. import org.apache.poi.util.IOUtils;
  29. import org.apache.poi.util.LittleEndian;
  30. import org.apache.poi.util.LocaleUtil;
  31. /**
  32. * See
  33. * http://www.redferni.uklinux.net/visio/
  34. * http://www.gnome.ru/projects/docs/vsdocs.html
  35. * http://www.gnome.ru/projects/docs/slide1.png
  36. * http://www.gnome.ru/projects/docs/slide2.png
  37. */
  38. public final class HDGFDiagram extends POIReadOnlyDocument {
  39. private static final String VISIO_HEADER = "Visio (TM) Drawing\r\n";
  40. private long docSize;
  41. private Pointer trailerPointer;
  42. private TrailerStream trailer;
  43. public HDGFDiagram(POIFSFileSystem fs) throws IOException {
  44. this(fs.getRoot());
  45. }
  46. public HDGFDiagram(DirectoryNode dir) throws IOException {
  47. super(dir);
  48. // Grab the document stream
  49. final byte[] _docstream;
  50. try (InputStream is = dir.createDocumentInputStream("VisioDocument")) {
  51. _docstream = IOUtils.toByteArray(is);
  52. }
  53. // Check it's really visio
  54. String typeString = new String(_docstream, 0, 20, LocaleUtil.CHARSET_1252 );
  55. if(! typeString.equals(VISIO_HEADER)) {
  56. throw new IllegalArgumentException("Wasn't a valid visio document, started with " + typeString);
  57. }
  58. // Grab the version number, 0x1a -> 0x1b
  59. short version = LittleEndian.getShort(_docstream, 0x1a);
  60. // Grab the document size, 0x1c -> 0x1f
  61. docSize = LittleEndian.getUInt(_docstream, 0x1c);
  62. // ??? 0x20 -> 0x23
  63. // Create the Chunk+Pointer Factories for the document version
  64. PointerFactory ptrFactory = new PointerFactory(version);
  65. ChunkFactory chunkFactory = new ChunkFactory(version);
  66. // Grab the pointer to the trailer
  67. trailerPointer = ptrFactory.createPointer(_docstream, 0x24);
  68. // Now grab the trailer
  69. Stream stream = Stream.createStream(trailerPointer, _docstream, chunkFactory, ptrFactory);
  70. if (!(stream instanceof TrailerStream)) {
  71. throw new IllegalStateException("Stream is not a TrailerStream: " + stream);
  72. }
  73. trailer = (TrailerStream)stream;
  74. // Finally, find all our streams
  75. trailer.findChildren(_docstream);
  76. }
  77. /**
  78. * Returns the TrailerStream, which is at the root of the
  79. * tree of Streams.
  80. *
  81. * @return the TrailerStream
  82. */
  83. public TrailerStream getTrailerStream() { return trailer; }
  84. /**
  85. * Returns all the top level streams, which are the streams
  86. * pointed to by the TrailerStream.
  87. *
  88. * @return the top level streams
  89. */
  90. public Stream[] getTopLevelStreams() { return trailer.getPointedToStreams(); }
  91. public long getDocumentSize() { return docSize; }
  92. /**
  93. * Prints out some simple debug on the base contents of the file.
  94. * @see org.apache.poi.hdgf.dev.VSDDumper
  95. */
  96. public void debug() {
  97. System.err.println("Trailer is at " + trailerPointer.getOffset());
  98. System.err.println("Trailer has type " + trailerPointer.getType());
  99. System.err.println("Trailer has length " + trailerPointer.getLength());
  100. System.err.println("Trailer has format " + trailerPointer.getFormat());
  101. for(int i=0; i<trailer.getPointedToStreams().length; i++) {
  102. Stream stream = trailer.getPointedToStreams()[i];
  103. Pointer ptr = stream.getPointer();
  104. System.err.println("Looking at pointer " + i);
  105. System.err.println("\tType is " + ptr.getType() + "\t\t" + Integer.toHexString(ptr.getType()));
  106. System.err.println("\tOffset is " + ptr.getOffset() + "\t\t" + Long.toHexString(ptr.getOffset()));
  107. System.err.println("\tAddress is " + ptr.getAddress() + "\t" + Long.toHexString(ptr.getAddress()));
  108. System.err.println("\tLength is " + ptr.getLength() + "\t\t" + Long.toHexString(ptr.getLength()));
  109. System.err.println("\tFormat is " + ptr.getFormat() + "\t\t" + Long.toHexString(ptr.getFormat()));
  110. System.err.println("\tCompressed is " + ptr.destinationCompressed());
  111. System.err.println("\tStream is " + stream.getClass());
  112. if(stream instanceof PointerContainingStream) {
  113. PointerContainingStream pcs = (PointerContainingStream)stream;
  114. if(pcs.getPointedToStreams() != null && pcs.getPointedToStreams().length > 0) {
  115. System.err.println("\tContains " + pcs.getPointedToStreams().length + " other pointers/streams");
  116. for(int j=0; j<pcs.getPointedToStreams().length; j++) {
  117. Stream ss = pcs.getPointedToStreams()[j];
  118. Pointer sptr = ss.getPointer();
  119. System.err.println("\t\t" + j + " - Type is " + sptr.getType() + "\t\t" + Integer.toHexString(sptr.getType()));
  120. System.err.println("\t\t" + j + " - Length is " + sptr.getLength() + "\t\t" + Long.toHexString(sptr.getLength()));
  121. }
  122. }
  123. }
  124. if(stream instanceof StringsStream) {
  125. System.err.println("\t\t**strings**");
  126. StringsStream ss = (StringsStream)stream;
  127. System.err.println("\t\t" + ss._getContentsLength());
  128. }
  129. }
  130. }
  131. }