LibreOffice Module filter (master) 1
typedetectionexport.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
22#include <string_view>
23
24#include <com/sun/star/xml/sax/XAttributeList.hpp>
25#include <com/sun/star/xml/sax/Writer.hpp>
26#include <com/sun/star/io/XActiveDataSource.hpp>
27#include <tools/urlobj.hxx>
29
31#include "xmlfiltercommon.hxx"
32
34#include <rtl/ref.hxx>
35
36using namespace com::sun::star::uno;
37using namespace com::sun::star::io;
38using namespace com::sun::star::lang;
39using namespace com::sun::star::xml::sax;
40
41
43: mxContext( xContext )
44{
45}
46
47static OUString createRelativeURL( std::u16string_view rFilterName, const OUString& rURL )
48{
49 if( !rURL.isEmpty() &&
50 !rURL.startsWith("http:") &&
51 !rURL.startsWith("https:") &&
52 !rURL.startsWith("jar:") &&
53 !rURL.startsWith("ftp:") )
54 {
55 INetURLObject aURL( rURL );
56 OUString aName(aURL.GetLastName());
57 if( aName.isEmpty() )
58 {
59 sal_Int32 nPos = rURL.lastIndexOf( '/' );
60 if( nPos == -1 )
61 {
62 aName = rURL;
63 }
64 else
65 {
66 aName = rURL.copy( nPos + 1 );
67 }
68 }
69
70 return OUString( OUString::Concat("vnd.sun.star.Package:") + rFilterName + "/" + aName );
71 }
72 else
73 {
74 return rURL;
75 }
76}
77
78void TypeDetectionExporter::doExport( const Reference< XOutputStream >& xOS, const std::vector<filter_info_impl*>& rFilters )
79{
80 try
81 {
82 static constexpr OUStringLiteral sComponentData ( u"oor:component-data" );
83 static constexpr OUStringLiteral sNode ( u"node" );
84 static constexpr OUStringLiteral sName ( u"oor:name" );
85 static constexpr OUStringLiteral sWhiteSpace ( u" " );
86 static constexpr OUStringLiteral sUIName ( u"UIName" );
87 static constexpr OUStringLiteral sComma ( u"," );
88 static constexpr OUStringLiteral sDelim ( u";" );
89 static constexpr OUStringLiteral sData ( u"Data" );
90 static constexpr OUStringLiteral sDocTypePrefix ( u"doctype:" );
91 static constexpr OUStringLiteral sFilterAdaptorService( u"com.sun.star.comp.Writer.XmlFilterAdaptor" );
92 static constexpr OUStringLiteral sXSLTFilterService ( u"com.sun.star.documentconversion.XSLTFilter" );
93
94
95 // set up sax writer and connect to given output stream
96 Reference< XWriter > xHandler = Writer::create( mxContext );
97 xHandler->setOutputStream( xOS );
98
99 rtl::Reference<::comphelper::AttributeList> pAttrList = new ::comphelper::AttributeList;
100 pAttrList->AddAttribute ( "xmlns:oor", "http://openoffice.org/2001/registry" );
101 pAttrList->AddAttribute ( "xmlns:xs", "http://www.w3.org/2001/XMLSchema" );
102 pAttrList->AddAttribute ( sName, "TypeDetection" );
103 pAttrList->AddAttribute ( "oor:package", "org.openoffice.Office" );
104
105 xHandler->startDocument();
106 xHandler->ignorableWhitespace ( sWhiteSpace );
107 xHandler->startElement( sComponentData, pAttrList );
108
109 // export types
110 {
111 pAttrList = new ::comphelper::AttributeList;
112 pAttrList->AddAttribute ( sName, "Types" );
113 xHandler->ignorableWhitespace ( sWhiteSpace );
114 xHandler->startElement( sNode, pAttrList );
115
116 for (auto const& filter : rFilters)
117 {
118 pAttrList = new ::comphelper::AttributeList;
119 pAttrList->AddAttribute( sName, filter->maType );
120 xHandler->ignorableWhitespace ( sWhiteSpace );
121 xHandler->startElement( sNode, pAttrList );
122 OUString sValue = "0" + sComma + sComma;
123 if( !filter->maDocType.isEmpty() )
124 {
125 sValue += sDocTypePrefix + filter->maDocType;
126 }
127 sValue += sComma + sComma + filter->maExtension + sComma +
128 OUString::number( filter->mnDocumentIconID ) + sComma;
129
130 addProperty( xHandler, sData, sValue );
131 addLocaleProperty( xHandler, sUIName, filter->maInterfaceName );
132 xHandler->ignorableWhitespace ( sWhiteSpace );
133 xHandler->endElement( sNode );
134 }
135
136 xHandler->ignorableWhitespace ( sWhiteSpace );
137 xHandler->endElement( sNode );
138 }
139
140 // export filters
141 {
142 pAttrList = new ::comphelper::AttributeList;
143 pAttrList->AddAttribute ( sName, "Filters" );
144 xHandler->ignorableWhitespace ( sWhiteSpace );
145 xHandler->startElement( sNode, pAttrList );
146
147 for (auto const& filter : rFilters)
148 {
149 pAttrList = new ::comphelper::AttributeList;
150 pAttrList->AddAttribute( sName, filter->maFilterName );
151 xHandler->ignorableWhitespace ( sWhiteSpace );
152 xHandler->startElement( sNode, pAttrList );
153 addLocaleProperty( xHandler, sUIName, filter->maInterfaceName );
154
155 const application_info_impl* pAppInfo = getApplicationInfo( filter->maExportService );
156 OUString sValue =
157 "0" +
158 sComma +
159 filter->maType +
160 sComma +
161 filter->maDocumentService +
162 sComma +
163 sFilterAdaptorService +
164 sComma +
165 OUString::number( filter->maFlags ) +
166 sComma +
167 sXSLTFilterService +
168 sDelim +
169 OUString::boolean( filter->mbNeedsXSLT2 ) +
170 sDelim +
171 pAppInfo->maXMLImporter +
172 sDelim +
173 pAppInfo->maXMLExporter +
174 sDelim +
175 createRelativeURL( filter->maFilterName, filter->maImportXSLT ) +
176 sDelim +
177 createRelativeURL( filter->maFilterName, filter->maExportXSLT ) +
178 sDelim +
179 // entry DTD obsolete and removed, but delimiter kept
180 sDelim +
181 filter->maComment +
182 sComma +
183 "0" +
184 sComma +
185 createRelativeURL( filter->maFilterName, filter->maImportTemplate );
186 addProperty( xHandler, sData, sValue );
187 xHandler->ignorableWhitespace ( sWhiteSpace );
188 xHandler->endElement( sNode );
189 }
190
191 xHandler->endElement( sNode );
192 }
193
194 // finish
195 xHandler->ignorableWhitespace ( sWhiteSpace );
196 xHandler->endElement( sComponentData );
197 xHandler->endDocument();
198 }
199 catch( const Exception& )
200 {
201 TOOLS_WARN_EXCEPTION("filter.xslt", "");
202 }
203}
204
205void TypeDetectionExporter::addProperty( const Reference< XWriter >& xHandler, const OUString& rName, const OUString& rValue )
206{
207 try
208 {
209 static constexpr OUStringLiteral sProp( u"prop" );
210 static constexpr OUStringLiteral sValue( u"value" );
211 static constexpr OUStringLiteral sWhiteSpace ( u" " );
212
213 rtl::Reference<::comphelper::AttributeList>pAttrList = new ::comphelper::AttributeList;
214 pAttrList->AddAttribute ( "oor:name", rName );
215 pAttrList->AddAttribute ( "oor:type", "xs:string" );
216
217 xHandler->ignorableWhitespace ( sWhiteSpace );
218 xHandler->startElement( sProp, pAttrList );
219 xHandler->ignorableWhitespace ( sWhiteSpace );
220 xHandler->startElement( sValue, pAttrList );
221 xHandler->characters( rValue );
222 xHandler->endElement( sValue );
223 xHandler->ignorableWhitespace ( sWhiteSpace );
224 xHandler->endElement( sProp );
225 }
226 catch( const Exception& )
227 {
228 TOOLS_WARN_EXCEPTION("filter.xslt", "");
229 }
230}
231
232void TypeDetectionExporter::addLocaleProperty( const Reference< XWriter >& xHandler, const OUString& rName, const OUString& rValue )
233{
234 try
235 {
236 static constexpr OUStringLiteral sProp( u"prop" );
237 static constexpr OUStringLiteral sValue( u"value" );
238 static constexpr OUStringLiteral sWhiteSpace ( u" " );
239
240 rtl::Reference<::comphelper::AttributeList> pAttrList = new ::comphelper::AttributeList;
241 pAttrList->AddAttribute ( "oor:name", rName );
242 pAttrList->AddAttribute ( "oor:type", "xs:string" );
243
244 xHandler->ignorableWhitespace ( sWhiteSpace );
245 xHandler->startElement( sProp, pAttrList );
246 pAttrList = new ::comphelper::AttributeList;
247 pAttrList->AddAttribute ( "xml:lang", "en-US" );
248 xHandler->ignorableWhitespace ( sWhiteSpace );
249 xHandler->startElement( sValue, pAttrList );
250 xHandler->characters( rValue );
251 xHandler->endElement( sValue );
252 xHandler->ignorableWhitespace ( sWhiteSpace );
253 xHandler->endElement( sProp );
254 }
255 catch( const Exception& )
256 {
257 TOOLS_WARN_EXCEPTION("filter.xslt", "");
258 }
259}
260
261/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral sComponentData
css::uno::Reference< css::uno::XComponentContext > mxContext
void doExport(const css::uno::Reference< css::io::XOutputStream > &xOS, const std::vector< filter_info_impl * > &rFilters)
TypeDetectionExporter(css::uno::Reference< css::uno::XComponentContext > const &mxContext)
static void addLocaleProperty(const css::uno::Reference< css::xml::sax::XWriter > &xWriter, const OUString &rName, const OUString &rValue)
static void addProperty(const css::uno::Reference< css::xml::sax::XWriter > &xWriter, const OUString &rName, const OUString &rValue)
#define TOOLS_WARN_EXCEPTION(area, stream)
URL aURL
uno::Reference< uno::XComponentContext > mxContext
float u
OUString sName
OUString aName
sal_uInt16 nPos
@ Exception
static OUString createRelativeURL(std::u16string_view rFilterName, const OUString &rURL)
const application_info_impl * getApplicationInfo(std::u16string_view rServiceName)