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