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 :
21 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
22 : #include <com/sun/star/xml/sax/Writer.hpp>
23 : #include <com/sun/star/beans/PropertyValue.hpp>
24 : #include <com/sun/star/io/XActiveDataSource.hpp>
25 : #include <tools/urlobj.hxx>
26 :
27 : #include "typedetectionexport.hxx"
28 : #include "xmlfiltersettingsdialog.hxx"
29 :
30 : #include <comphelper/attributelist.hxx>
31 :
32 : using namespace com::sun::star::beans;
33 : using namespace com::sun::star::uno;
34 : using namespace com::sun::star::io;
35 : using namespace com::sun::star::lang;
36 : using namespace com::sun::star::xml::sax;
37 :
38 :
39 0 : TypeDetectionExporter::TypeDetectionExporter( Reference< XComponentContext >& xContext )
40 0 : : mxContext( xContext )
41 : {
42 0 : }
43 :
44 0 : static OUString createRelativeURL( const OUString& rFilterName, const OUString& rURL )
45 : {
46 0 : if( !rURL.isEmpty() &&
47 0 : !rURL.startsWith("http:") &&
48 0 : !rURL.startsWith("https:") &&
49 0 : !rURL.startsWith("jar:") &&
50 0 : !rURL.startsWith("ftp:") )
51 : {
52 0 : INetURLObject aURL( rURL );
53 0 : OUString aName( aURL.GetName() );
54 0 : if( aName.isEmpty() )
55 : {
56 0 : sal_Int32 nPos = rURL.lastIndexOf( '/' );
57 0 : if( nPos == -1 )
58 : {
59 0 : aName = rURL;
60 : }
61 : else
62 : {
63 0 : aName = rURL.copy( nPos + 1 );
64 : }
65 : }
66 :
67 0 : return OUString( "vnd.sun.star.Package:" + rFilterName + "/" + aName );
68 : }
69 : else
70 : {
71 0 : return rURL;
72 : }
73 : }
74 :
75 0 : void TypeDetectionExporter::doExport( Reference< XOutputStream > xOS, const XMLFilterVector& rFilters )
76 : {
77 : try
78 : {
79 0 : const OUString sComponentData ( "oor:component-data" );
80 0 : const OUString sNode ( "node" );
81 0 : const OUString sName ( "oor:name" );
82 0 : const OUString sWhiteSpace ( " " );
83 0 : const OUString sUIName ( "UIName" );
84 0 : const OUString sComma ( "," );
85 0 : const OUString sDelim ( ";" );
86 0 : const OUString sData ( "Data" );
87 0 : const OUString sDocTypePrefix ( "doctype:" );
88 0 : const OUString sFilterAdaptorService( "com.sun.star.comp.Writer.XmlFilterAdaptor" );
89 0 : const OUString sXSLTFilterService ( "com.sun.star.documentconversion.XSLTFilter" );
90 0 : const OUString sCdataAttribute ( "CDATA" );
91 :
92 :
93 : // set up sax writer and connect to given output stream
94 0 : Reference< XWriter > xHandler = Writer::create( mxContext );
95 0 : xHandler->setOutputStream( xOS );
96 :
97 0 : ::comphelper::AttributeList * pAttrList = new ::comphelper::AttributeList;
98 0 : pAttrList->AddAttribute ( OUString( "xmlns:oor" ), sCdataAttribute, OUString( "http://openoffice.org/2001/registry" ) );
99 0 : pAttrList->AddAttribute ( OUString( "xmlns:xs" ), sCdataAttribute, OUString( "http://www.w3.org/2001/XMLSchema" ) );
100 0 : pAttrList->AddAttribute ( sName, sCdataAttribute, OUString( "TypeDetection" ) );
101 0 : pAttrList->AddAttribute ( OUString( "oor:package" ), sCdataAttribute, OUString( "org.openoffice.Office" ) );
102 0 : Reference < XAttributeList > xAttrList (pAttrList);
103 :
104 0 : xHandler->startDocument();
105 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
106 0 : xHandler->startElement( sComponentData, xAttrList );
107 :
108 : // export types
109 : {
110 0 : xAttrList = pAttrList = new ::comphelper::AttributeList;
111 0 : pAttrList->AddAttribute ( sName, sCdataAttribute, OUString( "Types" ) );
112 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
113 0 : xHandler->startElement( sNode, xAttrList );
114 :
115 0 : XMLFilterVector::const_iterator aIter( rFilters.begin() );
116 0 : while( aIter != rFilters.end() )
117 : {
118 0 : const filter_info_impl* pFilter = (*aIter);
119 :
120 0 : xAttrList = pAttrList = new ::comphelper::AttributeList;
121 0 : pAttrList->AddAttribute( sName, sCdataAttribute, pFilter->maType );
122 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
123 0 : xHandler->startElement( sNode, xAttrList );
124 0 : OUString sValue("0");
125 0 : sValue += sComma;
126 0 : sValue += sComma;
127 0 : if( !pFilter->maDocType.isEmpty() )
128 : {
129 0 : sValue += sDocTypePrefix;
130 0 : sValue += pFilter->maDocType;
131 : }
132 0 : sValue += sComma;
133 0 : sValue += sComma;
134 0 : sValue += pFilter->maExtension;
135 0 : sValue += sComma;
136 0 : sValue += OUString::number( pFilter->mnDocumentIconID );
137 0 : sValue += sComma;
138 :
139 0 : addProperty( xHandler, sData, sValue );
140 0 : addLocaleProperty( xHandler, sUIName, pFilter->maInterfaceName );
141 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
142 0 : xHandler->endElement( sNode );
143 :
144 0 : ++aIter;
145 0 : }
146 :
147 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
148 0 : xHandler->endElement( sNode );
149 : }
150 :
151 : // export filters
152 : {
153 0 : xAttrList = pAttrList = new ::comphelper::AttributeList;
154 0 : pAttrList->AddAttribute ( sName, sCdataAttribute, OUString( "Filters" ) );
155 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
156 0 : xHandler->startElement( sNode, xAttrList );
157 :
158 0 : XMLFilterVector::const_iterator aIter( rFilters.begin() );
159 0 : while( aIter != rFilters.end() )
160 : {
161 0 : const filter_info_impl* pFilter = (*aIter);
162 :
163 0 : xAttrList = pAttrList = new ::comphelper::AttributeList;
164 0 : pAttrList->AddAttribute( sName, sCdataAttribute, pFilter->maFilterName );
165 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
166 0 : xHandler->startElement( sNode, xAttrList );
167 0 : addLocaleProperty( xHandler, sUIName, pFilter->maInterfaceName );
168 :
169 0 : OUString sValue("0");
170 0 : sValue += sComma;
171 0 : sValue += pFilter->maType;
172 : sValue += sComma,
173 0 : sValue += pFilter->maDocumentService;
174 : sValue += sComma,
175 0 : sValue += sFilterAdaptorService;
176 : sValue += sComma,
177 0 : sValue += OUString::number( pFilter->maFlags );
178 0 : sValue += sComma;
179 0 : sValue += sXSLTFilterService;
180 0 : sValue += sDelim;
181 0 : sValue += OUString::boolean( pFilter->mbNeedsXSLT2 );
182 0 : sValue += sDelim;
183 :
184 0 : const application_info_impl* pAppInfo = getApplicationInfo( pFilter->maExportService );
185 0 : sValue += pAppInfo->maXMLImporter;
186 0 : sValue += sDelim;
187 0 : sValue += pAppInfo->maXMLExporter;
188 0 : sValue += sDelim;
189 :
190 0 : sValue += createRelativeURL( pFilter->maFilterName, pFilter->maImportXSLT );
191 0 : sValue += sDelim;
192 0 : sValue += createRelativeURL( pFilter->maFilterName, pFilter->maExportXSLT );
193 0 : sValue += sDelim;
194 : // entry DTD obsolete and removed, but delimiter kept
195 0 : sValue += sDelim;
196 0 : sValue += pFilter->maComment;
197 0 : sValue += sComma;
198 0 : sValue += "0";
199 0 : sValue += sComma;
200 0 : sValue += createRelativeURL( pFilter->maFilterName, pFilter->maImportTemplate );
201 0 : addProperty( xHandler, sData, sValue );
202 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
203 0 : xHandler->endElement( sNode );
204 0 : ++aIter;
205 0 : }
206 :
207 0 : xHandler->endElement( sNode );
208 : }
209 :
210 : // finish
211 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
212 0 : xHandler->endElement( sComponentData );
213 0 : xHandler->endDocument();
214 : }
215 0 : catch( const Exception& )
216 : {
217 : OSL_FAIL( "TypeDetectionExporter::doExport exception catched!" );
218 : }
219 0 : }
220 :
221 0 : void TypeDetectionExporter::addProperty( Reference< XWriter > xHandler, const OUString& rName, const OUString& rValue )
222 : {
223 : try
224 : {
225 0 : const OUString sCdataAttribute( "CDATA" );
226 0 : const OUString sProp( "prop" );
227 0 : const OUString sValue( "value" );
228 0 : const OUString sWhiteSpace ( " " );
229 :
230 0 : ::comphelper::AttributeList * pAttrList = new ::comphelper::AttributeList;
231 0 : pAttrList->AddAttribute ( OUString( "oor:name" ), sCdataAttribute, rName );
232 0 : pAttrList->AddAttribute ( OUString( "oor:type" ), sCdataAttribute, OUString( "xs:string" ) );
233 0 : Reference < XAttributeList > xAttrList (pAttrList);
234 :
235 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
236 0 : xHandler->startElement( sProp, xAttrList );
237 0 : xAttrList = NULL;
238 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
239 0 : xHandler->startElement( sValue,xAttrList );
240 0 : xHandler->characters( rValue );
241 0 : xHandler->endElement( sValue );
242 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
243 0 : xHandler->endElement( sProp );
244 : }
245 0 : catch( const Exception& )
246 : {
247 : OSL_FAIL( "TypeDetectionExporter::addProperty exception catched!" );
248 : }
249 0 : }
250 :
251 0 : void TypeDetectionExporter::addLocaleProperty( Reference< XWriter > xHandler, const OUString& rName, const OUString& rValue )
252 : {
253 : try
254 : {
255 0 : const OUString sCdataAttribute( "CDATA" );
256 0 : const OUString sProp( "prop" );
257 0 : const OUString sValue( "value" );
258 0 : const OUString sWhiteSpace ( " " );
259 :
260 0 : ::comphelper::AttributeList * pAttrList = new ::comphelper::AttributeList;
261 0 : pAttrList->AddAttribute ( OUString( "oor:name" ), sCdataAttribute, rName );
262 0 : pAttrList->AddAttribute ( OUString( "oor:type" ), sCdataAttribute, OUString( "xs:string" ) );
263 0 : Reference < XAttributeList > xAttrList (pAttrList);
264 :
265 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
266 0 : xHandler->startElement( sProp, xAttrList );
267 0 : xAttrList = pAttrList = new ::comphelper::AttributeList;
268 0 : pAttrList->AddAttribute ( OUString( "xml:lang" ), sCdataAttribute, OUString( "en-US" ) );
269 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
270 0 : xHandler->startElement( sValue, xAttrList );
271 0 : xHandler->characters( rValue );
272 0 : xHandler->endElement( sValue );
273 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
274 0 : xHandler->endElement( sProp );
275 : }
276 0 : catch( const Exception& )
277 : {
278 : OSL_FAIL( "TypeDetectionExporter::addLocaleProperty exception catched!" );
279 : }
280 6 : }
281 :
282 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|