incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From devin...@apache.org
Subject svn commit: r1172449 - /incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/
Date Mon, 19 Sep 2011 05:36:47 GMT
Author: devinhan
Date: Mon Sep 19 05:36:47 2011
New Revision: 1172449

URL: http://svn.apache.org/viewvc?rev=1172449&view=rev
Log:
changeset:   18:f1ac402b35a1
user:        Michael Stahl <mst@openoffice.org>
date:        Tue Nov 23 11:34:13 2010 +0100
replace previous implementation of package checks with a new one based on
package validation of ODFDOM.

Added:
    incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFPackageErrorHandler.java
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/ODFPackageValidator.java
    incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFRootPackageValidator.java
    incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFStreamValidator.java
    incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFValidationResult.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=1172449&r1=1172448&r2=1172449&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:36:47 2011
@@ -24,7 +24,6 @@ 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;
@@ -42,24 +41,18 @@ class ManifestFilter extends NamespaceFi
     
     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, Set<String> zipEntries)
+            ManifestEntryListener aManifestEntryListener)
     {
         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
@@ -109,10 +102,6 @@ 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=1172449&r1=1172448&r2=1172449&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:36:47 2011
@@ -23,12 +23,8 @@
 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;
+import org.xml.sax.ErrorHandler;
 
 
 /**
@@ -53,21 +49,9 @@ public class ODFFileValidator extends OD
         return m_aFile.getAbsolutePath();
     }
 
-    List<ZipEntry> getZipEntries() throws Exception
+    protected OdfPackage getPackage(ErrorHandler handler) 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 );
+        return OdfPackage.loadPackage(m_aFile, handler);
     }
 
 }

Added: incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFPackageErrorHandler.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFPackageErrorHandler.java?rev=1172449&view=auto
==============================================================================
--- incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFPackageErrorHandler.java
(added)
+++ incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFPackageErrorHandler.java
Mon Sep 19 05:36:47 2011
@@ -0,0 +1,120 @@
+/************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
+ *
+ * Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
+ *
+ * Use is subject to license terms.
+ *
+ * Licensed 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. You can also
+ * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
+ *
+ * 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.odftoolkit.odfvalidator;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.odftoolkit.odfdom.pkg.OdfValidationException;
+import org.odftoolkit.odfdom.pkg.ValidationConstraint;
+import org.odftoolkit.odfdom.pkg.OdfPackageConstraint;
+import org.odftoolkit.odfdom.dom.OdfSchemaConstraint;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+class ODFPackageErrorHandler implements ErrorHandler {
+
+    List<SAXParseException> m_Saved = new ArrayList<SAXParseException>();
+
+    public void warning(SAXParseException exception) throws SAXException {
+        m_Saved.add(exception);
+    }
+
+    public void error(SAXParseException exception) throws SAXException {
+        m_Saved.add(exception);
+    }
+
+    public void fatalError(SAXParseException exception) throws SAXException {
+        throw exception;
+    }
+
+    /// deferred because version is not available when odfdom checks mimetype
+    /// @returns true iff there was an error and package is not valid
+    boolean validate(Logger manifestLogger, Logger mimeTypeLogger,
+                     OdfVersion version)
+    {
+        boolean bRet = false;
+        LOOP:
+        for (SAXParseException e : m_Saved) {
+            if (e instanceof OdfValidationException) {
+                ValidationConstraint constraint =
+                    ((OdfValidationException) e).getConstraint();
+                if (constraint instanceof OdfPackageConstraint) {
+                    switch (((OdfPackageConstraint) constraint)) {
+                        case MANIFEST_DOES_NOT_LIST_FILE:
+                            switch (version) {
+                                case V1_0:
+                                case V1_1:
+                                    manifestLogger.logWarning(e.getMessage());
+                                break;
+                                default:
+                                    manifestLogger.logError(e.getMessage());
+                                    bRet = true;
+                                break;
+                            }
+                            continue LOOP;
+                        case MIMETYPE_NOT_FIRST_IN_PACKAGE:
+                        case MIMETYPE_NOT_IN_PACKAGE:
+                            switch (version) {
+                                case V1_0:
+                                case V1_1:
+                                    mimeTypeLogger.logWarning(e.getMessage());
+                                break;
+                                default:
+                                    mimeTypeLogger.logError(e.getMessage());
+                                    bRet = true;
+                                break;
+                            }
+                            continue LOOP;
+                        case MANIFEST_NOT_IN_PACKAGE:
+                        case MANIFEST_LISTS_NONEXISTENT_FILE:
+                            manifestLogger.logError(e.getMessage());
+                            bRet = true;
+                            continue LOOP;
+                        case MIMETYPE_IS_COMPRESSED:
+                        case MIMETYPE_HAS_EXTRA_FIELD:
+                        case MIMETYPE_DIFFERS_FROM_PACKAGE:
+                            mimeTypeLogger.logError(e.getMessage());
+                            bRet = true;
+                            continue LOOP;
+                        case MANIFEST_LISTS_DIRECTORY:
+                        case MANIFEST_DOES_NOT_LIST_DIRECTORY:
+                            manifestLogger.logWarning(e.getMessage());
+                            continue LOOP;
+                    }
+                } else if (constraint instanceof OdfSchemaConstraint) {
+                    switch (((OdfSchemaConstraint) constraint)) {
+                        case DOCUMENT_WITHOUT_ODF_MIMETYPE:
+                        case PACKAGE_SHALL_CONTAIN_CONTENT_OR_STYLES_XML:
+                            manifestLogger.logError(e.getMessage());
+                            bRet = true;
+                            continue LOOP;
+                    }
+                }
+            }
+            manifestLogger.logError(e); // unknown constraint: assume error
+        }
+        m_Saved.clear();
+        return bRet;
+    }
+}

Modified: incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFPackageValidator.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFPackageValidator.java?rev=1172449&r1=1172448&r2=1172449&view=diff
==============================================================================
--- incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFPackageValidator.java
(original)
+++ incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFPackageValidator.java
Mon Sep 19 05:36:47 2011
@@ -450,6 +450,7 @@ public abstract class ODFPackageValidato
                 aInStream = aPkg.getInputStream(getStreamName(OdfDocument.OdfXMLFile.SETTINGS.getFileName()));
             if( aInStream == null )
                 aInStream = aPkg.getInputStream(getStreamName(OdfDocument.OdfXMLFile.CONTENT.getFileName()));
+            if (aInStream == null) { return null; }
         }
         catch( Exception e )
         {
@@ -480,4 +481,4 @@ public abstract class ODFPackageValidato
         return aVersion;
     }
 
-}
\ No newline at end of file
+}

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=1172449&r1=1172448&r2=1172449&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:36:47 2011
@@ -24,48 +24,37 @@ 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;
+import org.odftoolkit.odfdom.doc.OdfDocument;
+import org.xml.sax.ErrorHandler;
 
 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;
-
+    private ODFPackageErrorHandler m_ErrorHandler = null;
 
     ODFRootPackageValidator(Logger.LogLevel nLogLevel, OdfValidatorMode eMode, OdfVersion
aVersion, SAXParseExceptionFilter aFilter, ODFValidatorProvider aValidatorProvider) {
         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;
-    }
+    abstract OdfPackage getPackage(ErrorHandler handler) throws Exception;
 
     OdfPackage getPackage(Logger aLogger) {
         if (m_aPkg == null) {
             try {
-                m_aPkg = getPackage();
+                m_ErrorHandler = new ODFPackageErrorHandler();
+                m_aPkg = getPackage(m_ErrorHandler);
+                // for additional mimetype checking, load root document
+                try {
+                    OdfDocument.loadDocument(m_aPkg, "");
+                } catch (Exception e) {
+                    // ignore -- the interesting stuff is passed to handler
+                }
             } catch (IOException e) {
                 if (e.getMessage().startsWith("only DEFLATED entries can have EXT descriptor"))
{
                     aLogger.logFatalError("The document is encrypted. Validation of encrypted
documents is not supported.");
@@ -147,50 +136,14 @@ public abstract class ODFRootPackageVali
     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;
-            }
-        }
+        boolean bHasErrors = false;
 
         String aMimetype=getPackage(aLogger).getMediaTypeString();
         if( (aMimetype == null) || aMimetype.length() == 0 ) {
             aLogger.logFatalError("file is not a zip file, or has no mimetype.");
-            return true;
-        }
-
-        if( !(aMimetype.equals(ODFMediaTypes.TEXT_MEDIA_TYPE)
+            bHasErrors = true;
+        } else if(
+            ! (aMimetype.equals(ODFMediaTypes.TEXT_MEDIA_TYPE)
             || aMimetype.equals(ODFMediaTypes.TEXT_TEMPLATE_MEDIA_TYPE)
             || aMimetype.equals(ODFMediaTypes.GRAPHICS_MEDIA_TYPE)
             || aMimetype.equals(ODFMediaTypes.GRAPHICS_TEMPLATE_MEDIA_TYPE)
@@ -204,67 +157,39 @@ public abstract class ODFRootPackageVali
             || aMimetype.equals(ODFMediaTypes.FORMULA_MEDIA_TYPE)
             || aMimetype.equals(ODFMediaTypes.FORMULA_TEMPLATE_MEDIA_TYPE)
             || aMimetype.equals(ODFMediaTypes.TEXT_MASTER_MEDIA_TYPE)
-            || aMimetype.equals(ODFMediaTypes.TEXT_WEB_MEDIA_TYPE) ) ) {
+            || aMimetype.equals(ODFMediaTypes.TEXT_WEB_MEDIA_TYPE) ) )
+        {
                 aLogger.logInfo("mimetype is not an ODFMediaTypes mimetype.",false);
-                return true;
-        }
-
-        /* TODO: not supported by ODFDOM
-        if ( aDocFile .isMimeTypeValid() ) {
-            aLogger.logInfo("no errors",false);
-        } else {
-            aLogger.logError("file is not the first file in the ODF package or is compressed.");
-            return true;
+                bHasErrors = true;
         }
-        */
 
-        return false;
+        return bHasErrors;
     }
 
     private boolean validateManifest(PrintStream aOut, OdfVersion aVersion ) throws IOException,
