View | Details | Raw Unified | Return to bug 47183
Collapse All | Expand All

(-)src/scratchpad/src/org/apache/poi/hsmf/parsers/POIFSChunkParser.java (-3 / +56 lines)
Lines 17-28 Link Here
17
17
18
package org.apache.poi.hsmf.parsers;
18
package org.apache.poi.hsmf.parsers;
19
19
20
import java.io.ByteArrayInputStream;
20
import java.io.ByteArrayOutputStream;
21
import java.io.ByteArrayOutputStream;
21
import java.io.IOException;
22
import java.io.IOException;
22
import java.util.ArrayList;
23
import java.util.ArrayList;
23
import java.util.HashMap;
24
import java.util.HashMap;
24
import java.util.Iterator;
25
import java.util.Iterator;
26
import java.util.List;
27
import java.util.Map;
25
28
29
import org.apache.poi.hsmf.datatypes.AttachmentChunks;
26
import org.apache.poi.hsmf.datatypes.Chunk;
30
import org.apache.poi.hsmf.datatypes.Chunk;
27
import org.apache.poi.hsmf.datatypes.Chunks;
31
import org.apache.poi.hsmf.datatypes.Chunks;
28
import org.apache.poi.hsmf.datatypes.Types;
32
import org.apache.poi.hsmf.datatypes.Types;
Lines 89-102 Link Here
89
	 *  appropriate for the chunks we find in the file.
93
	 *  appropriate for the chunks we find in the file.
90
	 */
94
	 */
