# HG changeset patch
# User Michael Stahl <mst@openoffice.org>
# Date 1303140562 0
# Node ID 17c70429893f2272998d404440df3e79e4493355
# Parent  1cc59e7ad82d545407595f004c0449687946cf1f
rtftok01: writerfilter: add basic RTF scanner handler

diff --git a/writerfilter/inc/rtftok/RTFDocument.hxx b/writerfilter/inc/rtftok/RTFDocument.hxx
new file mode 100644
--- /dev/null
+++ b/writerfilter/inc/rtftok/RTFDocument.hxx
@@ -0,0 +1,70 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * This file is part of OpenOffice.org.
+ *
+ * OpenOffice.org is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 3
+ * only, as published by the Free Software Foundation.
+ *
+ * OpenOffice.org is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License version 3 for more details
+ * (a copy is included in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * version 3 along with OpenOffice.org.  If not, see
+ * <http://www.openoffice.org/license.html>
+ * for a copy of the LGPLv3 License.
+ *
+ ************************************************************************/
+
+#ifndef INCLUDED_RTF_DOCUMENT_HXX
+#define INCLUDED_RTF_DOCUMENT_HXX
+
+#include <boost/shared_ptr.hpp>
+
+#include <com/sun/star/io/XInputStream.hpp>
+
+#include <resourcemodel/WW8ResourceModel.hxx>
+
+
+namespace writerfilter {
+namespace rtftok {
+
+class WRITERFILTER_DLLPUBLIC RTFDocument
+    : public writerfilter::Reference<Stream>
+{
+
+public:
+    typedef ::boost::shared_ptr<RTFDocument> Pointer_t;
+
+    virtual ~RTFDocument() { }
+
+    virtual void resolve(Stream & rHandler) = 0;
+    virtual ::std::string getType() const = 0;
+
+};
+
+class WRITERFILTER_DLLPUBLIC RTFDocumentFactory
+{
+
+public:
+    static RTFDocument::Pointer_t
+    createDocument(
+        ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >
+            const & xInputStream );
+
+};
+
+} // namespace writerfilter
+} // namespace rtftok
+
+#endif // INCLUDED_RTF_DOCUMENT_HXX
+
diff --git a/writerfilter/source/rtftok/RTFDocumentImpl.cxx b/writerfilter/source/rtftok/RTFDocumentImpl.cxx
new file mode 100644
--- /dev/null
+++ b/writerfilter/source/rtftok/RTFDocumentImpl.cxx
@@ -0,0 +1,165 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * This file is part of OpenOffice.org.
+ *
+ * OpenOffice.org is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 3
+ * only, as published by the Free Software Foundation.
+ *
+ * OpenOffice.org is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License version 3 for more details
+ * (a copy is included in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * version 3 along with OpenOffice.org.  If not, see
+ * <http://www.openoffice.org/license.html>
+ * for a copy of the LGPLv3 License.
+ *
+ ************************************************************************/
+
+#include <RTFDocumentImpl.hxx>
+
+#include <algorithm>
+
+#include <comphelper/stlunosequence.hxx>
+
+#include <resourcemodel/TagLogger.hxx>
+#include <rtftok/RTFScanner.hxx>
+#include <rtftok/RTFScannerHandler.hxx>
+#include <rtftok/RTFInputSource.hxx>
+
+
+using namespace ::com::sun::star;
+
+
+namespace writerfilter {
+namespace rtftok {
+
+static TagLogger & getLogger()
+{
+    return *TagLogger::getInstance("DEBUG");
+}
+
+
+
+class RTFInputStream
+    : public RTFInputSource
+{
+public:
+    explicit RTFInputStream(
+            uno::Reference<io::XInputStream> const& xInputStream)
+        : m_xInputStream(xInputStream) { }
+    virtual ~RTFInputStream() { }
+    virtual size_t read(void *const buffer, size_t const maxLen)
+    {
+        static size_t const MAXBYTES( (1<<20) );
+        sal_Int32 const nBytes( (maxLen > MAXBYTES) ? MAXBYTES : maxLen );
+        uno::Sequence<sal_Int8> buf;
+        sal_Int32 const nRead = m_xInputStream->readBytes(buf, nBytes);
+        ::std::copy(::comphelper::stl_begin(buf), ::comphelper::stl_end(buf),
+                static_cast<sal_Int8*>(buffer));
+        OSL_ASSERT(nRead >= 0);
+        return nRead;
+    }
+private:
+    uno::Reference<io::XInputStream> m_xInputStream;
+};
+
+class RTFScannerHandlerImpl
+    : public RTFScannerHandler
+{
+public:
+    explicit RTFScannerHandlerImpl(Stream & rStream)
+        : m_rStream(rStream)
+    { }
+    virtual ~RTFScannerHandlerImpl() { }
+    virtual void dest(char const*const token, char const*const value)
+    { }
+    virtual void ctrl(char const*const token, char const*const value)
+    { }
+    virtual void lbrace()
+    { }
+    virtual void rbrace()
+    { }
+    virtual void addSpaces(unsigned int const count)
+    { }
+    virtual void addBinData(unsigned char const data)
+    { }
+    virtual void addChar(char const ch)
+    { }
+    virtual void addCharU(sal_Unicode const ch)
+    { }
+    virtual void addHexChar(char const hexch[2])
+    { }
+private:
+    Stream & m_rStream;
+};
+
+
+
+class RTFDocumentImpl::Impl
+{
+    friend class RTFDocumentImpl;
+
+    uno::Reference<io::XInputStream> m_xInputStream;
+
+    explicit Impl(uno::Reference<io::XInputStream> const& xInputStream)
+        : m_xInputStream(xInputStream)
+    {
+        OSL_ENSURE(xInputStream.is(), "RTFDocument: no input stream");
+        if (!xInputStream.is()) {
+            throw uno::RuntimeException();
+        }
+    }
+};
+
+RTFDocumentImpl::RTFDocumentImpl(
+        uno::Reference<io::XInputStream> const& xInputStream)
+    : m_pImpl( new Impl(xInputStream) )
+{
+}
+
+RTFDocumentImpl::~RTFDocumentImpl()
+{
+}
+
+void RTFDocumentImpl::resolve(Stream & rStream)
+{
+#ifdef DEBUG_RESOLVE
+    getLogger().startElement("RTFDocumentImpl.resolve");
+#endif
+
+    RTFInputStream input(m_pImpl->m_xInputStream);
+    RTFScannerHandlerImpl handler(rStream);
+
+    scan(input, handler);
+
+#ifdef DEBUG_RESOLVE
+    getLogger().endElement("RTFDocumentImpl.resolve");
+#endif
+}
+
+::std::string RTFDocumentImpl::getType() const
+{
+    return "RTFDocumentImpl";
+}
+
+
+RTFDocument::Pointer_t
+RTFDocumentFactory::createDocument(
+    uno::Reference< io::XInputStream > const & xInputStream)
+{
+    return RTFDocument::Pointer_t(new RTFDocumentImpl(xInputStream));
+}
+
+} // namespace writerfilter
+} // namespace rtftok
+
diff --git a/writerfilter/source/rtftok/RTFDocumentImpl.hxx b/writerfilter/source/rtftok/RTFDocumentImpl.hxx
new file mode 100644
--- /dev/null
+++ b/writerfilter/source/rtftok/RTFDocumentImpl.hxx
@@ -0,0 +1,60 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * This file is part of OpenOffice.org.
+ *
+ * OpenOffice.org is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 3
+ * only, as published by the Free Software Foundation.
+ *
+ * OpenOffice.org is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License version 3 for more details
+ * (a copy is included in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * version 3 along with OpenOffice.org.  If not, see
+ * <http://www.openoffice.org/license.html>
+ * for a copy of the LGPLv3 License.
+ *
+ ************************************************************************/
+
+#ifndef INCLUDED_RTF_DOCUMENT_IMPL_HXX
+#define INCLUDED_RTF_DOCUMENT_IMPL_HXX
+
+#include <memory>
+
+#include <rtftok/RTFDocument.hxx>
+
+
+namespace writerfilter {
+namespace rtftok {
+
+class RTFDocumentImpl
+    : public RTFDocument
+{
+public:
+    RTFDocumentImpl(
+        ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >
+            const& xInputStream);
+    virtual ~RTFDocumentImpl();
+
+    virtual void resolve(Stream & rHandler);
+    virtual ::std::string getType() const;
+
+private:
+    class Impl;
+    ::std::auto_ptr<Impl> m_pImpl;
+};
+
+} // namespace writerfilter
+} // namespace rtftok
+
+#endif // INCLUDED_RTF_DOCUMENT_IMPL_HXX
+
diff --git a/writerfilter/source/rtftok/makefile.mk b/writerfilter/source/rtftok/makefile.mk
--- a/writerfilter/source/rtftok/makefile.mk
+++ b/writerfilter/source/rtftok/makefile.mk
@@ -44,14 +44,32 @@
 
 # --- Files --------------------------------------------------------
 
-SLOFILES=$(SLO)$/RTFScanner.obj $(SLO)$/RTFParseException.obj
+SLOFILES= \
+	$(SLO)$/RTFScanner.obj \
+	$(SLO)$/RTFParseException.obj \
+	$(SLO)$/RTFDocumentImpl.obj \
 
 
 SHL1TARGET=$(TARGET)
 
-SHL1STDLIBS=$(SALLIB)\
-	$(CPPULIB)\
-	$(CPPUHELPERLIB)
+.IF "$(GUI)"=="UNX" || "$(GUI)"=="MAC"
+RESOURCEMODELLIB=-lresourcemodel
+.ELIF "$(GUI)"=="OS2"
+RESOURCEMODELLIB=$(LB)$/iresourcemodel.lib
+.ELIF "$(GUI)"=="WNT"
+.IF "$(COM)"=="GCC"
+RESOURCEMODELLIB=-lresourcemodel
+.ELSE
+RESOURCEMODELLIB=$(LB)$/iresourcemodel.lib
+.ENDIF
+.ENDIF
+
+SHL1STDLIBS= \
+	$(SALLIB) \
+	$(CPPULIB) \
+	$(CPPUHELPERLIB) \
+	$(RESOURCEMODELLIB) \
+
 SHL1IMPLIB=i$(SHL1TARGET)
 SHL1USE_EXPORTS=name
 
