1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef _XMLOFF_XMLEVENTIMPORTHELPER_HXX
32*cdf0e10cSrcweir #include "XMLEventImportHelper.hxx"
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
36*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
37*cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
38*cdf0e10cSrcweir #include "xmloff/xmlerror.hxx"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using ::rtl::OUString;
41*cdf0e10cSrcweir using ::com::sun::star::xml::sax::XAttributeList;
42*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
43*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir XMLEventImportHelper::XMLEventImportHelper() :
46*cdf0e10cSrcweir 	aFactoryMap(),
47*cdf0e10cSrcweir 	pEventNameMap(new NameMap()),
48*cdf0e10cSrcweir 	aEventNameMapList()
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir XMLEventImportHelper::~XMLEventImportHelper()
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir 	// delete factories
56*cdf0e10cSrcweir 	FactoryMap::iterator aEnd = aFactoryMap.end();
57*cdf0e10cSrcweir 	for(FactoryMap::iterator aIter = aFactoryMap.begin();
58*cdf0e10cSrcweir 		aIter != aEnd;
59*cdf0e10cSrcweir 		aIter++ )
60*cdf0e10cSrcweir 	{
61*cdf0e10cSrcweir 		delete aIter->second;
62*cdf0e10cSrcweir 	}
63*cdf0e10cSrcweir 	aFactoryMap.clear();
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	// delete name map
66*cdf0e10cSrcweir 	delete pEventNameMap;
67*cdf0e10cSrcweir }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir void XMLEventImportHelper::RegisterFactory(
70*cdf0e10cSrcweir 	const OUString& rLanguage,
71*cdf0e10cSrcweir 	XMLEventContextFactory* pFactory )
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	DBG_ASSERT(pFactory != NULL, "I need a factory.");
74*cdf0e10cSrcweir 	if (NULL != pFactory)
75*cdf0e10cSrcweir 	{
76*cdf0e10cSrcweir 		aFactoryMap[rLanguage] = pFactory;
77*cdf0e10cSrcweir 	}
78*cdf0e10cSrcweir }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir void XMLEventImportHelper::AddTranslationTable(
81*cdf0e10cSrcweir 	const XMLEventNameTranslation* pTransTable )
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir 	if (NULL != pTransTable)
84*cdf0e10cSrcweir 	{
85*cdf0e10cSrcweir 		// put translation table into map
86*cdf0e10cSrcweir 		for(const XMLEventNameTranslation* pTrans = pTransTable;
87*cdf0e10cSrcweir 			pTrans->sAPIName != NULL;
88*cdf0e10cSrcweir 			pTrans++)
89*cdf0e10cSrcweir 		{
90*cdf0e10cSrcweir 			XMLEventName aName( pTrans->nPrefix, pTrans->sXMLName );
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 			// check for conflicting entries
93*cdf0e10cSrcweir 			DBG_ASSERT(pEventNameMap->find(aName) == pEventNameMap->end(),
94*cdf0e10cSrcweir 					   "conflicting event translations");
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 			// assign new translation
97*cdf0e10cSrcweir 			(*pEventNameMap)[aName] =
98*cdf0e10cSrcweir 				OUString::createFromAscii(pTrans->sAPIName);
99*cdf0e10cSrcweir 		}
100*cdf0e10cSrcweir 	}
101*cdf0e10cSrcweir 	// else? ignore!
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir void XMLEventImportHelper::PushTranslationTable()
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir 	// save old map and install new one
107*cdf0e10cSrcweir 	aEventNameMapList.push_back(pEventNameMap);
108*cdf0e10cSrcweir 	pEventNameMap = new NameMap();
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir void XMLEventImportHelper::PopTranslationTable()
112*cdf0e10cSrcweir {
113*cdf0e10cSrcweir 	DBG_ASSERT(aEventNameMapList.size() > 0,
114*cdf0e10cSrcweir 			   "no translation tables left to pop");
115*cdf0e10cSrcweir 	if ( !aEventNameMapList.empty() )
116*cdf0e10cSrcweir 	{
117*cdf0e10cSrcweir 		// delete current and install old map
118*cdf0e10cSrcweir 		delete pEventNameMap;
119*cdf0e10cSrcweir 		pEventNameMap = aEventNameMapList.back();
120*cdf0e10cSrcweir 		aEventNameMapList.pop_back();
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir SvXMLImportContext* XMLEventImportHelper::CreateContext(
126*cdf0e10cSrcweir 	SvXMLImport& rImport,
127*cdf0e10cSrcweir 	sal_uInt16 nPrefix,
128*cdf0e10cSrcweir 	const OUString& rLocalName,
129*cdf0e10cSrcweir 	const Reference<XAttributeList> & xAttrList,
130*cdf0e10cSrcweir 	XMLEventsImportContext* rEvents,
131*cdf0e10cSrcweir 	const OUString& rXmlEventName,
132*cdf0e10cSrcweir 	const OUString& rLanguage)
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir 	SvXMLImportContext* pContext = NULL;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	// translate event name form xml to api
137*cdf0e10cSrcweir 	OUString sMacroName;
138*cdf0e10cSrcweir 	sal_uInt16 nMacroPrefix =
139*cdf0e10cSrcweir 		rImport.GetNamespaceMap().GetKeyByAttrName( rXmlEventName,
140*cdf0e10cSrcweir 														&sMacroName );
141*cdf0e10cSrcweir 	XMLEventName aEventName( nMacroPrefix, sMacroName );
142*cdf0e10cSrcweir 	NameMap::iterator aNameIter = pEventNameMap->find(aEventName);
143*cdf0e10cSrcweir 	if (aNameIter != pEventNameMap->end())
144*cdf0e10cSrcweir 	{
145*cdf0e10cSrcweir 		OUString aScriptLanguage;
146*cdf0e10cSrcweir 		sal_uInt16 nScriptPrefix = rImport.GetNamespaceMap().
147*cdf0e10cSrcweir 				GetKeyByAttrName( rLanguage, &aScriptLanguage );
148*cdf0e10cSrcweir 		if( XML_NAMESPACE_OOO != nScriptPrefix )
149*cdf0e10cSrcweir 			aScriptLanguage = rLanguage ;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 		// check for factory
152*cdf0e10cSrcweir 		FactoryMap::iterator aFactoryIterator =
153*cdf0e10cSrcweir 			aFactoryMap.find(aScriptLanguage);
154*cdf0e10cSrcweir 		if (aFactoryIterator != aFactoryMap.end())
155*cdf0e10cSrcweir 		{
156*cdf0e10cSrcweir 			// delegate to factory
157*cdf0e10cSrcweir 			pContext = aFactoryIterator->second->CreateContext(
158*cdf0e10cSrcweir 				rImport, nPrefix, rLocalName, xAttrList,
159*cdf0e10cSrcweir 				rEvents, aNameIter->second, aScriptLanguage);
160*cdf0e10cSrcweir 		}
161*cdf0e10cSrcweir 	}
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	// default context (if no context was created above)
164*cdf0e10cSrcweir 	if( NULL == pContext )
165*cdf0e10cSrcweir 	{
166*cdf0e10cSrcweir 		pContext = new SvXMLImportContext(rImport, nPrefix, rLocalName);
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         Sequence<OUString> aMsgParams(2);
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         aMsgParams[0] = rXmlEventName;
171*cdf0e10cSrcweir         aMsgParams[1] = rLanguage;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         rImport.SetError(XMLERROR_FLAG_ERROR | XMLERROR_ILLEGAL_EVENT,
174*cdf0e10cSrcweir                          aMsgParams);
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	}
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 	return pContext;
179*cdf0e10cSrcweir }
180