xref: /trunk/main/xmloff/source/forms/officeforms.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include "officeforms.hxx"
31 #include <xmloff/xmluconv.hxx>
32 #include <xmloff/xmltoken.hxx>
33 #include "xmloff/xmlnmspe.hxx"
34 #include <xmloff/xmlexp.hxx>
35 #include <xmloff/xmlimp.hxx>
36 #include <xmloff/nmspmap.hxx>
37 #include <comphelper/extract.hxx>
38 #include "strings.hxx"
39 #include <rtl/logfile.hxx>
40 
41 //.........................................................................
42 namespace xmloff
43 {
44 //.........................................................................
45 
46     using namespace ::com::sun::star::uno;
47     using namespace ::com::sun::star::beans;
48     using namespace ::com::sun::star::frame;
49     using namespace ::com::sun::star::xml;
50     using ::xmloff::token::XML_FORMS;
51 
52     //=========================================================================
53     //= OFormsRootImport
54     //=========================================================================
55     TYPEINIT1(OFormsRootImport, SvXMLImportContext);
56     //-------------------------------------------------------------------------
57     OFormsRootImport::OFormsRootImport( SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLocalName )
58         :SvXMLImportContext(rImport, nPrfx, rLocalName)
59     {
60     }
61 
62     //-------------------------------------------------------------------------
63     OFormsRootImport::~OFormsRootImport()
64     {
65     }
66 
67     //-------------------------------------------------------------------------
68     SvXMLImportContext* OFormsRootImport::CreateChildContext( sal_uInt16 _nPrefix, const ::rtl::OUString& _rLocalName,
69             const Reference< sax::XAttributeList>& xAttrList )
70     {
71         return GetImport().GetFormImport()->createContext( _nPrefix, _rLocalName, xAttrList );
72     }
73 
74     //-------------------------------------------------------------------------
75     void OFormsRootImport::implImportBool(const Reference< sax::XAttributeList >& _rxAttributes, OfficeFormsAttributes _eAttribute,
76             const Reference< XPropertySet >& _rxProps, const Reference< XPropertySetInfo >& _rxPropInfo,
77             const ::rtl::OUString& _rPropName, sal_Bool _bDefault)
78     {
79         // the complete attribute name to look for
80         ::rtl::OUString sCompleteAttributeName = GetImport().GetNamespaceMap().GetQNameByIndex(
81             OAttributeMetaData::getOfficeFormsAttributeNamespace(_eAttribute),
82             ::rtl::OUString::createFromAscii(OAttributeMetaData::getOfficeFormsAttributeName(_eAttribute)));
83 
84         // get and convert the value
85         ::rtl::OUString sAttributeValue = _rxAttributes->getValueByName(sCompleteAttributeName);
86         sal_Bool bValue = _bDefault;
87         GetImport().GetMM100UnitConverter().convertBool(bValue, sAttributeValue);
88 
89         // set the property
90         if (_rxPropInfo->hasPropertyByName(_rPropName))
91             _rxProps->setPropertyValue(_rPropName, ::cppu::bool2any(bValue));
92     }
93 
94     //-------------------------------------------------------------------------
95     void OFormsRootImport::StartElement( const Reference< sax::XAttributeList >& _rxAttrList )
96     {
97         ENTER_LOG_CONTEXT( "xmloff::OFormsRootImport - importing the complete tree" );
98         SvXMLImportContext::StartElement( _rxAttrList );
99 
100         try
101         {
102             Reference< XPropertySet > xDocProperties(GetImport().GetModel(), UNO_QUERY);
103             if ( xDocProperties.is() )
104             {   // an empty model is allowed: when doing a copy'n'paste from e.g. Writer to Calc,
105                 // this is done via streaming the controls as XML.
106                 Reference< XPropertySetInfo > xDocPropInfo;
107                 if (xDocProperties.is())
108                     xDocPropInfo = xDocProperties->getPropertySetInfo();
109 
110                 implImportBool(_rxAttrList, ofaAutomaticFocus, xDocProperties, xDocPropInfo, PROPERTY_AUTOCONTROLFOCUS, sal_False);
111                 implImportBool(_rxAttrList, ofaApplyDesignMode, xDocProperties, xDocPropInfo, PROPERTY_APPLYDESIGNMODE, sal_True);
112             }
113         }
114         catch(Exception&)
115         {
116             OSL_ENSURE(sal_False, "OFormsRootImport::StartElement: caught an exception while setting the document properties!");
117         }
118     }
119 
120     //-------------------------------------------------------------------------
121     void OFormsRootImport::EndElement()
122     {
123         SvXMLImportContext::EndElement();
124         LEAVE_LOG_CONTEXT( );
125     }
126 
127     //=====================================================================
128     //= OFormsRootExport
129     //=====================================================================
130     //---------------------------------------------------------------------
131     OFormsRootExport::OFormsRootExport( SvXMLExport& _rExp )
132         :m_pImplElement(NULL)
133     {
134         addModelAttributes(_rExp);
135 
136         m_pImplElement = new SvXMLElementExport(_rExp, XML_NAMESPACE_OFFICE, XML_FORMS, sal_True, sal_True);
137     }
138 
139     //---------------------------------------------------------------------
140     OFormsRootExport::~OFormsRootExport( )
141     {
142         delete m_pImplElement;
143     }
144 
145     //-------------------------------------------------------------------------
146     void OFormsRootExport::implExportBool(SvXMLExport& _rExp, OfficeFormsAttributes _eAttribute,
147         const Reference< XPropertySet >& _rxProps, const Reference< XPropertySetInfo >& _rxPropInfo,
148         const ::rtl::OUString& _rPropName, sal_Bool _bDefault)
149     {
150         // retrieve the property value
151         sal_Bool bValue = _bDefault;
152         if (_rxPropInfo->hasPropertyByName(_rPropName))
153             bValue = ::cppu::any2bool(_rxProps->getPropertyValue(_rPropName));
154 
155         // convert into a string
156         ::rtl::OUStringBuffer aValue;
157         _rExp.GetMM100UnitConverter().convertBool(aValue, bValue);
158 
159         // add the attribute
160         _rExp.AddAttribute(
161             OAttributeMetaData::getOfficeFormsAttributeNamespace(_eAttribute),
162             OAttributeMetaData::getOfficeFormsAttributeName(_eAttribute),
163             aValue.makeStringAndClear());
164     }
165 
166     //-------------------------------------------------------------------------
167     void OFormsRootExport::addModelAttributes(SvXMLExport& _rExp) SAL_THROW(())
168     {
169         try
170         {
171             Reference< XPropertySet > xDocProperties(_rExp.GetModel(), UNO_QUERY);
172             if ( xDocProperties.is() )
173             {   // an empty model is allowed: when doing a copy'n'paste from e.g. Writer to Calc,
174                 // this is done via streaming the controls as XML.
175                 Reference< XPropertySetInfo > xDocPropInfo;
176                 if (xDocProperties.is())
177                     xDocPropInfo = xDocProperties->getPropertySetInfo();
178 
179                 implExportBool(_rExp, ofaAutomaticFocus, xDocProperties, xDocPropInfo, PROPERTY_AUTOCONTROLFOCUS, sal_False);
180                 implExportBool(_rExp, ofaApplyDesignMode, xDocProperties, xDocPropInfo, PROPERTY_APPLYDESIGNMODE, sal_True);
181             }
182         }
183         catch(Exception&)
184         {
185             OSL_ENSURE(sal_False, "OFormsRootExport::addModelAttributes: caught an exception while retrieving the document properties!");
186         }
187     }
188 
189 //.........................................................................
190 }   // namespace xmloff
191 //.........................................................................
192 
193 
194