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