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/xmlnamespace.hxx>
26 #include <xmloff/xmlexp.hxx>
27 #include <xmloff/xmlimp.hxx>
28 #include <xmloff/namespacemap.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  using ::com::sun::star::xml::sax::XFastAttributeList;
43 
44  //= OFormsRootImport
46  :SvXMLImportContext(rImport)
47  {
48  }
49 
51  {
52  }
53 
54  css::uno::Reference< css::xml::sax::XFastContextHandler > OFormsRootImport::createFastChildContext(
55  sal_Int32 _nElement,
56  const css::uno::Reference< css::xml::sax::XFastAttributeList>& xAttrList )
57  {
58  SvXMLImportContext* pRet = nullptr;
59  try
60  {
61  pRet = GetImport().GetFormImport()->createContext( _nElement, xAttrList );
62  } catch (const Exception&)
63  {
64  DBG_UNHANDLED_EXCEPTION("xmloff.forms");
65  }
66  return pRet;
67  }
68 
69  void OFormsRootImport::implImportBool(const Reference< XFastAttributeList >& _rxAttributes, OfficeFormsAttributes _eAttribute,
70  const Reference< XPropertySet >& _rxProps, const Reference< XPropertySetInfo >& _rxPropInfo,
71  const OUString& _rPropName, bool _bDefault)
72  {
73  // the complete attribute name to look for
74  sal_Int32 nCompleteAttributeName = XML_ELEMENT(
75  FORM,
77 
78  // get and convert the value
79  OUString sAttributeValue = _rxAttributes->getOptionalValue(nCompleteAttributeName);
80  bool bValue = _bDefault;
81  (void)::sax::Converter::convertBool(bValue, sAttributeValue);
82 
83  // set the property
84  if (_rxPropInfo->hasPropertyByName(_rPropName))
85  {
86  _rxProps->setPropertyValue(_rPropName, makeAny(bValue));
87  }
88  }
89 
90  void OFormsRootImport::startFastElement( sal_Int32 /*nElement*/, const Reference< XFastAttributeList >& _rxAttrList )
91  {
92  ENTER_LOG_CONTEXT( "xmloff::OFormsRootImport - importing the complete tree" );
93 
94  try
95  {
96  Reference< XPropertySet > xDocProperties(GetImport().GetModel(), UNO_QUERY);
97  if ( xDocProperties.is() )
98  { // an empty model is allowed: when doing a copy'n'paste from e.g. Writer to Calc,
99  // this is done via streaming the controls as XML.
100  Reference< XPropertySetInfo > xDocPropInfo;
101  if (xDocProperties.is())
102  xDocPropInfo = xDocProperties->getPropertySetInfo();
103 
104  implImportBool(_rxAttrList, ofaAutomaticFocus, xDocProperties, xDocPropInfo, PROPERTY_AUTOCONTROLFOCUS, false);
105  implImportBool(_rxAttrList, ofaApplyDesignMode, xDocProperties, xDocPropInfo, PROPERTY_APPLYDESIGNMODE, true);
106  }
107  }
108  catch(Exception&)
109  {
110  TOOLS_WARN_EXCEPTION("xmloff.forms",
111  "caught an exception while setting the document properties!");
112  }
113  }
114 
116  {
118  }
119 
120  //= OFormsRootExport
122  {
123  addModelAttributes(_rExp);
124 
125  m_pImplElement.reset( new SvXMLElementExport(_rExp, XML_NAMESPACE_OFFICE, XML_FORMS, true, true) );
126  }
127 
129  {
130  }
131 
133  const Reference< XPropertySet >& _rxProps, const Reference< XPropertySetInfo >& _rxPropInfo,
134  const OUString& _rPropName, bool _bDefault)
135  {
136  // retrieve the property value
137  bool bValue = _bDefault;
138  if (_rxPropInfo->hasPropertyByName(_rPropName))
139  bValue = ::cppu::any2bool(_rxProps->getPropertyValue(_rPropName));
140 
141  // convert into a string
142  OUStringBuffer aValue;
143  ::sax::Converter::convertBool(aValue, bValue);
144 
145  // add the attribute
146  _rExp.AddAttribute(
149  aValue.makeStringAndClear());
150  }
151 
153  {
154  try
155  {
156  Reference< XPropertySet > xDocProperties(_rExp.GetModel(), UNO_QUERY);
157  if ( xDocProperties.is() )
158  { // an empty model is allowed: when doing a copy'n'paste from e.g. Writer to Calc,
159  // this is done via streaming the controls as XML.
160  Reference< XPropertySetInfo > xDocPropInfo;
161  if (xDocProperties.is())
162  xDocPropInfo = xDocProperties->getPropertySetInfo();
163 
164  implExportBool(_rExp, ofaAutomaticFocus, xDocProperties, xDocPropInfo, PROPERTY_AUTOCONTROLFOCUS, false);
165  implExportBool(_rExp, ofaApplyDesignMode, xDocProperties, xDocPropInfo, PROPERTY_APPLYDESIGNMODE, true);
166  }
167  }
168  catch(Exception&)
169  {
170  TOOLS_WARN_EXCEPTION("xmloff.forms",
171  "caught an exception while retrieving the document properties!");
172  }
173  }
174 
175 } // namespace xmloff
176 
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
rtl::Reference< ::xmloff::OFormLayerXMLImport > const & GetFormImport()
Definition: xmlimp.hxx:624
static const char * getOfficeFormsAttributeName(OfficeFormsAttributes _eAttrib)
calculates the xml attribute representation of an attribute of the office:forms element ...
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
static xmloff::token::XMLTokenEnum getOfficeFormsAttributeToken(OfficeFormsAttributes _eAttrib)
virtual ~OFormsRootImport() override
Definition: officeforms.cxx:50
#define LEAVE_LOG_CONTEXT()
Definition: logging.hxx:53
static void addModelAttributes(SvXMLExport &_rExp)
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlexp.hxx:420
OFormsRootExport(SvXMLExport &_rExp)
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:938
#define PROPERTY_AUTOCONTROLFOCUS
Definition: strings.hxx:108
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
#define DBG_UNHANDLED_EXCEPTION(...)
#define TOOLS_WARN_EXCEPTION(area, stream)
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
Definition: officeforms.cxx:54
static sal_uInt16 getOfficeFormsAttributeNamespace()
calculates the xml namedspace key of an attribute of the office:forms element
static bool convertBool(bool &rBool, std::u16string_view rString)
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:68
FORM
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
#define ENTER_LOG_CONTEXT(name)
Definition: logging.hxx:52
OFormsRootImport(SvXMLImport &_rImport)
Definition: officeforms.cxx:45
#define PROPERTY_APPLYDESIGNMODE
Definition: strings.hxx:109
static void implImportBool(const css::uno::Reference< css::xml::sax::XFastAttributeList > &_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:69
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
Definition: officeforms.cxx:90
bool any2bool(const css::uno::Any &rAny)
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)