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
10*63bba73cSAndrew Rist  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist  *
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.
19*63bba73cSAndrew Rist  *
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 
27cdf0e10cSrcweir #ifndef _XMLOFF_XMLEVENTIMPORTHELPER_HXX
28cdf0e10cSrcweir #include "XMLEventImportHelper.hxx"
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include <tools/debug.hxx>
31cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
32cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
33cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
34cdf0e10cSrcweir #include "xmloff/xmlerror.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using ::rtl::OUString;
37cdf0e10cSrcweir using ::com::sun::star::xml::sax::XAttributeList;
38cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
39cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
40cdf0e10cSrcweir 
XMLEventImportHelper()41cdf0e10cSrcweir XMLEventImportHelper::XMLEventImportHelper() :
42cdf0e10cSrcweir 	aFactoryMap(),
43cdf0e10cSrcweir 	pEventNameMap(new NameMap()),
44cdf0e10cSrcweir 	aEventNameMapList()
45cdf0e10cSrcweir {
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
~XMLEventImportHelper()49cdf0e10cSrcweir XMLEventImportHelper::~XMLEventImportHelper()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 	// delete factories
52cdf0e10cSrcweir 	FactoryMap::iterator aEnd = aFactoryMap.end();
53cdf0e10cSrcweir 	for(FactoryMap::iterator aIter = aFactoryMap.begin();
54cdf0e10cSrcweir 		aIter != aEnd;
55cdf0e10cSrcweir 		aIter++ )
56cdf0e10cSrcweir 	{
57cdf0e10cSrcweir 		delete aIter->second;
58cdf0e10cSrcweir 	}
59cdf0e10cSrcweir 	aFactoryMap.clear();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	// delete name map
62cdf0e10cSrcweir 	delete pEventNameMap;
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
RegisterFactory(const OUString & rLanguage,XMLEventContextFactory * pFactory)65cdf0e10cSrcweir void XMLEventImportHelper::RegisterFactory(
66cdf0e10cSrcweir 	const OUString& rLanguage,
67cdf0e10cSrcweir 	XMLEventContextFactory* pFactory )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	DBG_ASSERT(pFactory != NULL, "I need a factory.");
70cdf0e10cSrcweir 	if (NULL != pFactory)
71cdf0e10cSrcweir 	{
72cdf0e10cSrcweir 		aFactoryMap[rLanguage] = pFactory;
73cdf0e10cSrcweir 	}
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
AddTranslationTable(const XMLEventNameTranslation * pTransTable)76cdf0e10cSrcweir void XMLEventImportHelper::AddTranslationTable(
77cdf0e10cSrcweir 	const XMLEventNameTranslation* pTransTable )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir 	if (NULL != pTransTable)
80cdf0e10cSrcweir 	{
81cdf0e10cSrcweir 		// put translation table into map
82cdf0e10cSrcweir 		for(const XMLEventNameTranslation* pTrans = pTransTable;
83cdf0e10cSrcweir 			pTrans->sAPIName != NULL;
84cdf0e10cSrcweir 			pTrans++)
85cdf0e10cSrcweir 		{
86cdf0e10cSrcweir 			XMLEventName aName( pTrans->nPrefix, pTrans->sXMLName );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 			// check for conflicting entries
89cdf0e10cSrcweir 			DBG_ASSERT(pEventNameMap->find(aName) == pEventNameMap->end(),
90cdf0e10cSrcweir 					   "conflicting event translations");
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 			// assign new translation
93cdf0e10cSrcweir 			(*pEventNameMap)[aName] =
94cdf0e10cSrcweir 				OUString::createFromAscii(pTrans->sAPIName);
95cdf0e10cSrcweir 		}
96cdf0e10cSrcweir 	}
97cdf0e10cSrcweir 	// else? ignore!
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
PushTranslationTable()100cdf0e10cSrcweir void XMLEventImportHelper::PushTranslationTable()
101cdf0e10cSrcweir {
102cdf0e10cSrcweir 	// save old map and install new one
103cdf0e10cSrcweir 	aEventNameMapList.push_back(pEventNameMap);
104cdf0e10cSrcweir 	pEventNameMap = new NameMap();
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
PopTranslationTable()107cdf0e10cSrcweir void XMLEventImportHelper::PopTranslationTable()
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	DBG_ASSERT(aEventNameMapList.size() > 0,
110cdf0e10cSrcweir 			   "no translation tables left to pop");
111cdf0e10cSrcweir 	if ( !aEventNameMapList.empty() )
112cdf0e10cSrcweir 	{
113cdf0e10cSrcweir 		// delete current and install old map
114cdf0e10cSrcweir 		delete pEventNameMap;
115cdf0e10cSrcweir 		pEventNameMap = aEventNameMapList.back();
116cdf0e10cSrcweir 		aEventNameMapList.pop_back();
117cdf0e10cSrcweir 	}
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 
CreateContext(SvXMLImport & rImport,sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList,XMLEventsImportContext * rEvents,const OUString & rXmlEventName,const OUString & rLanguage)121cdf0e10cSrcweir SvXMLImportContext* XMLEventImportHelper::CreateContext(
122cdf0e10cSrcweir 	SvXMLImport& rImport,
123cdf0e10cSrcweir 	sal_uInt16 nPrefix,
124cdf0e10cSrcweir 	const OUString& rLocalName,
125cdf0e10cSrcweir 	const Reference<XAttributeList> & xAttrList,
126cdf0e10cSrcweir 	XMLEventsImportContext* rEvents,
127cdf0e10cSrcweir 	const OUString& rXmlEventName,
128cdf0e10cSrcweir 	const OUString& rLanguage)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir 	SvXMLImportContext* pContext = NULL;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 	// translate event name form xml to api
133cdf0e10cSrcweir 	OUString sMacroName;
134cdf0e10cSrcweir 	sal_uInt16 nMacroPrefix =
135cdf0e10cSrcweir 		rImport.GetNamespaceMap().GetKeyByAttrName( rXmlEventName,
136cdf0e10cSrcweir 														&sMacroName );
137cdf0e10cSrcweir 	XMLEventName aEventName( nMacroPrefix, sMacroName );
138cdf0e10cSrcweir 	NameMap::iterator aNameIter = pEventNameMap->find(aEventName);
139cdf0e10cSrcweir 	if (aNameIter != pEventNameMap->end())
140cdf0e10cSrcweir 	{
141cdf0e10cSrcweir 		OUString aScriptLanguage;
142cdf0e10cSrcweir 		sal_uInt16 nScriptPrefix = rImport.GetNamespaceMap().
143cdf0e10cSrcweir 				GetKeyByAttrName( rLanguage, &aScriptLanguage );
144cdf0e10cSrcweir 		if( XML_NAMESPACE_OOO != nScriptPrefix )
145cdf0e10cSrcweir 			aScriptLanguage = rLanguage ;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 		// check for factory
148cdf0e10cSrcweir 		FactoryMap::iterator aFactoryIterator =
149cdf0e10cSrcweir 			aFactoryMap.find(aScriptLanguage);
150cdf0e10cSrcweir 		if (aFactoryIterator != aFactoryMap.end())
151cdf0e10cSrcweir 		{
152cdf0e10cSrcweir 			// delegate to factory
153cdf0e10cSrcweir 			pContext = aFactoryIterator->second->CreateContext(
154cdf0e10cSrcweir 				rImport, nPrefix, rLocalName, xAttrList,
155cdf0e10cSrcweir 				rEvents, aNameIter->second, aScriptLanguage);
156cdf0e10cSrcweir 		}
157cdf0e10cSrcweir 	}
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	// default context (if no context was created above)
160cdf0e10cSrcweir 	if( NULL == pContext )
161cdf0e10cSrcweir 	{
162cdf0e10cSrcweir 		pContext = new SvXMLImportContext(rImport, nPrefix, rLocalName);
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         Sequence<OUString> aMsgParams(2);
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         aMsgParams[0] = rXmlEventName;
167cdf0e10cSrcweir         aMsgParams[1] = rLanguage;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         rImport.SetError(XMLERROR_FLAG_ERROR | XMLERROR_ILLEGAL_EVENT,
170cdf0e10cSrcweir                          aMsgParams);
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	}
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 	return pContext;
175cdf0e10cSrcweir }
176