xref: /trunk/main/desktop/source/migration/cfgfilter.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_desktop.hxx"
27 #include "cfgfilter.hxx"
28 
29 #include <com/sun/star/beans/NamedValue.hpp>
30 #include <unotools/textsearch.hxx>
31 
32 using namespace rtl;
33 using namespace com::sun::star;
34 using namespace com::sun::star::uno;
35 using namespace com::sun::star::lang;
36 using namespace com::sun::star::beans;
37 using namespace com::sun::star::configuration::backend;
38 
39 namespace desktop {
40 
CConfigFilter(const strings_v * include,const strings_v * exclude)41 CConfigFilter::CConfigFilter(const strings_v* include,  const strings_v* exclude)
42     : m_pvInclude(include)
43     , m_pvExclude(exclude)
44 {
45 }
46 
initialize(const Sequence<Any> & seqArgs)47 void SAL_CALL CConfigFilter::initialize(const Sequence< Any >& seqArgs)
48 {
49     NamedValue nv;
50     for (sal_Int32 i=0; i < seqArgs.getLength(); i++)
51     {
52         if (seqArgs[i] >>= nv)
53         {
54             if (nv.Name.equalsAscii("Source"))
55                 nv.Value >>= m_xSourceLayer;
56             if (nv.Name.equalsAscii("ComponentName"))
57                 nv.Value >>= m_aCurrentComponent;
58         }
59     }
60     if (m_aCurrentComponent.getLength() == 0)
61         m_aCurrentComponent = OUString::createFromAscii("unknown.component");
62 
63     if (!m_xSourceLayer.is()) {
64         throw Exception();
65     }
66 
67 }
68 
69 
pushElement(rtl::OUString aName,sal_Bool bUse)70 void CConfigFilter::pushElement(rtl::OUString aName, sal_Bool bUse)
71 {
72     OUString aPath;
73     if (!m_elementStack.empty()) {
74         aPath = m_elementStack.top().path; // or use base path
75         aPath += OUString::createFromAscii("/");
76     }
77     aPath += aName;
78 
79     // create element
80     element elem;
81     elem.name = aName;
82     elem.path = aPath;
83     elem.use = bUse;
84     m_elementStack.push(elem);
85 }
86 
checkCurrentElement()87 sal_Bool CConfigFilter::checkCurrentElement()
88 {
89     return m_elementStack.top().use;
90 }
91 
checkElement(rtl::OUString aName)92 sal_Bool CConfigFilter::checkElement(rtl::OUString aName)
93 {
94 
95     sal_Bool bResult = sal_False;
96 
97     // get full pathname for element
98     OUString aFullPath;
99     if (!m_elementStack.empty())
100         aFullPath = m_elementStack.top().path + OUString::createFromAscii("/");
101 
102     aFullPath += aName;
103 
104     // check whether any include patterns patch this path
105     for (strings_v::const_iterator i_in = m_pvInclude->begin();
106         i_in != m_pvInclude->end(); i_in++)
107     {
108         // pattern is beginning of path
109         // or path is a begiing for pattern
110         if (i_in->match(aFullPath.copy(0, i_in->getLength()>aFullPath.getLength()
111             ? aFullPath.getLength() : i_in->getLength()), 0))
112         {
113             bResult = sal_True;
114             break; // one match is enough
115         }
116     }
117     // if match is found, check for exclusion
118     if (bResult)
119     {
120         for (strings_v::const_iterator i_ex = m_pvExclude->begin();
121             i_ex != m_pvExclude->end(); i_ex++)
122         {
123             if (aFullPath.match(*i_ex, 0)) // pattern is beginning of path
124             {
125                 bResult = sal_False;
126                 break; // one is enough...
127             }
128         }
129     }
130     return bResult;
131 }
132 
popElement()133 void CConfigFilter::popElement()
134 {
135     m_elementStack.pop();
136 }
137 
138 
readData(const Reference<configuration::backend::XLayerHandler> & layerHandler)139 void SAL_CALL CConfigFilter::readData(
140         const Reference< configuration::backend::XLayerHandler >& layerHandler)
141 {
142     // when readData is called, the submitted handler will be stored
143     // in m_xLayerHandler. we will then submit ourself as a handler to
144     // the SourceLayer in m_xSourceLayer.
145     // when the source calls our handler functions we will use the patterns that
146     // where given in the ctor to decide whether they should be relaied to the caller
147 
148     if (m_xSourceLayer.is() && layerHandler.is())
149     {
150         m_xLayerHandler = layerHandler;
151         m_xSourceLayer->readData(Reference<XLayerHandler>(static_cast< XLayerHandler* >(this)));
152     } else
153     {
154         throw NullPointerException();
155     }
156 }
157 
158 // XLayerHandler
startLayer()159 void SAL_CALL CConfigFilter::startLayer()
160 {
161     m_xLayerHandler->startLayer();
162 }
163 
endLayer()164 void SAL_CALL CConfigFilter::endLayer()
165 {
166     m_xLayerHandler->endLayer();
167 }
168 
overrideNode(const OUString & aName,sal_Int16 aAttributes,sal_Bool bClear)169 void SAL_CALL CConfigFilter::overrideNode(
170         const OUString& aName,
171         sal_Int16 aAttributes,
172         sal_Bool bClear)
173 {
174     if (checkElement(aName))
175     {
176         m_xLayerHandler->overrideNode(aName, aAttributes, bClear);
177         pushElement(aName);
178     }
179     else
180         pushElement(aName, sal_False);
181 }
182 
addOrReplaceNode(const OUString & aName,sal_Int16 aAttributes)183 void SAL_CALL CConfigFilter::addOrReplaceNode(
184         const OUString& aName,
185         sal_Int16 aAttributes)
186 {
187     if (checkElement(aName))
188     {
189         m_xLayerHandler->addOrReplaceNode(aName, aAttributes);
190         pushElement(aName);
191     }
192     else
193         pushElement(aName, sal_False);
194 }
195 
addOrReplaceNodeFromTemplate(const OUString & aName,const com::sun::star::configuration::backend::TemplateIdentifier & aTemplate,sal_Int16 aAttributes)196 void SAL_CALL  CConfigFilter::addOrReplaceNodeFromTemplate(
197         const OUString& aName,
198         const com::sun::star::configuration::backend::TemplateIdentifier& aTemplate,
199         sal_Int16 aAttributes )
200 {
201     if (checkElement(aName))
202     {
203         m_xLayerHandler->addOrReplaceNodeFromTemplate(aName, aTemplate, aAttributes);
204         pushElement(aName);
205     }
206     else
207         pushElement(aName, sal_False);
208 }
209 
endNode()210 void SAL_CALL  CConfigFilter::endNode()
211 {
212     if (checkCurrentElement())
213     {
214         m_xLayerHandler->endNode();
215     }
216     popElement();
217 }
218 
dropNode(const OUString & aName)219 void SAL_CALL  CConfigFilter::dropNode(
220         const OUString& aName )
221 {
222     // does not get pushed
223     if (checkElement(aName))
224     {
225         m_xLayerHandler->dropNode(aName);
226     }
227 }
228 
overrideProperty(const OUString & aName,sal_Int16 aAttributes,const Type & aType,sal_Bool bClear)229 void SAL_CALL  CConfigFilter::overrideProperty(
230         const OUString& aName,
231         sal_Int16 aAttributes,
232         const Type& aType,
233         sal_Bool bClear )
234 {
235     if (checkElement(aName)){
236         m_xLayerHandler->overrideProperty(aName, aAttributes, aType, bClear);
237         pushElement(aName);
238     }
239     else
240         pushElement(aName, sal_False);
241 }
242 
setPropertyValue(const Any & aValue)243 void SAL_CALL  CConfigFilter::setPropertyValue(
244         const Any& aValue )
245 {
246     if (checkCurrentElement())
247         m_xLayerHandler->setPropertyValue(aValue);
248 }
249 
setPropertyValueForLocale(const Any & aValue,const OUString & aLocale)250 void SAL_CALL CConfigFilter::setPropertyValueForLocale(
251         const Any& aValue,
252         const OUString& aLocale )
253 {
254     if (checkCurrentElement())
255         m_xLayerHandler->setPropertyValueForLocale(aValue, aLocale);
256 }
257 
endProperty()258 void SAL_CALL  CConfigFilter::endProperty()
259 {
260     if (checkCurrentElement())
261     {
262         m_xLayerHandler->endProperty();
263     }
264     popElement();
265 
266 }
267 
addProperty(const rtl::OUString & aName,sal_Int16 aAttributes,const Type & aType)268 void SAL_CALL  CConfigFilter::addProperty(
269         const rtl::OUString& aName,
270         sal_Int16 aAttributes,
271         const Type& aType )
272 {
273     if (checkElement(aName))
274         m_xLayerHandler->addProperty(aName, aAttributes, aType);
275 }
276 
addPropertyWithValue(const rtl::OUString & aName,sal_Int16 aAttributes,const Any & aValue)277 void SAL_CALL  CConfigFilter::addPropertyWithValue(
278         const rtl::OUString& aName,
279         sal_Int16 aAttributes,
280         const Any& aValue )
281 {
282     // add property with value doesn't push the property
283     if (checkElement(aName))
284         m_xLayerHandler->addPropertyWithValue(aName, aAttributes, aValue);
285 
286 }
287 
288 } // namespace desktop
289