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_xmloff.hxx"
26 #include <com/sun/star/xml/sax/SAXParseException.hpp>
27 #include <com/sun/star/xml/sax/SAXException.hpp>
28 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
29 #include <com/sun/star/xml/sax/XAttributeList.hpp>
30 #include <com/sun/star/beans/XPropertySetInfo.hpp>
31 #include <xmloff/nmspmap.hxx>
32 #include <xmloff/xmltoken.hxx>
33 #include "xmloff/xmlnmspe.hxx"
34 
35 #ifndef _XMLOFF_TRANSFOERMERBASE_HXX
36 #include "TransformerBase.hxx"
37 #endif
38 #include "MutableAttrList.hxx"
39 
40 #ifndef _XMLOFF_METATCONTEXT_HXX
41 #include "DocumentTContext.hxx"
42 #endif
43 
44 using ::rtl::OUString;
45 
46 using namespace ::xmloff::token;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::xml::sax;
49 using namespace ::com::sun::star::beans;
50 
51 TYPEINIT1( XMLDocumentTransformerContext, XMLTransformerContext );
52 
XMLDocumentTransformerContext(XMLTransformerBase & rImp,const OUString & rQName)53 XMLDocumentTransformerContext::XMLDocumentTransformerContext( XMLTransformerBase& rImp,
54 							  				  const OUString& rQName ) :
55 	XMLTransformerContext( rImp, rQName )
56 {
57 }
58 
~XMLDocumentTransformerContext()59 XMLDocumentTransformerContext::~XMLDocumentTransformerContext()
60 {
61 }
62 
StartElement(const Reference<XAttributeList> & rAttrList)63 void XMLDocumentTransformerContext::StartElement( const Reference< XAttributeList >& rAttrList )
64 {
65 	Reference< XAttributeList > xAttrList( rAttrList );
66 
67 	sal_Bool bMimeFound = sal_False;
68 	OUString aClass;
69 	OUString aClassQName(
70 					GetTransformer().GetNamespaceMap().GetQNameByKey(
71 								XML_NAMESPACE_OFFICE, GetXMLToken(XML_CLASS ) ) );
72 
73 	XMLMutableAttributeList *pMutableAttrList = 0;
74 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
75 	for( sal_Int16 i=0; i < nAttrCount; i++ )
76 	{
77 		const OUString& rAttrName = xAttrList->getNameByIndex( i );
78 		OUString aLocalName;
79 		sal_uInt16 nPrefix =
80 			GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
81 																 &aLocalName );
82 		if( XML_NAMESPACE_OFFICE == nPrefix &&
83 			IsXMLToken( aLocalName, XML_MIMETYPE ) )
84 		{
85 			const OUString& rValue = xAttrList->getValueByIndex( i );
86 			static const char * aTmp[] =
87 			{
88 				"application/vnd.oasis.openoffice.",
89 				"application/x-vnd.oasis.openoffice.",
90 				"application/vnd.oasis.opendocument.",
91 				"application/x-vnd.oasis.document.",
92 				NULL
93 			};
94 			for (int k=0; aTmp[k]; k++)
95 			{
96 				::rtl::OUString sTmpString = ::rtl::OUString::createFromAscii(aTmp[k]);
97 				if( rValue.matchAsciiL( aTmp[k], sTmpString.getLength() ) )
98 				{
99 					aClass = rValue.copy( sTmpString.getLength() );
100 					break;
101 				}
102 			}
103 
104 			if( !pMutableAttrList )
105 			{
106 				pMutableAttrList = new XMLMutableAttributeList( xAttrList );
107 				xAttrList = pMutableAttrList;
108 			}
109 			pMutableAttrList->SetValueByIndex( i, aClass );
110 			pMutableAttrList->RenameAttributeByIndex(i, aClassQName );
111 			bMimeFound = sal_True;
112 			break;
113 		}
114 	}
115 
116 	if( !bMimeFound )
117 	{
118 		const Reference< XPropertySet > rPropSet =
119 			GetTransformer().GetPropertySet();
120 
121 		if( rPropSet.is() )
122 		{
123 			Reference< XPropertySetInfo > xPropSetInfo(
124 				rPropSet->getPropertySetInfo() );
125 			OUString aPropName(RTL_CONSTASCII_USTRINGPARAM("Class"));
126 			if( xPropSetInfo.is() && xPropSetInfo->hasPropertyByName( aPropName ) )
127 			{
128 				Any aAny = rPropSet->getPropertyValue( aPropName );
129 				aAny >>= aClass;
130 			}
131 		}
132 
133 		if( aClass.getLength() )
134 		{
135 			if( !pMutableAttrList )
136 			{
137 				pMutableAttrList = new XMLMutableAttributeList( xAttrList );
138 				xAttrList = pMutableAttrList;
139 			}
140 
141 			pMutableAttrList->AddAttribute( aClassQName, aClass );
142 		}
143 	}
144 	XMLTransformerContext::StartElement( xAttrList );
145 }
146