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