From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- xmloff/source/xforms/SchemaContext.cxx | 62 ++ xmloff/source/xforms/SchemaContext.hxx | 53 ++ xmloff/source/xforms/SchemaRestrictionContext.cxx | 335 +++++++++ xmloff/source/xforms/SchemaRestrictionContext.hxx | 60 ++ xmloff/source/xforms/SchemaSimpleTypeContext.cxx | 74 ++ xmloff/source/xforms/SchemaSimpleTypeContext.hxx | 54 ++ xmloff/source/xforms/TokenContext.cxx | 90 +++ xmloff/source/xforms/TokenContext.hxx | 74 ++ xmloff/source/xforms/XFormsBindContext.cxx | 155 +++++ xmloff/source/xforms/XFormsBindContext.hxx | 59 ++ xmloff/source/xforms/XFormsInstanceContext.cxx | 162 +++++ xmloff/source/xforms/XFormsInstanceContext.hxx | 69 ++ xmloff/source/xforms/XFormsModelContext.cxx | 110 +++ xmloff/source/xforms/XFormsModelContext.hxx | 54 ++ xmloff/source/xforms/XFormsModelExport.hxx | 33 + xmloff/source/xforms/XFormsSubmissionContext.cxx | 137 ++++ xmloff/source/xforms/XFormsSubmissionContext.hxx | 54 ++ xmloff/source/xforms/xformsapi.cxx | 296 ++++++++ xmloff/source/xforms/xformsapi.hxx | 83 +++ xmloff/source/xforms/xformsexport.cxx | 802 ++++++++++++++++++++++ xmloff/source/xforms/xformsimport.cxx | 184 +++++ 21 files changed, 3000 insertions(+) create mode 100644 xmloff/source/xforms/SchemaContext.cxx create mode 100644 xmloff/source/xforms/SchemaContext.hxx create mode 100644 xmloff/source/xforms/SchemaRestrictionContext.cxx create mode 100644 xmloff/source/xforms/SchemaRestrictionContext.hxx create mode 100644 xmloff/source/xforms/SchemaSimpleTypeContext.cxx create mode 100644 xmloff/source/xforms/SchemaSimpleTypeContext.hxx create mode 100644 xmloff/source/xforms/TokenContext.cxx create mode 100644 xmloff/source/xforms/TokenContext.hxx create mode 100644 xmloff/source/xforms/XFormsBindContext.cxx create mode 100644 xmloff/source/xforms/XFormsBindContext.hxx create mode 100644 xmloff/source/xforms/XFormsInstanceContext.cxx create mode 100644 xmloff/source/xforms/XFormsInstanceContext.hxx create mode 100644 xmloff/source/xforms/XFormsModelContext.cxx create mode 100644 xmloff/source/xforms/XFormsModelContext.hxx create mode 100644 xmloff/source/xforms/XFormsModelExport.hxx create mode 100644 xmloff/source/xforms/XFormsSubmissionContext.cxx create mode 100644 xmloff/source/xforms/XFormsSubmissionContext.hxx create mode 100644 xmloff/source/xforms/xformsapi.cxx create mode 100644 xmloff/source/xforms/xformsapi.hxx create mode 100644 xmloff/source/xforms/xformsexport.cxx create mode 100644 xmloff/source/xforms/xformsimport.cxx (limited to 'xmloff/source/xforms') diff --git a/xmloff/source/xforms/SchemaContext.cxx b/xmloff/source/xforms/SchemaContext.cxx new file mode 100644 index 000000000..573805a25 --- /dev/null +++ b/xmloff/source/xforms/SchemaContext.cxx @@ -0,0 +1,62 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "SchemaContext.hxx" + +#include "SchemaSimpleTypeContext.hxx" + +#include +#include +#include +#include +#include + +#include + +using com::sun::star::uno::Reference; +using com::sun::star::xml::sax::XFastAttributeList; +using com::sun::star::xforms::XDataTypeRepository; +using namespace xmloff::token; + + +SchemaContext::SchemaContext( + SvXMLImport& rImport, + const Reference& rRepository ) : + TokenContext( rImport ), + mxRepository( rRepository ) +{ +} + +void SchemaContext::HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & ) +{ +} + +SvXMLImportContext* SchemaContext::HandleChild( + sal_Int32 nElementToken, + const Reference& ) +{ + if ( nElementToken == XML_ELEMENT(XSD, XML_SIMPLETYPE) ) + return new SchemaSimpleTypeContext( GetImport(), mxRepository ); + else + XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElementToken); + return nullptr; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/SchemaContext.hxx b/xmloff/source/xforms/SchemaContext.hxx new file mode 100644 index 000000000..e6896e760 --- /dev/null +++ b/xmloff/source/xforms/SchemaContext.hxx @@ -0,0 +1,53 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include "TokenContext.hxx" +#include + +namespace com::sun::star { + namespace xml::sax { class XAttributeList; } + namespace beans { class XPropertySet; } + namespace xforms { class XDataTypeRepository; } +} + +class SvXMLImport; +class SvXMLImportContext; + +/** import the data type declarations from an xsd:schema element */ +class SchemaContext : public TokenContext +{ + css::uno::Reference mxRepository; + +public: + SchemaContext( SvXMLImport& rImport, + const css::uno::Reference& rRepository ); + + // implement TokenContext methods: + +protected: + virtual void HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) override; + + virtual SvXMLImportContext* HandleChild( + sal_Int32 nElementToken, + const css::uno::Reference& xAttrList ) override; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/SchemaRestrictionContext.cxx b/xmloff/source/xforms/SchemaRestrictionContext.cxx new file mode 100644 index 000000000..2785ad9e9 --- /dev/null +++ b/xmloff/source/xforms/SchemaRestrictionContext.cxx @@ -0,0 +1,335 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "SchemaRestrictionContext.hxx" +#include "xformsapi.hxx" + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +using com::sun::star::uno::Reference; +using com::sun::star::uno::Exception; +using com::sun::star::uno::Any; +using namespace com::sun::star; +using com::sun::star::util::Duration; +using com::sun::star::xml::sax::XFastAttributeList; +using com::sun::star::xforms::XDataTypeRepository; +using namespace xmloff::token; + + +SchemaRestrictionContext::SchemaRestrictionContext( + SvXMLImport& rImport, + Reference const & rRepository, + const OUString& sTypeName ) : + TokenContext( rImport ), + mxRepository( rRepository ), + msTypeName( sTypeName ) +{ + SAL_WARN_IF( !mxRepository.is(), "xmloff", "need repository" ); +} + +void SchemaRestrictionContext::CreateDataType() +{ + // only do something if we don't have a data type already + if( mxDataType.is() ) + return; + + SAL_WARN_IF( msBaseName.isEmpty(), "xmloff", "no base name?" ); + SAL_WARN_IF( !mxRepository.is(), "xmloff", "no repository?" ); + + try + { + mxDataType = + mxRepository->cloneDataType( + xforms_getBasicTypeName( mxRepository, + GetImport().GetNamespaceMap(), + msBaseName ), + msTypeName ); + } + catch( const Exception& ) + { + TOOLS_WARN_EXCEPTION("xmloff", "exception during type creation"); + } + SAL_WARN_IF( !mxDataType.is(), "xmloff", "can't create type" ); +} + +void SchemaRestrictionContext::HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) +{ + switch (aIter.getToken() & TOKEN_MASK) + { + case XML_BASE: + msBaseName = aIter.toString(); + break; + } +} + +typedef Any (*convert_t)( const OUString& ); + +static Any xforms_string( const OUString& rValue ) +{ + return Any( rValue ); +} + +static Any xforms_int32( const OUString& rValue ) +{ + sal_Int32 nValue; + bool bSuccess = ::sax::Converter::convertNumber( nValue, rValue ); + return bSuccess ? Any( nValue ) : Any(); +} + +static Any xforms_int16( const OUString& rValue ) +{ + sal_Int32 nValue; + bool bSuccess = ::sax::Converter::convertNumber( nValue, rValue ); + return bSuccess ? Any( static_cast( nValue ) ) : Any(); +} + +static Any xforms_whitespace( const OUString& rValue ) +{ + Any aValue; + if( IsXMLToken( rValue, XML_PRESERVE ) ) + aValue <<= css::xsd::WhiteSpaceTreatment::Preserve; + else if( IsXMLToken( rValue, XML_REPLACE ) ) + aValue <<= css::xsd::WhiteSpaceTreatment::Replace; + else if( IsXMLToken( rValue, XML_COLLAPSE ) ) + aValue <<= css::xsd::WhiteSpaceTreatment::Collapse; + return aValue; +} + +static Any xforms_double( const OUString& rValue ) +{ + double fValue; + bool bSuccess = ::sax::Converter::convertDouble( fValue, rValue ); + return bSuccess ? Any( fValue ) : Any(); +} + +static Any xforms_date( const OUString& rValue ) +{ + Any aAny; + + // parse ISO date + sal_Int32 nPos1 = rValue.indexOf( '-' ); + sal_Int32 nPos2 = rValue.indexOf( '-', nPos1 + 1 ); + if( nPos1 > 0 && nPos2 > 0 ) + { + util::Date aDate; + aDate.Year = static_cast( + o3tl::toInt32(rValue.subView( 0, nPos1 )) ); + aDate.Month = static_cast( + o3tl::toInt32(rValue.subView( nPos1 + 1, nPos2 - nPos1 - 1 )) ); + aDate.Day = static_cast( + o3tl::toInt32(rValue.subView( nPos2 + 1 )) ); + aAny <<= aDate; + } + return aAny; +} + +static Any xforms_dateTime( const OUString& rValue ) +{ + util::DateTime aDateTime; + bool const bSuccess = ::sax::Converter::parseDateTime(aDateTime, rValue); + return bSuccess ? Any( aDateTime ) : Any(); +} + +static Any xforms_time( const OUString& rValue ) +{ + Any aAny; + Duration aDuration; + if (::sax::Converter::convertDuration( aDuration, rValue )) + { + css::util::Time aTime; + aTime.Hours = aDuration.Hours; + aTime.Minutes = aDuration.Minutes; + aTime.Seconds = aDuration.Seconds; + aTime.NanoSeconds = aDuration.NanoSeconds; + aAny <<= aTime; + } + return aAny; +} + + +SvXMLImportContext* SchemaRestrictionContext::HandleChild( + sal_Int32 nElementToken, + const Reference& xAttrList ) +{ + // find value + OUString sValue; + for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) ) + { + if( ( aIter.getToken() & TOKEN_MASK) == XML_VALUE ) + { + sValue = aIter.toString(); + break; + } + } + + // determine property name + suitable converter + OUString sPropertyName; + convert_t pConvert = nullptr; + switch( nElementToken & TOKEN_MASK ) + { + case XML_LENGTH: + sPropertyName = "Length"; + pConvert = &xforms_int32; + break; + case XML_MINLENGTH: + sPropertyName = "MinLength"; + pConvert = &xforms_int32; + break; + case XML_MAXLENGTH: + sPropertyName = "MaxLength"; + pConvert = &xforms_int32; + break; + case XML_TOTALDIGITS: + sPropertyName = "TotalDigits"; + pConvert = &xforms_int32; + break; + case XML_FRACTIONDIGITS: + sPropertyName = "FractionDigits"; + pConvert = &xforms_int32; + break; + case XML_PATTERN: + sPropertyName = "Pattern"; + pConvert = &xforms_string; + break; + case XML_WHITESPACE: + sPropertyName = "WhiteSpace"; + pConvert = &xforms_whitespace; + break; + case XML_MININCLUSIVE: + case XML_MINEXCLUSIVE: + case XML_MAXINCLUSIVE: + case XML_MAXEXCLUSIVE: + { + // these attributes are mapped to different properties. + // To determine the property name, we use an attribute + // dependent prefix and a type dependent suffix. The + // converter is only type dependent. + + // first, attribute-dependent prefix + switch( nElementToken & TOKEN_MASK ) + { + case XML_MININCLUSIVE: + sPropertyName = "MinInclusive"; + break; + case XML_MINEXCLUSIVE: + sPropertyName = "MinExclusive"; + break; + case XML_MAXINCLUSIVE: + sPropertyName = "MaxInclusive"; + break; + case XML_MAXEXCLUSIVE: + sPropertyName = "MaxExclusive"; + break; + } + + // second, type-dependent suffix + converter + switch( xforms_getTypeClass( mxRepository, + GetImport().GetNamespaceMap(), + msBaseName ) ) + { + case css::xsd::DataTypeClass::DECIMAL: + case css::xsd::DataTypeClass::DOUBLE: + case css::xsd::DataTypeClass::FLOAT: + sPropertyName += "Double"; + pConvert = &xforms_double; + break; + case css::xsd::DataTypeClass::DATETIME: + sPropertyName += "DateTime"; + pConvert = &xforms_dateTime; + break; + case css::xsd::DataTypeClass::DATE: + sPropertyName += "Date"; + pConvert = &xforms_date; + break; + case css::xsd::DataTypeClass::TIME: + sPropertyName += "Time"; + pConvert = &xforms_time; + break; + case css::xsd::DataTypeClass::gYear: + case css::xsd::DataTypeClass::gDay: + case css::xsd::DataTypeClass::gMonth: + sPropertyName += "Int"; + pConvert = &xforms_int16; + break; + + case css::xsd::DataTypeClass::STRING: + case css::xsd::DataTypeClass::anyURI: + case css::xsd::DataTypeClass::BOOLEAN: + // invalid: These shouldn't have min/max-inclusive + break; + + /* data types not yet supported: + case css::xsd::DataTypeClass::DURATION: + case css::xsd::DataTypeClass::gYearMonth: + case css::xsd::DataTypeClass::gMonthDay: + case css::xsd::DataTypeClass::hexBinary: + case css::xsd::DataTypeClass::base64Binary: + case css::xsd::DataTypeClass::QName: + case css::xsd::DataTypeClass::NOTATION: + */ + } + } + break; + + default: + OSL_FAIL( "unknown facet" ); + } + + // finally, set the property + CreateDataType(); + if( mxDataType.is() + && !sPropertyName.isEmpty() + && pConvert != nullptr + && mxDataType->getPropertySetInfo()->hasPropertyByName(sPropertyName) ) + { + try + { + mxDataType->setPropertyValue( sPropertyName, pConvert( sValue ) ); + } + catch( const Exception& ) + { + ; // can't set property? Then ignore. + } + } + + return new SvXMLImportContext( GetImport() ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/SchemaRestrictionContext.hxx b/xmloff/source/xforms/SchemaRestrictionContext.hxx new file mode 100644 index 000000000..47cf4b088 --- /dev/null +++ b/xmloff/source/xforms/SchemaRestrictionContext.hxx @@ -0,0 +1,60 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include "TokenContext.hxx" +#include + +namespace com::sun::star { + namespace xml::sax { class XAttributeList; } + namespace beans { class XPropertySet; } + namespace xforms { class XDataTypeRepository; } +} + +class SvXMLImport; +class SvXMLImportContext; + +/** import the xsd:restriction element */ +class SchemaRestrictionContext : public TokenContext +{ + css::uno::Reference mxRepository; + css::uno::Reference mxDataType; + OUString const msTypeName; + OUString msBaseName; + +public: + SchemaRestrictionContext( SvXMLImport& rImport, + css::uno::Reference const & rRepository, + const OUString& sTypeName ); + +private: + // create mxDataType (if not already present) + void CreateDataType(); + + // implement TokenContext methods: + + virtual void HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) override; + + virtual SvXMLImportContext* HandleChild( + sal_Int32 nElementToken, + const css::uno::Reference& xAttrList ) override; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/SchemaSimpleTypeContext.cxx b/xmloff/source/xforms/SchemaSimpleTypeContext.cxx new file mode 100644 index 000000000..cd2451930 --- /dev/null +++ b/xmloff/source/xforms/SchemaSimpleTypeContext.cxx @@ -0,0 +1,74 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "SchemaSimpleTypeContext.hxx" + +#include "SchemaRestrictionContext.hxx" +#include +#include +#include +#include +#include + +#include +#include + +using com::sun::star::uno::Reference; +using com::sun::star::xml::sax::XFastAttributeList; +using com::sun::star::xforms::XDataTypeRepository; +using namespace xmloff::token; + + +SchemaSimpleTypeContext::SchemaSimpleTypeContext( + SvXMLImport& rImport, + const Reference& rRepository ) : + TokenContext( rImport ), + mxRepository( rRepository ) +{ +} + +void SchemaSimpleTypeContext::HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) +{ + switch (aIter.getToken() & TOKEN_MASK) + { + case XML_NAME: + msTypeName = aIter.toString(); + break; + } +} + +SvXMLImportContext* SchemaSimpleTypeContext::HandleChild( + sal_Int32 nElementToken, + const Reference& ) +{ + switch( nElementToken ) + { + case XML_ELEMENT(XSD, XML_RESTRICTION): + return new SchemaRestrictionContext( GetImport(), + mxRepository, msTypeName ); + break; + default: + XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElementToken); + } + + return nullptr; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/SchemaSimpleTypeContext.hxx b/xmloff/source/xforms/SchemaSimpleTypeContext.hxx new file mode 100644 index 000000000..3489e2a6c --- /dev/null +++ b/xmloff/source/xforms/SchemaSimpleTypeContext.hxx @@ -0,0 +1,54 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include "TokenContext.hxx" +#include + +namespace com::sun::star { + namespace xml::sax { class XAttributeList; } + namespace beans { class XPropertySet; } + namespace xforms { class XDataTypeRepository; } +} + +class SvXMLImport; +class SvXMLImportContext; + +/** import the xsd:simpleType element */ +class SchemaSimpleTypeContext : public TokenContext +{ + css::uno::Reference mxRepository; + OUString msTypeName; + +public: + SchemaSimpleTypeContext( SvXMLImport& rImport, + const css::uno::Reference& rRepository ); + + // implement TokenContext methods: + +protected: + virtual void HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) override; + + virtual SvXMLImportContext* HandleChild( + sal_Int32 nElementToken, + const css::uno::Reference& xAttrList ) override; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/TokenContext.cxx b/xmloff/source/xforms/TokenContext.cxx new file mode 100644 index 000000000..928ea8788 --- /dev/null +++ b/xmloff/source/xforms/TokenContext.cxx @@ -0,0 +1,90 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include +#include + +#include "TokenContext.hxx" +#include +#include +#include +#include + +#include + +using com::sun::star::uno::Reference; + +TokenContext::TokenContext( SvXMLImport& rImport ) + : SvXMLImportContext( rImport ) +{ +} + +void TokenContext::startFastElement( + sal_Int32 /*nElement*/, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) +{ + // iterate over attributes + // - if in map: call HandleAttribute + // - xmlns:... : ignore + // - other: warning + + for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) ) + HandleAttribute( aIter ); +} + +css::uno::Reference< css::xml::sax::XFastContextHandler > TokenContext::createFastChildContext( + sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) +{ + // call handle child, and pass down arguments + SvXMLImportContext* pContext = HandleChild( nElement, xAttrList ); + // error handling: create default context and generate warning + if( pContext == nullptr ) + { + GetImport().SetError( XMLERROR_UNKNOWN_ELEMENT, SvXMLImport::getNameFromToken( nElement ) ); + } + return pContext; +} + + css::uno::Reference< css::xml::sax::XFastContextHandler > TokenContext::createUnknownChildContext( + const OUString& Namespace, const OUString& Name, const css::uno::Reference< css::xml::sax::XFastAttributeList >& ) +{ + GetImport().SetError( XMLERROR_UNKNOWN_ELEMENT, Namespace + " " + Name ); + return nullptr; +} + +static bool lcl_IsWhiteSpace( sal_Unicode c ) +{ + return c == ' ' + || c == u'\x0009' + || c == u'\x000A' + || c == u'\x000D'; +} + +void TokenContext::characters( const OUString& rCharacters ) +{ + // get iterators for string data + const sal_Unicode* pBegin = rCharacters.getStr(); + const sal_Unicode* pEnd = &( pBegin[ rCharacters.getLength() ] ); + + // raise error if non-whitespace character is found + if( !::std::all_of( pBegin, pEnd, lcl_IsWhiteSpace ) ) + GetImport().SetError( XMLERROR_UNKNOWN_CHARACTERS, rCharacters ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/TokenContext.hxx b/xmloff/source/xforms/TokenContext.hxx new file mode 100644 index 000000000..84ed18ab5 --- /dev/null +++ b/xmloff/source/xforms/TokenContext.hxx @@ -0,0 +1,74 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include +#include + +namespace com::sun::star { + namespace xml::sax { class XFastAttributeList; } + namespace uno { template class Reference; } +} + +class SvXMLImport; + +/** handle attributes through an SvXMLTokenMap */ +class TokenContext : public SvXMLImportContext +{ +public: + TokenContext( SvXMLImport& rImport ); + + // implement SvXMLImportContext methods: + + /** call HandleAttribute for each attribute in the token map; + * create a warning for all others. Classes that wish to override + * StartElement need to call the parent method. */ + virtual void SAL_CALL startFastElement( + sal_Int32 nElement, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; + + /** call HandleChild for each child element in the token map; + * create a warning for all others. Classes that wish to override + * CreateChildContext may want to call the parent method for + * handling of defaults. */ + virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( + sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override; + + virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext( + const OUString& Namespace, const OUString& Name, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& Attribs ) override; + + /** Create a warning for all non-namespace character + * content. Classes that wish to deal with character content have + * to override this method anyway, and will thus get rid of the + * warnings. */ + virtual void SAL_CALL characters( const OUString& rChars ) override; + +protected: + /** will be called for each attribute */ + virtual void HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) = 0; + + /** will be called for each child element */ + virtual SvXMLImportContext* HandleChild( + sal_Int32 nElementToken, + const css::uno::Reference& xAttrList ) = 0; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/XFormsBindContext.cxx b/xmloff/source/xforms/XFormsBindContext.cxx new file mode 100644 index 000000000..9c799d53f --- /dev/null +++ b/xmloff/source/xforms/XFormsBindContext.cxx @@ -0,0 +1,155 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "XFormsBindContext.hxx" + +#include "xformsapi.hxx" + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +using com::sun::star::uno::Reference; +using com::sun::star::uno::Any; +using com::sun::star::uno::UNO_QUERY; +using com::sun::star::container::XNameContainer; +using com::sun::star::xml::sax::XFastAttributeList; +using com::sun::star::xforms::XModel2; +using namespace xmloff::token; + +// helper function; see below +static void lcl_fillNamespaceContainer( const SvXMLNamespaceMap&, + Reference const & ); + +XFormsBindContext::XFormsBindContext( + SvXMLImport& rImport, + const Reference& xModel ) : + TokenContext( rImport ), + mxModel( xModel ) +{ + // attach binding to model + mxBinding = mxModel->createBinding(); + SAL_WARN_IF( !mxBinding.is(), "xmloff", "can't create binding" ); + mxModel->getBindings()->insert( Any( mxBinding ) ); +} + +void XFormsBindContext::HandleAttribute( const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) +{ + switch( aIter.getToken() & TOKEN_MASK ) + { + case XML_NODESET: + xforms_setValue( mxBinding, "BindingExpression", aIter.toString() ); + break; + case XML_ID: + xforms_setValue( mxBinding, "BindingID", aIter.toString() ); + break; + case XML_READONLY: + xforms_setValue( mxBinding, "ReadonlyExpression", aIter.toString() ); + break; + case XML_RELEVANT: + xforms_setValue( mxBinding, "RelevantExpression", aIter.toString() ); + break; + case XML_REQUIRED: + xforms_setValue( mxBinding, "RequiredExpression", aIter.toString() ); + break; + case XML_CONSTRAINT: + xforms_setValue( mxBinding, "ConstraintExpression", aIter.toString() ); + break; + case XML_CALCULATE: + xforms_setValue( mxBinding, "CalculateExpression", aIter.toString() ); + break; + case XML_TYPE: + xforms_setValue( mxBinding, "Type", + xforms_getTypeName( mxModel->getDataTypeRepository(), + GetImport().GetNamespaceMap(), + aIter.toString() ) ); + break; + default: + assert( false && "should not happen" ); + break; + } +} + +void XFormsBindContext::startFastElement( + sal_Int32 nElement, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) +{ + // we need to register the namespaces + Reference xContainer( + mxBinding->getPropertyValue( "BindingNamespaces" ), + UNO_QUERY ); + + SAL_WARN_IF( !xContainer.is(), "xmloff", "binding should have a namespace container" ); + if( xContainer.is() ) + lcl_fillNamespaceContainer( GetImport().GetNamespaceMap(), xContainer); + + // call super-class for attribute handling + TokenContext::startFastElement( nElement, xAttrList ); +} + +/** will be called for each child element */ +SvXMLImportContext* XFormsBindContext::HandleChild( + sal_Int32, + const Reference& ) +{ + assert( false && "no children supported" ); + return nullptr; +} + + +static void lcl_fillNamespaceContainer( + const SvXMLNamespaceMap& aMap, + Reference const & xContainer ) +{ + sal_uInt16 nKeyIter = aMap.GetFirstKey(); + do + { + // get prefix and namespace + const OUString& sPrefix = aMap.GetPrefixByKey( nKeyIter ); + const OUString& sNamespace = aMap.GetNameByKey( nKeyIter ); + + // as a hack, we will ignore our own 'default' namespaces + SAL_WARN_IF( sPrefix.isEmpty(), "xmloff", "no prefix?" ); + if( !sPrefix.startsWith("_") && + nKeyIter >= XML_NAMESPACE_META_SO52) + { + // insert prefix (use replace if already known) + if( xContainer->hasByName( sPrefix ) ) + xContainer->replaceByName( sPrefix, Any( sNamespace ) ); + else + xContainer->insertByName( sPrefix, Any( sNamespace ) ); + } + + // proceed to next + nKeyIter = aMap.GetNextKey( nKeyIter ); + } + while( nKeyIter != XML_NAMESPACE_UNKNOWN ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/XFormsBindContext.hxx b/xmloff/source/xforms/XFormsBindContext.hxx new file mode 100644 index 000000000..5aa784674 --- /dev/null +++ b/xmloff/source/xforms/XFormsBindContext.hxx @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include "TokenContext.hxx" +#include + +namespace com::sun::star { + namespace xml::sax { class XAttributeList; } + namespace beans { class XPropertySet; } + namespace xforms { class XModel2; } +} + +class SvXMLImport; +class SvXMLImportContext; + +/** import the xforms:binding element */ +class XFormsBindContext : public TokenContext +{ + const css::uno::Reference mxModel; + css::uno::Reference mxBinding; + +public: + XFormsBindContext( SvXMLImport& rImport, + const css::uno::Reference& xModel ); + + // implement SvXMLImportContext & TokenContext methods: + + virtual void SAL_CALL startFastElement( + sal_Int32 nElement, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override; + +protected: + virtual void HandleAttribute( const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) override; + + virtual SvXMLImportContext* HandleChild( + sal_Int32 nElementToken, + const css::uno::Reference& xAttrList ) override; + +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/XFormsInstanceContext.cxx b/xmloff/source/xforms/XFormsInstanceContext.cxx new file mode 100644 index 000000000..37d56a94a --- /dev/null +++ b/xmloff/source/xforms/XFormsInstanceContext.cxx @@ -0,0 +1,162 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "XFormsInstanceContext.hxx" + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + + +using com::sun::star::uno::Reference; +using com::sun::star::uno::Any; +using com::sun::star::uno::Sequence; +using com::sun::star::xforms::XModel2; +using com::sun::star::beans::PropertyValue; +using com::sun::star::xml::sax::XAttributeList; + +using xmloff::token::XML_SRC; +using xmloff::token::XML_ID; + +XFormsInstanceContext::XFormsInstanceContext( + SvXMLImport& rImport, + const Reference & xModel ) : + TokenContext( rImport ), + mxModel( xModel ) +{ + SAL_WARN_IF( !mxModel.is(), "xmloff", "need model" ); +} + +css::uno::Reference< css::xml::sax::XFastContextHandler > XFormsInstanceContext::createFastChildContext( + sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& ) +{ + SvXMLImportContext* pContext = nullptr; + + // only the first element child of an xforms:instance element + // is used as an instance. The other children remainder must be + // ignored. + if( mxInstance.is() ) + { + const OUString& rLocalName = SvXMLImport::getNameFromToken( nElement ); + GetImport().SetError( XMLERROR_XFORMS_ONLY_ONE_INSTANCE_ELEMENT, rLocalName ); + } + else + { + // create new DomBuilderContext. Save reference to tree in Model. + DomBuilderContext* pInstance = new DomBuilderContext( GetImport(), nElement ); + mxInstance = pInstance->getTree(); + pContext = pInstance; + } + + SAL_WARN_IF( pContext == nullptr, "xmloff", "no context!" ); + return pContext; + +} + +css::uno::Reference< css::xml::sax::XFastContextHandler > XFormsInstanceContext::createUnknownChildContext( + const OUString & rNamespace, const OUString &rName, const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*Attribs*/) +{ + SvXMLImportContext* pContext = nullptr; + + // only the first element child of an xforms:instance element + // is used as an instance. The other children remainder must be + // ignored. + if( mxInstance.is() ) + { + GetImport().SetError( XMLERROR_XFORMS_ONLY_ONE_INSTANCE_ELEMENT, rName ); + } + else + { + // create new DomBuilderContext. Save reference to tree in Model. + DomBuilderContext* pInstance = new DomBuilderContext( GetImport(), rNamespace, rName ); + mxInstance = pInstance->getTree(); + pContext = pInstance; + } + + SAL_WARN_IF( pContext == nullptr, "xmloff", "no context!" ); + return pContext; + +} + +void XFormsInstanceContext::endFastElement(sal_Int32 ) +{ + Sequence aSequence( 3 ); + PropertyValue* pSequence = aSequence.getArray(); + pSequence[0].Name = "Instance"; + pSequence[0].Value <<= mxInstance; + pSequence[1].Name = "ID"; + pSequence[1].Value <<= msId; + pSequence[2].Name = "URL"; + pSequence[2].Value <<= msURL; + + mxModel->getInstances()->insert( Any( aSequence ) ); +} + +void XFormsInstanceContext::endUnknownElement(const OUString & /*Namespace*/, const OUString & /*Name*/) +{ + Sequence aSequence( 3 ); + PropertyValue* pSequence = aSequence.getArray(); + pSequence[0].Name = "Instance"; + pSequence[0].Value <<= mxInstance; + pSequence[1].Name = "ID"; + pSequence[1].Value <<= msId; + pSequence[2].Name = "URL"; + pSequence[2].Value <<= msURL; + + mxModel->getInstances()->insert( Any( aSequence ) ); +} + +void XFormsInstanceContext::HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) +{ + switch( aIter.getToken() & TOKEN_MASK ) + { + case XML_SRC: + msURL = aIter.toString(); + break; + case XML_ID: + msId = aIter.toString(); + break; + default: + XMLOFF_WARN_UNKNOWN("xmloff", aIter); + assert( false && "this should not happen" ); + break; + } +} + +SvXMLImportContext* XFormsInstanceContext::HandleChild( + sal_Int32, + const Reference& ) +{ + assert( false && "to be handled by CreateChildContext" ); + return nullptr; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/XFormsInstanceContext.hxx b/xmloff/source/xforms/XFormsInstanceContext.hxx new file mode 100644 index 000000000..7583c2da3 --- /dev/null +++ b/xmloff/source/xforms/XFormsInstanceContext.hxx @@ -0,0 +1,69 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include "TokenContext.hxx" +#include + +namespace com::sun::star { + namespace xml::sax { class XAttributeList; } + namespace xml::dom { class XDocument; } + namespace beans { class XPropertySet; } + namespace xforms { class XModel2; } +} + +class SvXMLImport; +class SvXMLImportContext; + +/** import the xforms:instance element */ +class XFormsInstanceContext : public TokenContext +{ + css::uno::Reference mxModel; + css::uno::Reference mxInstance; + OUString msId; + OUString msURL; + +public: + XFormsInstanceContext( SvXMLImport& rImport, + const css::uno::Reference & xModel ); + + // implement SvXMLImportContext & TokenContext methods: + // We override CreateChildContext, because we want to read + // arbitrary DOM elements. For the attributes, we use the + // TokenContext mechanism. + + virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( + sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override; + virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext( + const OUString& Namespace, const OUString& Name, + const css::uno::Reference< css::xml::sax::XFastAttributeList >& Attribs ) override; + + virtual void SAL_CALL endFastElement(sal_Int32 nElement) override; + virtual void SAL_CALL endUnknownElement(const OUString & Namespace, const OUString & Name) override; + +protected: + virtual void HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) override; + + virtual SvXMLImportContext* HandleChild( + sal_Int32 nElementToken, + const css::uno::Reference& xAttrList ) override; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/XFormsModelContext.cxx b/xmloff/source/xforms/XFormsModelContext.cxx new file mode 100644 index 000000000..479909fb5 --- /dev/null +++ b/xmloff/source/xforms/XFormsModelContext.cxx @@ -0,0 +1,110 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "XFormsModelContext.hxx" + +#include "XFormsBindContext.hxx" +#include "XFormsSubmissionContext.hxx" +#include "XFormsInstanceContext.hxx" +#include "SchemaContext.hxx" +#include "xformsapi.hxx" + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + + +using com::sun::star::util::XUpdatable; +using namespace com::sun::star::uno; +using namespace xmloff::token; + + +XFormsModelContext::XFormsModelContext( SvXMLImport& rImport ) : + TokenContext( rImport ), + mxModel( xforms_createXFormsModel() ) +{ +} + +void XFormsModelContext::HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) +{ + switch( aIter.getToken() & TOKEN_MASK) + { + case XML_ID: + mxModel->setPropertyValue( "ID", Any( aIter.toString() ) ); + break; + case XML_SCHEMA: + GetImport().SetError( XMLERROR_XFORMS_NO_SCHEMA_SUPPORT ); + break; + default: + XMLOFF_WARN_UNKNOWN("xmloff", aIter); + assert( false && "this should not happen" ); + break; + } +} + +SvXMLImportContext* XFormsModelContext::HandleChild( + sal_Int32 nElementToken, + const Reference& ) +{ + SvXMLImportContext* pContext = nullptr; + + switch( nElementToken ) + { + case XML_ELEMENT(XFORMS, XML_INSTANCE): + pContext = new XFormsInstanceContext( GetImport(), mxModel ); + break; + case XML_ELEMENT(XFORMS, XML_BIND): + pContext = new XFormsBindContext( GetImport(), mxModel ); + break; + case XML_ELEMENT(XFORMS, XML_SUBMISSION): + pContext = new XFormsSubmissionContext( GetImport(), mxModel ); + break; + case XML_ELEMENT(XSD, XML_SCHEMA): + pContext = new SchemaContext( GetImport(), mxModel->getDataTypeRepository() ); + break; + default: + XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElementToken); + assert( false && "Boooo!" ); + break; + } + + return pContext; +} + +void XFormsModelContext::endFastElement(sal_Int32 ) +{ + // update before putting model into document + Reference xUpdate( mxModel, UNO_QUERY ); + if( xUpdate.is() ) + xUpdate->update(); + + GetImport().initXForms(); + xforms_addXFormsModel( GetImport().GetModel(), mxModel ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/XFormsModelContext.hxx b/xmloff/source/xforms/XFormsModelContext.hxx new file mode 100644 index 000000000..0579850df --- /dev/null +++ b/xmloff/source/xforms/XFormsModelContext.hxx @@ -0,0 +1,54 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include "TokenContext.hxx" +#include +#include + +namespace com::sun::star { + namespace xml::sax { class XAttributeList; } + namespace beans { class XPropertySet; } +} + +class SvXMLImport; +class SvXMLImportContext; + +/** import the xforms:model element */ +class XFormsModelContext : public TokenContext +{ + css::uno::Reference mxModel; + +public: + XFormsModelContext( SvXMLImport& rImport ); + + // implement SvXMLImportContext & TokenContext methods: + + virtual void SAL_CALL endFastElement(sal_Int32 nElement) override; + +protected: + virtual void HandleAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) override; + + virtual SvXMLImportContext* HandleChild( + sal_Int32 nElementToken, + const css::uno::Reference& xAttrList ) override; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/XFormsModelExport.hxx b/xmloff/source/xforms/XFormsModelExport.hxx new file mode 100644 index 000000000..03d83e97c --- /dev/null +++ b/xmloff/source/xforms/XFormsModelExport.hxx @@ -0,0 +1,33 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include + +class SvXMLExport; +namespace com::sun::star { + namespace uno { template class Reference; } + namespace beans { class XPropertySet; } +} + +void exportXFormsModel( SvXMLExport&, + const css::uno::Reference& ); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/XFormsSubmissionContext.cxx b/xmloff/source/xforms/XFormsSubmissionContext.cxx new file mode 100644 index 000000000..2e57d5f43 --- /dev/null +++ b/xmloff/source/xforms/XFormsSubmissionContext.cxx @@ -0,0 +1,137 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "XFormsSubmissionContext.hxx" + +#include "xformsapi.hxx" + +#include +#include +#include +#include + +#include + +#include + +#include +#include + +using com::sun::star::xml::sax::XAttributeList; +using com::sun::star::xforms::XModel2; + +using namespace com::sun::star::uno; +using namespace xmloff::token; + + +XFormsSubmissionContext::XFormsSubmissionContext( + SvXMLImport& rImport, + const Reference& xModel ) : + TokenContext( rImport ) +{ + // register submission with model + SAL_WARN_IF( !xModel.is(), "xmloff", "need model" ); + mxSubmission = xModel->createSubmission().get(); + SAL_WARN_IF( !mxSubmission.is(), "xmloff", "can't create submission" ); + xModel->getSubmissions()->insert( Any( mxSubmission ) ); +} + +namespace { + +Any toBool( std::string_view rValue ) +{ + Any aValue; + bool bValue(false); + if (::sax::Converter::convertBool( bValue, rValue )) + { + aValue <<= bValue; + } + return aValue; +} + +} // namespace + +void XFormsSubmissionContext::HandleAttribute( const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) +{ + switch( aIter.getToken() & TOKEN_MASK ) + { + case XML_ID: + xforms_setValue( mxSubmission, "ID", aIter.toString() ); + break; + case XML_BIND: + xforms_setValue( mxSubmission, "Bind", aIter.toString() ); + break; + case XML_REF: + xforms_setValue( mxSubmission, "Ref", aIter.toString() ); + break; + case XML_ACTION: + xforms_setValue( mxSubmission, "Action", aIter.toString() ); + break; + case XML_METHOD: + xforms_setValue( mxSubmission, "Method", aIter.toString() ); + break; + case XML_VERSION: + xforms_setValue( mxSubmission, "Version", aIter.toString() ); + break; + case XML_INDENT: + xforms_setValue( mxSubmission, "Indent", toBool( aIter.toView() ) ); + break; + case XML_MEDIATYPE: + xforms_setValue( mxSubmission, "MediaType", aIter.toString() ); + break; + case XML_ENCODING: + xforms_setValue( mxSubmission, "Encoding", aIter.toString() ); + break; + case XML_OMIT_XML_DECLARATION: + xforms_setValue( mxSubmission, "OmitXmlDeclaration", + toBool( aIter.toView() ) ); + break; + case XML_STANDALONE: + xforms_setValue( mxSubmission, "Standalone", toBool( aIter.toView() ) ); + break; + case XML_CDATA_SECTION_ELEMENTS: + xforms_setValue( mxSubmission, "CDataSectionElement", aIter.toString() ); + break; + case XML_REPLACE: + xforms_setValue( mxSubmission, "Replace", aIter.toString() ); + break; + case XML_SEPARATOR: + xforms_setValue( mxSubmission, "Separator", aIter.toString() ); + break; + case XML_INCLUDENAMESPACEPREFIXES: + xforms_setValue( mxSubmission, "IncludeNamespacePrefixes", aIter.toString() ); + break; + default: + XMLOFF_WARN_UNKNOWN("xmloff", aIter); + assert( false && "unknown attribute" ); + break; + } +} + +/** will be called for each child element */ +SvXMLImportContext* XFormsSubmissionContext::HandleChild( + sal_Int32, + const Reference& ) +{ + assert( false && "no children supported" ); + return nullptr; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/XFormsSubmissionContext.hxx b/xmloff/source/xforms/XFormsSubmissionContext.hxx new file mode 100644 index 000000000..b000db0eb --- /dev/null +++ b/xmloff/source/xforms/XFormsSubmissionContext.hxx @@ -0,0 +1,54 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include "TokenContext.hxx" +#include + +namespace com::sun::star { + namespace xml::sax { class XAttributeList; } + namespace beans { class XPropertySet; } + namespace xforms { class XModel2; } +} + +class SvXMLImport; +class SvXMLImportContext; + +/** import the xforms:submission element */ +class XFormsSubmissionContext : public TokenContext +{ + css::uno::Reference mxSubmission; + +public: + XFormsSubmissionContext( SvXMLImport& rImport, + const css::uno::Reference& xModel ); + + // implement TokenContext methods: + +protected: + virtual void HandleAttribute( const sax_fastparser::FastAttributeList::FastAttributeIter & aIter ) override; + + virtual SvXMLImportContext* HandleChild( + sal_Int32 nElementToken, + const css::uno::Reference& xAttrList ) override; + +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/xformsapi.cxx b/xmloff/source/xforms/xformsapi.cxx new file mode 100644 index 000000000..d8345275c --- /dev/null +++ b/xmloff/source/xforms/xformsapi.cxx @@ -0,0 +1,296 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "xformsapi.hxx" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +using com::sun::star::uno::Reference; +using com::sun::star::uno::Sequence; +using com::sun::star::uno::UNO_QUERY; +using com::sun::star::uno::UNO_QUERY_THROW; +using com::sun::star::beans::XPropertySet; +using com::sun::star::container::XNameAccess; +using com::sun::star::xforms::XFormsSupplier; +using com::sun::star::xforms::XDataTypeRepository; +using com::sun::star::xforms::Model; +using com::sun::star::xforms::XModel2; +using com::sun::star::container::XNameContainer; +using com::sun::star::uno::Any; +using com::sun::star::uno::Exception; + +using namespace com::sun::star; +using namespace xmloff::token; + +Reference xforms_createXFormsModel() +{ + Reference xModel = Model::create( comphelper::getProcessComponentContext() ); + + return xModel; +} + +void xforms_addXFormsModel( + const Reference& xDocument, + const Reference& xModel ) +{ + bool bSuccess = false; + try + { + Reference xSupplier( xDocument, UNO_QUERY ); + if( xSupplier.is() ) + { + Reference xForms = xSupplier->getXForms(); + if( xForms.is() ) + { + OUString sName; + xModel->getPropertyValue("ID") >>= sName; + xForms->insertByName( sName, Any( xModel ) ); + bSuccess = true; + } + } + } + catch( const Exception& ) + { + ; // no success! + } + + // TODO: implement proper error handling + SAL_WARN_IF( !bSuccess, "xmloff", "can't import model" ); +} + +static Reference lcl_findXFormsBindingOrSubmission( + Reference const & xDocument, + const OUString& rBindingID, + bool bBinding ) +{ + // find binding by iterating over all models, and look for the + // given binding ID + + Reference xRet; + try + { + // get supplier + Reference xSupplier( xDocument, UNO_QUERY ); + if( xSupplier.is() ) + { + // get XForms models + Reference xForms = xSupplier->getXForms(); + if( xForms.is() ) + { + // iterate over all models + const Sequence aNames = xForms->getElementNames(); + for( const auto& rName : aNames ) + { + Reference xModel( + xForms->getByName( rName ), UNO_QUERY ); + if( xModel.is() ) + { + // ask model for bindings + Reference xBindings( + bBinding + ? xModel->getBindings() + : xModel->getSubmissions(), + UNO_QUERY_THROW ); + + // finally, ask binding for name + if( xBindings->hasByName( rBindingID ) ) + xRet.set( xBindings->getByName( rBindingID ), + UNO_QUERY ); + } + + if (xRet.is()) + break; + } + } + } + } + catch( const Exception& ) + { + ; // no success! + } + + // TODO: if (!xRet.is()) rImport.SetError(...); + + return xRet; +} + +Reference xforms_findXFormsBinding( + Reference const & xDocument, + const OUString& rBindingID ) +{ + return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, true ); +} + +Reference xforms_findXFormsSubmission( + Reference const & xDocument, + const OUString& rBindingID ) +{ + return lcl_findXFormsBindingOrSubmission( xDocument, rBindingID, false ); +} + +void xforms_setValueAny( Reference const & xPropertySet, + const OUString& rName, + const Any& rAny ) +{ + xPropertySet->setPropertyValue( rName, rAny ); +} + +#define TOKEN_MAP_ENTRY(NAMESPACE,TOKEN) { XML_NAMESPACE_##NAMESPACE, xmloff::token::XML_##TOKEN, xmloff::token::XML_##TOKEN } +const SvXMLTokenMapEntry aTypes[] = +{ + TOKEN_MAP_ENTRY( XSD, STRING ), + TOKEN_MAP_ENTRY( XSD, DECIMAL ), + TOKEN_MAP_ENTRY( XSD, DOUBLE ), + TOKEN_MAP_ENTRY( XSD, FLOAT ), + TOKEN_MAP_ENTRY( XSD, BOOLEAN ), + TOKEN_MAP_ENTRY( XSD, ANYURI ), + TOKEN_MAP_ENTRY( XSD, DATETIME_XSD ), + TOKEN_MAP_ENTRY( XSD, DATE ), + TOKEN_MAP_ENTRY( XSD, TIME ), + TOKEN_MAP_ENTRY( XSD, YEAR ), + TOKEN_MAP_ENTRY( XSD, MONTH ), + TOKEN_MAP_ENTRY( XSD, DAY ), + XML_TOKEN_MAP_END +}; + +sal_uInt16 xforms_getTypeClass( + const Reference& xRepository, + const SvXMLNamespaceMap& rNamespaceMap, + const OUString& rXMLName ) +{ + // translate name into token for local name + OUString sLocalName; + sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrValueQName(rXMLName, &sLocalName); + static const SvXMLTokenMap aMap( aTypes ); + sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName ); + + sal_uInt16 nTypeClass = css::xsd::DataTypeClass::STRING; + if( nToken != XML_TOK_UNKNOWN ) + { + // we found an XSD name: then get the proper API name for it + SAL_WARN_IF( !xRepository.is(), "xmloff", "can't find type without repository"); + switch( nToken ) + { + case XML_STRING: + nTypeClass = css::xsd::DataTypeClass::STRING; + break; + case XML_ANYURI: + nTypeClass = css::xsd::DataTypeClass::anyURI; + break; + case XML_DECIMAL: + nTypeClass = css::xsd::DataTypeClass::DECIMAL; + break; + case XML_DOUBLE: + nTypeClass = css::xsd::DataTypeClass::DOUBLE; + break; + case XML_FLOAT: + nTypeClass = css::xsd::DataTypeClass::FLOAT; + break; + case XML_BOOLEAN: + nTypeClass = css::xsd::DataTypeClass::BOOLEAN; + break; + case XML_DATETIME_XSD: + nTypeClass = css::xsd::DataTypeClass::DATETIME; + break; + case XML_DATE: + nTypeClass = css::xsd::DataTypeClass::DATE; + break; + case XML_TIME: + nTypeClass = css::xsd::DataTypeClass::TIME; + break; + case XML_YEAR: + nTypeClass = css::xsd::DataTypeClass::gYear; + break; + case XML_DAY: + nTypeClass = css::xsd::DataTypeClass::gDay; + break; + case XML_MONTH: + nTypeClass = css::xsd::DataTypeClass::gMonth; + break; + + /* data types not yet supported: + nTypeClass = css::xsd::DataTypeClass::DURATION; + nTypeClass = css::xsd::DataTypeClass::gYearMonth; + nTypeClass = css::xsd::DataTypeClass::gMonthDay; + nTypeClass = css::xsd::DataTypeClass::hexBinary; + nTypeClass = css::xsd::DataTypeClass::base64Binary; + nTypeClass = css::xsd::DataTypeClass::QName; + nTypeClass = css::xsd::DataTypeClass::NOTATION; + */ + } + } + + return nTypeClass; +} + + +OUString xforms_getTypeName( + const Reference& xRepository, + const SvXMLNamespaceMap& rNamespaceMap, + const OUString& rXMLName ) +{ + OUString sLocalName; + sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrValueQName(rXMLName, &sLocalName); + static const SvXMLTokenMap aMap( aTypes ); + sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName ); + return ( nToken == XML_TOK_UNKNOWN ) + ? rXMLName + : xforms_getBasicTypeName( xRepository, rNamespaceMap, rXMLName ); +} + +OUString xforms_getBasicTypeName( + const Reference& xRepository, + const SvXMLNamespaceMap& rNamespaceMap, + const OUString& rXMLName ) +{ + OUString sTypeName = rXMLName; + try + { + sTypeName = + xRepository->getBasicDataType( + xforms_getTypeClass( xRepository, rNamespaceMap, rXMLName ) ) + ->getName(); + } + catch( const Exception& ) + { + TOOLS_WARN_EXCEPTION("xmloff", "exception during type creation"); + } + return sTypeName; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/xformsapi.hxx b/xmloff/source/xforms/xformsapi.hxx new file mode 100644 index 000000000..ba66e985b --- /dev/null +++ b/xmloff/source/xforms/xformsapi.hxx @@ -0,0 +1,83 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#pragma once + + +// this is a collection of several functions to make dealing with the XForms +// API a little easier + + +#include + +namespace com::sun::star { + namespace beans { class XPropertySet; } + namespace frame { class XModel; } + namespace uno { template class Reference; } + namespace xforms { class XDataTypeRepository; class XModel2; } +} +class SvXMLNamespaceMap; + +css::uno::Reference xforms_createXFormsModel(); + +void xforms_addXFormsModel( + const css::uno::Reference& xDocument, + const css::uno::Reference& xModel ); + +css::uno::Reference xforms_findXFormsBinding( css::uno::Reference const &, const OUString& ); + +css::uno::Reference xforms_findXFormsSubmission( css::uno::Reference const &, const OUString& ); + +void xforms_setValueAny( + css::uno::Reference const & xPropSet, + const OUString& rName, + const css::uno::Any& rAny ); + +template +inline void xforms_setValue( + const css::uno::Reference& xPropSet, + const OUString& rName, + const T& aValue ) +{ + xforms_setValueAny( xPropSet, rName, css::uno::Any( aValue ) ); +} +template<> +inline void xforms_setValue( + const css::uno::Reference& xPropSet, + const OUString& rName, + const css::uno::Any& aValue ) +{ + xforms_setValueAny( xPropSet, rName, aValue ); +} + +sal_uInt16 xforms_getTypeClass( + const css::uno::Reference& xRepository, + const SvXMLNamespaceMap& rNamespaceMap, + const OUString& rXMLName ); + +OUString xforms_getTypeName( + const css::uno::Reference& xRepository, + const SvXMLNamespaceMap& rNamespaceMap, + const OUString& rXMLName ); + +OUString xforms_getBasicTypeName( + const css::uno::Reference& xRepository, + const SvXMLNamespaceMap& rNamespaceMap, + const OUString& rXMLName ); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/xformsexport.cxx b/xmloff/source/xforms/xformsexport.cxx new file mode 100644 index 000000000..363a8f674 --- /dev/null +++ b/xmloff/source/xforms/xformsexport.cxx @@ -0,0 +1,802 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include + +#include "XFormsModelExport.hxx" + +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace com::sun::star; +using namespace com::sun::star::uno; +using namespace xmloff::token; + +using com::sun::star::beans::XPropertySet; +using com::sun::star::beans::XPropertySetInfo; +using com::sun::star::container::XIndexAccess; +using com::sun::star::container::XNameAccess; +using com::sun::star::container::XNameContainer; +using com::sun::star::container::XEnumerationAccess; +using com::sun::star::container::XEnumeration; +using com::sun::star::xml::dom::XDocument; +using com::sun::star::form::binding::XBindableValue; +using com::sun::star::form::binding::XListEntrySink; +using com::sun::star::form::submission::XSubmissionSupplier; +using com::sun::star::beans::PropertyValue; +using com::sun::star::xforms::XDataTypeRepository; +using com::sun::star::xforms::XFormsSupplier; +using com::sun::star::util::Duration; + +void exportXForms( SvXMLExport& rExport ) +{ + Reference xSupplier( rExport.GetModel(), UNO_QUERY ); + if( !xSupplier.is() ) + return; + + Reference xForms = xSupplier->getXForms(); + if( xForms.is() ) + { + const Sequence aNames = xForms->getElementNames(); + + for( const auto& rName : aNames ) + { + Reference xModel( xForms->getByName( rName ), + UNO_QUERY ); + exportXFormsModel( rExport, xModel ); + } + } +} + + +static void exportXFormsInstance( SvXMLExport&, const Sequence& ); +static void exportXFormsBinding( SvXMLExport&, const Reference& ); +static void exportXFormsSubmission( SvXMLExport&, const Reference& ); +static void exportXFormsSchemas( SvXMLExport&, const Reference& ); + + +typedef OUString (*convert_t)( const Any& ); + +namespace { + +struct ExportTable +{ + const char* pPropertyName; + sal_uInt16 const nNamespace; + sal_uInt16 nToken; + convert_t const aConverter; +}; + +} + +static void lcl_export( const Reference& rPropertySet, + SvXMLExport& rExport, + const ExportTable* pTable ); + +#define TABLE_ENTRY(NAME,NAMESPACE,TOKEN,CONVERTER) { NAME,XML_NAMESPACE_##NAMESPACE,xmloff::token::XML_##TOKEN, CONVERTER } +#define TABLE_END { nullptr, 0, 0, nullptr } + +// any conversion functions +static OUString xforms_string( const Any& ); +static OUString xforms_bool( const Any& ); +static OUString xforms_whitespace( const Any& ); +template static OUString xforms_convert( const Any& ); +template static OUString xforms_convertRef( const Any& ); + +static void xforms_formatDate( OUStringBuffer& aBuffer, const util::Date& aDate ); +static void xforms_formatTime( OUStringBuffer& aBuffer, const css::util::Time& aTime ); +static void xforms_formatDateTime( OUStringBuffer& aBuffer, const util::DateTime& aDateTime ); + +static void convertNumber(OUStringBuffer & b, sal_Int32 n) { + b.append(n); +} + +convert_t const xforms_int32 = &xforms_convert; +convert_t const xforms_double = &xforms_convert; +convert_t const xforms_dateTime = &xforms_convertRef; +convert_t const xforms_date = &xforms_convertRef; +convert_t const xforms_time = &xforms_convertRef; + +// other functions +static OUString lcl_getXSDType( SvXMLExport const & rExport, + const Reference& xType ); + + +// the model + + +const ExportTable aXFormsModelTable[] = +{ + TABLE_ENTRY( "ID", NONE, ID, xforms_string ), + TABLE_ENTRY( "SchemaRef", NONE, SCHEMA, xforms_string ), + TABLE_END +}; + +void exportXFormsModel( SvXMLExport& rExport, + const Reference& xModelPropSet ) +{ + // no model -> don't do anything! + Reference xModel( xModelPropSet, UNO_QUERY ); + if( ! xModel.is() || ! xModelPropSet.is() ) + return; + + lcl_export( xModelPropSet, rExport, aXFormsModelTable ); + SvXMLElementExport aModelElement( rExport, XML_NAMESPACE_XFORMS, XML_MODEL, + true, true ); + + // instances + Reference xInstances( xModel->getInstances(), + UNO_QUERY_THROW); + sal_Int32 nCount = xInstances->getCount(); + sal_Int32 i = 0; + for( i = 0; i < nCount; i++ ) + { + Sequence aInstance; + xInstances->getByIndex( i ) >>= aInstance; + exportXFormsInstance( rExport, aInstance ); + } + + + // bindings + Reference xBindings( xModel->getBindings(), UNO_QUERY_THROW); + nCount = xBindings->getCount(); + for( i = 0; i < nCount; i++ ) + { + Reference aBinding( xBindings->getByIndex( i ), + UNO_QUERY_THROW ); + exportXFormsBinding( rExport, aBinding ); + } + + // submissions + Reference xSubmissions( xModel->getSubmissions(), + UNO_QUERY_THROW ); + nCount = xSubmissions->getCount(); + for( i = 0; i < nCount; i++ ) + { + Reference xSubmission( xSubmissions->getByIndex( i ), + UNO_QUERY_THROW ); + exportXFormsSubmission( rExport, xSubmission ); + } + + // schemas + exportXFormsSchemas( rExport, xModel ); +} + + +// the instance + + +void exportXFormsInstance( SvXMLExport& rExport, + const Sequence& xInstance ) +{ + OUString sId; + OUString sURL; + Reference xDoc; + + for( const auto& rProp : xInstance ) + { + OUString sName = rProp.Name; + const Any& rAny = rProp.Value; + if ( sName == "ID" ) + rAny >>= sId; + else if ( sName == "URL" ) + rAny >>= sURL; + else if ( sName == "Instance" ) + rAny >>= xDoc; + } + + if( !sId.isEmpty() ) + rExport.AddAttribute( XML_NAMESPACE_NONE, XML_ID, sId ); + + if( !sURL.isEmpty() ) + rExport.AddAttribute( XML_NAMESPACE_NONE, XML_SRC, sURL ); + + SvXMLElementExport aElem( rExport, XML_NAMESPACE_XFORMS, XML_INSTANCE, + true, true ); + rExport.IgnorableWhitespace(); + if( xDoc.is() ) + { + exportDom( rExport, xDoc ); + } +} + + +// the binding + + +const ExportTable aXFormsBindingTable[] = +{ + TABLE_ENTRY( "BindingID", NONE, ID, xforms_string ), + TABLE_ENTRY( "BindingExpression", NONE, NODESET, xforms_string ), + TABLE_ENTRY( "ReadonlyExpression", NONE, READONLY, xforms_string ), + TABLE_ENTRY( "RelevantExpression", NONE, RELEVANT, xforms_string ), + TABLE_ENTRY( "RequiredExpression", NONE, REQUIRED, xforms_string ), + TABLE_ENTRY( "ConstraintExpression", NONE, CONSTRAINT, xforms_string ), + TABLE_ENTRY( "CalculateExpression", NONE, CALCULATE, xforms_string ), + // type handled separately, for type name <-> XSD type conversion + // TABLE_ENTRY( "Type", NONE, TYPE, xforms_string ), + TABLE_END +}; + +void exportXFormsBinding( SvXMLExport& rExport, + const Reference& xBinding ) +{ + // name check; generate binding ID if necessary + { + OUString sName; + xBinding->getPropertyValue( "BindingID" ) >>= sName; + if( sName.isEmpty() ) + { + // if we don't have a name yet, generate one on the fly + sal_Int64 nId = reinterpret_cast( xBinding.get() ); + sName = "bind_" + OUString::number( nId , 16 ); + xBinding->setPropertyValue( "BindingID", Any(sName)); + } + } + + lcl_export( xBinding, rExport, aXFormsBindingTable ); + + // handle type attribute + { + OUString sTypeName; + xBinding->getPropertyValue( "Type" ) >>= sTypeName; + + try + { + // now get type, and determine whether it's a standard type. If + // so, export the XSD name + Reference xModel( + xBinding->getPropertyValue( "Model" ), + UNO_QUERY ); + Reference xRepository( + xModel.is() ? xModel->getDataTypeRepository() : Reference() ); + if( xRepository.is() ) + { + Reference xDataType = + xRepository->getDataType( sTypeName ); + + // if it's a basic data type, write out the XSD name + // for the XSD type class + bool bIsBasic = false; + xDataType->getPropertyValue( "IsBasic" ) >>= bIsBasic; + if( bIsBasic ) + sTypeName = lcl_getXSDType( rExport, xDataType ); + } + } + catch( Exception& ) + { + ; // ignore; just use typename + } + + // now that we have the proper type name, write out the attribute + if( !sTypeName.isEmpty() ) + { + rExport.AddAttribute( XML_NAMESPACE_NONE, XML_TYPE, + sTypeName ); + } + } + + // we need to ensure all the namespaces in the binding will work correctly. + // to do so, we will write out all missing namespace declaractions. + const SvXMLNamespaceMap& rMap = rExport.GetNamespaceMap(); + Reference xNamespaces( + xBinding->getPropertyValue( "ModelNamespaces" ), UNO_QUERY); + if( xNamespaces.is() ) + { + // iterate over Prefixes for this binding + const Sequence aPrefixes = xNamespaces->getElementNames(); + for( const OUString& rPrefix : aPrefixes ) + { + OUString sURI; + xNamespaces->getByName( rPrefix ) >>= sURI; + + // check whether prefix/URI pair is in map; else write declaration + // (we don't need to change the map, since this element has no + // other content) + sal_uInt16 nKey = rMap.GetKeyByPrefix( rPrefix ); + if( nKey == XML_NAMESPACE_UNKNOWN || + rMap.GetNameByKey( nKey ) != sURI ) + { + // add declaration if it doesn't already exist + SvXMLAttributeList& rAttrList = rExport.GetAttrList(); + OUString sName = "xmlns:" + rPrefix; + sal_Int16 nFound = rAttrList.GetIndexByName(sName); + // duplicate xmlns:script, http://openoffice.org/2000/script seen + assert(nFound == -1 || rAttrList.getValueByIndex(nFound) == sURI); + if (nFound != -1) + continue; + rAttrList.AddAttribute(sName, sURI); + } + } + } + + SvXMLElementExport aElement( rExport, XML_NAMESPACE_XFORMS, XML_BIND, + true, true ); +} + + +// the submission + + +const ExportTable aXFormsSubmissionTable[] = +{ + TABLE_ENTRY( "ID", NONE, ID, xforms_string ), + TABLE_ENTRY( "Bind", NONE, BIND, xforms_string ), + TABLE_ENTRY( "Ref", NONE, REF, xforms_string ), + TABLE_ENTRY( "Action", NONE, ACTION, xforms_string ), + TABLE_ENTRY( "Method", NONE, METHOD, xforms_string ), + TABLE_ENTRY( "Version", NONE, VERSION, xforms_string ), + TABLE_ENTRY( "Indent", NONE, INDENT, xforms_bool ), + TABLE_ENTRY( "MediaType", NONE, MEDIATYPE, xforms_string ), + TABLE_ENTRY( "Encoding", NONE, ENCODING, xforms_string ), + TABLE_ENTRY( "OmitXmlDeclaration", NONE, OMIT_XML_DECLARATION, xforms_bool ), + TABLE_ENTRY( "Standalone", NONE, STANDALONE, xforms_bool ), + TABLE_ENTRY( "CDataSectionElement", NONE, CDATA_SECTION_ELEMENTS, xforms_string ), + TABLE_ENTRY( "Replace", NONE, REPLACE, xforms_string ), + TABLE_ENTRY( "Separator", NONE, SEPARATOR, xforms_string ), + TABLE_ENTRY( "IncludeNamespacePrefixes", NONE, INCLUDENAMESPACEPREFIXES, xforms_string ), + TABLE_END +}; + +void exportXFormsSubmission( SvXMLExport& rExport, + const Reference& xSubmission ) +{ + lcl_export( xSubmission, rExport, aXFormsSubmissionTable ); + SvXMLElementExport aElement( rExport, XML_NAMESPACE_XFORMS, XML_SUBMISSION, + true, true ); +} + + +// export data types as XSD schema + + +const ExportTable aDataTypeFacetTable[] = +{ + TABLE_ENTRY( "Length", XSD, LENGTH, xforms_int32 ), + TABLE_ENTRY( "MinLength", XSD, MINLENGTH, xforms_int32 ), + TABLE_ENTRY( "MaxLength", XSD, MAXLENGTH, xforms_int32 ), + TABLE_ENTRY( "MinInclusiveInt", XSD, MININCLUSIVE, xforms_int32 ), + TABLE_ENTRY( "MinExclusiveInt", XSD, MINEXCLUSIVE, xforms_int32 ), + TABLE_ENTRY( "MaxInclusiveInt", XSD, MAXINCLUSIVE, xforms_int32 ), + TABLE_ENTRY( "MaxExclusiveInt", XSD, MAXEXCLUSIVE, xforms_int32 ), + TABLE_ENTRY( "MinInclusiveDouble", XSD, MININCLUSIVE, xforms_double ), + TABLE_ENTRY( "MinExclusiveDouble", XSD, MINEXCLUSIVE, xforms_double ), + TABLE_ENTRY( "MaxInclusiveDouble", XSD, MAXINCLUSIVE, xforms_double ), + TABLE_ENTRY( "MaxExclusiveDouble", XSD, MAXEXCLUSIVE, xforms_double ), + TABLE_ENTRY( "MinInclusiveDate", XSD, MININCLUSIVE, xforms_date ), + TABLE_ENTRY( "MinExclusiveDate", XSD, MINEXCLUSIVE, xforms_date ), + TABLE_ENTRY( "MaxInclusiveDate", XSD, MAXINCLUSIVE, xforms_date ), + TABLE_ENTRY( "MaxExclusiveDate", XSD, MAXEXCLUSIVE, xforms_date ), + TABLE_ENTRY( "MinInclusiveTime", XSD, MININCLUSIVE, xforms_time ), + TABLE_ENTRY( "MinExclusiveTime", XSD, MINEXCLUSIVE, xforms_time ), + TABLE_ENTRY( "MaxInclusiveTime", XSD, MAXINCLUSIVE, xforms_time ), + TABLE_ENTRY( "MaxExclusiveTime", XSD, MAXEXCLUSIVE, xforms_time ), + TABLE_ENTRY( "MinInclusiveDateTime", XSD, MININCLUSIVE, xforms_dateTime ), + TABLE_ENTRY( "MinExclusiveDateTime", XSD, MINEXCLUSIVE, xforms_dateTime ), + TABLE_ENTRY( "MaxInclusiveDateTime", XSD, MAXINCLUSIVE, xforms_dateTime ), + TABLE_ENTRY( "MaxExclusiveDateTime", XSD, MAXEXCLUSIVE, xforms_dateTime ), + TABLE_ENTRY( "Pattern", XSD, PATTERN, xforms_string ), + // ??? XML_ENUMERATION, + TABLE_ENTRY( "WhiteSpace", XSD, WHITESPACE, xforms_whitespace ), + TABLE_ENTRY( "TotalDigits", XSD, TOTALDIGITS, xforms_int32 ), + TABLE_ENTRY( "FractionDigits", XSD, FRACTIONDIGITS, xforms_int32 ), + TABLE_END +}; + +// export facets through table; use the same table as lcl_export does +static void lcl_exportDataTypeFacets( SvXMLExport& rExport, + const Reference& rPropertySet, + const ExportTable* pTable ) +{ + Reference xInfo = rPropertySet->getPropertySetInfo(); + for( const ExportTable* pCurrent = pTable; + pCurrent->pPropertyName != nullptr; + pCurrent++ ) + { + OUString sName( OUString::createFromAscii( pCurrent->pPropertyName ) ); + if( xInfo->hasPropertyByName( sName ) ) + { + OUString sValue = (*pCurrent->aConverter)( + rPropertySet->getPropertyValue( sName ) ); + + if( !sValue.isEmpty() ) + { + rExport.AddAttribute( XML_NAMESPACE_NONE, XML_VALUE, sValue ); + SvXMLElementExport aFacet( + rExport, + pCurrent->nNamespace, + static_cast( pCurrent->nToken ), + true, true ); + } + } + } +} + +static OUString lcl_getXSDType( SvXMLExport const & rExport, + const Reference& xType ) +{ + // we use string as default... + XMLTokenEnum eToken = XML_STRING; + + sal_uInt16 nDataTypeClass = 0; + xType->getPropertyValue( "TypeClass" ) >>= nDataTypeClass; + switch( nDataTypeClass ) + { + case css::xsd::DataTypeClass::STRING: + eToken = XML_STRING; + break; + case css::xsd::DataTypeClass::anyURI: + eToken = XML_ANYURI; + break; + case css::xsd::DataTypeClass::DECIMAL: + eToken = XML_DECIMAL; + break; + case css::xsd::DataTypeClass::DOUBLE: + eToken = XML_DOUBLE; + break; + case css::xsd::DataTypeClass::FLOAT: + eToken = XML_FLOAT; + break; + case css::xsd::DataTypeClass::BOOLEAN: + eToken = XML_BOOLEAN; + break; + case css::xsd::DataTypeClass::DATETIME: + eToken = XML_DATETIME_XSD; + break; + case css::xsd::DataTypeClass::TIME: + eToken = XML_TIME; + break; + case css::xsd::DataTypeClass::DATE: + eToken = XML_DATE; + break; + case css::xsd::DataTypeClass::gYear: + eToken = XML_YEAR; + break; + case css::xsd::DataTypeClass::gDay: + eToken = XML_DAY; + break; + case css::xsd::DataTypeClass::gMonth: + eToken = XML_MONTH; + break; + case css::xsd::DataTypeClass::DURATION: + case css::xsd::DataTypeClass::gYearMonth: + case css::xsd::DataTypeClass::gMonthDay: + case css::xsd::DataTypeClass::hexBinary: + case css::xsd::DataTypeClass::base64Binary: + case css::xsd::DataTypeClass::QName: + case css::xsd::DataTypeClass::NOTATION: + default: + OSL_FAIL( "unknown data type" ); + } + + return rExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_XSD, + GetXMLToken( eToken ) ); +} + +static void lcl_exportDataType( SvXMLExport& rExport, + const Reference& xType ) +{ + // we do not need to export basic types; exit if we have one + bool bIsBasic = false; + xType->getPropertyValue( "IsBasic" ) >>= bIsBasic; + if( bIsBasic ) + return; + + // no basic type -> export + + // + OUString sName; + xType->getPropertyValue( "Name" ) >>= sName; + rExport.AddAttribute( XML_NAMESPACE_NONE, XML_NAME, sName ); + SvXMLElementExport aSimpleType( rExport, + XML_NAMESPACE_XSD, XML_SIMPLETYPE, + true, true ); + + // + rExport.AddAttribute( XML_NAMESPACE_NONE, XML_BASE, + lcl_getXSDType( rExport, xType ) ); + SvXMLElementExport aRestriction( rExport, + XML_NAMESPACE_XSD, + XML_RESTRICTION, + true, true ); + + // export facets + lcl_exportDataTypeFacets( rExport, + xType, + aDataTypeFacetTable ); +} + +void exportXFormsSchemas( SvXMLExport& rExport, + const Reference& xModel ) +{ + // TODO: for now, we'll fake this... + { + SvXMLElementExport aSchemaElem( rExport, XML_NAMESPACE_XSD, XML_SCHEMA, + true, true ); + + // now get data type repository, and export + Reference xTypes = xModel->getDataTypeRepository(); + if( xTypes.is() ) + { + Reference xEnum = xTypes->createEnumeration(); + SAL_WARN_IF( !xEnum.is(), "xmloff", "no enum?" ); + while( xEnum->hasMoreElements() ) + { + Reference xType( xEnum->nextElement(), UNO_QUERY ); + lcl_exportDataType( rExport, xType ); + } + } + } + + // export other, 'foreign' schemas + Reference xPropSet( xModel, UNO_QUERY ); + if( xPropSet.is() ) + { + Reference xDocument( + xPropSet->getPropertyValue( "ForeignSchema" ), + UNO_QUERY ); + + if( xDocument.is() ) + exportDom( rExport, xDocument ); + } +} + + +// helper functions + + +static void lcl_export( const Reference& rPropertySet, + SvXMLExport& rExport, + const ExportTable* pTable ) +{ + for( const ExportTable* pCurrent = pTable; + pCurrent->pPropertyName != nullptr; + pCurrent++ ) + { + Any aAny = rPropertySet->getPropertyValue( + OUString::createFromAscii( pCurrent->pPropertyName ) ); + OUString sValue = (*pCurrent->aConverter)( aAny ); + + if( !sValue.isEmpty() ) + rExport.AddAttribute( + pCurrent->nNamespace, + static_cast( pCurrent->nToken ), + sValue ); + } +} + + +// any conversion functions + + +template +OUString xforms_convert( const Any& rAny ) +{ + OUStringBuffer aBuffer; + T aData = T(); + if( rAny >>= aData ) + { + FUNC( aBuffer, aData ); + } + return aBuffer.makeStringAndClear(); +} + +template +OUString xforms_convertRef( const Any& rAny ) +{ + OUStringBuffer aBuffer; + T aData; + if( rAny >>= aData ) + { + FUNC( aBuffer, aData ); + } + return aBuffer.makeStringAndClear(); +} + +OUString xforms_string( const Any& rAny ) +{ + OUString aResult; + rAny >>= aResult; + return aResult; +} + +OUString xforms_bool( const Any& rAny ) +{ + bool bResult = bool(); + if( rAny >>= bResult ) + return GetXMLToken( bResult ? XML_TRUE : XML_FALSE ); + OSL_FAIL( "expected boolean value" ); + return OUString(); +} + +void xforms_formatDate( OUStringBuffer& aBuffer, const util::Date& rDate ) +{ + aBuffer.append( OUString::number( rDate.Year ) + + "-" + OUString::number( rDate.Month ) + + "-" + OUString::number( rDate.Day ) ); +} + +void xforms_formatTime( OUStringBuffer& aBuffer, const css::util::Time& rTime ) +{ + Duration aDuration; + aDuration.Hours = rTime.Hours; + aDuration.Minutes = rTime.Minutes; + aDuration.Seconds = rTime.Seconds; + aDuration.NanoSeconds = rTime.NanoSeconds; + ::sax::Converter::convertDuration( aBuffer, aDuration ); +} + +void xforms_formatDateTime( OUStringBuffer& aBuffer, const util::DateTime& aDateTime ) +{ + ::sax::Converter::convertDateTime(aBuffer, aDateTime, nullptr); +} + +OUString xforms_whitespace( const Any& rAny ) +{ + OUString sResult; + sal_uInt16 n = sal_uInt16(); + if( rAny >>= n ) + { + switch( n ) + { + case css::xsd::WhiteSpaceTreatment::Preserve: + sResult = GetXMLToken( XML_PRESERVE ); + break; + case css::xsd::WhiteSpaceTreatment::Replace: + sResult = GetXMLToken( XML_REPLACE ); + break; + case css::xsd::WhiteSpaceTreatment::Collapse: + sResult = GetXMLToken( XML_COLLAPSE ); + break; + } + } + return sResult; +} + + +/// return name of Binding +static OUString lcl_getXFormsBindName( const Reference& xBinding ) +{ + OUString sProp( "BindingID" ); + + OUString sReturn; + if( xBinding.is() && + xBinding->getPropertySetInfo()->hasPropertyByName( sProp ) ) + { + xBinding->getPropertyValue( sProp ) >>= sReturn; + } + return sReturn; +} + +// return name of binding +OUString getXFormsBindName( const Reference& xControl ) +{ + Reference xBindable( xControl, UNO_QUERY ); + return xBindable.is() + ? lcl_getXFormsBindName( + Reference( xBindable->getValueBinding(), UNO_QUERY )) + : OUString(); +} + +// return name of list binding +OUString getXFormsListBindName( const Reference& xControl ) +{ + Reference xListEntrySink( xControl, UNO_QUERY ); + return xListEntrySink.is() + ? lcl_getXFormsBindName( + Reference( xListEntrySink->getListEntrySource(), + UNO_QUERY ) ) + : OUString(); +} + +OUString getXFormsSubmissionName( const Reference& xBinding ) +{ + OUString sReturn; + + Reference xSubmissionSupplier( xBinding, UNO_QUERY ); + if( xSubmissionSupplier.is() ) + { + Reference xPropertySet( + xSubmissionSupplier->getSubmission(), UNO_QUERY ); + OUString sProp( "ID" ); + if( xPropertySet.is() && + xPropertySet->getPropertySetInfo()->hasPropertyByName( sProp ) ) + { + xPropertySet->getPropertyValue( sProp ) >>= sReturn; + } + } + + return sReturn; +} + +void getXFormsSettings( const Reference< XNameAccess >& _rXForms, Sequence< PropertyValue >& _out_rSettings ) +{ + _out_rSettings = Sequence< PropertyValue >(); + + OSL_PRECOND( _rXForms.is(), "getXFormsSettings: invalid XForms container!" ); + if ( !_rXForms.is() ) + return; + + try + { + // we want to export some special properties of our XForms models as config-item-map-named, + // which implies we need a PropertyValue whose value is an XNameAccess, whose keys + // are the names of the XForm models, and which in turn provides named sequences of + // PropertyValues - which denote the actual property values of the given named model. + + const Sequence< OUString > aModelNames( _rXForms->getElementNames() ); + + Reference< XNameContainer > xModelSettings = document::NamedPropertyValues::create( comphelper::getProcessComponentContext() ); + + for ( auto const & modelName : aModelNames ) + { + Reference< XPropertySet > xModelProps( _rXForms->getByName( modelName ), UNO_QUERY_THROW ); + + static constexpr OUStringLiteral sExternalData = u"ExternalData"; + Sequence aModelSettings{ comphelper::makePropertyValue( + sExternalData, xModelProps->getPropertyValue(sExternalData)) }; + + xModelSettings->insertByName( modelName, Any( aModelSettings ) ); + } + + if ( xModelSettings->hasElements() ) + { + _out_rSettings = { comphelper::makePropertyValue("XFormModels", xModelSettings) }; + } + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION("xmloff"); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/xforms/xformsimport.cxx b/xmloff/source/xforms/xformsimport.cxx new file mode 100644 index 000000000..69daa630b --- /dev/null +++ b/xmloff/source/xforms/xformsimport.cxx @@ -0,0 +1,184 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "XFormsModelContext.hxx" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "xformsapi.hxx" +#include +#include + +using std::pair; +using com::sun::star::uno::Reference; +using com::sun::star::uno::Exception; +using com::sun::star::uno::UNO_QUERY; +using com::sun::star::uno::UNO_QUERY_THROW; +using com::sun::star::uno::UNO_SET_THROW; +using com::sun::star::uno::Sequence; +using com::sun::star::beans::XPropertySet; +using com::sun::star::beans::XPropertySetInfo; +using com::sun::star::beans::PropertyValue; +using com::sun::star::frame::XModel; +using com::sun::star::container::XNameAccess; +using com::sun::star::form::binding::XValueBinding; +using com::sun::star::form::binding::XBindableValue; +using com::sun::star::form::binding::XListEntrySource; +using com::sun::star::form::binding::XListEntrySink; +using com::sun::star::form::submission::XSubmission; +using com::sun::star::form::submission::XSubmissionSupplier; + +SvXMLImportContext* createXFormsModelContext( + SvXMLImport& rImport ) +{ + return new XFormsModelContext( rImport ); +} + +void bindXFormsValueBinding(Reference const& xModel, + const pair, OUString>& aPair) +{ + Reference xBindable( + aPair.first, + UNO_QUERY ); + Reference xBinding( + xforms_findXFormsBinding( xModel, aPair.second ), + UNO_QUERY ); + + if( xBindable.is() && xBinding.is() ) + { + try + { + xBindable->setValueBinding( xBinding ); + } + catch( const Exception& ) + { + // ignore problems during binding + // TODO: call XML error handling + } + } +} + +void bindXFormsListBinding(Reference const& xModel, + const ::pair, OUString>& aPair) +{ + Reference xListEntrySink( + aPair.first, + UNO_QUERY ); + Reference xListEntrySource( + xforms_findXFormsBinding( xModel, aPair.second ), + UNO_QUERY ); + + if( xListEntrySink.is() && xListEntrySource.is() ) + { + try + { + xListEntrySink->setListEntrySource( xListEntrySource ); + } + catch( const Exception& ) + { + // ignore problems during binding + // TODO: call XML error handling + } + } +} + +void bindXFormsSubmission(Reference const& xModel, + const pair, OUString>& aPair) +{ + Reference xSubmissionSupp( aPair.first, UNO_QUERY ); + Reference xSubmission( + xforms_findXFormsSubmission( xModel, aPair.second ), + UNO_QUERY ); + + if( xSubmissionSupp.is() && xSubmission.is() ) + { + try + { + xSubmissionSupp->setSubmission( xSubmission ); + } + catch( const Exception& ) + { + // ignore problems during binding + // TODO: call XML error handling + } + } +} + +void applyXFormsSettings( const Reference< XNameAccess >& _rXForms, const Sequence< PropertyValue >& _rSettings ) +{ + OSL_PRECOND( _rXForms.is(), "applyXFormsSettings: invalid XForms container!" ); + if ( !_rXForms.is() ) + return; + + Reference< XNameAccess > xModelSettings( ::comphelper::NamedValueCollection::get( _rSettings, u"XFormModels" ), UNO_QUERY ); + if ( !xModelSettings.is() ) + { + OSL_FAIL( "applyXFormsSettings: wrong type for the XFormModels settings!" ); + return; + } + + try + { + const Sequence< OUString > aSettingsForModels( xModelSettings->getElementNames() ); + for ( auto const & modelName : aSettingsForModels ) + { + // the settings for this particular model + Sequence< PropertyValue > aModelSettings; + OSL_VERIFY( xModelSettings->getByName( modelName ) >>= aModelSettings ); + + // the model itself + if ( !_rXForms->hasByName( modelName ) ) + { + OSL_FAIL( "applyXFormsSettings: have settings for a non-existent XForms model!" ); + continue; + } + + // propagate the settings, being tolerant by omitting properties which are not supported + Reference< XPropertySet > xModelProps( _rXForms->getByName( modelName ), UNO_QUERY_THROW ); + Reference< XPropertySetInfo > xModelPSI( xModelProps->getPropertySetInfo(), UNO_SET_THROW ); + + for ( auto const & setting : std::as_const(aModelSettings) ) + { + if ( !xModelPSI->hasPropertyByName( setting.Name ) ) + { + OSL_FAIL( "applyXFormsSettings: non-existent model property!" ); + continue; + } + + xModelProps->setPropertyValue( setting.Name, setting.Value ); + } + } + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION("xmloff"); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3