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