/* ********** START implementation of POIFSReaderListener ********** */ /** * Process a POIFSReaderEvent that this listener had registered * for * * @param evt the POIFSReaderEvent */ public void ProcessPOIFSReaderEvent(POIFSReaderEvent evt) { DocumentInputStream istream = evt.Stream; POIFSDocumentPath path = evt.Path; String name = evt.Name; try { int size = (int)(istream.Length - istream.Position); byte[] data = new byte[size]; istream.Read(data); DocumentDescriptor descriptor = new DocumentDescriptor(path, name); Console.WriteLine("Adding document: " + descriptor + " (" + size + " bytes)"); dataMap[descriptor] = data; DirectoryEntry entry = root; for (int k = 0; k < path.Length; k++) { String componentName = path.GetComponent(k); Entry nextEntry = null; try { nextEntry = entry.GetEntry(componentName); } catch (FileNotFoundException) { try { nextEntry = entry.CreateDirectory(componentName); } catch (IOException) { Console.WriteLine("Unable to Create directory"); //e.printStackTrace(); throw; } } entry = (DirectoryEntry)nextEntry; } entry.CreateDocument(name, size, this); } catch (IOException) { } }
/// <summary> /// equality. Two DocumentDescriptor instances are equal if they /// have equal paths and names /// </summary> /// <param name="o">the object we're checking equality for</param> /// <returns>true if the object is equal to this object</returns> public override bool Equals(Object o) { bool rval = false; if ((o != null) && (o.GetType() == this.GetType())) { if (this == o) { rval = true; } else { DocumentDescriptor descriptor = ( DocumentDescriptor )o; rval = this.path.Equals(descriptor.path) && this.name.Equals(descriptor.name); } } return(rval); }
/** * Register a POIFSReaderListener for a particular document * * @param listener the listener * @param path the path of the document of interest * @param documentName the name of the document of interest */ public void RegisterListener(POIFSReaderListener listener, POIFSDocumentPath path, String documentName) { if (!omnivorousListeners.Contains(listener)) { // not an omnivorous listener (if it was, this method is a // no-op) ArrayList descriptors = (ArrayList)selectiveListeners[listener]; if (descriptors == null) { // this listener has not Registered before descriptors = new ArrayList(); selectiveListeners[listener] = descriptors; } DocumentDescriptor descriptor = new DocumentDescriptor(path, documentName); if (descriptors.Add(descriptor) >= 0) { // this listener wasn't alReady listening for this // document -- Add the listener to the Set of // listeners for this document ArrayList listeners = (ArrayList)chosenDocumentDescriptors[descriptor]; if (listeners == null) { // nobody was listening for this document before listeners = new ArrayList(); chosenDocumentDescriptors[descriptor] = listeners; } listeners.Add(listener); } } }
private void DropDocument(POIFSReaderListener listener, DocumentDescriptor descriptor) { ArrayList listeners = (ArrayList)chosenDocumentDescriptors[descriptor]; listeners.Remove(listener); if (listeners.Count == 0) { chosenDocumentDescriptors.Remove(descriptor); } }
public void TestEquality() { String[] names = { "c1", "c2", "c3", "c4", "c5" }; POIFSDocumentPath a1 = new POIFSDocumentPath(); POIFSDocumentPath a2 = new POIFSDocumentPath(null); POIFSDocumentPath a3 = new POIFSDocumentPath(new String[0]); POIFSDocumentPath a4 = new POIFSDocumentPath(a1, null); POIFSDocumentPath a5 = new POIFSDocumentPath(a1, new String[0]); POIFSDocumentPath[] paths = { a1, a2, a3, a4, a5 }; for (int j = 0; j < paths.Length; j++) { for (int k = 0; k < paths.Length; k++) { for (int m = 0; m < names.Length; m++) { DocumentDescriptor d1 = new DocumentDescriptor(paths[j], names[m]); for (int n = 0; n < names.Length; n++) { DocumentDescriptor d2 = new DocumentDescriptor(paths[k], names[n]); if (m == n) { Assert.AreEqual(d1, d2, "" + j + "," + k + "," + m + "," + n); } else { Assert.IsTrue(!d1.Equals(d2), "" + j + "," + k + "," + m + "," + n); } } } } } a2 = new POIFSDocumentPath(a1, new String[] { "foo" }); a3 = new POIFSDocumentPath(a2, new String[] { "bar" }); a4 = new POIFSDocumentPath(a3, new String[] { "fubar" }); a5 = new POIFSDocumentPath(a4, new String[] { "foobar" }); POIFSDocumentPath[] builtUpPaths = { a1, a2, a3, a4, a5 }; POIFSDocumentPath[] fullPaths = { new POIFSDocumentPath(), new POIFSDocumentPath(new String[] { "foo" }), new POIFSDocumentPath(new String[] { "foo", "bar" }), new POIFSDocumentPath(new String[] { "foo", "bar", "fubar" }), new POIFSDocumentPath(new String[] { "foo", "bar", "fubar", "foobar" }) }; for (int k = 0; k < builtUpPaths.Length; k++) { for (int j = 0; j < fullPaths.Length; j++) { for (int m = 0; m < names.Length; m++) { DocumentDescriptor d1 = new DocumentDescriptor(fullPaths[j], names[m]); for (int n = 0; n < names.Length; n++) { DocumentDescriptor d2 = new DocumentDescriptor(builtUpPaths[k], names[n]); if ((k == j) && (m == n)) { Assert.AreEqual(d1, d2, "" + j + "," + k + "," + m + "," + n); } else { Assert.IsTrue(!(d1.Equals(d2)), "" + j + "," + k + "," + m + "," + n); } } } } } POIFSDocumentPath[] badPaths = { new POIFSDocumentPath(new String[] { "_foo" }), new POIFSDocumentPath(new String[] { "foo", "_bar" }), new POIFSDocumentPath(new String[] { "foo", "bar", "_fubar" }), new POIFSDocumentPath(new String[] { "foo", "bar", "fubar", "_foobar" }) }; for (int k = 0; k < builtUpPaths.Length; k++) { for (int j = 0; j < badPaths.Length; j++) { for (int m = 0; m < names.Length; m++) { DocumentDescriptor d1 = new DocumentDescriptor(badPaths[j], names[m]); for (int n = 0; n < names.Length; n++) { DocumentDescriptor d2 = new DocumentDescriptor(builtUpPaths[k], names[n]); Assert.IsTrue(!(d1.Equals(d2)), "" + j + "," + k + "," + m + "," + n); } } } } }
/* ********** END implementation of POIFSReaderListener ********** */ /* ********** START implementation of POIFSWriterListener ********** */ /** * Process a POIFSWriterEvent that this listener had registered * for * * @param evt the POIFSWriterEvent */ public void ProcessPOIFSWriterEvent(POIFSWriterEvent evt) { try { DocumentDescriptor descriptor = new DocumentDescriptor(evt.Path, evt.Name); Console.WriteLine("looking up document: " + descriptor + " (" + evt.Limit + " bytes)"); evt.Stream.Write((byte[])dataMap[descriptor]); } catch (IOException) { Console.WriteLine("Unable to Write document"); //e.printStackTrace(); //System.exit(1); throw; } }