incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From devin...@apache.org
Subject svn commit: r1172445 - in /incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator: ManifestFilter.java ODFFileValidator.java ODFRootPackageValidator.java ODFStreamValidator.java
Date Mon, 19 Sep 2011 05:35:42 GMT
Author: devinhan
Date: Mon Sep 19 05:35:42 2011
New Revision: 1172445

URL: http://svn.apache.org/viewvc?rev=1172445&view=rev
Log:
changeset:   14:f518d9fb2c7e
user:        Michael Stahl <mst@openoffice.org>
date:        Thu Apr 08 16:14:39 2010 +0200
improve checks of package conformance:
 - check that files in manifest are actually in package
 - check that all files in package have manifiest entries
 - check that "mimetype" exists
 - check that "mimetype" is first file in package
 - check that "mimetype" is not compressed and has no "extra field"

Modified:
    incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ManifestFilter.java
    incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFFileValidator.java
    incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFRootPackageValidator.java
    incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFStreamValidator.java

Modified: incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ManifestFilter.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ManifestFilter.java?rev=1172445&r1=1172444&r2=1172445&view=diff
==============================================================================
--- incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ManifestFilter.java (original)
+++ incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ManifestFilter.java Mon
Sep 19 05:35:42 2011
@@ -24,6 +24,7 @@ package org.odftoolkit.odfvalidator;
 
 import java.io.IOException;
 import java.io.StringReader;
+import java.util.Set;
 import org.xml.sax.Attributes;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
@@ -40,17 +41,25 @@ class ManifestFilter extends NamespaceFi
     private static final String MEDIA_TYPE = "media-type";
     
     private Logger m_aLogger;
-    
+
+    private Set<String> m_ZipEntries = null;
+    private boolean m_bInvalidManifestEntry = false;
+
     private ManifestListener m_aManifestListener = null;
     private ManifestEntryListener m_aManifestEntryListener = null;
     
     /** Creates a new instance of KnownIssueFilter */
-    ManifestFilter( Logger aLogger, ManifestListener aManifestListener, ManifestEntryListener
aManifestEntryListener ) {
+    ManifestFilter(Logger aLogger, ManifestListener aManifestListener,
+            ManifestEntryListener aManifestEntryListener, Set<String> zipEntries)
+    {
         m_aLogger = aLogger;
+        m_ZipEntries = zipEntries;
         m_aManifestListener = aManifestListener;
         m_aManifestEntryListener = aManifestEntryListener;
     }
 
+    public boolean hasInvalidManifestEntry() { return m_bInvalidManifestEntry; }
+
     @Override
     public InputSource resolveEntity(String aPublicId, String aSystemId) throws SAXException,
IOException {
         // Ignore the external OOo Manifest DTD which was errornously included
@@ -100,6 +109,10 @@ class ManifestFilter extends NamespaceFi
                     if( m_aManifestEntryListener != null )
                         m_aManifestEntryListener.foundManifestEntry( new ManifestEntry(aFullPath,aMediaType)
);
                 }
+                if (!m_ZipEntries.remove(aFullPath) && !aFullPath.endsWith("/"))
{
+                    m_aLogger.logError("manifest entry does not exist in package: " + aFullPath);
+                    m_bInvalidManifestEntry = true;
+                }
             }
         }
     }

Modified: incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFFileValidator.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFFileValidator.java?rev=1172445&r1=1172444&r2=1172445&view=diff
==============================================================================
--- incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFFileValidator.java (original)
+++ incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFFileValidator.java Mon
Sep 19 05:35:42 2011
@@ -23,6 +23,11 @@
 package org.odftoolkit.odfvalidator;
 
 import java.io.File;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 import org.odftoolkit.odfdom.pkg.OdfPackage;
 
 
@@ -47,7 +52,19 @@ public class ODFFileValidator extends OD
     {
         return m_aFile.getAbsolutePath();
     }
