xref: /AOO41X/main/xmloff/source/script/XMLEventImportHelper.cxx (revision 63bba73cc51e0afb45f8a8d578158724bb5afee8)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 
27 #ifndef _XMLOFF_XMLEVENTIMPORTHELPER_HXX
28 #include "XMLEventImportHelper.hxx"
29 #endif
30 #include <tools/debug.hxx>
31 #include <xmloff/xmlimp.hxx>
32 #include <xmloff/nmspmap.hxx>
33 #include "xmloff/xmlnmspe.hxx"
34 #include "xmloff/xmlerror.hxx"
35 
36 using ::rtl::OUString;
37 using ::com::sun::star::xml::sax::XAttributeList;
38 using ::com::sun::star::uno::Reference;
39 using ::com::sun::star::uno::Sequence;
40 
XMLEventImportHelper()41 XMLEventImportHelper::XMLEventImportHelper() :
42     aFactoryMap(),
43     pEventNameMap(new NameMap()),
44     aEventNameMapList()
45 {
46 }
47 
48 
~XMLEventImportHelper()49 XMLEventImportHelper::~XMLEventImportHelper()
50 {
51     // delete factories
52     FactoryMap::iterator aEnd = aFactoryMap.end();
53     for(FactoryMap::iterator aIter = aFactoryMap.begin();
54         aIter != aEnd;
55         aIter++ )
56     {
57         delete aIter->second;
58     }
59     aFactoryMap.clear();
60 
61     // delete name map
62     delete pEventNameMap;
63 }
64 
RegisterFactory(const OUString & rLanguage,XMLEventContextFactory * pFactory)65 void XMLEventImportHelper::RegisterFactory(
66     const OUString& rLanguage,
67     XMLEventContextFactory* pFactory )
68 {
69     DBG_ASSERT(pFactory != NULL, "I need a factory.");
70     if (NULL != pFactory)
71     {
72         aFactoryMap[rLanguage] = pFactory;
73     }
74 }
75 
AddTranslationTable(const XMLEventNameTranslation * pTransTable)76 void XMLEventImportHelper::AddTranslationTable(
77     const XMLEventNameTranslation* pTransTable )
78 {
79     if (NULL != pTransTable)
80     {
81         // put translation table into map
82         for(const XMLEventNameTranslation* pTrans = pTransTable;
83             pTrans->sAPIName != NULL;
84             pTrans++)
85         {
86             XMLEventName aName( pTrans->nPrefix, pTrans->sXMLName );
87 
88             // check for conflicting entries
89             DBG_ASSERT(pEventNameMap->find(aName) == pEventNameMap->end(),
90                        "conflicting event translations");
91 
92             // assign new translation
93             (*pEventNameMap)[aName] =
94                 OUString::createFromAscii(pTrans->sAPIName);
95         }
96     }
97     // else? ignore!
98 }
99 
PushTranslationTable()100 void XMLEventImportHelper::PushTranslationTable()
101 {
102     // save old map and install new one
103     aEventNameMapList.push_back(pEventNameMap);
104     pEventNameMap = new NameMap();
105 }
106 
PopTranslationTable()107 void XMLEventImportHelper::PopTranslationTable()
108 {
109     DBG_ASSERT(aEventNameMapList.size() > 0,
110                "no translation tables left to pop");
111     if ( !aEventNameMapList.empty() )
112     {
113         // delete current and install old map
114         delete pEventNameMap;
115         pEventNameMap = aEventNameMapList.back();
116         aEventNameMapList.pop_back();
117     }
118 }
119 
120 
CreateContext(SvXMLImport & rImport,sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList,XMLEventsImportContext * rEvents,const OUString & rXmlEventName,const OUString & rLanguage)121 SvXMLImportContext* XMLEventImportHelper::CreateContext(
122     SvXMLImport& rImport,
123     sal_uInt16 nPrefix,
124     const OUString& rLocalName,
125     const Reference<XAttributeList> & xAttrList,
126     XMLEventsImportContext* rEvents,
127     const OUString& rXmlEventName,
128     const OUString& rLanguage)
129 {
130     SvXMLImportContext* pContext = NULL;
131 
132     // translate event name form xml to api
133     OUString sMacroName;
134     sal_uInt16 nMacroPrefix =
135         rImport.GetNamespaceMap().GetKeyByAttrName( rXmlEventName,
136                                                         &sMacroName );
137     XMLEventName aEventName( nMacroPrefix, sMacroName );
138     NameMap::iterator aNameIter = pEventNameMap->find(aEventName);
139     if (aNameIter != pEventNameMap->end())
140     {
141         OUString aScriptLanguage;
142         sal_uInt16 nScriptPrefix = rImport.GetNamespaceMap().
143                 GetKeyByAttrName( rLanguage, &aScriptLanguage );
144         if( XML_NAMESPACE_OOO != nScriptPrefix )
145             aScriptLanguage = rLanguage ;
146 
147         // check for factory
148         FactoryMap::iterator aFactoryIterator =
149             aFactoryMap.find(aScriptLanguage);
150         if (aFactoryIterator != aFactoryMap.end())
151         {
152             // delegate to factory
153             pContext = aFactoryIterator->second->CreateContext(
154                 rImport, nPrefix, rLocalName, xAttrList,
155                 rEvents, aNameIter->second, aScriptLanguage);
156         }
157     }
158 
159     // default context (if no context was created above)
160     if( NULL == pContext )
161     {
162         pContext = new SvXMLImportContext(rImport, nPrefix, rLocalName);
163 
164         Sequence<OUString> aMsgParams(2);
165 
166         aMsgParams[0] = rXmlEventName;
167         aMsgParams[1] = rLanguage;
168 
169         rImport.SetError(XMLERROR_FLAG_ERROR | XMLERROR_ILLEGAL_EVENT,
170                          aMsgParams);
171 
172     }
173 
174     return pContext;
175 }
176