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:
018: package org.apache.poi.hslf.dev;
019:
020: import org.apache.poi.hslf.*;
021: import org.apache.poi.hslf.record.*;
022: import org.apache.poi.hslf.usermodel.SlideShow;
023:
024: import org.apache.poi.util.LittleEndian;
025:
026: import java.io.*;
027: import java.util.Hashtable;
028:
029: /**
030: * Gets all the different things that have Slide IDs (of sorts)
031: * in them, and displays them, so you can try to guess what they
032: * all mean
033: */
034: public class SlideIdListing {
035: private static byte[] fileContents;
036:
037: public static void main(String[] args) throws Exception {
038: if (args.length < 1) {
039: System.err.println("Need to give a filename");
040: System.exit(1);
041: }
042:
043: // Create the slideshow object, for normal working with
044: HSLFSlideShow hss = new HSLFSlideShow(args[0]);
045: SlideShow ss = new SlideShow(hss);
046:
047: // Grab the base contents
048: fileContents = hss.getUnderlyingBytes();
049: Record[] records = hss.getRecords();
050: Record[] latestRecords = ss.getMostRecentCoreRecords();
051:
052: // Grab any records that interest us
053: Document document = null;
054: for (int i = 0; i < latestRecords.length; i++) {
055: if (latestRecords[i] instanceof Document) {
056: document = (Document) latestRecords[i];
057: }
058: }
059:
060: System.out.println("");
061:
062: // Look for SlidePersistAtoms, and report what they have to
063: // say about possible slide IDs
064: SlideListWithText[] slwts = document.getSlideListWithTexts();
065: for (int i = 0; i < slwts.length; i++) {
066: Record[] cr = slwts[i].getChildRecords();
067: for (int j = 0; j < cr.length; j++) {
068: if (cr[j] instanceof SlidePersistAtom) {
069: SlidePersistAtom spa = (SlidePersistAtom) cr[j];
070: System.out
071: .println("SlidePersistAtom knows about slide:");
072: System.out.println("\t" + spa.getRefID());
073: System.out.println("\t" + spa.getSlideIdentifier());
074: }
075: }
076: }
077:
078: System.out.println("");
079:
080: // Look for latest core records that are slides or notes
081: for (int i = 0; i < latestRecords.length; i++) {
082: if (latestRecords[i] instanceof Slide) {
083: Slide s = (Slide) latestRecords[i];
084: SlideAtom sa = s.getSlideAtom();
085: System.out
086: .println("Found the latest version of a slide record:");
087: System.out.println("\tCore ID is " + s.getSheetId());
088: System.out.println("\t(Core Records count is " + i
089: + ")");
090: System.out.println("\tDisk Position is "
091: + s.getLastOnDiskOffset());
092: System.out
093: .println("\tMaster ID is " + sa.getMasterID());
094: System.out.println("\tNotes ID is " + sa.getNotesID());
095: }
096: }
097: System.out.println("");
098: for (int i = 0; i < latestRecords.length; i++) {
099: if (latestRecords[i] instanceof Notes) {
100: Notes n = (Notes) latestRecords[i];
101: NotesAtom na = n.getNotesAtom();
102: System.out
103: .println("Found the latest version of a notes record:");
104: System.out.println("\tCore ID is " + n.getSheetId());
105: System.out.println("\t(Core Records count is " + i
106: + ")");
107: System.out.println("\tDisk Position is "
108: + n.getLastOnDiskOffset());
109: System.out.println("\tMatching slide is "
110: + na.getSlideID());
111: }
112: }
113:
114: System.out.println("");
115:
116: // Find any persist ones first
117: int pos = 0;
118: for (int i = 0; i < records.length; i++) {
119: Record r = records[i];
120:
121: if (r.getRecordType() == 6001l) {
122: // PersistPtrFullBlock
123: System.out.println("Found PersistPtrFullBlock at "
124: + pos + " (" + Integer.toHexString(pos) + ")");
125: }
126: if (r.getRecordType() == 6002l) {
127: // PersistPtrIncrementalBlock
128: System.out
129: .println("Found PersistPtrIncrementalBlock at "
130: + pos + " (" + Integer.toHexString(pos)
131: + ")");
132: PersistPtrHolder pph = (PersistPtrHolder) r;
133:
134: // Check the sheet offsets
135: int[] sheetIDs = pph.getKnownSlideIDs();
136: Hashtable sheetOffsets = pph.getSlideLocationsLookup();
137: for (int j = 0; j < sheetIDs.length; j++) {
138: Integer id = new Integer(sheetIDs[j]);
139: Integer offset = (Integer) sheetOffsets.get(id);
140:
141: System.out.println(" Knows about sheet " + id);
142: System.out.println(" That sheet lives at "
143: + offset);
144:
145: Record atPos = findRecordAtPos(offset.intValue());
146: System.out
147: .println(" The record at that pos is of type "
148: + atPos.getRecordType());
149: System.out
150: .println(" The record at that pos has class "
151: + atPos.getClass().getName());
152:
153: if (!(atPos instanceof PositionDependentRecord)) {
154: System.out
155: .println(" ** The record class isn't position aware! **");
156: }
157: }
158: }
159:
160: // Increase the position by the on disk size
161: ByteArrayOutputStream baos = new ByteArrayOutputStream();
162: r.writeOut(baos);
163: pos += baos.size();
164: }
165:
166: System.out.println("");
167: }
168:
169: // Finds the record at a given position
170: public static Record findRecordAtPos(int pos) {
171: long type = LittleEndian.getUShort(fileContents, pos + 2);
172: long rlen = LittleEndian.getUInt(fileContents, pos + 4);
173:
174: Record r = Record.createRecordForType(type, fileContents, pos,
175: (int) rlen + 8);
176:
177: return r;
178: }
179: }
|