-    
+
+    List<ZipEntry> getZipEntries() throws Exception
+    {
+        ZipFile zip = new ZipFile(m_aFile);
+        List<ZipEntry> ret = new ArrayList<ZipEntry>();
+        Enumeration<? extends ZipEntry> entries = zip.entries();
+        while (entries.hasMoreElements()) {
+            ret.add(entries.nextElement());
+        }
+        zip.close();
+        return ret;
+    }
+
     protected OdfPackage getPackage() throws Exception
     {
         return OdfPackage.loadPackage( m_aFile );

Modified: incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFRootPackageValidator.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFRootPackageValidator.java?rev=1172445&r1=1172444&r2=1172445&view=diff
==============================================================================
--- incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFRootPackageValidator.java
(original)
+++ incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFRootPackageValidator.java
Mon Sep 19 05:35:42 2011
@@ -24,14 +24,21 @@ package org.odftoolkit.odfvalidator;
 
 import java.io.IOException;
 import java.io.PrintStream;
+import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
 import java.util.Vector;
+import java.util.zip.ZipEntry;
 import java.util.zip.ZipException;
 import javax.xml.validation.Validator;
 import org.odftoolkit.odfdom.pkg.OdfPackage;
 
 public abstract class ODFRootPackageValidator extends ODFPackageValidator implements ManifestEntryListener
{
 
+    static final String s_Mimetype = "mimetype";
+
+    private List<ZipEntry> m_ZipEntries = null;
     private OdfPackage m_aPkg = null;
     private Vector<ManifestEntry> m_aSubDocs = null;
 
@@ -40,8 +47,21 @@ public abstract class ODFRootPackageVali
         super(nLogLevel, eMode, aVersion, aFilter, aValidatorProvider);
     }
 
+    abstract List<ZipEntry> getZipEntries() throws Exception;
+
     abstract OdfPackage getPackage() throws Exception;
-    
+
+    List<ZipEntry> getZipEntries(Logger aLogger) {
+        if (m_ZipEntries == null) {
+            try {
+                m_ZipEntries = getZipEntries();
+            } catch (Exception e) {
+                aLogger.logFatalError(e.getMessage());
+            }
+        }
+        return m_ZipEntries;
+    }
+
     OdfPackage getPackage(Logger aLogger) {
         if (m_aPkg == null) {
             try {
@@ -68,7 +88,7 @@ public abstract class ODFRootPackageVali
     @Override
     protected boolean validatePre(PrintStream aOut, OdfVersion aVersion ) throws ODFValidatorException,
IOException
     {
-        boolean bErrorsFound = validateMimetype( aOut );
+        boolean bErrorsFound = validateMimetype(aOut, aVersion);
         bErrorsFound |= validateManifest(aOut, aVersion  );
 
         return bErrorsFound;
@@ -124,10 +144,46 @@ public abstract class ODFRootPackageVali
         }
     }
 
-    private boolean validateMimetype( PrintStream aOut )
+    private boolean validateMimetype(PrintStream aOut, OdfVersion aVersion)
     {
         Logger aLogger = new Logger(getLoggerName(),"MIMETYPE",aOut, m_nLogLevel);
 
+        List<ZipEntry> zipEntries = getZipEntries(aLogger);
+
+        if (zipEntries.size() == 0) {
+            aLogger.logFatalError("ZIP file is empty.");
+            return true;
+        }
+        // n.b.: mimetype is optional (only "should") in 1.1
+        ZipEntry mimetype = null;
+        for (int i = 0; i < zipEntries.size(); ++i) {
+            if (zipEntries.get(i).getName().equals(s_Mimetype)) {
+                mimetype = zipEntries.get(i);
+                if (i != 0) {
+                    aLogger.logWarning(s_Mimetype +
+                        " is not the first file in the ODF package.");
+                }
+            }
+        }
+        if (mimetype == null) {
+            String msg = "ODF package contains no " + s_Mimetype + ".";
+            if (aVersion == OdfVersion.V1_0 || aVersion == OdfVersion.V1_1) {
+                aLogger.logWarning(msg);
+            } else {
+                aLogger.logError(msg);
+                return true;
+            }
+        } else {
+            if (mimetype.getMethod() != ZipEntry.STORED) {
+                aLogger.logError(s_Mimetype + " is compressed.");
+                return true;
+            }
+            if (mimetype.getExtra() != null) {
+                aLogger.logError(s_Mimetype + " has extra field.");
+                return true;
+            }
+        }
+
         String aMimetype=getPackage(aLogger).getMediaType();
         if( (aMimetype == null) || aMimetype.length() == 0 ) {
             aLogger.logFatalError("file is not a zip file, or has no mimetype.");
@@ -169,12 +225,40 @@ public abstract class ODFRootPackageVali
     {
         boolean bRet;
         Logger aLogger = new Logger(getLoggerName(),OdfPackage.OdfFile.MANIFEST.getPath(),aOut,
m_nLogLevel);
-        ManifestFilter aFilter = new ManifestFilter(aLogger,m_aResult, this);
+        Set<String> zipEntrySet = new HashSet<String>();
+        List<ZipEntry> entries = getZipEntries(aLogger);
+        for (ZipEntry entry : entries) {
+            String name = entry.getName();
+            // filter files that are not listed in manifest.xml and directories
+            if (!name.equals("mimetype") && !name.startsWith("META-INF/") &&
+                !name.endsWith("/")) {
+                    zipEntrySet.add(name);
+            }
+        }
+        ManifestFilter aFilter =
+            new ManifestFilter(aLogger, m_aResult, this, zipEntrySet);
         Validator aManifestValidator = m_aValidatorProvider.getManifestValidator(aOut,aVersion);
         if( aManifestValidator != null )
         {
             bRet = validateEntry(aOut, aFilter,
                          aManifestValidator, aLogger, OdfPackage.OdfFile.MANIFEST.getPath()
);
+            if (!zipEntrySet.isEmpty()) {
+                // seems to be allowed in 1.1
+                String msg = "no manifest entry for package entry: ";
+                if (aVersion == OdfVersion.V1_0 || aVersion == OdfVersion.V1_1) {
+                    for (String entry : zipEntrySet) {
+                        aLogger.logWarning(msg + entry);
+                    }
+                } else {
+                    bRet = true;
+                    for (String entry : zipEntrySet) {
+                        aLogger.logError(msg + entry);
+                    }
+                }
+            }
+            if (aFilter.hasInvalidManifestEntry()) {
+                bRet = true;
+            }
         }
         else
         {

Modified: incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFStreamValidator.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFStreamValidator.java?rev=1172445&r1=1172444&r2=1172445&view=diff
==============================================================================
--- incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFStreamValidator.java
(original)
+++ incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFStreamValidator.java
Mon Sep 19 05:35:42 2011
@@ -23,10 +23,11 @@
 package org.odftoolkit.odfvalidator;
 
 import java.io.InputStream;
+import java.util.List;
+import java.util.zip.ZipEntry;
 import org.odftoolkit.odfdom.pkg.OdfPackage;
 
 
-
 /**
  * Validator for Streams
  */
@@ -50,6 +51,10 @@ public class ODFStreamValidator extends 
         m_aBaseURI=aBaseURI;
     }
 
+    List<ZipEntry> getZipEntries() throws Exception {
+        throw new RuntimeException("not implemented");
+    }
+
     OdfPackage getPackage() throws Exception
     {
         return OdfPackage.loadPackage( m_aInputStream );



Mime
View raw message