1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _CPPUHELPER_IMPLEMENTATIONENTRY_
25 #include <cppuhelper/implementationentry.hxx>
26 #endif
27 #include <osl/module.hxx>
28 #include <tools/solar.h>
29 #include <RtfFilter.hxx>
30 
31 using namespace ::rtl;
32 using namespace ::cppu;
33 using namespace ::com::sun::star;
34 
RtfFilter(const uno::Reference<uno::XComponentContext> & rxContext)35 RtfFilter::RtfFilter( const uno::Reference< uno::XComponentContext >& rxContext)  :
36     m_xContext( rxContext )
37 {
38 }
39 
~RtfFilter()40 RtfFilter::~RtfFilter()
41 {
42 }
43 
filter(const uno::Sequence<beans::PropertyValue> & aDescriptor)44 sal_Bool RtfFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
45    throw (uno::RuntimeException)
46 {
47     OSL_TRACE("%s", OSL_THIS_FUNC);
48     if( m_xSrcDoc.is() )
49     {
50         uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
51         uno::Reference< uno::XInterface > xIfc( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.RtfExport" ))), uno::UNO_QUERY_THROW);
52         if (!xIfc.is())
53             return sal_False;
54         uno::Reference< document::XExporter > xExprtr(xIfc, uno::UNO_QUERY_THROW);
55         uno::Reference< document::XFilter > xFltr(xIfc, uno::UNO_QUERY_THROW);
56         if (!xExprtr.is() || !xFltr.is())
57             return sal_False;
58         xExprtr->setSourceDocument(m_xSrcDoc);
59         return xFltr->filter(aDescriptor);
60     }
61     else if ( m_xDstDoc.is() )
62     {
63         uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
64         uno::Reference< uno::XInterface > xIfc( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.RtfImport" ))), uno::UNO_QUERY_THROW);
65         if (!xIfc.is())
66             return sal_False;
67         uno::Reference< document::XImporter > xImprtr(xIfc, uno::UNO_QUERY_THROW);
68         uno::Reference< document::XFilter > xFltr(xIfc, uno::UNO_QUERY_THROW);
69         if (!xImprtr.is() || !xFltr.is())
70             return sal_False;
71         xImprtr->setTargetDocument(m_xDstDoc);
72         return xFltr->filter(aDescriptor);
73     }
74     return sal_False;
75 }
76 
cancel()77 void RtfFilter::cancel(  ) throw (uno::RuntimeException)
78 {
79 }
80 
setSourceDocument(const uno::Reference<lang::XComponent> & xDoc)81 void RtfFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
82    throw (lang::IllegalArgumentException, uno::RuntimeException)
83 {
84    m_xSrcDoc = xDoc;
85 }
86 
setTargetDocument(const uno::Reference<lang::XComponent> & xDoc)87 void RtfFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
88    throw (lang::IllegalArgumentException, uno::RuntimeException)
89 {
90    m_xDstDoc = xDoc;
91 }
92 
initialize(const uno::Sequence<uno::Any> &)93 void RtfFilter::initialize( const uno::Sequence< uno::Any >& /*aArguments*/ ) throw (uno::Exception, uno::RuntimeException)
94 {
95     // The DOCX exporter here extracts 'type' of the filter, ie 'Word' or
96     // 'Word Template' but we don't need it for RTF.
97 }
98 
getImplementationName()99 OUString RtfFilter::getImplementationName(  ) throw (uno::RuntimeException)
100 {
101    return RtfFilter_getImplementationName();
102 }
103 
104 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
105 #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
supportsService(const OUString & rServiceName)106 sal_Bool RtfFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
107 {
108     return (rServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) ) ||
109             rServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME2 ) ));
110 }
111 
getSupportedServiceNames()112 uno::Sequence< OUString > RtfFilter::getSupportedServiceNames(  ) throw (uno::RuntimeException)
113 {
114     return RtfFilter_getSupportedServiceNames();
115 }
116 
117 /* Helpers, used by shared lib exports. */
118 
RtfFilter_getImplementationName()119 OUString RtfFilter_getImplementationName () throw (uno::RuntimeException)
120 {
121    return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.RtfFilter" ) );
122 }
123 
RtfFilter_getSupportedServiceNames()124 uno::Sequence< OUString > RtfFilter_getSupportedServiceNames(  ) throw (uno::RuntimeException)
125 {
126    uno::Sequence < OUString > aRet(2);
127    OUString* pArray = aRet.getArray();
128    pArray[0] =  OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1 ) );
129    pArray[1] =  OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME2 ) );
130    return aRet;
131 }
132 #undef SERVICE_NAME1
133 #undef SERVICE_NAME2
134 
RtfFilter_createInstance(const uno::Reference<uno::XComponentContext> & xContext)135 uno::Reference< uno::XInterface > RtfFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
136                 throw( uno::Exception )
137 {
138    return (cppu::OWeakObject*) new RtfFilter( xContext );
139 }
140 
141 /* vi:set shiftwidth=4 expandtab: */
142