[Libreoffice-commits] .: filter/source
Kohei Yoshida
kohei at kemper.freedesktop.org
Wed Aug 3 18:14:26 PDT 2011
filter/source/xmlfilterdetect/filterdetect.cxx | 71 +++++++++++++++++--------
1 file changed, 51 insertions(+), 20 deletions(-)
New commits:
commit eb88ad05c3fdecd6bee4a127eeccc2645af8b6ac
Author: Kohei Yoshida <kyoshida at novell.com>
Date: Mon Aug 1 16:52:31 2011 -0400
Check if the XML stream contains '<?xml' at the beginning.
If not, we'll simply bail out. Note that this will temporarily break
type detections for those esoteric, non-XML file format types that
incorrectly depended on the XML filter detect service to detect their
format types. To make them work again, please write a different
detection service other than the css.comp.filters.XMLFilterDetect.
diff --git a/filter/source/xmlfilterdetect/filterdetect.cxx b/filter/source/xmlfilterdetect/filterdetect.cxx
index 7f70a4a..17c4b19 100644
--- a/filter/source/xmlfilterdetect/filterdetect.cxx
+++ b/filter/source/xmlfilterdetect/filterdetect.cxx
@@ -32,6 +32,7 @@
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
+#include <cstring>
#include "filterdetect.hxx"
#include <osl/diagnose.h>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
@@ -43,7 +44,6 @@
#include <com/sun/star/xml/sax/XParser.hpp>
#include <com/sun/star/xml/XImportFilter.hpp>
#include <com/sun/star/xml/XExportFilter.hpp>
-#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/frame/XController.hpp>
#include <com/sun/star/task/XStatusIndicator.hpp>
#include <com/sun/star/task/XStatusIndicatorFactory.hpp>
@@ -91,11 +91,54 @@ using namespace com::sun::star::container;
using namespace com::sun::star::uno;
using namespace com::sun::star::beans;
+namespace {
-Reference< com::sun::star::frame::XModel > xModel;
+bool isXMLStream(const ::rtl::OString& aHeaderStrm)
+{
+ const char* p = aHeaderStrm.getStr();
+ size_t n = aHeaderStrm.getLength();
+ size_t i = 0;
-::rtl::OUString SAL_CALL supportedByType( const ::rtl::OUString clipBoardFormat , const ::rtl::OString resultString, const ::rtl::OUString checkType);
+ // Skip all preceding blank characters.
+ for (i = 0; i < n; ++i, ++p)
+ {
+ sal_Char c = *p;
+ if (c == ' ' || c == '\n' || c == '\t')
+ continue;
+ break;
+ }
+ n -= i;
+
+ // First text must be '<?xml', else it's not a valid XML file stream.
+ const char* sInitChars = "<?xml";
+ const size_t nInitCharLen = std::strlen(sInitChars);
+ for (i = 0; i < n; ++i, ++p)
+ {
+ if (i < nInitCharLen)
+ {
+ if (*p != sInitChars[i])
+ return false;
+ }
+ }
+ return true;
+}
+
+::rtl::OUString supportedByType( const ::rtl::OUString clipBoardFormat , const ::rtl::OString resultString, const ::rtl::OUString checkType)
+{
+ ::rtl::OUString sTypeName;
+ if ( clipBoardFormat.match(OUString(RTL_CONSTASCII_USTRINGPARAM("doctype:"))) )
+ {
+ ::rtl::OString tryStr = ::rtl::OUStringToOString(clipBoardFormat.copy(8),RTL_TEXTENCODING_ASCII_US).getStr();
+ if (resultString.indexOf(tryStr) >= 0)
+ {
+ sTypeName = checkType;
+ }
+ }
+ return sTypeName;
+}
+
+}
::rtl::OUString SAL_CALL FilterDetect::detect( com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >& aArguments ) throw( com::sun::star::uno::RuntimeException )
{
@@ -145,6 +188,11 @@ Reference< com::sun::star::frame::XModel > xModel;
long bytestRead =xInStream->readBytes (aData, 4000);
resultString=::rtl::OString((const sal_Char *)aData.getConstArray(),bytestRead) ;
+ if (!isXMLStream(resultString))
+ // This is not an XML stream. It makes no sense to try to detect
+ // a non-XML file type here.
+ return ::rtl::OUString();
+
// test typedetect code
Reference <XNameAccess> xTypeCont(mxMSF->createInstance(OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.TypeDetection" ))),UNO_QUERY);
Sequence < ::rtl::OUString > myTypes= xTypeCont->getElementNames();
@@ -189,23 +237,6 @@ Reference< com::sun::star::frame::XModel > xModel;
return sTypeName;
}
-
-
-::rtl::OUString SAL_CALL supportedByType( const ::rtl::OUString clipBoardFormat , const ::rtl::OString resultString, const ::rtl::OUString checkType)
-{
-
- ::rtl::OUString sTypeName;
- if((clipBoardFormat.match(OUString( RTL_CONSTASCII_USTRINGPARAM( "doctype:" )))))
- {
- ::rtl::OString tryStr = ::rtl::OUStringToOString(clipBoardFormat.copy(8),RTL_TEXTENCODING_ASCII_US).getStr();
- if (resultString.indexOf(tryStr) >= 0)
- {
- sTypeName = checkType;
- }
- }
- return sTypeName;
-}
-
// XInitialization
void SAL_CALL FilterDetect::initialize( const Sequence< Any >& aArguments )
More information about the Libreoffice-commits
mailing list