32#include <rtl/ustring.hxx>
33#include <rtl/ustrbuf.hxx>
38#include <com/sun/star/xml/dom/XNode.hpp>
39#include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
40#include <com/sun/star/xml/dom/XDocumentFragment.hpp>
41#include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
42#include <com/sun/star/xml/xpath/XXPathObject.hpp>
43#include <com/sun/star/xml/xpath/XPathObjectType.hpp>
44#include <com/sun/star/beans/PropertyValue.hpp>
45#include <com/sun/star/io/TextInputStream.hpp>
46#include <com/sun/star/container/XEnumeration.hpp>
47#include <com/sun/star/container/XNameContainer.hpp>
48#include <com/sun/star/frame/XModel.hpp>
49#include <com/sun/star/xforms/XFormsSupplier.hpp>
50#include <com/sun/star/xforms/XDataTypeRepository.hpp>
51#include <com/sun/star/xsd/XDataType.hpp>
52#include <com/sun/star/xsd/DataTypeClass.hpp>
55using com::sun::star::beans::PropertyValue;
56using com::sun::star::io::TextInputStream;
57using com::sun::star::io::XTextInputStream2;
58using com::sun::star::container::XEnumeration;
59using com::sun::star::container::XNameContainer;
60using com::sun::star::xforms::XFormsSupplier;
71OUString Model::getDefaultServiceNameForNode(
const css::uno::Reference<css::xml::dom::XNode>& xNode )
74 OUString sService =
"com.sun.star.form.component.TextField";
77 OSL_ENSURE(
mxDataTypes.is(),
"no type repository?" );
81 OSL_ENSURE(
mxDataTypes->getDataType( sTypeName ).is(),
84 switch(
mxDataTypes->getDataType( sTypeName )->getTypeClass() )
86 case css::xsd::DataTypeClass::BOOLEAN:
87 sService =
"com.sun.star.form.component.CheckBox";
89 case css::xsd::DataTypeClass::DOUBLE:
90 case css::xsd::DataTypeClass::DECIMAL:
91 case css::xsd::DataTypeClass::FLOAT:
92 sService =
"com.sun.star.form.component.NumericField";
95 case css::xsd::DataTypeClass::STRING:
96 case css::xsd::DataTypeClass::DURATION:
97 case css::xsd::DataTypeClass::DATETIME:
98 case css::xsd::DataTypeClass::TIME:
99 case css::xsd::DataTypeClass::DATE:
100 case css::xsd::DataTypeClass::gYearMonth:
101 case css::xsd::DataTypeClass::gYear:
102 case css::xsd::DataTypeClass::gMonthDay:
103 case css::xsd::DataTypeClass::gDay:
104 case css::xsd::DataTypeClass::gMonth:
105 case css::xsd::DataTypeClass::hexBinary:
106 case css::xsd::DataTypeClass::base64Binary:
107 case css::xsd::DataTypeClass::anyURI:
108 case css::xsd::DataTypeClass::QName:
109 case css::xsd::DataTypeClass::NOTATION:
123 OSL_ENSURE( xNode->getParentNode().is(),
"need parent" );
126 sal_Int32 nFound = 0;
127 sal_Int32 nPosition = -1;
128 if( xNode->getParentNode().is() )
132 xIter = xIter->getNextSibling() )
134 if( xIter->getNodeType() == xNode->getNodeType() &&
135 xIter->getNodeName() == xNode->getNodeName() &&
136 xIter->getNamespaceURI() == xNode->getNamespaceURI() )
144 OSL_ENSURE( nFound > 0 && nPosition > 0,
"node not found???" );
149 rBuffer.insert( 0,
']' );
150 rBuffer.insert( 0, nPosition );
151 rBuffer.insert( 0,
'[' );
158 rBuffer.insert( 0, xNode->getNodeName() );
159 OUString
sPrefix = xNode->getPrefix();
162 rBuffer.insert( 0,
sPrefix +
":" );
175 rBuffer.insert( 0,
"')" );
178 OUString sInstanceName;
181 while( sInstanceName.isEmpty() && xEnum->hasMoreElements() )
184 xEnum->nextElement() >>= aValues;
192 if( xInstance == xDoc )
196 rBuffer.insert( 0,
"instance('" + sInstanceName );
203 OSL_ENSURE( xNode.is(),
"need node" );
211 xCurrent = xCurrent->getParentNode() )
217 switch( xCurrent->getNodeType() )
219 case NodeType_ELEMENT_NODE:
224 case NodeType_TEXT_NODE:
229 case NodeType_ATTRIBUTE_NODE:
234 case NodeType_DOCUMENT_NODE:
241 OSL_FAIL(
"unknown node type!" );
246 return aBuffer.makeStringAndClear();
257 sal_Int32
nLength = rString.getLength();
260 bool bWhitespace =
true;
261 for( sal_Int32
i = 0; bWhitespace && (
i <
nLength );
i++ )
264 bWhitespace = ( c ==
u'\x0009' ||
277 switch( xNode->getNodeType() )
279 case NodeType_ELEMENT_NODE:
283 case NodeType_TEXT_NODE:
285 OUString sContent = xNode->getNodeValue();
293 case NodeType_ATTRIBUTE_NODE:
298 case NodeType_DOCUMENT_NODE:
307 OSL_FAIL(
"unknown node type!" );
311 return aBuffer.makeStringAndClear();
318 switch( xNode->getNodeType() )
320 case NodeType_ELEMENT_NODE:
321 case NodeType_ATTRIBUTE_NODE:
325 case NodeType_TEXT_NODE:
326 case NodeType_DOCUMENT_NODE:
329 OSL_FAIL(
"no name for this node type!" );
333 return aBuffer.makeStringAndClear();
340 xBinding->getPropertyValue(
"BindingID" ) >>= sID;
341 OUString sExpression;
342 xBinding->getPropertyValue(
"BindingExpression" ) >>= sExpression;
347 sRet = sID +
" (" + sExpression +
") ";
359 xSubmission->getPropertyValue(
"ID" ) >>= sID;
363css::uno::Reference< ::css::beans::XPropertySet >
Model::cloneBindingAsGhost(
const css::uno::Reference< ::css::beans::XPropertySet > &xBinding )
371 pBinding->deferNotifications(
true);
375 copy( xBinding, xNewBinding );
381 Binding* pBinding = comphelper::getFromUnoTunnel<Binding>( xBinding );
382 if( pBinding !=
nullptr )
390 const OUString& sURL,
394 css::uno::Reference<css::xml::dom::XDocument> xInstance =
getDocumentBuilder()->newDocument();
395 DBG_ASSERT( xInstance.is(),
"failed to create DOM instance" );
402 bool bOnce = bURLOnce;
404 sal_Int32 nInstance =
mxInstances->addItem( aSequence );
412 std::u16string_view rName )
418 bFound = (
pValues[
n].Name == rName );
420 return bFound ? (
n - 1) : -1;
424 std::u16string_view rName )
433 bFound = (
sName == rName );
435 return bFound ? (
n - 1 ) : -1;
440 const OUString& sURL,
448 PropertyValue* pSeq =
aSeq.getArray();
456 pSeq =
aSeq.getArray();
462 pSeq[ nProp ].Value <<= sTo;
467 pSeq[ nProp ].Value <<= sURL;
472 pSeq[ nProp ].Value <<= bURLOnce;
492 xRet = xSupplier->getXForms();
498 const OUString& sName )
500 css::uno::Reference<css::xforms::XModel>
xModel;
503 && ! xModels->hasByName(
sName ) )
508 pModel->setID(
sName );
509 pModel->newInstance( OUString(), OUString(),
false );
510 pModel->initialize();
518 const OUString& sFrom,
519 const OUString& sTo )
523 && xModels->hasByName( sFrom )
524 && ! xModels->hasByName( sTo ) )
528 xModels->insertByName( sTo,
Any(
xModel ) );
529 xModels->removeByName( sFrom );
534 const OUString& sName )
538 && xModels->hasByName(
sName ) )
540 xModels->removeByName(
sName );
544css::uno::Reference<css::xml::dom::XNode>
Model::createElement(
const css::uno::Reference<css::xml::dom::XNode>& xParent,
545 const OUString& sName )
552 xNode = xParent->getOwnerDocument()->createElement(
sName );
557css::uno::Reference<css::xml::dom::XNode>
Model::createAttribute(
const css::uno::Reference<css::xml::dom::XNode>& xParent,
558 const OUString& sName )
568 OUString sUniqueName =
sName;
569 while( xElement->hasAttribute( sUniqueName ) )
576 xNode = xParent->getOwnerDocument()->createAttribute( sUniqueName );
581css::uno::Reference<css::xml::dom::XNode>
Model::renameNode(
const css::uno::Reference<css::xml::dom::XNode>& xNode,
582 const OUString& sName )
585 if( xNode->getNodeName() ==
sName )
589 if( xNode->getNodeType() == NodeType_ATTRIBUTE_NODE
590 && xNode->getParentNode().is()
595 OUString sOldDefaultBindingExpression =
600 if( xNode->getNodeType() == NodeType_ELEMENT_NODE )
607 OSL_ENSURE( xNode.is(),
"no element?" );
610 sal_Int32
nLength = xMap.is() ? xMap->getLength() : 0;
616 xElem->setAttributeNode( xOldElem->removeAttributeNode( xAttr ) );
622 xCurrent = xNode->getFirstChild() )
624 xNew->appendChild( xNode->removeChild( xCurrent ) );
627 xNode->getParentNode()->replaceChild( xNew, xNode );
629 else if( xNode->getNodeType() == NodeType_ATTRIBUTE_NODE )
633 xAttr->setValue( xNode->getNodeValue() );
637 xParent->removeChild( xNode );
638 xNew = xParent->appendChild( xAttr );
642 OSL_FAIL(
"can't rename this node type" );
649 OUString sNewDefaultBindingExpression =
653 Binding* pBinding = comphelper::getFromUnoTunnel<Binding>(
654 mxBindings->Collection<XPropertySet_t>::getItem(
n ) );
657 == sOldDefaultBindingExpression )
663 return xNew.is() ? xNew : xNode;
669 OSL_ENSURE( xNode.is(),
"no node?" );
676 sal_Int32 nBestScore = 0;
680 Binding* pBinding = comphelper::getFromUnoTunnel<Binding>(
681 mxBindings->Collection<XPropertySet_t>::getItem(
n ) );
683 OSL_ENSURE( pBinding !=
nullptr,
"no binding?" );
686 sal_Int32 nNodes = xNodeList.is() ? xNodeList->getLength() : 0;
687 if( nNodes > 0 && xNodeList->item( 0 ) == xNode )
693 sal_Int32 nScore = 0;
700 if( nScore > nBestScore )
702 pBestBinding = pBinding;
709 OSL_ENSURE( ( nBestScore == 0 ) == ( pBestBinding ==
nullptr ),
710 "score != binding?" );
711 if( bCreate && pBestBinding ==
nullptr )
714 pBestBinding->setBindingExpression(
730 OSL_ENSURE( _rxAttrNode.is(),
"lcl_serializeForDisplay( attr ): invalid argument!" );
731 if ( _rxAttrNode.is() )
733 OUString sValue = _rxAttrNode->getValue();
735 if ( sValue.indexOf( nQuote ) >= 0 )
738 sResult = _rxAttrNode->getName() +
"=" + OUStringChar(nQuote) + sValue + OUStringChar(nQuote) +
" ";
745 OUStringBuffer sResult;
750 xDocument->createDocumentFragment() );
751 OSL_ENSURE( xFragment.is(),
"xFragment" );
753 sal_Int32 nAttributeNodes = 0;
756 sal_Int32
nLength = xNodes->getLength();
761 switch ( xCurrent->getNodeType() )
763 case NodeType_DOCUMENT_NODE:
765 xCurrent = xCurrent->getFirstChild();
767 case NodeType_ATTRIBUTE_NODE:
783 xFragment->appendChild( xDocument->importNode( xCurrent,
true ) );
785 OSL_ENSURE( ( nAttributeNodes == 0 ) || ( nAttributeNodes ==
nLength ),
786 "lcl_serializeForDisplay: mixed attribute and non-attribute nodes?" );
787 if ( nAttributeNodes )
789 return sResult.makeStringAndClear();
798 xTextInputStream->setInputStream( aSerialization.
getInputStream() );
810 sResult.setLength(0);
811 while( ! xTextInputStream->isEOF() )
813 OUString sLine = xTextInputStream->readLine();
815 && !sLine.startsWith(
"<?xml" ) )
817 sResult.append( sLine +
"\n" );
821 return sResult.makeStringAndClear();
828 return getResource( RID_STR_XFORMS_CANT_EVALUATE );
831 switch( xResult->getObjectType() )
833 case XPathObjectType_XPATH_BOOLEAN:
834 return OUString::boolean(xResult->getBoolean());
836 case XPathObjectType_XPATH_STRING:
837 return "\"" + xResult->getString() +
"\"";
839 case XPathObjectType_XPATH_NODESET:
842 case XPathObjectType_XPATH_NUMBER:
843 return OUString::number(xResult->getDouble());
845 case XPathObjectType_XPATH_UNDEFINED:
846 case XPathObjectType_XPATH_POINT:
847 case XPathObjectType_XPATH_RANGE:
848 case XPathObjectType_XPATH_LOCATIONSET:
849 case XPathObjectType_XPATH_USERS:
850 case XPathObjectType_XPATH_XSLT_TREE:
858 const css::uno::Reference< ::css::beans::XPropertySet >& xBinding,
860 const OUString& sExpression )
862 Binding* pBinding = comphelper::getFromUnoTunnel<Binding>( xBinding );
863 if( pBinding ==
nullptr )
864 throw RuntimeException();
870 if( bIsBindingExpression )
879 std::vector<EvaluationContext> aContext =
881 for (
auto const& elem : aContext)
888 return aBuffer.makeStringAndClear();
902 const css::uno::Reference< ::css::xml::dom::XNode >& xNode,
903 const OUString& sValue )
919 sal_Int32 nValues = aValues.getLength();
920 const PropertyValue*
pValues = aValues.getConstArray();
921 for( sal_Int32 n = 0;
n < nValues;
n++ )
923 const PropertyValue& rValue =
pValues[
n];
924 if( pID !=
nullptr && rValue.Name ==
"ID")
925 rValue.Value >>= *pID;
926 if( pInstance !=
nullptr && rValue.Name ==
"Instance")
927 rValue.Value >>= *pInstance;
928 if( pURL !=
nullptr && rValue.Name ==
"URL")
929 rValue.Value >>= *pURL;
930 if( pURLOnce !=
nullptr && rValue.Name ==
"URLOnce")
931 rValue.Value >>= *pURLOnce;
937 const OUString* _pID,
939 const OUString* _pURL,
940 const bool* _pURLOnce )
946 bool bURLOnce =
false;
948 const OUString* pID = !sID.isEmpty() ? &sID :
nullptr;
950 const OUString* pURL = !sURL.isEmpty() ? &sURL :
nullptr;
951 const bool* pURLOnce = ( bURLOnce && pURL != nullptr ) ? &bURLOnce :
nullptr;
956 if (_pInstance !=
nullptr)
957 pInstance = _pInstance;
958 if (_pURL !=
nullptr)
960 if (_pURLOnce !=
nullptr)
961 pURLOnce = _pURLOnce;
967 if (pInstance !=
nullptr)
971 if (pURLOnce !=
nullptr)
975 aSequence.realloc( nCount );
976 PropertyValue* pSequence = aSequence.getArray();
980 pSequence[
nIndex ].Name =
"ID";
981 pSequence[
nIndex ].Value <<= *pID;
984 if(pInstance !=
nullptr)
986 pSequence[
nIndex ].Name =
"Instance";
987 pSequence[
nIndex ].Value <<= *pInstance;
992 pSequence[
nIndex ].Name =
"URL";
993 pSequence[
nIndex ].Value <<= *pURL;
996 if(pURLOnce !=
nullptr)
998 pSequence[
nIndex ].Name =
"URLOnce";
999 pSequence[
nIndex ].Value <<= *pURLOnce;
const PropertyValue * pValues
virtual void serialize() override
start the serialization process
virtual css::uno::Reference< css::io::XInputStream > getInputStream() override
get the serialized bytes.
void setSource(const css::uno::Reference< css::xml::dom::XDocumentFragment > &aFragment)
sets the XObject that is to serialized
sal_Int32 countItems() const
const T & getItem(sal_Int32 n) const
define the context for the evaluation of an XPath expression
css::uno::Reference< css::xml::dom::XNode > mxContextNode
#define DBG_ASSERT(sCon, aError)
Sequence< sal_Int8 > aSeq
static void lcl_OutInstance(OUStringBuffer &rBuffer, const Reference< XNode > &xNode, Model *pModel)
static bool lcl_isWhitespace(const OUString &rString)
static OUString lcl_serializeForDisplay(const Reference< XAttr > &_rxAttrNode)
static sal_Int32 lcl_findProp(const PropertyValue *pValues, sal_Int32 nLength, std::u16string_view rName)
static Reference< XNameContainer > lcl_getModels(const Reference< css::frame::XModel > &xComponent)
static void lcl_OutName(OUStringBuffer &rBuffer, const Reference< XNode > &xNode)
static void lcl_OutPosition(OUStringBuffer &rBuffer, const Reference< XNode > &xNode)
Reference< XComponentContext > getProcessComponentContext()
Reference< XModel > xModel
std::unique_ptr< char[]> aBuffer
bool isValidPrefixName(const OUString &sName, const Reference< XNameContainer > &)
Reference< XDocumentBuilder > getDocumentBuilder()
bool isValidQName(const OUString &sName, const Reference< XNameContainer > &)