Line data Source code
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 <com/sun/star/xml/sax/InputSource.hpp>
21 : #include <com/sun/star/xml/sax/Parser.hpp>
22 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
23 : #include <com/sun/star/beans/PropertyValue.hpp>
24 :
25 : #include <comphelper/processfactory.hxx>
26 : #include "typedetectionimport.hxx"
27 : #include "xmlfiltersettingsdialog.hxx"
28 :
29 : using namespace com::sun::star::lang;
30 : using namespace com::sun::star::uno;
31 : using namespace com::sun::star::io;
32 : using namespace com::sun::star::beans;
33 : using namespace com::sun::star::xml::sax;
34 : using namespace com::sun::star;
35 : using namespace std;
36 :
37 :
38 0 : TypeDetectionImporter::TypeDetectionImporter()
39 : : sRootNode( "oor:component-data" ),
40 : sNode( "node" ),
41 : sName( "oor:name" ),
42 : sProp( "prop" ),
43 : sValue( "value" ),
44 : sUIName( "UIName" ),
45 : sData( "Data" ),
46 : sFilters( "Filters" ),
47 : sTypes( "Types" ),
48 : sFilterAdaptorService( "com.sun.star.comp.Writer.XmlFilterAdaptor" ),
49 0 : sXSLTFilterService( "com.sun.star.documentconversion.XSLTFilter" )
50 : {
51 0 : }
52 :
53 0 : TypeDetectionImporter::~TypeDetectionImporter()
54 : {
55 0 : }
56 :
57 0 : void TypeDetectionImporter::doImport( const Reference< XComponentContext >& rxContext, Reference< XInputStream > xIS, XMLFilterVector& rFilters )
58 : {
59 : try
60 : {
61 0 : Reference< XParser > xParser = xml::sax::Parser::create( rxContext );
62 :
63 0 : TypeDetectionImporter* pImporter = new TypeDetectionImporter;
64 0 : Reference < XDocumentHandler > xDocHandler( pImporter );
65 0 : xParser->setDocumentHandler( xDocHandler );
66 :
67 0 : InputSource source;
68 0 : source.aInputStream = xIS;
69 :
70 : // start parsing
71 0 : xParser->parseStream( source );
72 :
73 0 : pImporter->fillFilterVector( rFilters );
74 : }
75 0 : catch( const Exception& /* e */ )
76 : {
77 : OSL_FAIL( "TypeDetectionImporter::doImport exception catched!" );
78 : }
79 0 : }
80 :
81 0 : void TypeDetectionImporter::fillFilterVector( XMLFilterVector& rFilters )
82 : {
83 : // create filter infos from imported filter nodes
84 0 : NodeVector::iterator aIter = maFilterNodes.begin();
85 0 : while( aIter != maFilterNodes.end() )
86 : {
87 0 : filter_info_impl* pFilter = createFilterForNode( (*aIter) );
88 0 : if( pFilter )
89 0 : rFilters.push_back( pFilter );
90 :
91 0 : delete (*aIter++);
92 : }
93 :
94 : // now delete type nodes
95 0 : aIter = maTypeNodes.begin();
96 0 : while( aIter != maTypeNodes.end() )
97 0 : delete (*aIter++);
98 0 : }
99 :
100 0 : static OUString getSubdata( int index, sal_Unicode delimiter, const OUString& rData )
101 : {
102 0 : sal_Int32 nLastIndex = 0;
103 :
104 0 : sal_Int32 nNextIndex = rData.indexOf( delimiter );
105 :
106 0 : OUString aSubdata;
107 :
108 0 : while( index )
109 : {
110 0 : nLastIndex = nNextIndex + 1;
111 0 : nNextIndex = rData.indexOf( delimiter, nLastIndex );
112 :
113 0 : index--;
114 :
115 0 : if( (index > 0) && (nLastIndex == 0) )
116 0 : return aSubdata;
117 : }
118 :
119 0 : if( nNextIndex == -1 )
120 : {
121 0 : aSubdata = rData.copy( nLastIndex );
122 : }
123 : else
124 : {
125 0 : aSubdata = rData.copy( nLastIndex, nNextIndex - nLastIndex );
126 : }
127 :
128 0 : return aSubdata;
129 : }
130 :
131 0 : Node* TypeDetectionImporter::findTypeNode( const OUString& rType )
132 : {
133 : // now delete type nodes
134 0 : for (NodeVector::const_iterator aIter(maTypeNodes.begin()), aEnd(maTypeNodes.end()); aIter != aEnd; ++aIter)
135 : {
136 0 : if( (*aIter)->maName == rType )
137 0 : return (*aIter);
138 : }
139 :
140 0 : return NULL;
141 : }
142 :
143 0 : filter_info_impl* TypeDetectionImporter::createFilterForNode( Node * pNode )
144 : {
145 0 : filter_info_impl* pFilter = new filter_info_impl;
146 :
147 0 : pFilter->maFilterName = pNode->maName;
148 0 : pFilter->maInterfaceName = pNode->maPropertyMap[sUIName];
149 :
150 0 : OUString aData = pNode->maPropertyMap[sData];
151 :
152 0 : sal_Unicode aComma(',');
153 :
154 0 : pFilter->maType = getSubdata( 1, aComma, aData );
155 0 : pFilter->maDocumentService = getSubdata( 2, aComma, aData );
156 :
157 0 : OUString aFilterService( getSubdata( 3, aComma, aData ) );
158 0 : pFilter->maFlags = getSubdata( 4, aComma, aData ).toInt32();
159 :
160 : // parse filter user data
161 0 : sal_Unicode aDelim(';');
162 0 : OUString aFilterUserData( getSubdata( 5, aComma, aData ) );
163 :
164 0 : OUString aAdapterService( getSubdata( 0, aDelim, aFilterUserData ) );
165 : //Import/ExportService
166 0 : pFilter->mbNeedsXSLT2 = getSubdata( 1, aDelim, aFilterUserData ).toBoolean();
167 0 : pFilter->maImportService = getSubdata( 2, aDelim, aFilterUserData );
168 0 : pFilter->maExportService = getSubdata( 3, aDelim, aFilterUserData );
169 0 : pFilter->maImportXSLT = getSubdata( 4, aDelim, aFilterUserData );
170 0 : pFilter->maExportXSLT = getSubdata( 5, aDelim, aFilterUserData );
171 0 : pFilter->maComment = getSubdata( 7, aDelim, aFilterUserData );
172 :
173 :
174 0 : pFilter->maImportTemplate = getSubdata( 7, aComma, aData );
175 :
176 0 : Node* pTypeNode = findTypeNode( pFilter->maType );
177 0 : if( pTypeNode )
178 : {
179 0 : OUString aTypeUserData( pTypeNode->maPropertyMap[sData] );
180 :
181 0 : pFilter->maDocType = getSubdata( 2, aComma, aTypeUserData );
182 0 : pFilter->maExtension = getSubdata( 4, aComma, aTypeUserData );
183 0 : pFilter->mnDocumentIconID = getSubdata( 5, aComma, aTypeUserData ).toInt32();
184 : }
185 :
186 0 : bool bOk = true;
187 :
188 0 : if( pTypeNode == NULL )
189 0 : bOk = false;
190 :
191 0 : if( pFilter->maFilterName.isEmpty() )
192 0 : bOk = false;
193 :
194 0 : if( pFilter->maInterfaceName.isEmpty() )
195 0 : bOk = false;
196 :
197 0 : if( pFilter->maType.isEmpty() )
198 0 : bOk = false;
199 :
200 0 : if( pFilter->maFlags == 0 )
201 0 : bOk = false;
202 :
203 0 : if( aFilterService != sFilterAdaptorService )
204 0 : bOk = false;
205 :
206 0 : if( aAdapterService != sXSLTFilterService )
207 0 : bOk = false;
208 :
209 0 : if( pFilter->maExtension.isEmpty() )
210 0 : bOk = false;
211 :
212 0 : if( !bOk )
213 : {
214 0 : delete pFilter;
215 0 : pFilter = NULL;
216 : }
217 :
218 0 : return pFilter;
219 : }
220 :
221 0 : void SAL_CALL TypeDetectionImporter::startDocument( )
222 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
223 : {
224 0 : }
225 :
226 0 : void SAL_CALL TypeDetectionImporter::endDocument( )
227 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
228 : {
229 0 : }
230 :
231 0 : void SAL_CALL TypeDetectionImporter::startElement( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs )
232 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
233 : {
234 0 : ImportState eNewState = e_Unknown;
235 :
236 0 : if( maStack.empty() )
237 : {
238 : // #109668# support legacy name as well on import
239 0 : if( aName == sRootNode || aName == "oor:node" )
240 : {
241 0 : eNewState = e_Root;
242 : }
243 : }
244 0 : else if( maStack.top() == e_Root )
245 : {
246 0 : if( aName == sNode )
247 : {
248 0 : OUString aNodeName( xAttribs->getValueByName( sName ) );
249 :
250 0 : if( aNodeName == sFilters )
251 : {
252 0 : eNewState = e_Filters;
253 : }
254 0 : else if( aNodeName == sTypes )
255 : {
256 0 : eNewState = e_Types;
257 0 : }
258 : }
259 : }
260 0 : else if( (maStack.top() == e_Filters) || (maStack.top() == e_Types) )
261 : {
262 0 : if( aName == sNode )
263 : {
264 0 : maNodeName = xAttribs->getValueByName( sName );
265 :
266 0 : eNewState = (maStack.top() == e_Filters) ? e_Filter : e_Type;
267 : }
268 : }
269 0 : else if( (maStack.top() == e_Filter) || (maStack.top() == e_Type))
270 : {
271 0 : if( aName == sProp )
272 : {
273 0 : maPropertyName = xAttribs->getValueByName( sName );
274 0 : eNewState = e_Property;
275 : }
276 : }
277 0 : else if( maStack.top() == e_Property )
278 : {
279 0 : if( aName == sValue )
280 : {
281 0 : eNewState = e_Value;
282 0 : maValue.clear();
283 : }
284 : }
285 :
286 0 : maStack.push( eNewState );
287 0 : }
288 0 : void SAL_CALL TypeDetectionImporter::endElement( const OUString& /* aName */ )
289 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
290 : {
291 0 : if( !maStack.empty() )
292 : {
293 0 : ImportState eCurrentState = maStack.top();
294 0 : switch( eCurrentState )
295 : {
296 : case e_Filter:
297 : case e_Type:
298 : {
299 0 : Node* pNode = new Node;
300 0 : pNode->maName = maNodeName;
301 0 : pNode->maPropertyMap = maPropertyMap;
302 0 : maPropertyMap.clear();
303 :
304 0 : if( eCurrentState == e_Filter )
305 : {
306 0 : maFilterNodes.push_back( pNode );
307 : }
308 : else
309 : {
310 0 : maTypeNodes.push_back( pNode );
311 : }
312 : }
313 0 : break;
314 :
315 : case e_Property:
316 0 : maPropertyMap[ maPropertyName ] = maValue;
317 0 : break;
318 0 : default: break;
319 : }
320 :
321 0 : maStack.pop();
322 : }
323 0 : }
324 0 : void SAL_CALL TypeDetectionImporter::characters( const OUString& aChars )
325 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
326 : {
327 0 : if( !maStack.empty() && maStack.top() == e_Value )
328 : {
329 0 : maValue += aChars;
330 : }
331 0 : }
332 0 : void SAL_CALL TypeDetectionImporter::ignorableWhitespace( const OUString& /* aWhitespaces */ )
333 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
334 : {
335 0 : }
336 0 : void SAL_CALL TypeDetectionImporter::processingInstruction( const OUString& /* aTarget */, const OUString& /* aData */ )
337 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
338 : {
339 0 : }
340 0 : void SAL_CALL TypeDetectionImporter::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /* xLocator */ )
341 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
342 : {
343 3 : }
344 :
345 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|