LibreOffice Module xmloff (master)  1
officeforms.cxx
Go to the documentation of this file.
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 #include "officeforms.hxx"
21 
22 #include <sax/tools/converter.hxx>
23 
24 #include <xmloff/xmltoken.hxx>
25 #include <xmloff/xmlnmspe.hxx>
26 #include <xmloff/xmlexp.hxx>
27 #include <xmloff/xmlimp.hxx>
28 #include <xmloff/nmspmap.hxx>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <comphelper/extract.hxx>
31 #include <tools/diagnose_ex.h>
32 #include "strings.hxx"
33 
34 namespace xmloff
35 {
36 
37  using namespace ::com::sun::star::uno;
38  using namespace ::com::sun::star::beans;
39  using namespace ::com::sun::star::xml;
41  using ::com::sun::star::xml::sax::XAttributeList;
42 
43  //= OFormsRootImport
44  OFormsRootImport::OFormsRootImport( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLocalName )
45  :SvXMLImportContext(rImport, nPrfx, rLocalName)
46  {
47  }
48 
50  {
51  }
52 
53  SvXMLImportContextRef OFormsRootImport::CreateChildContext( sal_uInt16 _nPrefix, const OUString& _rLocalName,
54  const Reference< XAttributeList>& xAttrList )
55  {
56  SvXMLImportContext* pRet = nullptr;
57  try
58  {
59  pRet = GetImport().GetFormImport()->createContext( _nPrefix, _rLocalName, xAttrList );
60  } catch (const Exception&)
61  {
62  DBG_UNHANDLED_EXCEPTION("xmloff.forms");
63  }
64  return pRet;
65  }
66 
67  void OFormsRootImport::implImportBool(const Reference< XAttributeList >& _rxAttributes, OfficeFormsAttributes _eAttribute,
68  const Reference< XPropertySet >& _rxProps, const Reference< XPropertySetInfo >& _rxPropInfo,
69  const OUString& _rPropName, bool _bDefault)
70  {
71  // the complete attribute name to look for
72  OUString sCompleteAttributeName = GetImport().GetNamespaceMap().GetQNameByIndex(
74  OUString::createFromAscii(OAttributeMetaData::getOfficeFormsAttributeName(_eAttribute)));
75 
76  // get and convert the value
77  OUString sAttributeValue = _rxAttributes->getValueByName(sCompleteAttributeName);
78  bool bValue = _bDefault;
79  (void)::sax::Converter::convertBool(bValue, sAttributeValue);
80 
81  // set the property
82  if (_rxPropInfo->hasPropertyByName(_rPropName))
83  {
84  _rxProps->setPropertyValue(_rPropName, makeAny(bValue));
85  }
86  }
87 
88  void OFormsRootImport::StartElement( const Reference< XAttributeList >& _rxAttrList )
89  {
90  ENTER_LOG_CONTEXT( "xmloff::OFormsRootImport - importing the complete tree" );
91  SvXMLImportContext::StartElement( _rxAttrList );
92 
93  try
94  {
95  Reference< XPropertySet > xDocProperties(GetImport().GetModel(), UNO_QUERY);
96  if ( xDocProperties.is() )
97  { // an empty model is allowed: when doing a copy'n'paste from e.g. Writer to Calc,
98  // this is done via streaming the controls as XML.
99  Reference< XPropertySetInfo > xDocPropInfo;
100  if (xDocProperties.is())
101  xDocPropInfo = xDocProperties->getPropertySetInfo();
102 
103  implImportBool(_rxAttrList, ofaAutomaticFocus, xDocProperties, xDocPropInfo, PROPERTY_AUTOCONTROLFOCUS, false);
104  implImportBool(_rxAttrList, ofaApplyDesignMode, xDocProperties, xDocPropInfo, PROPERTY_APPLYDESIGNMODE, true);
105  }
106  }
107  catch(Exception&)
108  {
109  OSL_FAIL("OFormsRootImport::StartElement: caught an exception while setting the document properties!");
110  }
111  }
112 
114  {
117  }
118 
119  //= OFormsRootExport
121  {
122  addModelAttributes(_rExp);
123 
124  m_pImplElement.reset( new SvXMLElementExport(_rExp, XML_NAMESPACE_OFFICE, XML_FORMS, true, true) );
125  }
126 
128  {
129  }
130 
132  const Reference< XPropertySet >& _rxProps, const Reference< XPropertySetInfo >& _rxPropInfo,
133  const OUString& _rPropName, bool _bDefault)
134  {
135  // retrieve the property value
136  bool bValue = _bDefault;
137  if (_rxPropInfo->hasPropertyByName(_rPropName))
138  bValue = ::cppu::any2bool(_rxProps->getPropertyValue(_rPropName));
139 
140  // convert into a string
141  OUStringBuffer aValue;
142  ::sax::Converter::convertBool(aValue, bValue);
143 
144  // add the attribute
145  _rExp.AddAttribute(
148  aValue.makeStringAndClear());
149  }
150 
152  {
153  try
154  {
155  Reference< XPropertySet > xDocProperties(_rExp.GetModel(), UNO_QUERY);
156  if ( xDocProperties.is() )
157  { // an empty model is allowed: when doing a copy'n'paste from e.g. Writer to Calc,
158  // this is done via streaming the controls as XML.
159  Reference< XPropertySetInfo > xDocPropInfo;
160  if (xDocProperties.is())
161  xDocPropInfo = xDocProperties->getPropertySetInfo();
162 
163  implExportBool(_rExp, ofaAutomaticFocus, xDocProperties, xDocPropInfo, PROPERTY_AUTOCONTROLFOCUS, false);
164  implExportBool(_rExp, ofaApplyDesignMode, xDocProperties, xDocPropInfo, PROPERTY_APPLYDESIGNMODE, true);
165  }
166  }
167  catch(Exception&)
168  {
169  OSL_FAIL("OFormsRootExport::addModelAttributes: caught an exception while retrieving the document properties!");
170  }
171  }
172 
173 } // namespace xmloff
174 
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &_rxAttrList) override
StartElement is called after a context has been constructed and before an elements context is parsed...
Definition: officeforms.cxx:88
virtual void EndElement()
EndElement is called before a context will be destructed, but after an elements context has been pars...
Definition: xmlictxt.cxx:62
rtl::Reference< ::xmloff::OFormLayerXMLImport > const & GetFormImport()
Definition: xmlimp.hxx:618
static const char * getOfficeFormsAttributeName(OfficeFormsAttributes _eAttrib)
calculates the xml attribute representation of an attribute of the office:forms element ...
static bool convertBool(bool &rBool, const OUString &rString)
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
Definition: officeforms.cxx:53
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
virtual ~OFormsRootImport() override
Definition: officeforms.cxx:49
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:397
#define LEAVE_LOG_CONTEXT()
Definition: logging.hxx:54
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList)
StartElement is called after a context has been constructed and before an elements context is parsed...
Definition: xmlictxt.cxx:58
virtual void EndElement() override
EndElement is called before a context will be destructed, but after an elements context has been pars...
static void addModelAttributes(SvXMLExport &_rExp)
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlexp.hxx:415
OFormsRootExport(SvXMLExport &_rExp)
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:926
#define PROPERTY_AUTOCONTROLFOCUS
Definition: strings.hxx:109
#define DBG_UNHANDLED_EXCEPTION(...)
static sal_uInt16 getOfficeFormsAttributeNamespace()
calculates the xml namedspace key of an attribute of the office:forms element
void implImportBool(const css::uno::Reference< css::xml::sax::XAttributeList > &_rxAttributes, OfficeFormsAttributes _eAttribute, const css::uno::Reference< css::beans::XPropertySet > &_rxProps, const css::uno::Reference< css::beans::XPropertySetInfo > &_rxPropInfo, const OUString &_rPropName, bool _bDefault)
Definition: officeforms.cxx:67
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
static void implExportBool(SvXMLExport &_rExp, OfficeFormsAttributes _eAttribute, const css::uno::Reference< css::beans::XPropertySet > &_rxProps, const css::uno::Reference< css::beans::XPropertySetInfo > &_rxPropInfo, const OUString &_rPropName, bool _bDefault)
std::unique_ptr< SvXMLElementExport > m_pImplElement
Definition: officeforms.hxx:67
OFormsRootImport(SvXMLImport &_rImport, sal_uInt16 _nPrfx, const OUString &_rLocalName)
Definition: officeforms.cxx:44
#define ENTER_LOG_CONTEXT(name)
Definition: logging.hxx:53
#define PROPERTY_APPLYDESIGNMODE
Definition: strings.hxx:110
OUString GetQNameByIndex(sal_uInt16 nIdx, const OUString &rLocalName) const
Definition: nmspmap.cxx:407
bool any2bool(const css::uno::Any &rAny)
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
Definition: xmlnmspe.hxx:29
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)