ZipException, IllegalStateException, ODFValidatorException
     {
         boolean bRet;
         Logger aLogger = new Logger(getLoggerName(),OdfPackage.OdfFile.MANIFEST.getPath(),aOut,
m_nLogLevel);
-        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);
+        ManifestFilter aFilter = new ManifestFilter(aLogger, m_aResult, this);
         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
         {
             aLogger.logInfo( "Validation of " + OdfPackage.OdfFile.MANIFEST.getPath() + "
skipped.", false);
             bRet = parseEntry(aOut, aFilter, aLogger, OdfPackage.OdfFile.MANIFEST.getPath()
, false);
         }
+        // UGLY: do something that causes ODFDOM to parse the manifest, which
+        // may cause m_ErrorHandler to be called
+        m_aPkg.getFileEntries();
+        // hack: just create logger again, too lazy to create a Pair class
+        // and return it from validateMimetype...
+        Logger aMimetypeLogger =
+            new Logger(getLoggerName(),"MIMETYPE",aOut, m_nLogLevel);
+        bRet |= m_ErrorHandler.validate(aLogger, aMimetypeLogger, aVersion);
         return bRet;
     }
 

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=1172449&r1=1172448&r2=1172449&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:36:47 2011
@@ -23,9 +23,8 @@
 package org.odftoolkit.odfvalidator;
 
 import java.io.InputStream;
