public void TestConstructor() { DirectoryProperty property1 = new DirectoryProperty("directory"); RawDataBlock[] rawBlocks = new RawDataBlock[4]; MemoryStream stream = new MemoryStream(new byte[2048]); for (int j = 0; j < 4; j++) { rawBlocks[j] = new RawDataBlock(stream); } POIFSDocument document = new POIFSDocument("document", rawBlocks, 2000); DocumentProperty property2 = document.DocumentProperty; DirectoryNode parent = new DirectoryNode(property1, (POIFSFileSystem)null, null); DocumentNode node = new DocumentNode(property2, parent); // Verify we can retrieve the document Assert.AreEqual(property2.Document, node.Document); // Verify we can Get the size Assert.AreEqual(property2.Size, node.Size); // Verify isDocumentEntry returns true Assert.IsTrue(node.IsDocumentEntry); // Verify isDirectoryEntry returns false Assert.IsTrue(!node.IsDirectoryEntry); // Verify GetName behaves correctly Assert.AreEqual(property2.Name, node.Name); // Verify GetParent behaves correctly Assert.AreEqual(parent, node.Parent); }
public void TestEmptyConstructor() { POIFSFileSystem fs = new POIFSFileSystem(); DirectoryProperty property1 = new DirectoryProperty("parent"); DirectoryProperty property2 = new DirectoryProperty("child"); DirectoryNode parent = new DirectoryNode(property1, fs, null); DirectoryNode node = new DirectoryNode(property2, fs, parent); Assert.AreEqual(0, parent.Path.Length); Assert.AreEqual(1, node.Path.Length); Assert.AreEqual("child", node.Path.GetComponent(0)); // Verify that GetEntries behaves correctly int count = 0; IEnumerator iter = node.Entries; while (iter.MoveNext()) { count++; } Assert.AreEqual(0, count); // Verify behavior of IsEmpty Assert.IsTrue(node.IsEmpty); // Verify behavior of EntryCount Assert.AreEqual(0, node.EntryCount); // Verify behavior of Entry try { node.GetEntry("foo"); Assert.Fail("Should have caught FileNotFoundException"); } catch (FileNotFoundException ) { // as expected } // Verify behavior of isDirectoryEntry Assert.IsTrue(node.IsDirectoryEntry); // Verify behavior of GetName Assert.AreEqual(property2.Name, node.Name); // Verify behavior of isDocumentEntry Assert.IsTrue(!node.IsDocumentEntry); // Verify behavior of GetParent Assert.AreEqual(parent, node.Parent); }
protected void PopulatePropertyTree(DirectoryProperty root) { try { int index = root.ChildIndex; if (!Property.IsValidIndex(index)) { return; } Stack <Property> children = new Stack <Property>(); children.Push(_properties[index]); while (children.Count != 0) { Property property = children.Pop(); if (property == null) { // unknown / unsupported / corrupted property, skip continue; } root.AddChild(property); if (property.IsDirectory) { PopulatePropertyTree((DirectoryProperty)property); } index = property.PreviousChildIndex; if (Property.IsValidIndex(index)) { children.Push(_properties[index]); } index = property.NextChildIndex; if (Property.IsValidIndex(index)) { children.Push(_properties[index]); } } } catch (IOException ex) { throw ex; } }
protected void PopulatePropertyTree(DirectoryProperty root) { try { int index = root.ChildIndex; if (!Property.IsValidIndex(index)) return; Stack<Property> children = new Stack<Property>(); children.Push(_properties[index]); while (children.Count != 0) { Property property = children.Pop(); if (property == null) { // unknown / unsupported / corrupted property, skip continue; } root.AddChild(property); if (property.IsDirectory) { PopulatePropertyTree((DirectoryProperty)property); } index = property.PreviousChildIndex; if (Property.IsValidIndex(index)) { children.Push(_properties[index]); } index = property.NextChildIndex; if (Property.IsValidIndex(index)) { children.Push(_properties[index]); } } } catch (IOException ex) { throw ex; } }
/// <summary> /// Create a DirectoryNode. This method Is not public by design; it /// Is intended strictly for the internal use of this package /// </summary> /// <param name="property">the DirectoryProperty for this DirectoryEntry</param> /// <param name="filesystem">the POIFSFileSystem we belong to</param> /// <param name="parent">the parent of this entry</param> public DirectoryNode(DirectoryProperty property, POIFSFileSystem filesystem, DirectoryNode parent) : base(property, parent) { if (parent == null) { _path = new POIFSDocumentPath(); } else { _path = new POIFSDocumentPath(parent._path, new String[] { property.Name }); } _filesystem = filesystem; _entries = new Hashtable(); IEnumerator iter = property.Children; while (iter.MoveNext()) { Property child = ( Property ) iter.Current; Entry childNode = null; if (child.IsDirectory) { childNode = new DirectoryNode(( DirectoryProperty ) child, _filesystem, this); } else { childNode = new DocumentNode(( DocumentProperty ) child, this); } _entries[childNode.Name]=childNode; } }
private void PopulatePropertyTree(DirectoryProperty root) { int index = root.ChildIndex; if (!Property.IsValidIndex(index)) { // property has no children return; } Stack children = new Stack(); children.Push(_properties[index]); while (!(children.Count == 0)) { Property property = ( Property )children.Pop(); if (property == null) { continue; } root.AddChild(property); if (property.IsDirectory) { PopulatePropertyTree(( DirectoryProperty )property); } index = property.PreviousChildIndex; if (Property.IsValidIndex(index)) { children.Push(_properties[index]); } index = property.NextChildIndex; if (Property.IsValidIndex(index)) { children.Push(_properties[index]); } } }
/// <summary> /// Add a new DirectoryProperty /// </summary> /// <param name="directory">The directory.</param> public void AddDirectory(DirectoryProperty directory) { _property_table.AddProperty(directory); }
private void PopulatePropertyTree(DirectoryProperty root) { int index = root.ChildIndex; if (!Property.IsValidIndex(index)) { // property has no children return; } Stack children = new Stack(); children.Push(_properties[index]); while (!(children.Count==0)) { Property property = ( Property ) children.Pop(); if (property == null) { continue; } root.AddChild(property); if (property.IsDirectory) { PopulatePropertyTree(( DirectoryProperty ) property); } index = property.PreviousChildIndex; if (Property.IsValidIndex(index)) { children.Push(_properties[index]); } index = property.NextChildIndex; if (Property.IsValidIndex(index)) { children.Push(_properties[index]); } } }
/// <summary> /// Create a new DirectoryEntry /// </summary> /// <param name="name">the name of the new DirectoryEntry</param> /// <returns>the name of the new DirectoryEntry</returns> public DirectoryEntry CreateDirectory(String name) { DirectoryProperty property = new DirectoryProperty(name); DirectoryNode rval = new DirectoryNode(property, _filesystem, this); (( DirectoryProperty ) Property).AddChild(property); _filesystem.AddDirectory(property); _entries[name]=rval; return rval; }
public void TestNonEmptyConstructor() { DirectoryProperty property1 = new DirectoryProperty("parent"); DirectoryProperty property2 = new DirectoryProperty("child1"); property1.AddChild(property2); property1.AddChild(new DocumentProperty("child2", 2000)); property2.AddChild(new DocumentProperty("child3", 30000)); DirectoryNode node = new DirectoryNode(property1, new POIFSFileSystem(), null); // Verify that GetEntries behaves correctly int count = 0; IEnumerator iter = node.Entries; while (iter.MoveNext()) { count++; //iter.Current; } Assert.AreEqual(2, count); // Verify behavior of IsEmpty Assert.IsTrue(!node.IsEmpty); // Verify behavior of EntryCount Assert.AreEqual(2, node.EntryCount); // Verify behavior of Entry DirectoryNode child1 = (DirectoryNode)node.GetEntry("child1"); child1.GetEntry("child3"); node.GetEntry("child2"); try { node.GetEntry("child3"); Assert.Fail("Should have caught FileNotFoundException"); } catch (FileNotFoundException) { // as expected } // Verify behavior of isDirectoryEntry Assert.IsTrue(node.IsDirectoryEntry); // Verify behavior of GetName Assert.AreEqual(property1.Name, node.Name); // Verify behavior of isDocumentEntry Assert.IsTrue(!node.IsDocumentEntry); // Verify behavior of GetParent Assert.IsNull(node.Parent); }
private void VerifyReadingProperty(int index, byte[] input, int offset, String name) { DirectoryProperty property = new DirectoryProperty(index, input, offset); MemoryStream stream = new MemoryStream(128); byte[] expected = new byte[128]; Array.Copy(input, offset, expected, 0, 128); property.WriteData(stream); byte[] output = stream.ToArray(); Assert.AreEqual(128, output.Length); for (int j = 0; j < 128; j++) { Assert.AreEqual(expected[j], output[j], "mismatch at offset " + j); } Assert.AreEqual(index, property.Index); Assert.AreEqual(name, property.Name); Assert.IsTrue(!property.Children.MoveNext()); }
private void CreateBasicDirectoryProperty() { String name = "MyDirectory"; _property = new DirectoryProperty(name); _testblock = new byte[128]; int index = 0; for (; index < 0x40; index++) { _testblock[index] = (byte)0; } int limit = Math.Min(31, name.Length); _testblock[index++] = (byte)(2 * (limit + 1)); _testblock[index++] = (byte)0; _testblock[index++] = (byte)1; _testblock[index++] = (byte)1; for (; index < 0x50; index++) { _testblock[index] = (byte)0xff; } for (; index < 0x80; index++) { _testblock[index] = (byte)0; } byte[] name_bytes = Encoding.UTF8.GetBytes(name); for (index = 0; index < limit; index++) { _testblock[index * 2] = name_bytes[index]; } }