1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*63bba73cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*63bba73cSAndrew Rist * distributed with this work for additional information
6*63bba73cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*63bba73cSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*63bba73cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*63bba73cSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist * KIND, either express or implied. See the License for the
17*63bba73cSAndrew Rist * specific language governing permissions and limitations
18*63bba73cSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*63bba73cSAndrew Rist *************************************************************/
21*63bba73cSAndrew Rist
22*63bba73cSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include <com/sun/star/xml/sax/SAXParseException.hpp>
27cdf0e10cSrcweir #include <com/sun/star/xml/sax/SAXException.hpp>
28cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
29cdf0e10cSrcweir #include <com/sun/star/xml/sax/XAttributeList.hpp>
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySetInfo.hpp>
31cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
32cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
33cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
34cdf0e10cSrcweir
35cdf0e10cSrcweir #ifndef _XMLOFF_TRANSFOERMERBASE_HXX
36cdf0e10cSrcweir #include "TransformerBase.hxx"
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #include "MutableAttrList.hxx"
39cdf0e10cSrcweir
40cdf0e10cSrcweir #ifndef _XMLOFF_METATCONTEXT_HXX
41cdf0e10cSrcweir #include "DocumentTContext.hxx"
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir
44cdf0e10cSrcweir using ::rtl::OUString;
45cdf0e10cSrcweir
46cdf0e10cSrcweir using namespace ::xmloff::token;
47cdf0e10cSrcweir using namespace ::com::sun::star::uno;
48cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
49cdf0e10cSrcweir using namespace ::com::sun::star::beans;
50cdf0e10cSrcweir
51cdf0e10cSrcweir TYPEINIT1( XMLDocumentTransformerContext, XMLTransformerContext );
52cdf0e10cSrcweir
XMLDocumentTransformerContext(XMLTransformerBase & rImp,const OUString & rQName)53cdf0e10cSrcweir XMLDocumentTransformerContext::XMLDocumentTransformerContext( XMLTransformerBase& rImp,
54cdf0e10cSrcweir const OUString& rQName ) :
55cdf0e10cSrcweir XMLTransformerContext( rImp, rQName )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir }
58cdf0e10cSrcweir
~XMLDocumentTransformerContext()59cdf0e10cSrcweir XMLDocumentTransformerContext::~XMLDocumentTransformerContext()
60cdf0e10cSrcweir {
61cdf0e10cSrcweir }
62cdf0e10cSrcweir
StartElement(const Reference<XAttributeList> & rAttrList)63cdf0e10cSrcweir void XMLDocumentTransformerContext::StartElement( const Reference< XAttributeList >& rAttrList )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir Reference< XAttributeList > xAttrList( rAttrList );
66cdf0e10cSrcweir
67cdf0e10cSrcweir sal_Bool bMimeFound = sal_False;
68cdf0e10cSrcweir OUString aClass;
69cdf0e10cSrcweir OUString aClassQName(
70cdf0e10cSrcweir GetTransformer().GetNamespaceMap().GetQNameByKey(
71cdf0e10cSrcweir XML_NAMESPACE_OFFICE, GetXMLToken(XML_CLASS ) ) );
72cdf0e10cSrcweir
73cdf0e10cSrcweir XMLMutableAttributeList *pMutableAttrList = 0;
74cdf0e10cSrcweir sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
75cdf0e10cSrcweir for( sal_Int16 i=0; i < nAttrCount; i++ )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir const OUString& rAttrName = xAttrList->getNameByIndex( i );
78cdf0e10cSrcweir OUString aLocalName;
79cdf0e10cSrcweir sal_uInt16 nPrefix =
80cdf0e10cSrcweir GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
81cdf0e10cSrcweir &aLocalName );
82cdf0e10cSrcweir if( XML_NAMESPACE_OFFICE == nPrefix &&
83cdf0e10cSrcweir IsXMLToken( aLocalName, XML_MIMETYPE ) )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir const OUString& rValue = xAttrList->getValueByIndex( i );
86cdf0e10cSrcweir static const char * aTmp[] =
87cdf0e10cSrcweir {
88cdf0e10cSrcweir "application/vnd.oasis.openoffice.",
89cdf0e10cSrcweir "application/x-vnd.oasis.openoffice.",
90cdf0e10cSrcweir "application/vnd.oasis.opendocument.",
91cdf0e10cSrcweir "application/x-vnd.oasis.document.",
92cdf0e10cSrcweir NULL
93cdf0e10cSrcweir };
94cdf0e10cSrcweir for (int k=0; aTmp[k]; k++)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir ::rtl::OUString sTmpString = ::rtl::OUString::createFromAscii(aTmp[k]);
97cdf0e10cSrcweir if( rValue.matchAsciiL( aTmp[k], sTmpString.getLength() ) )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir aClass = rValue.copy( sTmpString.getLength() );
100cdf0e10cSrcweir break;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir }
103cdf0e10cSrcweir
104cdf0e10cSrcweir if( !pMutableAttrList )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir pMutableAttrList = new XMLMutableAttributeList( xAttrList );
107cdf0e10cSrcweir xAttrList = pMutableAttrList;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir pMutableAttrList->SetValueByIndex( i, aClass );
110cdf0e10cSrcweir pMutableAttrList->RenameAttributeByIndex(i, aClassQName );
111cdf0e10cSrcweir bMimeFound = sal_True;
112cdf0e10cSrcweir break;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir }
115cdf0e10cSrcweir
116cdf0e10cSrcweir if( !bMimeFound )
117cdf0e10cSrcweir {
118cdf0e10cSrcweir const Reference< XPropertySet > rPropSet =
119cdf0e10cSrcweir GetTransformer().GetPropertySet();
120cdf0e10cSrcweir
121cdf0e10cSrcweir if( rPropSet.is() )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir Reference< XPropertySetInfo > xPropSetInfo(
124cdf0e10cSrcweir rPropSet->getPropertySetInfo() );
125cdf0e10cSrcweir OUString aPropName(RTL_CONSTASCII_USTRINGPARAM("Class"));
126cdf0e10cSrcweir if( xPropSetInfo.is() && xPropSetInfo->hasPropertyByName( aPropName ) )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir Any aAny = rPropSet->getPropertyValue( aPropName );
129cdf0e10cSrcweir aAny >>= aClass;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir }
132cdf0e10cSrcweir
133cdf0e10cSrcweir if( aClass.getLength() )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir if( !pMutableAttrList )
136cdf0e10cSrcweir {
137cdf0e10cSrcweir pMutableAttrList = new XMLMutableAttributeList( xAttrList );
138cdf0e10cSrcweir xAttrList = pMutableAttrList;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir
141cdf0e10cSrcweir pMutableAttrList->AddAttribute( aClassQName, aClass );
142cdf0e10cSrcweir }
143cdf0e10cSrcweir }
144cdf0e10cSrcweir XMLTransformerContext::StartElement( xAttrList );
145cdf0e10cSrcweir }
146