update dev300-m58
[ooovba.git] / configmgr / source / xml / layerparser.cxx
blob10797b5a3474ea74f094cadcf4c447a4dfae1663
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: layerparser.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include "com/sun/star/configuration/backend/NodeAttribute.hpp"
35 #include "layerparser.hxx"
37 // -----------------------------------------------------------------------------
38 #include "wrapexception.hxx"
40 #define WRAP_PARSE_EXCEPTIONS() \
41 PASS_EXCEPTION(sax::SAXException) \
42 PASS_EXCEPTION(uno::RuntimeException) \
43 WRAP_CONFIGDATA_EXCEPTIONS( raiseParseException ) \
44 WRAP_OTHER_EXCEPTIONS( raiseParseException )
46 #define WRAP_PARSE_EXCEPTIONS_MSG( msg ) \
47 PASS_EXCEPTION(sax::SAXException) \
48 PASS_EXCEPTION(uno::RuntimeException) \
49 WRAP_CONFIGDATA_EXCEPTIONS1( raiseParseException, msg ) \
50 WRAP_OTHER_EXCEPTIONS1( raiseParseException, msg )
52 // -----------------------------------------------------------------------------
54 namespace configmgr
56 // -----------------------------------------------------------------------------
57 namespace xml
59 // -----------------------------------------------------------------------------
60 namespace uno = ::com::sun::star::uno;
61 namespace sax = ::com::sun::star::xml::sax;
62 // -----------------------------------------------------------------------------
64 LayerParser::LayerParser(uno::Reference< uno::XComponentContext > const & _xContext, uno::Reference< backenduno::XLayerHandler > const & _xHandler)
65 : BasicParser(_xContext)
66 , m_xHandler(_xHandler)
67 , m_bRemoved(false)
68 , m_bNewProp(false)
70 if (!m_xHandler.is())
72 rtl::OUString sMessage(RTL_CONSTASCII_USTRINGPARAM("Cannot create LayerParser: Unexpected NULL Handler"));
73 throw uno::RuntimeException(sMessage, NULL);
76 // -----------------------------------------------------------------------------
78 LayerParser::~LayerParser()
81 // -----------------------------------------------------------------------------
83 void SAL_CALL LayerParser::startDocument( )
84 throw (sax::SAXException, uno::RuntimeException)
86 BasicParser::startDocument();
88 try
90 OSL_ENSURE(isEmptyNode(), "BasicParser does not mark new document as empty");
92 m_xHandler->startLayer();
93 m_bRemoved = false;
94 m_bNewProp = false;
96 WRAP_PARSE_EXCEPTIONS_MSG("LayerParser - Starting layer")
98 // -----------------------------------------------------------------------------
100 void SAL_CALL LayerParser::endDocument( ) throw (sax::SAXException, uno::RuntimeException)
102 if (isEmptyNode()) OSL_TRACE("Configuration Parser: XML layer document ended without any data");
104 BasicParser::endDocument();
108 m_xHandler->endLayer();
110 WRAP_PARSE_EXCEPTIONS_MSG("LayerParser - Ending layer")
112 // -----------------------------------------------------------------------------
114 void SAL_CALL LayerParser::startElement( const rtl::OUString& aName, const uno::Reference< sax::XAttributeList >& xAttribs )
115 throw (sax::SAXException, uno::RuntimeException)
117 if ( this->isSkipping() )
119 this->startSkipping( aName, xAttribs );
120 return;
123 ElementInfo aInfo = getDataParser().parseElementInfo(aName,xAttribs);
127 switch (aInfo.type)
129 case ElementType::group: case ElementType::set:
130 OSL_ENSURE( false, "Layer XML parser - Unexpected: found group/set element (should be 'node')\n");
131 // fall thru
132 case ElementType::layer:
133 case ElementType::node:
134 this->startNode(aInfo,xAttribs);
135 OSL_ASSERT( this->isInNode() && !this->isInProperty() );
136 break;
138 case ElementType::property:
139 this->startProperty(aInfo,xAttribs);
140 OSL_ASSERT( this->isInUnhandledProperty() );
141 break;
143 case ElementType::value:
144 this->startValueData(xAttribs);
145 OSL_ASSERT( this->isInValueData() );
146 break;
148 default: // skip unknown elements
149 OSL_ENSURE( aInfo.type >= ElementType::other, "Layer XML parser - Unexpected: found schema element in layer data\n");
150 OSL_ENSURE( aInfo.type < ElementType::other, "Layer XML parser - Warning: ignoring unknown element tag\n");
151 this->startSkipping( aName, xAttribs );
152 return;
155 WRAP_PARSE_EXCEPTIONS_MSG("LayerParser - Starting Element")
157 // -----------------------------------------------------------------------------
159 void SAL_CALL LayerParser::endElement( const rtl::OUString& aName )
160 throw (sax::SAXException, uno::RuntimeException)
162 if ( this->wasSkipping(aName) )
163 return;
167 if ( this->isInValueData())
168 this->endValueData();
170 else if (this->isInProperty())
171 this->endProperty();
173 else if (this->isInNode())
174 this->endNode();
176 else {
177 this->raiseParseException("Layer parser: Invalid XML: endElement without matching startElement");
180 WRAP_PARSE_EXCEPTIONS_MSG("LayerParser - Ending Element")
182 // -----------------------------------------------------------------------------
183 // -----------------------------------------------------------------------------
185 void LayerParser::startNode( ElementInfo const & aInfo, const uno::Reference< sax::XAttributeList >& xAttribs )
187 this->checkNotRemoved();
189 BasicParser::startNode(aInfo,xAttribs);
191 switch (aInfo.op)
193 case Operation::none:
194 case Operation::modify:
195 m_xHandler->overrideNode(aInfo.name,aInfo.flags,false);
196 break;
198 case Operation::clear:
199 m_xHandler->overrideNode(aInfo.name,aInfo.flags,true);
200 break;
202 case Operation::replace:
203 case Operation::fuse:
205 backenduno::TemplateIdentifier aTemplate;
206 sal_Int16 flags = aInfo.flags;
207 if (aInfo.op == Operation::fuse) {
208 flags |=
209 com::sun::star::configuration::backend::NodeAttribute::FUSE;
211 if (getDataParser().getInstanceType(xAttribs,aTemplate.Name,aTemplate.Component))
212 m_xHandler->addOrReplaceNodeFromTemplate(aInfo.name,aTemplate,flags);
213 else
214 m_xHandler->addOrReplaceNode(aInfo.name,flags);
216 break;
218 case Operation::remove:
219 m_xHandler->dropNode(aInfo.name);
220 m_bRemoved = true;
221 break;
223 default: OSL_ASSERT(false);
224 case Operation::unknown:
225 this->raiseParseException("Layer parser: Invalid Data: unknown operation");
228 // -----------------------------------------------------------------------------
230 void LayerParser::endNode()
232 if (!this->isInRemoved())
233 m_xHandler->endNode();
234 else
235 m_bRemoved = false;
237 BasicParser::endNode();
239 // -----------------------------------------------------------------------------
241 void LayerParser::startProperty( ElementInfo const & aInfo, const uno::Reference< sax::XAttributeList >& xAttribs )
243 this->checkNotRemoved();
245 BasicParser::startProperty(aInfo,xAttribs);
247 switch (aInfo.op)
249 case Operation::none:
250 case Operation::modify:
251 m_xHandler->overrideProperty(aInfo.name,aInfo.flags,getActivePropertyType(),false);
252 OSL_ASSERT(!m_bNewProp);
253 break;
255 case Operation::clear:
256 m_xHandler->overrideProperty(aInfo.name,aInfo.flags,getActivePropertyType(),true);
257 OSL_ASSERT(!m_bNewProp);
258 break;
260 case Operation::replace:
261 // defer to later
262 m_bNewProp = true;
263 break;
265 case Operation::fuse:
266 this->raiseParseException("Layer parser: Invalid Data: operation 'fuse' is not permitted for properties");
268 case Operation::remove:
269 this->raiseParseException("Layer parser: Invalid Data: operation 'remove' is not permitted for properties");
271 default: OSL_ASSERT(false);
272 case Operation::unknown:
273 this->raiseParseException("Layer parser: Invalid Data: unknown operation");
275 OSL_POSTCOND(this->isInUnhandledProperty(),"Error: Property not reckognized as unhandled");
277 // -----------------------------------------------------------------------------
279 void LayerParser::addOrReplaceCurrentProperty(const uno::Any& aValue)
281 const ElementInfo& currentInfo = getActiveNodeInfo() ;
283 OSL_ASSERT(currentInfo.op == Operation::replace) ;
285 if (aValue.hasValue())
287 m_xHandler->addPropertyWithValue(currentInfo.name,
288 currentInfo.flags, aValue) ;
290 else
292 m_xHandler->addProperty(currentInfo.name, currentInfo.flags,
293 getActivePropertyType()) ;
296 // -----------------------------------------------------------------------------
298 void LayerParser::endProperty()
300 OSL_ASSERT(!this->isInRemoved());
302 if (m_bNewProp)
304 OSL_ASSERT(getActiveNodeInfo().op == Operation::replace);
305 if (this->isInUnhandledProperty())
307 // No value tag is treated as NULL
308 addOrReplaceCurrentProperty( uno::Any() ) ;
310 m_bNewProp = false;
312 else
314 OSL_ASSERT(getActiveNodeInfo().op != Operation::replace);
315 m_xHandler->endProperty();
318 BasicParser::endProperty();
320 // -----------------------------------------------------------------------------
322 void LayerParser::startValueData(const uno::Reference< sax::XAttributeList >& xAttribs)
324 this->checkNotRemoved();
326 BasicParser::startValueData(xAttribs);
328 // -----------------------------------------------------------------------------
330 void LayerParser::endValueData()
332 uno::Any aValue = this->getCurrentValue();
334 if (m_bNewProp)
336 if (this->isValueDataLocalized())
337 getLogger().warning("Language attribute ignored for value of added property.",
338 "endValueData()","configuration::xml::SchemaParser");
340 addOrReplaceCurrentProperty(aValue) ;
342 else if (this->isValueDataLocalized())
344 rtl::OUString aLocale = this->getValueDataLocale();
346 m_xHandler->setPropertyValueForLocale(aValue,aLocale);
348 else
350 m_xHandler->setPropertyValue(aValue);
353 BasicParser::endValueData();
355 OSL_POSTCOND(!this->isInUnhandledProperty(),"Error: Property not reckognized as unhandled");
357 // -----------------------------------------------------------------------------
359 void LayerParser::checkNotRemoved()
361 if (m_bRemoved)
362 raiseParseException("Layer parser: Invalid Data: Data inside removed node.");
364 // -----------------------------------------------------------------------------
366 // -----------------------------------------------------------------------------
367 } // namespace
369 // -----------------------------------------------------------------------------
370 } // namespace