1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include <com/sun/star/xml/sax/SAXParseException.hpp>
31 #include <com/sun/star/xml/sax/SAXException.hpp>
32 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
33 #include <com/sun/star/xml/sax/XAttributeList.hpp>
34 #include <com/sun/star/beans/XPropertySetInfo.hpp>
35 #include <xmloff/nmspmap.hxx>
36 #include <xmloff/xmltoken.hxx>
37 #include "xmloff/xmlnmspe.hxx"
38 
39 #ifndef _XMLOFF_TRANSFOERMERBASE_HXX
40 #include "TransformerBase.hxx"
41 #endif
42 #include "MutableAttrList.hxx"
43 
44 #ifndef _XMLOFF_METATCONTEXT_HXX
45 #include "DocumentTContext.hxx"
46 #endif
47 
48 using ::rtl::OUString;
49 
50 using namespace ::xmloff::token;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::xml::sax;
53 using namespace ::com::sun::star::beans;
54 
55 TYPEINIT1( XMLDocumentTransformerContext, XMLTransformerContext );
56 
57 XMLDocumentTransformerContext::XMLDocumentTransformerContext( XMLTransformerBase& rImp,
58 							  				  const OUString& rQName ) :
59 	XMLTransformerContext( rImp, rQName )
60 {
61 }
62 
63 XMLDocumentTransformerContext::~XMLDocumentTransformerContext()
64 {
65 }
66 
67 void XMLDocumentTransformerContext::StartElement( const Reference< XAttributeList >& rAttrList )
68 {
69 	Reference< XAttributeList > xAttrList( rAttrList );
70 
71 	sal_Bool bMimeFound = sal_False;
72 	OUString aClass;
73 	OUString aClassQName(
74 					GetTransformer().GetNamespaceMap().GetQNameByKey(
75 								XML_NAMESPACE_OFFICE, GetXMLToken(XML_CLASS ) ) );
76 
77 	XMLMutableAttributeList *pMutableAttrList = 0;
78 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
79 	for( sal_Int16 i=0; i < nAttrCount; i++ )
80 	{
81 		const OUString& rAttrName = xAttrList->getNameByIndex( i );
82 		OUString aLocalName;
83 		sal_uInt16 nPrefix =
84 			GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
85 																 &aLocalName );
86 		if( XML_NAMESPACE_OFFICE == nPrefix &&
87 			IsXMLToken( aLocalName, XML_MIMETYPE ) )
88 		{
89 			const OUString& rValue = xAttrList->getValueByIndex( i );
90 			static const char * aTmp[] =
91 			{
92 				"application/vnd.oasis.openoffice.",
93 				"application/x-vnd.oasis.openoffice.",
94 				"application/vnd.oasis.opendocument.",
95 				"application/x-vnd.oasis.document.",
96 				NULL
97 			};
98 			for (int k=0; aTmp[k]; k++)
99 			{
100 				::rtl::OUString sTmpString = ::rtl::OUString::createFromAscii(aTmp[k]);
101 				if( rValue.matchAsciiL( aTmp[k], sTmpString.getLength() ) )
102 				{
103 					aClass = rValue.copy( sTmpString.getLength() );
104 					break;
105 				}
106 			}
107 
108 			if( !pMutableAttrList )
109 			{
110 				pMutableAttrList = new XMLMutableAttributeList( xAttrList );
111 				xAttrList = pMutableAttrList;
112 			}
113 			pMutableAttrList->SetValueByIndex( i, aClass );
114 			pMutableAttrList->RenameAttributeByIndex(i, aClassQName );
115 			bMimeFound = sal_True;
116 			break;
117 		}
118 	}
119 
120 	if( !bMimeFound )
121 	{
122 		const Reference< XPropertySet > rPropSet =
123 			GetTransformer().GetPropertySet();
124 
125 		if( rPropSet.is() )
126 		{
127 			Reference< XPropertySetInfo > xPropSetInfo(
128 				rPropSet->getPropertySetInfo() );
129 			OUString aPropName(RTL_CONSTASCII_USTRINGPARAM("Class"));
130 			if( xPropSetInfo.is() && xPropSetInfo->hasPropertyByName( aPropName ) )
131 			{
132 				Any aAny = rPropSet->getPropertyValue( aPropName );
133 				aAny >>= aClass;
134 			}
135 		}
136 
137 		if( aClass.getLength() )
138 		{
139 			if( !pMutableAttrList )
140 			{
141 				pMutableAttrList = new XMLMutableAttributeList( xAttrList );
142 				xAttrList = pMutableAttrList;
143 			}
144 
145 			pMutableAttrList->AddAttribute( aClassQName, aClass );
146 		}
147 	}
148 	XMLTransformerContext::StartElement( xAttrList );
149 }
150