-import java.util.List;
-import java.util.zip.ZipEntry;
 import org.odftoolkit.odfdom.pkg.OdfPackage;
+import org.xml.sax.ErrorHandler;
 
 
 /**
@@ -51,13 +50,9 @@ public class ODFStreamValidator extends 
         m_aBaseURI=aBaseURI;
     }
 
-    List<ZipEntry> getZipEntries() throws Exception {
-        throw new RuntimeException("not implemented");
-    }
-
-    OdfPackage getPackage() throws Exception
+    OdfPackage getPackage(ErrorHandler handler) throws Exception
     {
-        return OdfPackage.loadPackage( m_aInputStream );
+        return OdfPackage.loadPackage(m_aInputStream, m_aBaseURI, handler);
     }
     
     String getLoggerName() {

Modified: incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFValidationResult.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFValidationResult.java?rev=1172449&r1=1172448&r2=1172449&view=diff
==============================================================================
--- incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFValidationResult.java
(original)
+++ incubator/odf/trunk/validator/src/org/odftoolkit/odfvalidator/ODFValidationResult.java
Mon Sep 19 05:36:47 2011
@@ -49,7 +49,7 @@ public class ODFValidationResult impleme
     }
 
     private String m_aGenerator = null;
-    private String m_aMediaType = null;
+    private String m_aMediaType = "";
 
     private HashMap<String,Long> m_aForeignElementMap = null;
     private HashMap<String,Long> m_aForeignAttributeMap = null;



Mime
View raw message