1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: layerparser.cxx,v $
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 // -----------------------------------------------------------------------------
56 // -----------------------------------------------------------------------------
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
)
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();
90 OSL_ENSURE(isEmptyNode(), "BasicParser does not mark new document as empty");
92 m_xHandler
->startLayer();
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
);
123 ElementInfo aInfo
= getDataParser().parseElementInfo(aName
,xAttribs
);
129 case ElementType::group
: case ElementType::set
:
130 OSL_ENSURE( false, "Layer XML parser - Unexpected: found group/set element (should be 'node')\n");
132 case ElementType::layer
:
133 case ElementType::node
:
134 this->startNode(aInfo
,xAttribs
);
135 OSL_ASSERT( this->isInNode() && !this->isInProperty() );
138 case ElementType::property
:
139 this->startProperty(aInfo
,xAttribs
);
140 OSL_ASSERT( this->isInUnhandledProperty() );
143 case ElementType::value
:
144 this->startValueData(xAttribs
);
145 OSL_ASSERT( this->isInValueData() );
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
);
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
) )
167 if ( this->isInValueData())
168 this->endValueData();
170 else if (this->isInProperty())
173 else if (this->isInNode())
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
);
193 case Operation::none
:
194 case Operation::modify
:
195 m_xHandler
->overrideNode(aInfo
.name
,aInfo
.flags
,false);
198 case Operation::clear
:
199 m_xHandler
->overrideNode(aInfo
.name
,aInfo
.flags
,true);
202 case Operation::replace
:
203 case Operation::fuse
:
205 backenduno::TemplateIdentifier aTemplate
;
206 sal_Int16 flags
= aInfo
.flags
;
207 if (aInfo
.op
== Operation::fuse
) {
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
);
214 m_xHandler
->addOrReplaceNode(aInfo
.name
,flags
);
218 case Operation::remove
:
219 m_xHandler
->dropNode(aInfo
.name
);
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();
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
);
249 case Operation::none
:
250 case Operation::modify
:
251 m_xHandler
->overrideProperty(aInfo
.name
,aInfo
.flags
,getActivePropertyType(),false);
252 OSL_ASSERT(!m_bNewProp
);
255 case Operation::clear
:
256 m_xHandler
->overrideProperty(aInfo
.name
,aInfo
.flags
,getActivePropertyType(),true);
257 OSL_ASSERT(!m_bNewProp
);
260 case Operation::replace
:
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
) ;
292 m_xHandler
->addProperty(currentInfo
.name
, currentInfo
.flags
,
293 getActivePropertyType()) ;
296 // -----------------------------------------------------------------------------
298 void LayerParser::endProperty()
300 OSL_ASSERT(!this->isInRemoved());
304 OSL_ASSERT(getActiveNodeInfo().op
== Operation::replace
);
305 if (this->isInUnhandledProperty())
307 // No value tag is treated as NULL
308 addOrReplaceCurrentProperty( uno::Any() ) ;
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();
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
);
350 m_xHandler
->setPropertyValue(aValue
);
353 BasicParser::endValueData();
355 OSL_POSTCOND(!this->isInUnhandledProperty(),"Error: Property not reckognized as unhandled");
357 // -----------------------------------------------------------------------------
359 void LayerParser::checkNotRemoved()
362 raiseParseException("Layer parser: Invalid Data: Data inside removed node.");
364 // -----------------------------------------------------------------------------
366 // -----------------------------------------------------------------------------
369 // -----------------------------------------------------------------------------