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 "EventOOoTContext.hxx"
21 : #include "EventMap.hxx"
22 : #include "MutableAttrList.hxx"
23 : #include "xmloff/xmlnmspe.hxx"
24 : #include "ActionMapTypesOOo.hxx"
25 : #include "AttrTransformerAction.hxx"
26 : #include "TransformerActions.hxx"
27 : #include "TransformerBase.hxx"
28 : #include <comphelper/stl_types.hxx>
29 : #include <rtl/ustrbuf.hxx>
30 :
31 : #include <boost/unordered_map.hpp>
32 :
33 : using namespace ::com::sun::star::uno;
34 : using namespace ::com::sun::star::xml::sax;
35 : using namespace ::xmloff::token;
36 :
37 : class XMLTransformerOOoEventMap_Impl:
38 : public ::boost::unordered_map< OUString, NameKey_Impl,
39 : OUStringHash, ::comphelper::UStringEqual >
40 : {
41 : public:
42 :
43 : void AddMap( XMLTransformerEventMapEntry *pInit );
44 :
45 : XMLTransformerOOoEventMap_Impl( XMLTransformerEventMapEntry *pInit,
46 : XMLTransformerEventMapEntry *pInit2 );
47 : ~XMLTransformerOOoEventMap_Impl();
48 : };
49 :
50 2 : void XMLTransformerOOoEventMap_Impl::AddMap( XMLTransformerEventMapEntry *pInit )
51 : {
52 2 : XMLTransformerOOoEventMap_Impl::key_type aKey;
53 4 : XMLTransformerOOoEventMap_Impl::mapped_type aData;
54 72 : while( pInit->m_pOOoName )
55 : {
56 68 : aKey = OUString::createFromAscii(pInit->m_pOOoName);
57 :
58 : OSL_ENSURE( find( aKey ) == end(), "duplicate event map entry" );
59 :
60 68 : aData.m_nPrefix = pInit->m_nOASISPrefix;
61 68 : aData.m_aLocalName = OUString::createFromAscii(pInit->m_pOASISName);
62 :
63 68 : XMLTransformerOOoEventMap_Impl::value_type aVal( aKey, aData );
64 :
65 68 : if( !insert( aVal ).second )
66 : {
67 : OSL_FAIL( "duplicate OOo event name extry" );
68 : }
69 :
70 68 : ++pInit;
71 70 : }
72 2 : }
73 :
74 1 : XMLTransformerOOoEventMap_Impl::XMLTransformerOOoEventMap_Impl(
75 : XMLTransformerEventMapEntry *pInit,
76 1 : XMLTransformerEventMapEntry *pInit2 )
77 : {
78 1 : if( pInit )
79 1 : AddMap( pInit );
80 1 : if( pInit )
81 1 : AddMap( pInit2 );
82 1 : }
83 :
84 1 : XMLTransformerOOoEventMap_Impl::~XMLTransformerOOoEventMap_Impl()
85 : {
86 1 : }
87 :
88 : // -----------------------------------------------------------------------------
89 :
90 0 : TYPEINIT1( XMLEventOOoTransformerContext, XMLPersElemContentTContext );
91 :
92 3 : XMLEventOOoTransformerContext::XMLEventOOoTransformerContext(
93 : XMLTransformerBase& rImp,
94 : const OUString& rQName,
95 : sal_Bool bPersistent ) :
96 : XMLPersElemContentTContext( rImp, rQName,
97 3 : rImp.GetNamespaceMap().GetKeyByAttrName( rQName ), XML_EVENT_LISTENER ),
98 3 : m_bPersistent( bPersistent )
99 : {
100 3 : }
101 :
102 6 : XMLEventOOoTransformerContext::~XMLEventOOoTransformerContext()
103 : {
104 6 : }
105 :
106 : XMLTransformerOOoEventMap_Impl
107 1 : *XMLEventOOoTransformerContext::CreateEventMap()
108 : {
109 : return new XMLTransformerOOoEventMap_Impl( aTransformerEventMap,
110 1 : aFormTransformerEventMap );
111 : }
112 :
113 76 : void XMLEventOOoTransformerContext::FlushEventMap(
114 : XMLTransformerOOoEventMap_Impl *p )
115 : {
116 76 : delete p;
117 76 : }
118 :
119 3 : sal_uInt16 XMLEventOOoTransformerContext::GetEventName(
120 : const OUString& rName,
121 : OUString& rNewName,
122 : XMLTransformerOOoEventMap_Impl& rMap )
123 : {
124 3 : XMLTransformerOOoEventMap_Impl::key_type aKey( rName );
125 3 : XMLTransformerOOoEventMap_Impl::const_iterator aIter = rMap.find( aKey );
126 3 : if( aIter == rMap.end() )
127 : {
128 0 : rNewName = rName;
129 0 : return XML_NAMESPACE_UNKNOWN;
130 : }
131 : else
132 : {
133 3 : rNewName = (*aIter).second.m_aLocalName;
134 3 : return (*aIter).second.m_nPrefix;
135 3 : }
136 : }
137 :
138 :
139 3 : void XMLEventOOoTransformerContext::StartElement(
140 : const Reference< XAttributeList >& rAttrList )
141 : {
142 : XMLTransformerActions *pActions =
143 3 : GetTransformer().GetUserDefinedActions( OOO_EVENT_ACTIONS );
144 : OSL_ENSURE( pActions, "go no actions" );
145 :
146 6 : OUString aLocation, aMacroName;
147 3 : sal_Int16 nMacroName = -1;
148 6 : Reference< XAttributeList > xAttrList( rAttrList );
149 3 : XMLMutableAttributeList *pMutableAttrList = 0;
150 3 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
151 18 : for( sal_Int16 i=0; i < nAttrCount; i++ )
152 : {
153 15 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
154 30 : OUString aLocalName;
155 : sal_uInt16 nPrefix =
156 15 : GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
157 15 : &aLocalName );
158 30 : XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
159 : XMLTransformerActions::const_iterator aIter =
160 15 : pActions->find( aKey );
161 15 : if( !(aIter == pActions->end() ) )
162 : {
163 15 : if( !pMutableAttrList )
164 : {
165 : pMutableAttrList =
166 3 : new XMLMutableAttributeList( xAttrList );
167 3 : xAttrList = pMutableAttrList;
168 : }
169 15 : const OUString& rAttrValue = xAttrList->getValueByIndex( i );
170 15 : switch( (*aIter).second.m_nActionType )
171 : {
172 : case XML_ATACTION_HREF:
173 : // TODO
174 0 : break;
175 : case XML_ATACTION_EVENT_NAME:
176 : pMutableAttrList->SetValueByIndex( i,
177 3 : GetTransformer().GetEventName( rAttrValue ) );
178 3 : break;
179 : case XML_ATACTION_ADD_NAMESPACE_PREFIX:
180 : {
181 3 : OUString aAttrValue( rAttrValue );
182 : sal_uInt16 nValPrefix =
183 3 : static_cast<sal_uInt16>((*aIter).second.m_nParam1);
184 6 : if( GetTransformer().AddNamespacePrefix( aAttrValue,
185 6 : nValPrefix ) )
186 3 : pMutableAttrList->SetValueByIndex( i, aAttrValue );
187 : }
188 3 : break;
189 : case XML_ATACTION_MACRO_LOCATION:
190 6 : aLocation = rAttrValue;
191 6 : pMutableAttrList->RemoveAttributeByIndex( i );
192 6 : --i;
193 6 : --nAttrCount;
194 6 : break;
195 : case XML_ATACTION_MACRO_NAME:
196 3 : aMacroName = rAttrValue;
197 3 : nMacroName = i;
198 3 : break;
199 : case XML_ATACTION_COPY:
200 0 : break;
201 : default:
202 : OSL_ENSURE( !this, "unknown action" );
203 0 : break;
204 15 : }
205 : }
206 15 : }
207 :
208 3 : if( nMacroName != -1 && !aLocation.isEmpty() )
209 : {
210 3 : if( !IsXMLToken( aLocation, XML_APPLICATION ) )
211 3 : aLocation = GetXMLToken( XML_DOCUMENT );
212 3 : OUStringBuffer sTmp( aLocation.getLength() + aMacroName.getLength() + 1 );
213 3 : sTmp = aLocation;
214 3 : sTmp.append( sal_Unicode( ':' ) );
215 3 : sTmp.append( aMacroName );
216 : pMutableAttrList->SetValueByIndex( nMacroName,
217 3 : sTmp.makeStringAndClear() );
218 : }
219 :
220 3 : if( m_bPersistent )
221 0 : XMLPersElemContentTContext::StartElement( xAttrList );
222 : else
223 6 : GetTransformer().GetDocHandler()->startElement( GetExportQName(),
224 9 : xAttrList );
225 3 : }
226 :
227 3 : void XMLEventOOoTransformerContext::EndElement()
228 : {
229 3 : if( m_bPersistent )
230 0 : XMLPersElemContentTContext::EndElement();
231 : else
232 3 : GetTransformer().GetDocHandler()->endElement( GetExportQName() );
233 3 : }
234 :
235 0 : XMLTransformerContext * XMLEventOOoTransformerContext::CreateChildContext(
236 : sal_uInt16 nPrefix,
237 : const OUString& rLocalName,
238 : const OUString& rQName,
239 : const Reference< XAttributeList >& xAttrList )
240 : {
241 0 : if( m_bPersistent )
242 0 : return XMLPersElemContentTContext::CreateChildContext(nPrefix, rLocalName, rQName, xAttrList);
243 : else
244 0 : return XMLTransformerContext::CreateChildContext(nPrefix, rLocalName, rQName, xAttrList);
245 : }
246 :
247 0 : sal_Bool XMLEventOOoTransformerContext::IsPersistent() const
248 : {
249 0 : return m_bPersistent;
250 : }
251 :
252 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|