001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017: package org.apache.poi.hdgf.streams;
018:
019: import java.io.FileInputStream;
020:
021: import org.apache.poi.hdgf.chunks.Chunk;
022: import org.apache.poi.hdgf.chunks.ChunkFactory;
023: import org.apache.poi.hdgf.pointers.Pointer;
024: import org.apache.poi.hdgf.pointers.PointerFactory;
025: import org.apache.poi.poifs.filesystem.DocumentEntry;
026: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
027:
028: public class TestStreamComplex extends StreamTest {
029: private byte[] contents;
030: private int trailerPointerAt = 0x24;
031: private int trailerDataAt = 0x8a94;
032: private ChunkFactory chunkFactory;
033: private PointerFactory ptrFactory;
034:
035: protected void setUp() throws Exception {
036: String dirname = System.getProperty("HDGF.testdata.path");
037: String filename = dirname + "/Test_Visio-Some_Random_Text.vsd";
038: ptrFactory = new PointerFactory(11);
039: chunkFactory = new ChunkFactory(11);
040:
041: FileInputStream fin = new FileInputStream(filename);
042: POIFSFileSystem filesystem = new POIFSFileSystem(fin);
043:
044: DocumentEntry docProps = (DocumentEntry) filesystem.getRoot()
045: .getEntry("VisioDocument");
046:
047: // Grab the document stream
048: contents = new byte[docProps.getSize()];
049: filesystem.createDocumentInputStream("VisioDocument").read(
050: contents);
051: }
052:
053: /**
054: * Test creating the trailer, but not looking for children
055: */
056: public void testTrailer() {
057: // Find the trailer
058: Pointer trailerPtr = ptrFactory.createPointer(contents,
059: trailerPointerAt);
060:
061: assertEquals(20, trailerPtr.getType());
062: assertEquals(trailerDataAt, trailerPtr.getOffset());
063:
064: Stream stream = Stream.createStream(trailerPtr, contents,
065: chunkFactory, ptrFactory);
066: assertTrue(stream instanceof TrailerStream);
067: TrailerStream ts = (TrailerStream) stream;
068:
069: assertNotNull(ts.getChildPointers());
070: assertNull(ts.getPointedToStreams());
071:
072: assertEquals(20, ts.getChildPointers().length);
073: assertEquals(0x16, ts.getChildPointers()[0].getType());
074: assertEquals(0x17, ts.getChildPointers()[1].getType());
075: assertEquals(0x17, ts.getChildPointers()[2].getType());
076: assertEquals(0xff, ts.getChildPointers()[3].getType());
077: }
078:
079: public void testChunks() {
080: Pointer trailerPtr = ptrFactory.createPointer(contents,
081: trailerPointerAt);
082: TrailerStream ts = (TrailerStream) Stream.createStream(
083: trailerPtr, contents, chunkFactory, ptrFactory);
084:
085: // Should be 7th one
086: Pointer chunkPtr = ts.getChildPointers()[5];
087: assertFalse(chunkPtr.destinationHasStrings());
088: assertTrue(chunkPtr.destinationHasChunks());
089: assertFalse(chunkPtr.destinationHasPointers());
090:
091: Stream stream = Stream.createStream(chunkPtr, contents,
092: chunkFactory, ptrFactory);
093: assertNotNull(stream);
094: assertTrue(stream instanceof ChunkStream);
095:
096: // Now find the chunks within it
097: ChunkStream cs = (ChunkStream) stream;
098: cs.findChunks();
099: }
100:
101: public void testStrings() {
102: Pointer trailerPtr = ptrFactory.createPointer(contents,
103: trailerPointerAt);
104: TrailerStream ts = (TrailerStream) Stream.createStream(
105: trailerPtr, contents, chunkFactory, ptrFactory);
106:
107: // Should be the 1st one
108: Pointer stringPtr = ts.getChildPointers()[0];
109: assertTrue(stringPtr.destinationHasStrings());
110: assertFalse(stringPtr.destinationHasChunks());
111: assertFalse(stringPtr.destinationHasPointers());
112:
113: Stream stream = Stream.createStream(stringPtr, contents,
114: chunkFactory, ptrFactory);
115: assertNotNull(stream);
116: assertTrue(stream instanceof StringsStream);
117: }
118:
119: public void testPointerToStrings() {
120: // The stream at 0x347f has strings
121: // The stream at 0x4312 has a pointer to 0x347f
122: // The stream at 0x44d3 has a pointer to 0x4312
123: // (it's the 2nd one of 3, and the block is compressed)
124:
125: TestPointer ptr44d3 = new TestPointer(true, 0x44d3, 0x51, 0x4e,
126: (short) 0x56);
127: ptr44d3.hasPointers = true;
128: PointerContainingStream s44d3 = (PointerContainingStream) Stream
129: .createStream(ptr44d3, contents, chunkFactory,
130: ptrFactory);
131:
132: // Type: 0d Addr: 014ff644 Offset: 4312 Len: 48 Format: 54 From: 44d3
133: Pointer ptr4312 = s44d3.getChildPointers()[1];
134: assertEquals(0x0d, ptr4312.getType());
135: assertEquals(0x4312, ptr4312.getOffset());
136: assertEquals(0x48, ptr4312.getLength());
137: assertEquals(0x54, ptr4312.getFormat());
138: assertTrue(ptr4312.destinationHasPointers());
139: assertFalse(ptr4312.destinationHasStrings());
140:
141: PointerContainingStream s4312 = (PointerContainingStream) Stream
142: .createStream(ptr4312, contents, chunkFactory,
143: ptrFactory);
144:
145: // Check it has 0x347f
146: // Type: 1f Addr: 01540004 Offset: 347f Len: 8e8 Format: 46 From: 4312
147: assertEquals(2, s4312.getChildPointers().length);
148: Pointer ptr347f = s4312.getChildPointers()[0];
149: assertEquals(0x1f, ptr347f.getType());
150: assertEquals(0x347f, ptr347f.getOffset());
151: assertEquals(0x8e8, ptr347f.getLength());
152: assertEquals(0x46, ptr347f.getFormat());
153: assertFalse(ptr347f.destinationHasPointers());
154: assertTrue(ptr347f.destinationHasStrings());
155:
156: // Find the children of 0x4312
157: assertNull(s4312.getPointedToStreams());
158: s4312.findChildren(contents);
159: // Should have two, both strings
160: assertNotNull(s4312.getPointedToStreams());
161: assertEquals(2, s4312.getPointedToStreams().length);
162: assertTrue(s4312.getPointedToStreams()[0] instanceof StringsStream);
163: assertTrue(s4312.getPointedToStreams()[1] instanceof StringsStream);
164: }
165:
166: public void testTrailerContents() {
167: Pointer trailerPtr = ptrFactory.createPointer(contents,
168: trailerPointerAt);
169: TrailerStream ts = (TrailerStream) Stream.createStream(
170: trailerPtr, contents, chunkFactory, ptrFactory);
171:
172: assertNotNull(ts.getChildPointers());
173: assertNull(ts.getPointedToStreams());
174: assertEquals(20, ts.getChildPointers().length);
175:
176: ts.findChildren(contents);
177:
178: assertNotNull(ts.getChildPointers());
179: assertNotNull(ts.getPointedToStreams());
180: assertEquals(20, ts.getChildPointers().length);
181: assertEquals(20, ts.getPointedToStreams().length);
182:
183: // Step down:
184: // 8 -> 4 -> 5 -> 1 -> 0 == String
185: assertNotNull(ts.getPointedToStreams()[8]);
186: assertTrue(ts.getPointedToStreams()[8] instanceof PointerContainingStream);
187:
188: PointerContainingStream s8 = (PointerContainingStream) ts
189: .getPointedToStreams()[8];
190: assertNotNull(s8.getPointedToStreams());
191:
192: assertNotNull(s8.getPointedToStreams()[4]);
193: assertTrue(s8.getPointedToStreams()[4] instanceof PointerContainingStream);
194:
195: PointerContainingStream s84 = (PointerContainingStream) s8
196: .getPointedToStreams()[4];
197: assertNotNull(s84.getPointedToStreams());
198:
199: assertNotNull(s84.getPointedToStreams()[5]);
200: assertTrue(s84.getPointedToStreams()[5] instanceof PointerContainingStream);
201:
202: PointerContainingStream s845 = (PointerContainingStream) s84
203: .getPointedToStreams()[5];
204: assertNotNull(s845.getPointedToStreams());
205:
206: assertNotNull(s845.getPointedToStreams()[1]);
207: assertTrue(s845.getPointedToStreams()[1] instanceof PointerContainingStream);
208:
209: PointerContainingStream s8451 = (PointerContainingStream) s845
210: .getPointedToStreams()[1];
211: assertNotNull(s8451.getPointedToStreams());
212:
213: assertNotNull(s8451.getPointedToStreams()[0]);
214: assertTrue(s8451.getPointedToStreams()[0] instanceof StringsStream);
215: assertTrue(s8451.getPointedToStreams()[1] instanceof StringsStream);
216: }
217:
218: public void testChunkWithText() throws Exception {
219: // Parent ChunkStream is at 0x7194
220: // This is one of the last children of the trailer
221: Pointer trailerPtr = ptrFactory.createPointer(contents,
222: trailerPointerAt);
223: TrailerStream ts = (TrailerStream) Stream.createStream(
224: trailerPtr, contents, chunkFactory, ptrFactory);
225:
226: ts.findChildren(contents);
227:
228: assertNotNull(ts.getChildPointers());
229: assertNotNull(ts.getPointedToStreams());
230: assertEquals(20, ts.getChildPointers().length);
231: assertEquals(20, ts.getPointedToStreams().length);
232:
233: assertEquals(0x7194, ts.getChildPointers()[13].getOffset());
234: assertEquals(0x7194, ts.getPointedToStreams()[13].getPointer()
235: .getOffset());
236:
237: PointerContainingStream ps7194 = (PointerContainingStream) ts
238: .getPointedToStreams()[13];
239:
240: // First child is at 0x64b3
241: assertEquals(0x64b3, ps7194.getChildPointers()[0].getOffset());
242: assertEquals(0x64b3, ps7194.getPointedToStreams()[0]
243: .getPointer().getOffset());
244:
245: ChunkStream cs = (ChunkStream) ps7194.getPointedToStreams()[0];
246:
247: // Should be 26bc bytes un-compressed
248: assertEquals(0x26bc, cs.getStore().getContents().length);
249: // And should have lots of children
250: assertEquals(131, cs.getChunks().length);
251:
252: // One of which is Text
253: boolean hasText = false;
254: for (int i = 0; i < cs.getChunks().length; i++) {
255: if (cs.getChunks()[i].getName().equals("Text")) {
256: hasText = true;
257: }
258: }
259: assertTrue(hasText);
260: // Which is the 72nd command
261: assertEquals("Text", cs.getChunks()[72].getName());
262:
263: Chunk text = cs.getChunks()[72];
264: assertEquals("Text", text.getName());
265:
266: // Which contains our text
267: assertEquals(1, text.getCommands().length);
268: assertEquals("Test View\n", text.getCommands()[0].getValue());
269:
270: // Almost at the end is some more text
271: assertEquals("Text", cs.getChunks()[128].getName());
272: text = cs.getChunks()[128];
273: assertEquals("Text", text.getName());
274:
275: assertEquals(1, text.getCommands().length);
276: assertEquals("Some random text, on a page\n", text
277: .getCommands()[0].getValue());
278: }
279: }
|