Return-Path: X-Original-To: apmail-poi-commits-archive@minotaur.apache.org Delivered-To: apmail-poi-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 69036781B for ; Fri, 25 Nov 2011 21:56:49 +0000 (UTC) Received: (qmail 11438 invoked by uid 500); 25 Nov 2011 21:56:49 -0000 Delivered-To: apmail-poi-commits-archive@poi.apache.org Received: (qmail 11401 invoked by uid 500); 25 Nov 2011 21:56:49 -0000 Mailing-List: contact commits-help@poi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@poi.apache.org Delivered-To: mailing list commits@poi.apache.org Received: (qmail 11393 invoked by uid 99); 25 Nov 2011 21:56:49 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Nov 2011 21:56:49 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Nov 2011 21:56:45 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 70770238890A for ; Fri, 25 Nov 2011 21:56:24 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1206355 - in /poi/trunk/src: java/org/apache/poi/poifs/filesystem/EntryUtils.java testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java Date: Fri, 25 Nov 2011 21:56:24 -0000 To: commits@poi.apache.org From: nick@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111125215624.70770238890A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: nick Date: Fri Nov 25 21:56:19 2011 New Revision: 1206355 URL: http://svn.apache.org/viewvc?rev=1206355&view=rev Log: Start on a new method for POIFS EntryUtils for checking to see if the two Directories have the same contents, plus stub out some tests for EntryUtils (to follow) Added: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java?rev=1206355&r1=1206354&r2=1206355&view=diff ============================================================================== --- poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java (original) +++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java Fri Nov 25 21:56:19 2011 @@ -16,9 +16,12 @@ ==================================================================== */ package org.apache.poi.poifs.filesystem; +import java.io.FileNotFoundException; import java.io.IOException; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import org.apache.poi.util.Internal; @@ -96,4 +99,118 @@ public class EntryUtils // System.err.println("CopyNodes called"); copyNodes( source.getRoot(), target.getRoot(), excepts ); } + + /** + * Checks to see if the two Directories hold the same contents. + * For this to be true, they must have entries with the same names, + * no entries in one but not the other, and the size+contents + * of each entry must match. + * TODO Some sort of excepts support + */ + public static boolean areDirectoriesIdentical(DirectoryNode dirA, DirectoryNode dirB) { + // First up, check they have the same number of children + if (dirA.getEntryCount() != dirB.getEntryCount()) { + return false; + } + + // Next, check entries and their types/sizes + Map aSizes = new HashMap(); + final int isDirectory = -12345; + for (Entry a : dirA) { + String aName = a.getName(); + if (a.isDirectoryEntry()) { + aSizes.put(aName, isDirectory); + } else { + aSizes.put(aName, ((DocumentNode)a).getSize()); + } + } + for (Entry b : dirB) { + String bName = b.getName(); + if (! aSizes.containsKey(bName)) { + // In B but not A + return false; + } + + int size; + if (b.isDirectoryEntry()) { + size = isDirectory; + } else { + size = ((DocumentNode)b).getSize(); + } + if (size != aSizes.get(bName)) { + // Either the wrong type, or they're different sizes + return false; + } + + // Track it as checked + aSizes.remove(bName); + } + if (!aSizes.isEmpty()) { + // Nodes were in A but not B + return false; + } + + // If that passed, check entry contents + for (Entry a : dirA) { + try { + Entry b = dirB.getEntry(a.getName()); + boolean match; + if (a.isDirectoryEntry()) { + match = areDirectoriesIdentical( + (DirectoryNode)a, (DirectoryNode)b); + } else { + match = areDocumentsIdentical( + (DocumentNode)a, (DocumentNode)b); + } + if (!match) return false; + } catch(FileNotFoundException e) { + // Shouldn't really happen... + return false; + } catch(IOException e) { + // Something's messed up with one document, not a match + return false; + } + } + + // If we get here, they match! + return true; + } + + /** + * Checks to see if two Documents have the same name + * and the same contents. (Their parent directories are + * not checked) + */ + public static boolean areDocumentsIdentical(DocumentNode docA, DocumentNode docB) throws IOException { + if (! docA.getName().equals(docB.getName())) { + // Names don't match, not the same + return false; + } + if (docA.getSize() != docB.getSize()) { + // Wrong sizes, can't have the same contents + return false; + } + + boolean matches = true; + DocumentInputStream inpA = null, inpB = null; + try { + inpA = new DocumentInputStream(docA); + inpB = new DocumentInputStream(docB); + + int readA, readB; + do { + readA = inpA.read(); + readB = inpB.read(); + if (readA != readB) { + matches = false; + break; + } + } while(readA != -1 && readB != -1); + } finally { + if (inpA != null) inpA.close(); + if (inpB != null) inpB.close(); + } + + return matches; + } } Added: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java?rev=1206355&view=auto ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java (added) +++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java Fri Nov 25 21:56:19 2011 @@ -0,0 +1,38 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.poifs.filesystem; + +import junit.framework.TestCase; + +import org.apache.poi.POIDataSamples; + +public class TestEntryUtils extends TestCase { + private static final POIDataSamples dataSamples = POIDataSamples.getPOIFSInstance(); + + public void testCopyRecursively() throws Exception { + // TODO + } + + public void testAreDocumentsIdentical() throws Exception { + // TODO + } + + public void testAreDirectoriesIdentical() throws Exception { + // TODO + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org For additional commands, e-mail: commits-help@poi.apache.org