Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / xmloff / source / forms / officeforms.cxx
blobbf272678c2157a27778833eddd3207a9f1cc32d3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "officeforms.hxx"
22 #include <sax/tools/converter.hxx>
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 <comphelper/extract.hxx>
30 #include "strings.hxx"
32 namespace xmloff
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::frame;
38 using namespace ::com::sun::star::xml;
39 using ::xmloff::token::XML_FORMS;
40 using ::com::sun::star::xml::sax::XAttributeList;
42 //= OFormsRootImport
43 OFormsRootImport::OFormsRootImport( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLocalName )
44 :SvXMLImportContext(rImport, nPrfx, rLocalName)
48 OFormsRootImport::~OFormsRootImport()
52 SvXMLImportContext* OFormsRootImport::CreateChildContext( sal_uInt16 _nPrefix, const OUString& _rLocalName,
53 const Reference< XAttributeList>& xAttrList )
55 SvXMLImportContext* pRet = nullptr;
56 try
58 pRet = GetImport().GetFormImport()->createContext( _nPrefix, _rLocalName, xAttrList );
59 } catch (const Exception& rException)
61 SAL_WARN("xmloff.forms", "OFormsRootImport::CreateChildContext: " << rException.Message);
63 return pRet;
66 void OFormsRootImport::implImportBool(const Reference< XAttributeList >& _rxAttributes, OfficeFormsAttributes _eAttribute,
67 const Reference< XPropertySet >& _rxProps, const Reference< XPropertySetInfo >& _rxPropInfo,
68 const OUString& _rPropName, bool _bDefault)
70 // the complete attribute name to look for
71 OUString sCompleteAttributeName = GetImport().GetNamespaceMap().GetQNameByIndex(
72 OAttributeMetaData::getOfficeFormsAttributeNamespace(_eAttribute),
73 OUString::createFromAscii(OAttributeMetaData::getOfficeFormsAttributeName(_eAttribute)));
75 // get and convert the value
76 OUString sAttributeValue = _rxAttributes->getValueByName(sCompleteAttributeName);
77 bool bValue = _bDefault;
78 ::sax::Converter::convertBool(bValue, sAttributeValue);
80 // set the property
81 if (_rxPropInfo->hasPropertyByName(_rPropName))
83 _rxProps->setPropertyValue(_rPropName, makeAny(bValue));
87 void OFormsRootImport::StartElement( const Reference< XAttributeList >& _rxAttrList )
89 ENTER_LOG_CONTEXT( "xmloff::OFormsRootImport - importing the complete tree" );
90 SvXMLImportContext::StartElement( _rxAttrList );
92 try
94 Reference< XPropertySet > xDocProperties(GetImport().GetModel(), UNO_QUERY);
95 if ( xDocProperties.is() )
96 { // an empty model is allowed: when doing a copy'n'paste from e.g. Writer to Calc,
97 // this is done via streaming the controls as XML.
98 Reference< XPropertySetInfo > xDocPropInfo;
99 if (xDocProperties.is())
100 xDocPropInfo = xDocProperties->getPropertySetInfo();
102 implImportBool(_rxAttrList, ofaAutomaticFocus, xDocProperties, xDocPropInfo, PROPERTY_AUTOCONTROLFOCUS, false);
103 implImportBool(_rxAttrList, ofaApplyDesignMode, xDocProperties, xDocPropInfo, PROPERTY_APPLYDESIGNMODE, true);
106 catch(Exception&)
108 OSL_FAIL("OFormsRootImport::StartElement: caught an exception while setting the document properties!");
112 void OFormsRootImport::EndElement()
114 SvXMLImportContext::EndElement();
115 LEAVE_LOG_CONTEXT( );
118 //= OFormsRootExport
119 OFormsRootExport::OFormsRootExport( SvXMLExport& _rExp )
120 :m_pImplElement(nullptr)
122 addModelAttributes(_rExp);
124 m_pImplElement = new SvXMLElementExport(_rExp, XML_NAMESPACE_OFFICE, XML_FORMS, true, true);
127 OFormsRootExport::~OFormsRootExport( )
129 delete m_pImplElement;
132 void OFormsRootExport::implExportBool(SvXMLExport& _rExp, OfficeFormsAttributes _eAttribute,
133 const Reference< XPropertySet >& _rxProps, const Reference< XPropertySetInfo >& _rxPropInfo,
134 const OUString& _rPropName, bool _bDefault)
136 // retrieve the property value
137 bool bValue = _bDefault;
138 if (_rxPropInfo->hasPropertyByName(_rPropName))
139 bValue = ::cppu::any2bool(_rxProps->getPropertyValue(_rPropName));
141 // convert into a string
142 OUStringBuffer aValue;
143 ::sax::Converter::convertBool(aValue, bValue);
145 // add the attribute
146 _rExp.AddAttribute(
147 OAttributeMetaData::getOfficeFormsAttributeNamespace(_eAttribute),
148 OAttributeMetaData::getOfficeFormsAttributeName(_eAttribute),
149 aValue.makeStringAndClear());
152 void OFormsRootExport::addModelAttributes(SvXMLExport& _rExp)
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();
164 implExportBool(_rExp, ofaAutomaticFocus, xDocProperties, xDocPropInfo, PROPERTY_AUTOCONTROLFOCUS, false);
165 implExportBool(_rExp, ofaApplyDesignMode, xDocProperties, xDocPropInfo, PROPERTY_APPLYDESIGNMODE, true);
168 catch(Exception&)
170 OSL_FAIL("OFormsRootExport::addModelAttributes: caught an exception while retrieving the document properties!");
174 } // namespace xmloff
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */