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