91
	public Chunks identifyChunks() {
95
	public Chunks identifyChunks() {
96
		return Chunks.getInstance(this.isNewChunkVersion(this.directoryMap));
97
	}
98
	
99
	/**
100
	 * Returns a list of the standard chunk types, as 
101
	 *  appropriate for the chunks we find in the file attachment.
102
	 */
103
	private AttachmentChunks identifyAttachmentChunks(Map attachmentMap) {
104
		return AttachmentChunks.getInstance(this.isNewChunkVersion(attachmentMap));
105
	}
106
	
107
	/**
108
	 * Return chunk version of the map in parameter
109
	 */
110
	private boolean isNewChunkVersion(Map map) {
92
		// Are they of the old or new type of strings?
111
		// Are they of the old or new type of strings?
93
		boolean hasOldStrings = false;
112
		boolean hasOldStrings = false;
94
		boolean hasNewStrings = false;
113
		boolean hasNewStrings = false;
95
		String oldStringEnd = Types.asFileEnding(Types.OLD_STRING);
114
		String oldStringEnd = Types.asFileEnding(Types.OLD_STRING);
96
		String newStringEnd = Types.asFileEnding(Types.NEW_STRING);
115
		String newStringEnd = Types.asFileEnding(Types.NEW_STRING);
97
		
116
		
98
		for(Iterator i = directoryMap.keySet().iterator(); i.hasNext();) {
117
		for(Iterator i = map.keySet().iterator(); i.hasNext();) {
99
			String entry = (String)i.next();
118
			String entry = (String)i.next();
119
			
100
			if(entry.endsWith( oldStringEnd )) {
120
			if(entry.endsWith( oldStringEnd )) {
101
				hasOldStrings = true;
121
				hasOldStrings = true;
102
			}
122
			}
Lines 108-116 Link Here
108
		if(hasOldStrings && hasNewStrings) {
128
		if(hasOldStrings && hasNewStrings) {
109
			throw new IllegalStateException("Your file contains string chunks of both the old and new types. Giving up");
129
			throw new IllegalStateException("Your file contains string chunks of both the old and new types. Giving up");
110
		} else if(hasNewStrings) {
130
		} else if(hasNewStrings) {
111
			return Chunks.getInstance(true);
131
			return true;
112
		}
132
		}
113
		return Chunks.getInstance(false);
133
		return false;
114
	}
134
	}
115
	
135
	
116
	/**
136
	/**
Lines 165-170 Link Here
165
		return getDocumentNode(this.directoryMap, chunk);
185
		return getDocumentNode(this.directoryMap, chunk);
166
	}
186
	}
167
	
187
	
188
	/**
189
	 * 
190
	 * @return a map containing attachment name (String) and data (ByteArrayInputStream)
191
	 */
192
	public Map getAttachmentList() {
193
		Map attachments = new HashMap();
194
		List attachmentList = new ArrayList();
195
		for(Iterator i = directoryMap.keySet().iterator(); i.hasNext();) {
196
			String entry = (String)i.next();
197
			
198
			if(entry.startsWith(AttachmentChunks.namePrefix)) {
199
				String attachmentIdString = entry.replace(AttachmentChunks.namePrefix, "");
200
				try {
201
					int attachmentId = Integer.parseInt(attachmentIdString);
202
					attachmentList.add((HashMap)directoryMap.get(entry));
203
				} catch (NumberFormatException nfe) {
204
					System.err.println("Invalid attachment id");
205
				}
206
			}
207
		}
208
		for (Iterator iterator = attachmentList.iterator(); iterator.hasNext();) {
209
			HashMap AttachmentChunkMap = (HashMap) iterator.next();
210
			AttachmentChunks attachmentChunks = this.identifyAttachmentChunks(AttachmentChunkMap);
211
			try {
212
				Chunk fileName = this.getDocumentNode(AttachmentChunkMap, attachmentChunks.attachLongFileName);
213
				Chunk content = this.getDocumentNode(AttachmentChunkMap, attachmentChunks.attachData);
214
				attachments.put(fileName.toString(), new ByteArrayInputStream(content.getValueByteArray().toByteArray()));
215
			} catch (ChunkNotFoundException e) {
216
				System.err.println("Invalid attachment chunk");
217
			}
218
		}
219
		return attachments;
220
	}
168
	
221
	
169
	/**
222
	/**
170
	 * Processes an iterator returned by a POIFS call to getRoot().getEntries()
223
	 * Processes an iterator returned by a POIFS call to getRoot().getEntries()
(-)src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java (+10 lines)
Lines 21-26 Link Here
21
import java.io.FileInputStream;
21
import java.io.FileInputStream;
22
import java.io.IOException;
22
import java.io.IOException;
23
import java.io.InputStream;
23
import java.io.InputStream;
24
import java.util.Map;
24
25
25
import org.apache.poi.hsmf.datatypes.Chunk;
26
import org.apache.poi.hsmf.datatypes.Chunk;
26
import org.apache.poi.hsmf.datatypes.Chunks;
27
import org.apache.poi.hsmf.datatypes.Chunks;
Lines 159-162 Link Here
159
	public String getMessageClass() throws ChunkNotFoundException {
160
	public String getMessageClass() throws ChunkNotFoundException {
160
		return getStringFromChunk(chunks.messageClass);
161
		return getStringFromChunk(chunks.messageClass);
161
	}	
162
	}	
163
	
164
	/**
165
	 * Gets the message attachments.
166
	 * 
167
	 * @return a map containing attachment name (String) and data (ByteArrayInputStream)
168
	 */
169
	public Map getAttachmentFiles() {
170
		return this.chunkParser.getAttachmentList();
171
	}	
162
}
172
}
(-)src/scratchpad/testcases/org/apache/poi/hsmf/AllTests.java (+1 lines)
Lines 35-40 Link Here
35
    suite.addTestSuite(org.apache.poi.hsmf.model.TestSimpleFileRead.class);
35
    suite.addTestSuite(org.apache.poi.hsmf.model.TestSimpleFileRead.class);
36
    suite.addTestSuite(org.apache.poi.hsmf.model.TestOutlook30FileRead.class);
36
    suite.addTestSuite(org.apache.poi.hsmf.model.TestOutlook30FileRead.class);
37
    suite.addTestSuite(org.apache.poi.hsmf.model.TestChunkData.class);
37
    suite.addTestSuite(org.apache.poi.hsmf.model.TestChunkData.class);
38
    suite.addTestSuite(org.apache.poi.hsmf.model.TestFileWithAttachmentsRead.class);
38
    
39
    
39
    return suite;
40
    return suite;
40
  }
41
  }

Return to bug 47183