xref: /aoo41x/main/forms/source/xforms/model_ui.cxx (revision 24acc546)
1*24acc546SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*24acc546SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*24acc546SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*24acc546SAndrew Rist  * distributed with this work for additional information
6*24acc546SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*24acc546SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*24acc546SAndrew Rist  * "License"); you may not use this file except in compliance
9*24acc546SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*24acc546SAndrew Rist  *
11*24acc546SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*24acc546SAndrew Rist  *
13*24acc546SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*24acc546SAndrew Rist  * software distributed under the License is distributed on an
15*24acc546SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*24acc546SAndrew Rist  * KIND, either express or implied.  See the License for the
17*24acc546SAndrew Rist  * specific language governing permissions and limitations
18*24acc546SAndrew Rist  * under the License.
19*24acc546SAndrew Rist  *
20*24acc546SAndrew Rist  *************************************************************/
21*24acc546SAndrew Rist 
22*24acc546SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_forms.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "model.hxx"
28cdf0e10cSrcweir #include "model_helper.hxx"
29cdf0e10cSrcweir #include "mip.hxx"
30cdf0e10cSrcweir #include "evaluationcontext.hxx"
31cdf0e10cSrcweir #include "unohelper.hxx"
32cdf0e10cSrcweir #include "submission/serialization_app_xml.hxx"
33cdf0e10cSrcweir #include "resourcehelper.hxx"
34cdf0e10cSrcweir #include "xmlhelper.hxx"
35cdf0e10cSrcweir #include "convert.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <rtl/ustring.hxx>
38cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
39cdf0e10cSrcweir #include <tools/debug.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // UNO classes
42cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNode.hpp>
43cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
44cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocumentFragment.hpp>
45cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
46cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XXPathObject.hpp>
47cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XPathObjectType.hpp>
48cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
49cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
50cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSink.hpp>
51cdf0e10cSrcweir #include <com/sun/star/io/XTextInputStream.hpp>
52cdf0e10cSrcweir #include <com/sun/star/container/XEnumeration.hpp>
53cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
54cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
55cdf0e10cSrcweir #include <com/sun/star/xforms/XFormsSupplier.hpp>
56cdf0e10cSrcweir #include <com/sun/star/xforms/XDataTypeRepository.hpp>
57cdf0e10cSrcweir #include <com/sun/star/xsd/XDataType.hpp>
58cdf0e10cSrcweir #include <com/sun/star/xsd/DataTypeClass.hpp>
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 
61cdf0e10cSrcweir using rtl::OUString;
62cdf0e10cSrcweir using rtl::OUStringBuffer;
63cdf0e10cSrcweir using com::sun::star::beans::PropertyValue;
64cdf0e10cSrcweir using com::sun::star::io::XInputStream;
65cdf0e10cSrcweir using com::sun::star::io::XActiveDataSink;
66cdf0e10cSrcweir using com::sun::star::io::XTextInputStream;
67cdf0e10cSrcweir using com::sun::star::container::XEnumeration;
68cdf0e10cSrcweir using com::sun::star::container::XNameContainer;
69cdf0e10cSrcweir using com::sun::star::xforms::XFormsSupplier;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir using namespace xforms;
72cdf0e10cSrcweir using namespace com::sun::star::uno;
73cdf0e10cSrcweir using namespace com::sun::star::xml::dom;
74cdf0e10cSrcweir using namespace com::sun::star::xml::xpath;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 
78cdf0e10cSrcweir //
79cdf0e10cSrcweir // implement XFormsUIHelper1
80cdf0e10cSrcweir //
81cdf0e10cSrcweir 
getDefaultServiceNameForNode(const XNode_t & xNode)82cdf0e10cSrcweir OUString Model::getDefaultServiceNameForNode( const XNode_t& xNode )
83cdf0e10cSrcweir     throw( RuntimeException )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir     // determine service for control. string/text field is default.
86cdf0e10cSrcweir     OUString sService = OUSTRING("com.sun.star.form.component.TextField");
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     // query repository for suitable type
89cdf0e10cSrcweir     OSL_ENSURE( mxDataTypes.is(), "no type repository?" );
90cdf0e10cSrcweir     OUString sTypeName = queryMIP( xNode ).getTypeName();
91cdf0e10cSrcweir     if( mxDataTypes->hasByName( sTypeName ) )
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         OSL_ENSURE( mxDataTypes->getDataType( sTypeName ).is(),
94cdf0e10cSrcweir                     "has or has not?" );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         switch( mxDataTypes->getDataType( sTypeName )->getTypeClass() )
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::BOOLEAN:
99cdf0e10cSrcweir             sService = OUSTRING("com.sun.star.form.component.CheckBox");
100cdf0e10cSrcweir             break;
101cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::DOUBLE:
102cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::DECIMAL:
103cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::FLOAT:
104cdf0e10cSrcweir             sService = OUSTRING("com.sun.star.form.component.NumericField");
105cdf0e10cSrcweir             break;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::STRING:
108cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::DURATION:
109cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::DATETIME:
110cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::TIME:
111cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::DATE:
112cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::gYearMonth:
113cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::gYear:
114cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::gMonthDay:
115cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::gDay:
116cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::gMonth:
117cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::hexBinary:
118cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::base64Binary:
119cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::anyURI:
120cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::QName:
121cdf0e10cSrcweir         case com::sun::star::xsd::DataTypeClass::NOTATION:
122cdf0e10cSrcweir         default:
123cdf0e10cSrcweir             // keep default
124cdf0e10cSrcweir             break;
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     return sService;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
lcl_OutPosition(OUStringBuffer & rBuffer,const Reference<XNode> & xNode)132cdf0e10cSrcweir void lcl_OutPosition( OUStringBuffer& rBuffer,
133cdf0e10cSrcweir                       const Reference<XNode>& xNode )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     OSL_ENSURE( xNode->getParentNode().is(), "need parent" );
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     // count # of occurences of this node
138cdf0e10cSrcweir     sal_Int32 nFound = 0;
139cdf0e10cSrcweir     sal_Int32 nPosition = -1;
140cdf0e10cSrcweir     if( xNode->getParentNode().is() )
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         for( Reference<XNode> xIter = xNode->getParentNode()->getFirstChild();
143cdf0e10cSrcweir              xIter != NULL;
144cdf0e10cSrcweir              xIter = xIter->getNextSibling() )
145cdf0e10cSrcweir         {
146cdf0e10cSrcweir             if( xIter->getNodeType() == xNode->getNodeType() &&
147cdf0e10cSrcweir                 xIter->getNodeName() == xNode->getNodeName() &&
148cdf0e10cSrcweir                 xIter->getNamespaceURI() == xNode->getNamespaceURI() )
149cdf0e10cSrcweir             {
150cdf0e10cSrcweir                 nFound++;
151cdf0e10cSrcweir                 if( xIter == xNode )
152cdf0e10cSrcweir                     nPosition = nFound;
153cdf0e10cSrcweir             }
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir     OSL_ENSURE( nFound > 0  &&  nPosition > 0, "node not found???" );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     // output position (if necessary)
159cdf0e10cSrcweir     if( nFound > 1 )
160cdf0e10cSrcweir     {
161cdf0e10cSrcweir         rBuffer.insert( 0, sal_Unicode(']') );
162cdf0e10cSrcweir         rBuffer.insert( 0, nPosition );
163cdf0e10cSrcweir         rBuffer.insert( 0, sal_Unicode('[') );
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
lcl_OutName(OUStringBuffer & rBuffer,const Reference<XNode> & xNode)167cdf0e10cSrcweir void lcl_OutName( OUStringBuffer& rBuffer,
168cdf0e10cSrcweir                   const Reference<XNode>& xNode )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir     rBuffer.insert( 0, xNode->getNodeName() );
171cdf0e10cSrcweir     OUString sPrefix = xNode->getPrefix();
172cdf0e10cSrcweir     if( sPrefix.getLength() > 0 )
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         rBuffer.insert( 0, sal_Unicode(':') );
175cdf0e10cSrcweir         rBuffer.insert( 0, sPrefix );
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
lcl_OutInstance(OUStringBuffer & rBuffer,const Reference<XNode> & xNode,Model * pModel)179cdf0e10cSrcweir void lcl_OutInstance( OUStringBuffer& rBuffer,
180cdf0e10cSrcweir                       const Reference<XNode>& xNode,
181cdf0e10cSrcweir                       Model* pModel )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir     Reference<XDocument> xDoc = xNode->getOwnerDocument();
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     if( xDoc != pModel->getDefaultInstance() )
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         rBuffer.insert( 0, OUSTRING("')") );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         // iterate over instances, and find the right one
190cdf0e10cSrcweir         OUString sInstanceName;
191cdf0e10cSrcweir         Reference<XEnumeration> xEnum =
192cdf0e10cSrcweir             pModel->getInstances()->createEnumeration();
193cdf0e10cSrcweir         while( ( sInstanceName.getLength() == 0 ) && xEnum->hasMoreElements() )
194cdf0e10cSrcweir         {
195cdf0e10cSrcweir             Sequence<PropertyValue> aValues;
196cdf0e10cSrcweir             xEnum->nextElement() >>= aValues;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir             // get ID and instance
199cdf0e10cSrcweir             OUString sId;
200cdf0e10cSrcweir             Reference<XDocument> xInstance;
201cdf0e10cSrcweir             getInstanceData( aValues, &sId, &xInstance, NULL, NULL );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir             // now check whether this was our instance:
204cdf0e10cSrcweir             if( xInstance == xDoc )
205cdf0e10cSrcweir                 sInstanceName = sId;
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         rBuffer.insert( 0, sInstanceName );
209cdf0e10cSrcweir         rBuffer.insert( 0, OUSTRING("instance('") );
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
getDefaultBindingExpressionForNode(const XNode_t & xNode,const EvaluationContext & rContext)213cdf0e10cSrcweir OUString Model::getDefaultBindingExpressionForNode(
214cdf0e10cSrcweir     const XNode_t& xNode,
215cdf0e10cSrcweir     const EvaluationContext& rContext)
216cdf0e10cSrcweir {
217cdf0e10cSrcweir     OSL_ENSURE( xNode.is(), "need node" );
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     // iterate upwards and put sections into the expression buffer.
220cdf0e10cSrcweir     // Stop iteration either at context node (relative expression) or
221cdf0e10cSrcweir     // at document root, whichever occurs first.
222cdf0e10cSrcweir     OUStringBuffer aBuffer;
223cdf0e10cSrcweir     for( Reference<XNode> xCurrent = xNode;
224cdf0e10cSrcweir          xCurrent.is()  &&  xCurrent != rContext.mxContextNode;
225cdf0e10cSrcweir          xCurrent = xCurrent->getParentNode() )
226cdf0e10cSrcweir     {
227cdf0e10cSrcweir         // insert a '/' for every step except the first
228cdf0e10cSrcweir         if( aBuffer.getLength() > 0 )
229cdf0e10cSrcweir             aBuffer.insert( 0, sal_Unicode('/') );
230cdf0e10cSrcweir 
231cdf0e10cSrcweir         switch( xCurrent->getNodeType() )
232cdf0e10cSrcweir         {
233cdf0e10cSrcweir         case NodeType_ELEMENT_NODE:
234cdf0e10cSrcweir             lcl_OutPosition( aBuffer, xCurrent );
235cdf0e10cSrcweir             lcl_OutName( aBuffer, xCurrent );
236cdf0e10cSrcweir             break;
237cdf0e10cSrcweir 
238cdf0e10cSrcweir         case NodeType_TEXT_NODE:
239cdf0e10cSrcweir             lcl_OutPosition( aBuffer, xCurrent );
240cdf0e10cSrcweir             aBuffer.insert( 0, OUSTRING("text()") );
241cdf0e10cSrcweir             break;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         case NodeType_ATTRIBUTE_NODE:
244cdf0e10cSrcweir             lcl_OutName( aBuffer, xCurrent );
245cdf0e10cSrcweir             aBuffer.insert( 0, sal_Unicode('@') );
246cdf0e10cSrcweir             break;
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         case NodeType_DOCUMENT_NODE:
249cdf0e10cSrcweir             // check for which instance we have
250cdf0e10cSrcweir             lcl_OutInstance( aBuffer, xCurrent, this );
251cdf0e10cSrcweir             break;
252cdf0e10cSrcweir 
253cdf0e10cSrcweir         default:
254cdf0e10cSrcweir             // unknown type? fail!
255cdf0e10cSrcweir             OSL_ENSURE( false, "unknown node type!" );
256cdf0e10cSrcweir             xCurrent.set( NULL );
257cdf0e10cSrcweir             aBuffer.makeStringAndClear();
258cdf0e10cSrcweir             // we'll remove the slash below
259cdf0e10cSrcweir             aBuffer.insert( 0, sal_Unicode('/') );
260cdf0e10cSrcweir             break;
261cdf0e10cSrcweir         }
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
265cdf0e10cSrcweir }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 
getDefaultBindingExpressionForNode(const XNode_t & xNode)269cdf0e10cSrcweir OUString Model::getDefaultBindingExpressionForNode( const XNode_t& xNode )
270cdf0e10cSrcweir     throw( RuntimeException )
271cdf0e10cSrcweir {
272cdf0e10cSrcweir     return getDefaultBindingExpressionForNode( xNode, getEvaluationContext() );
273cdf0e10cSrcweir }
274cdf0e10cSrcweir 
lcl_isWhitespace(const OUString & rString)275cdf0e10cSrcweir bool lcl_isWhitespace( const OUString& rString )
276cdf0e10cSrcweir {
277cdf0e10cSrcweir     sal_Int32 nLength = rString.getLength();
278cdf0e10cSrcweir     const sal_Unicode* pStr = rString.getStr();
279cdf0e10cSrcweir 
280cdf0e10cSrcweir     bool bWhitespace = true;
281cdf0e10cSrcweir     for( sal_Int32 i = 0; bWhitespace && ( i < nLength ); i++ )
282cdf0e10cSrcweir     {
283cdf0e10cSrcweir         sal_Unicode c = pStr[i];
284cdf0e10cSrcweir         bWhitespace = ( c == sal_Unicode(0x09) ||
285cdf0e10cSrcweir                         c == sal_Unicode(0x0A) ||
286cdf0e10cSrcweir                         c == sal_Unicode(0x0D) ||
287cdf0e10cSrcweir                         c == sal_Unicode(0x20) );
288cdf0e10cSrcweir     }
289cdf0e10cSrcweir     return bWhitespace;
290cdf0e10cSrcweir }
291cdf0e10cSrcweir 
getNodeDisplayName(const XNode_t & xNode,sal_Bool bDetail)292cdf0e10cSrcweir OUString Model::getNodeDisplayName( const XNode_t& xNode,
293cdf0e10cSrcweir                                     sal_Bool bDetail )
294cdf0e10cSrcweir     throw( RuntimeException )
295cdf0e10cSrcweir {
296cdf0e10cSrcweir     OUStringBuffer aBuffer;
297cdf0e10cSrcweir 
298cdf0e10cSrcweir     switch( xNode->getNodeType() )
299cdf0e10cSrcweir     {
300cdf0e10cSrcweir     case NodeType_ELEMENT_NODE:
301cdf0e10cSrcweir         lcl_OutName( aBuffer, xNode );
302cdf0e10cSrcweir         break;
303cdf0e10cSrcweir 
304cdf0e10cSrcweir     case NodeType_TEXT_NODE:
305cdf0e10cSrcweir         {
306cdf0e10cSrcweir             OUString sContent = xNode->getNodeValue();
307cdf0e10cSrcweir             if( bDetail || ! lcl_isWhitespace( sContent ) )
308cdf0e10cSrcweir             {
309cdf0e10cSrcweir                 aBuffer.append( sal_Unicode('"') );
310cdf0e10cSrcweir                 aBuffer.append( Convert::collapseWhitespace( sContent ) );
311cdf0e10cSrcweir                 aBuffer.append( sal_Unicode('"') );
312cdf0e10cSrcweir             }
313cdf0e10cSrcweir         }
314cdf0e10cSrcweir         break;
315cdf0e10cSrcweir 
316cdf0e10cSrcweir     case NodeType_ATTRIBUTE_NODE:
317cdf0e10cSrcweir         lcl_OutName( aBuffer, xNode );
318cdf0e10cSrcweir         aBuffer.insert( 0, sal_Unicode('@') );
319cdf0e10cSrcweir         break;
320cdf0e10cSrcweir 
321cdf0e10cSrcweir     case NodeType_DOCUMENT_NODE:
322cdf0e10cSrcweir         if( xNode == getDefaultInstance() )
323cdf0e10cSrcweir             aBuffer.append( sal_Unicode('/') );
324cdf0e10cSrcweir         else
325cdf0e10cSrcweir             lcl_OutInstance( aBuffer, xNode, this );
326cdf0e10cSrcweir         break;
327cdf0e10cSrcweir 
328cdf0e10cSrcweir     default:
329cdf0e10cSrcweir         // unknown type? fail!
330cdf0e10cSrcweir         OSL_ENSURE( false, "unknown node type!" );
331cdf0e10cSrcweir         break;
332cdf0e10cSrcweir     }
333cdf0e10cSrcweir 
334cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
335cdf0e10cSrcweir }
336cdf0e10cSrcweir 
getNodeName(const XNode_t & xNode)337cdf0e10cSrcweir OUString Model::getNodeName( const XNode_t& xNode )
338cdf0e10cSrcweir     throw( RuntimeException )
339cdf0e10cSrcweir {
340cdf0e10cSrcweir     OUStringBuffer aBuffer;
341cdf0e10cSrcweir 
342cdf0e10cSrcweir     switch( xNode->getNodeType() )
343cdf0e10cSrcweir     {
344cdf0e10cSrcweir     case NodeType_ELEMENT_NODE:
345cdf0e10cSrcweir     case NodeType_ATTRIBUTE_NODE:
346cdf0e10cSrcweir         lcl_OutName( aBuffer, xNode );
347cdf0e10cSrcweir         break;
348cdf0e10cSrcweir 
349cdf0e10cSrcweir     case NodeType_TEXT_NODE:
350cdf0e10cSrcweir     case NodeType_DOCUMENT_NODE:
351cdf0e10cSrcweir     default:
352cdf0e10cSrcweir         // unknown type? fail!
353cdf0e10cSrcweir         OSL_ENSURE( false, "no name for this node type!" );
354cdf0e10cSrcweir         break;
355cdf0e10cSrcweir     }
356cdf0e10cSrcweir 
357cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
358cdf0e10cSrcweir }
359cdf0e10cSrcweir 
getBindingName(const XPropertySet_t & xBinding,sal_Bool)360cdf0e10cSrcweir OUString Model::getBindingName( const XPropertySet_t& xBinding,
361cdf0e10cSrcweir                                 sal_Bool /*bDetail*/ )
362cdf0e10cSrcweir     throw( RuntimeException )
363cdf0e10cSrcweir {
364cdf0e10cSrcweir     OUString sID;
365cdf0e10cSrcweir     xBinding->getPropertyValue( OUSTRING("BindingID" ) ) >>= sID;
366cdf0e10cSrcweir     OUString sExpression;
367cdf0e10cSrcweir     xBinding->getPropertyValue( OUSTRING("BindingExpression" ) ) >>= sExpression;
368cdf0e10cSrcweir 
369cdf0e10cSrcweir     OUStringBuffer aBuffer;
370cdf0e10cSrcweir     if( sID.getLength() > 0 )
371cdf0e10cSrcweir     {
372cdf0e10cSrcweir         aBuffer.append( sID );
373cdf0e10cSrcweir         aBuffer.append( OUSTRING(" (" ));
374cdf0e10cSrcweir         aBuffer.append( sExpression );
375cdf0e10cSrcweir         aBuffer.append( OUSTRING(")" ));
376cdf0e10cSrcweir     }
377cdf0e10cSrcweir     else
378cdf0e10cSrcweir         aBuffer.append( sExpression );
379cdf0e10cSrcweir 
380cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
381cdf0e10cSrcweir }
382cdf0e10cSrcweir 
getSubmissionName(const XPropertySet_t & xSubmission,sal_Bool)383cdf0e10cSrcweir OUString Model::getSubmissionName( const XPropertySet_t& xSubmission,
384cdf0e10cSrcweir                                    sal_Bool /*bDetail*/ )
385cdf0e10cSrcweir     throw( RuntimeException )
386cdf0e10cSrcweir {
387cdf0e10cSrcweir     OUString sID;
388cdf0e10cSrcweir     xSubmission->getPropertyValue( OUSTRING("ID") ) >>= sID;
389cdf0e10cSrcweir     return sID;
390cdf0e10cSrcweir }
391cdf0e10cSrcweir 
cloneBindingAsGhost(const XPropertySet_t & xBinding)392cdf0e10cSrcweir Model::XPropertySet_t Model::cloneBindingAsGhost( const XPropertySet_t &xBinding )
393cdf0e10cSrcweir 	throw( RuntimeException )
394cdf0e10cSrcweir {
395cdf0e10cSrcweir 	// Create a new binding instance first...
396cdf0e10cSrcweir 	Binding *pBinding = new Binding();
397cdf0e10cSrcweir 
398cdf0e10cSrcweir 	// ...and bump up the "defered notification counter"
399cdf0e10cSrcweir 	// to prevent this binding from contributing to the
400cdf0e10cSrcweir 	// MIPs table...
401cdf0e10cSrcweir 	pBinding->deferNotifications(true);
402cdf0e10cSrcweir 
403cdf0e10cSrcweir 	// Copy the propertyset and return result...
404cdf0e10cSrcweir     XPropertySet_t xNewBinding(pBinding);
405cdf0e10cSrcweir     copy( xBinding, xNewBinding );
406cdf0e10cSrcweir     return xNewBinding;
407cdf0e10cSrcweir }
408cdf0e10cSrcweir 
removeBindingIfUseless(const XPropertySet_t & xBinding)409cdf0e10cSrcweir void Model::removeBindingIfUseless( const XPropertySet_t& xBinding )
410cdf0e10cSrcweir     throw( RuntimeException )
411cdf0e10cSrcweir {
412cdf0e10cSrcweir     Binding* pBinding = Binding::getBinding( xBinding );
413cdf0e10cSrcweir     if( pBinding != NULL )
414cdf0e10cSrcweir     {
415cdf0e10cSrcweir         if( ! pBinding->isUseful() )
416cdf0e10cSrcweir             mpBindings->removeItem( pBinding );
417cdf0e10cSrcweir     }
418cdf0e10cSrcweir }
419cdf0e10cSrcweir 
newInstance(const rtl::OUString & sName,const rtl::OUString & sURL,sal_Bool bURLOnce)420cdf0e10cSrcweir Model::XDocument_t Model::newInstance( const rtl::OUString& sName,
421cdf0e10cSrcweir                          const rtl::OUString& sURL,
422cdf0e10cSrcweir                          sal_Bool bURLOnce )
423cdf0e10cSrcweir     throw( RuntimeException )
424cdf0e10cSrcweir {
425cdf0e10cSrcweir     // create a default instance with <instanceData> element
426cdf0e10cSrcweir     XDocument_t xInstance = getDocumentBuilder()->newDocument();
427cdf0e10cSrcweir     DBG_ASSERT( xInstance.is(), "failed to create DOM instance" );
428cdf0e10cSrcweir 
429cdf0e10cSrcweir     Reference<XNode>( xInstance, UNO_QUERY_THROW )->appendChild(
430cdf0e10cSrcweir         Reference<XNode>( xInstance->createElement( OUSTRING("instanceData") ),
431cdf0e10cSrcweir                           UNO_QUERY_THROW ) );
432cdf0e10cSrcweir 
433cdf0e10cSrcweir     Sequence<PropertyValue> aSequence;
434cdf0e10cSrcweir     bool bOnce = bURLOnce; // bool, so we can take address in setInstanceData
435cdf0e10cSrcweir     setInstanceData( aSequence, &sName, &xInstance, &sURL, &bOnce );
436cdf0e10cSrcweir     sal_Int32 nInstance = mpInstances->addItem( aSequence );
437cdf0e10cSrcweir     loadInstance( nInstance );
438cdf0e10cSrcweir 
439cdf0e10cSrcweir     return xInstance;
440cdf0e10cSrcweir }
441cdf0e10cSrcweir 
lcl_findProp(const PropertyValue * pValues,sal_Int32 nLength,const rtl::OUString & rName)442cdf0e10cSrcweir sal_Int32 lcl_findProp( const PropertyValue* pValues,
443cdf0e10cSrcweir                         sal_Int32 nLength,
444cdf0e10cSrcweir                         const rtl::OUString& rName )
445cdf0e10cSrcweir {
446cdf0e10cSrcweir     bool bFound = false;
447cdf0e10cSrcweir     sal_Int32 n = 0;
448cdf0e10cSrcweir     for( ; !bFound && n < nLength; n++ )
449cdf0e10cSrcweir     {
450cdf0e10cSrcweir         bFound = ( pValues[n].Name == rName );
451cdf0e10cSrcweir     }
452cdf0e10cSrcweir     return bFound ? ( n - 1) : -1;
453cdf0e10cSrcweir }
454cdf0e10cSrcweir 
lcl_findInstance(const InstanceCollection * pInstances,const rtl::OUString & rName)455cdf0e10cSrcweir sal_Int32 xforms::lcl_findInstance( const InstanceCollection* pInstances,
456cdf0e10cSrcweir                                     const rtl::OUString& rName )
457cdf0e10cSrcweir {
458cdf0e10cSrcweir     sal_Int32 nLength = pInstances->countItems();
459cdf0e10cSrcweir     sal_Int32 n = 0;
460cdf0e10cSrcweir     bool bFound = false;
461cdf0e10cSrcweir     for( ; !bFound  &&  n < nLength; n++ )
462cdf0e10cSrcweir     {
463cdf0e10cSrcweir         OUString sName;
464cdf0e10cSrcweir         getInstanceData( pInstances->getItem( n ), &sName, NULL, NULL, NULL );
465cdf0e10cSrcweir         bFound = ( sName == rName );
466cdf0e10cSrcweir     }
467cdf0e10cSrcweir     return bFound ? ( n - 1 ) : -1;
468cdf0e10cSrcweir }
469cdf0e10cSrcweir 
renameInstance(const rtl::OUString & sFrom,const rtl::OUString & sTo,const rtl::OUString & sURL,sal_Bool bURLOnce)470cdf0e10cSrcweir void Model::renameInstance( const rtl::OUString& sFrom,
471cdf0e10cSrcweir                             const rtl::OUString& sTo,
472cdf0e10cSrcweir                             const rtl::OUString& sURL,
473cdf0e10cSrcweir                             sal_Bool bURLOnce )
474cdf0e10cSrcweir     throw( RuntimeException )
475cdf0e10cSrcweir {
476cdf0e10cSrcweir     sal_Int32 nPos = lcl_findInstance( mpInstances, sFrom );
477cdf0e10cSrcweir     if( nPos != -1 )
478cdf0e10cSrcweir     {
479cdf0e10cSrcweir         Sequence<PropertyValue> aSeq = mpInstances->getItem( nPos );
480cdf0e10cSrcweir         PropertyValue* pSeq = aSeq.getArray();
481cdf0e10cSrcweir         sal_Int32 nLength = aSeq.getLength();
482cdf0e10cSrcweir 
483cdf0e10cSrcweir         sal_Int32 nProp = lcl_findProp( pSeq, nLength, OUSTRING("ID") );
484cdf0e10cSrcweir         if( nProp == -1 )
485cdf0e10cSrcweir         {
486cdf0e10cSrcweir             // add name property
487cdf0e10cSrcweir             aSeq.realloc( nLength + 1 );
488cdf0e10cSrcweir             pSeq = aSeq.getArray();
489cdf0e10cSrcweir             pSeq[ nLength ].Name = OUSTRING("ID");
490cdf0e10cSrcweir             nProp = nLength;
491cdf0e10cSrcweir         }
492cdf0e10cSrcweir 
493cdf0e10cSrcweir         // change name
494cdf0e10cSrcweir         pSeq[ nProp ].Value <<= sTo;
495cdf0e10cSrcweir 
496cdf0e10cSrcweir 		// change url
497cdf0e10cSrcweir         nProp = lcl_findProp( pSeq, nLength, OUSTRING("URL") );
498cdf0e10cSrcweir 		if(nProp != -1)
499cdf0e10cSrcweir 	        pSeq[ nProp ].Value <<= sURL;
500cdf0e10cSrcweir 
501cdf0e10cSrcweir 		// change urlonce
502cdf0e10cSrcweir         nProp = lcl_findProp( pSeq, nLength, OUSTRING("URLOnce") );
503cdf0e10cSrcweir 		if(nProp != -1)
504cdf0e10cSrcweir 	        pSeq[ nProp ].Value <<= bURLOnce;
505cdf0e10cSrcweir 
506cdf0e10cSrcweir         // set instance
507cdf0e10cSrcweir         mpInstances->setItem( nPos, aSeq );
508cdf0e10cSrcweir     }
509cdf0e10cSrcweir }
510cdf0e10cSrcweir 
removeInstance(const rtl::OUString & sName)511cdf0e10cSrcweir void Model::removeInstance( const rtl::OUString& sName )
512cdf0e10cSrcweir     throw( RuntimeException )
513cdf0e10cSrcweir {
514cdf0e10cSrcweir     sal_Int32 nPos = lcl_findInstance( mpInstances, sName );
515cdf0e10cSrcweir     if( nPos != -1 )
516cdf0e10cSrcweir         mpInstances->removeItem( mpInstances->getItem( nPos ) );
517cdf0e10cSrcweir }
518cdf0e10cSrcweir 
lcl_getModels(const Reference<com::sun::star::frame::XModel> & xComponent)519cdf0e10cSrcweir Reference<XNameContainer> lcl_getModels(
520cdf0e10cSrcweir     const Reference<com::sun::star::frame::XModel>& xComponent )
521cdf0e10cSrcweir {
522cdf0e10cSrcweir     Reference<XNameContainer> xRet;
523cdf0e10cSrcweir     Reference<XFormsSupplier> xSupplier( xComponent, UNO_QUERY );
524cdf0e10cSrcweir     if( xSupplier.is() )
525cdf0e10cSrcweir     {
526cdf0e10cSrcweir         xRet = xSupplier->getXForms();
527cdf0e10cSrcweir     }
528cdf0e10cSrcweir     return xRet;
529cdf0e10cSrcweir }
530cdf0e10cSrcweir 
newModel(const Reference<com::sun::star::frame::XModel> & xCmp,const OUString & sName)531cdf0e10cSrcweir Model::XModel_t Model::newModel( const Reference<com::sun::star::frame::XModel>& xCmp,
532cdf0e10cSrcweir                                  const OUString& sName )
533cdf0e10cSrcweir     throw( RuntimeException )
534cdf0e10cSrcweir {
535cdf0e10cSrcweir     Model::XModel_t xModel;
536cdf0e10cSrcweir     Reference<XNameContainer> xModels = lcl_getModels( xCmp );
537cdf0e10cSrcweir     if( xModels.is()
538cdf0e10cSrcweir         && ! xModels->hasByName( sName ) )
539cdf0e10cSrcweir     {
540cdf0e10cSrcweir         Model* pModel = new Model();
541cdf0e10cSrcweir         xModel.set( pModel );
542cdf0e10cSrcweir 
543cdf0e10cSrcweir         pModel->setID( sName );
544cdf0e10cSrcweir         pModel->newInstance( OUString(), OUString(), sal_False );
545cdf0e10cSrcweir         pModel->initialize();
546cdf0e10cSrcweir         xModels->insertByName( sName, makeAny( xModel ) );
547cdf0e10cSrcweir     }
548cdf0e10cSrcweir 
549cdf0e10cSrcweir     return xModel;
550cdf0e10cSrcweir }
551cdf0e10cSrcweir 
renameModel(const Reference<com::sun::star::frame::XModel> & xCmp,const OUString & sFrom,const OUString & sTo)552cdf0e10cSrcweir void Model::renameModel( const Reference<com::sun::star::frame::XModel>& xCmp,
553cdf0e10cSrcweir                          const OUString& sFrom,
554cdf0e10cSrcweir                          const OUString& sTo )
555cdf0e10cSrcweir     throw( RuntimeException )
556cdf0e10cSrcweir {
557cdf0e10cSrcweir     Reference<XNameContainer> xModels = lcl_getModels( xCmp );
558cdf0e10cSrcweir     if( xModels.is()
559cdf0e10cSrcweir         && xModels->hasByName( sFrom )
560cdf0e10cSrcweir         && ! xModels->hasByName( sTo ) )
561cdf0e10cSrcweir     {
562cdf0e10cSrcweir         Reference<XModel> xModel( xModels->getByName( sFrom ), UNO_QUERY );
563cdf0e10cSrcweir         xModel->setID( sTo );
564cdf0e10cSrcweir         xModels->insertByName( sTo, makeAny( xModel ) );
565cdf0e10cSrcweir         xModels->removeByName( sFrom );
566cdf0e10cSrcweir     }
567cdf0e10cSrcweir }
568cdf0e10cSrcweir 
removeModel(const Reference<com::sun::star::frame::XModel> & xCmp,const OUString & sName)569cdf0e10cSrcweir void Model::removeModel( const Reference<com::sun::star::frame::XModel>& xCmp,
570cdf0e10cSrcweir                          const OUString& sName )
571cdf0e10cSrcweir     throw( RuntimeException )
572cdf0e10cSrcweir {
573cdf0e10cSrcweir     Reference<XNameContainer> xModels = lcl_getModels( xCmp );
574cdf0e10cSrcweir     if( xModels.is()
575cdf0e10cSrcweir         && xModels->hasByName( sName ) )
576cdf0e10cSrcweir     {
577cdf0e10cSrcweir         xModels->removeByName( sName );
578cdf0e10cSrcweir     }
579cdf0e10cSrcweir }
580cdf0e10cSrcweir 
createElement(const XNode_t & xParent,const OUString & sName)581cdf0e10cSrcweir Model::XNode_t Model::createElement( const XNode_t& xParent,
582cdf0e10cSrcweir                                      const OUString& sName )
583cdf0e10cSrcweir     throw( RuntimeException )
584cdf0e10cSrcweir {
585cdf0e10cSrcweir     Reference<XNode> xNode;
586cdf0e10cSrcweir     if( xParent.is()
587cdf0e10cSrcweir         && isValidXMLName( sName ) )
588cdf0e10cSrcweir     {
589cdf0e10cSrcweir         // TODO: implement proper namespace handling
590cdf0e10cSrcweir         xNode.set( xParent->getOwnerDocument()->createElement( sName ),
591cdf0e10cSrcweir                    UNO_QUERY );
592cdf0e10cSrcweir     }
593cdf0e10cSrcweir     return xNode;
594cdf0e10cSrcweir }
595cdf0e10cSrcweir 
createAttribute(const XNode_t & xParent,const OUString & sName)596cdf0e10cSrcweir Model::XNode_t Model::createAttribute( const XNode_t& xParent,
597cdf0e10cSrcweir                                        const OUString& sName )
598cdf0e10cSrcweir     throw( RuntimeException )
599cdf0e10cSrcweir {
600cdf0e10cSrcweir     Reference<XNode> xNode;
601cdf0e10cSrcweir     Reference<XElement> xElement( xParent, UNO_QUERY );
602cdf0e10cSrcweir     if( xParent.is()
603cdf0e10cSrcweir         && xElement.is()
604cdf0e10cSrcweir         && isValidXMLName( sName ) )
605cdf0e10cSrcweir     {
606cdf0e10cSrcweir         // handle case where attribute already exists
607cdf0e10cSrcweir         sal_Int32 nCount = 0;
608cdf0e10cSrcweir         OUString sUniqueName = sName;
609cdf0e10cSrcweir         while( xElement->hasAttribute( sUniqueName ) )
610cdf0e10cSrcweir         {
611cdf0e10cSrcweir             nCount++;
612cdf0e10cSrcweir             sUniqueName = sName + OUString::valueOf( nCount );
613cdf0e10cSrcweir         }
614cdf0e10cSrcweir 
615cdf0e10cSrcweir         // TODO: implement proper namespace handling
616cdf0e10cSrcweir         xNode.set( xParent->getOwnerDocument()->createAttribute( sUniqueName ),
617cdf0e10cSrcweir                    UNO_QUERY );
618cdf0e10cSrcweir     }
619cdf0e10cSrcweir     return xNode;
620cdf0e10cSrcweir }
621cdf0e10cSrcweir 
renameNode(const XNode_t & xNode,const rtl::OUString & sName)622cdf0e10cSrcweir Model::XNode_t Model::renameNode( const XNode_t& xNode,
623cdf0e10cSrcweir                                   const rtl::OUString& sName )
624cdf0e10cSrcweir     throw( RuntimeException )
625cdf0e10cSrcweir {
626cdf0e10cSrcweir     // early out if we don't have to change the name
627cdf0e10cSrcweir     if( xNode->getNodeName() == sName )
628cdf0e10cSrcweir         return xNode;
629cdf0e10cSrcweir 
630cdf0e10cSrcweir     // refuse to change name if its an attribute, and the name is already used
631cdf0e10cSrcweir     if( xNode->getNodeType() == NodeType_ATTRIBUTE_NODE
632cdf0e10cSrcweir         && xNode->getParentNode().is()
633cdf0e10cSrcweir         && Reference<XElement>(xNode->getParentNode(), UNO_QUERY_THROW)->hasAttribute( sName ) )
634cdf0e10cSrcweir         return xNode;
635cdf0e10cSrcweir 
636cdf0e10cSrcweir     // note old binding expression so we can adjust bindings below
637cdf0e10cSrcweir     OUString sOldDefaultBindingExpression =
638cdf0e10cSrcweir         getDefaultBindingExpressionForNode( xNode );
639cdf0e10cSrcweir 
640cdf0e10cSrcweir     Reference<XDocument> xDoc = xNode->getOwnerDocument();
641cdf0e10cSrcweir     Reference<XNode> xNew;
642cdf0e10cSrcweir     if( xNode->getNodeType() == NodeType_ELEMENT_NODE )
643cdf0e10cSrcweir     {
644cdf0e10cSrcweir         Reference<XElement> xElem = xDoc->createElement( sName );
645cdf0e10cSrcweir         xNew.set( xElem, UNO_QUERY );
646cdf0e10cSrcweir 
647cdf0e10cSrcweir         // iterate over all attributes and append them to the new element
648cdf0e10cSrcweir         Reference<XElement> xOldElem( xNode, UNO_QUERY );
649cdf0e10cSrcweir         OSL_ENSURE( xNode.is(), "no element?" );
650cdf0e10cSrcweir 
651cdf0e10cSrcweir         Reference<XNamedNodeMap> xMap = xNode->getAttributes();
652cdf0e10cSrcweir         sal_Int32 nLength = xMap.is() ? xMap->getLength() : 0;
653cdf0e10cSrcweir         for( sal_Int32 n = 0; n < nLength; n++ )
654cdf0e10cSrcweir         {
655cdf0e10cSrcweir             Reference<XAttr> xAttr( xMap->item(n), UNO_QUERY );
656cdf0e10cSrcweir             xElem->setAttributeNode( xOldElem->removeAttributeNode( xAttr ) );
657cdf0e10cSrcweir         }
658cdf0e10cSrcweir 
659cdf0e10cSrcweir         // iterate over all children and append them to the new element
660cdf0e10cSrcweir         for( Reference<XNode> xCurrent = xNode->getFirstChild();
661cdf0e10cSrcweir              xCurrent.is();
662cdf0e10cSrcweir              xCurrent = xNode->getFirstChild() )
663cdf0e10cSrcweir         {
664cdf0e10cSrcweir             xNew->appendChild( xNode->removeChild( xCurrent ) );
665cdf0e10cSrcweir         }
666cdf0e10cSrcweir 
667cdf0e10cSrcweir         xNode->getParentNode()->replaceChild( xNew, xNode );
668cdf0e10cSrcweir     }
669cdf0e10cSrcweir     else if( xNode->getNodeType() == NodeType_ATTRIBUTE_NODE )
670cdf0e10cSrcweir     {
671cdf0e10cSrcweir         // create new attribute
672cdf0e10cSrcweir         Reference<XAttr> xAttr = xDoc->createAttribute( sName );
673cdf0e10cSrcweir         xAttr->setValue( xNode->getNodeValue() );
674cdf0e10cSrcweir 
675cdf0e10cSrcweir         // replace node
676cdf0e10cSrcweir         Reference<XNode> xParent = xNode->getParentNode();
677cdf0e10cSrcweir         xParent->removeChild( xNode );
678cdf0e10cSrcweir         xNew = xParent->appendChild( Reference<XNode>( xAttr, UNO_QUERY ) );
679cdf0e10cSrcweir     }
680cdf0e10cSrcweir     else
681cdf0e10cSrcweir     {
682cdf0e10cSrcweir         OSL_ENSURE( false, "can't rename this node type" );
683cdf0e10cSrcweir     }
684cdf0e10cSrcweir 
685cdf0e10cSrcweir     // adjust bindings (if necessary):
686cdf0e10cSrcweir     if( xNew.is() )
687cdf0e10cSrcweir     {
688cdf0e10cSrcweir         // iterate over bindings and replace default expressions
689cdf0e10cSrcweir         OUString sNewDefaultBindingExpression =
690cdf0e10cSrcweir             getDefaultBindingExpressionForNode( xNew );
691cdf0e10cSrcweir         for( sal_Int32 n = 0; n < mpBindings->countItems(); n++ )
692cdf0e10cSrcweir         {
693cdf0e10cSrcweir             Binding* pBinding = Binding::getBinding(
694cdf0e10cSrcweir                 mpBindings->Collection<XPropertySet_t>::getItem( n ) );
695cdf0e10cSrcweir 
696cdf0e10cSrcweir             if( pBinding->getBindingExpression()
697cdf0e10cSrcweir                     == sOldDefaultBindingExpression )
698cdf0e10cSrcweir                 pBinding->setBindingExpression( sNewDefaultBindingExpression );
699cdf0e10cSrcweir         }
700cdf0e10cSrcweir     }
701cdf0e10cSrcweir 
702cdf0e10cSrcweir     // return node; return old node if renaming failed
703cdf0e10cSrcweir     return xNew.is() ? xNew : xNode;
704cdf0e10cSrcweir }
705cdf0e10cSrcweir 
getBindingForNode(const XNode_t & xNode,sal_Bool bCreate)706cdf0e10cSrcweir Model::XPropertySet_t Model::getBindingForNode( const XNode_t& xNode,
707cdf0e10cSrcweir                                                 sal_Bool bCreate )
708cdf0e10cSrcweir     throw( RuntimeException )
709cdf0e10cSrcweir {
710cdf0e10cSrcweir     OSL_ENSURE( xNode.is(), "no node?" );
711cdf0e10cSrcweir 
712cdf0e10cSrcweir     // We will iterate over all bindings and determine the
713cdf0e10cSrcweir     // appropriateness of the respective binding for this node. The
714cdf0e10cSrcweir     // best one will be used. If we don't find any and bCreate is set,
715cdf0e10cSrcweir     // then we will create a suitable binding.
716cdf0e10cSrcweir     Binding* pBestBinding = NULL;
717cdf0e10cSrcweir     sal_Int32 nBestScore = 0;
718cdf0e10cSrcweir 
719cdf0e10cSrcweir     for( sal_Int32 n = 0; n < mpBindings->countItems(); n++ )
720cdf0e10cSrcweir     {
721cdf0e10cSrcweir         Binding* pBinding = Binding::getBinding(
722cdf0e10cSrcweir             mpBindings->Collection<XPropertySet_t>::getItem( n ) );
723cdf0e10cSrcweir 
724cdf0e10cSrcweir         OSL_ENSURE( pBinding != NULL, "no binding?" );
725cdf0e10cSrcweir         Reference<XNodeList> xNodeList = pBinding->getXNodeList();
726cdf0e10cSrcweir 
727cdf0e10cSrcweir         sal_Int32 nNodes = xNodeList.is() ? xNodeList->getLength() : 0;
728cdf0e10cSrcweir         if( nNodes > 0  &&  xNodeList->item( 0 ) == xNode )
729cdf0e10cSrcweir         {
730cdf0e10cSrcweir             // allright, we found a suitable node. Let's determine how
731cdf0e10cSrcweir             // well it fits. Score:
732cdf0e10cSrcweir             // - bind to exactly this node is better than whole nodeset
733cdf0e10cSrcweir             // - simple binding expressions is better than complex ones
734cdf0e10cSrcweir             sal_Int32 nScore = 0;
735cdf0e10cSrcweir             if( nNodes == 1 )
736cdf0e10cSrcweir                 nScore ++;
737cdf0e10cSrcweir             if( pBinding->isSimpleBindingExpression() )
738cdf0e10cSrcweir                 nScore ++;
739cdf0e10cSrcweir 
740cdf0e10cSrcweir             // if we found a better binding, remember it
741cdf0e10cSrcweir             if( nScore > nBestScore )
742cdf0e10cSrcweir             {
743cdf0e10cSrcweir                 pBestBinding = pBinding;
744cdf0e10cSrcweir                 nBestScore = nScore;
745cdf0e10cSrcweir             }
746cdf0e10cSrcweir         }
747cdf0e10cSrcweir     }
748cdf0e10cSrcweir 
749cdf0e10cSrcweir     // create binding, if none was found and bCreate is set
750cdf0e10cSrcweir     OSL_ENSURE( ( nBestScore == 0 ) == ( pBestBinding == NULL ),
751cdf0e10cSrcweir                 "score != binding?" );
752cdf0e10cSrcweir     if( bCreate  &&  pBestBinding == NULL )
753cdf0e10cSrcweir     {
754cdf0e10cSrcweir         pBestBinding = new Binding();
755cdf0e10cSrcweir         pBestBinding->setBindingExpression(
756cdf0e10cSrcweir             getDefaultBindingExpressionForNode( xNode ) );
757cdf0e10cSrcweir         mpBindings->addItem( pBestBinding );
758cdf0e10cSrcweir     }
759cdf0e10cSrcweir 
760cdf0e10cSrcweir     return pBestBinding;
761cdf0e10cSrcweir }
762cdf0e10cSrcweir 
removeBindingForNode(const XNode_t &)763cdf0e10cSrcweir void Model::removeBindingForNode( const XNode_t& )
764cdf0e10cSrcweir     throw( RuntimeException )
765cdf0e10cSrcweir {
766cdf0e10cSrcweir     // determine whether suitable binding is still used
767cdf0e10cSrcweir }
768cdf0e10cSrcweir 
lcl_serializeForDisplay(const Reference<XAttr> & _rxAttrNode)769cdf0e10cSrcweir OUString lcl_serializeForDisplay( const Reference< XAttr >& _rxAttrNode )
770cdf0e10cSrcweir {
771cdf0e10cSrcweir     ::rtl::OUString sResult;
772cdf0e10cSrcweir     OSL_ENSURE( _rxAttrNode.is(), "lcl_serializeForDisplay( attr ): invalid argument!" );
773cdf0e10cSrcweir     if ( _rxAttrNode.is() )
774cdf0e10cSrcweir     {
775cdf0e10cSrcweir         ::rtl::OUStringBuffer aBuffer;
776cdf0e10cSrcweir         aBuffer.append( _rxAttrNode->getName() );
777cdf0e10cSrcweir         aBuffer.appendAscii( "=" );
778cdf0e10cSrcweir         ::rtl::OUString sValue = _rxAttrNode->getValue();
779cdf0e10cSrcweir         sal_Unicode nQuote = '"';
780cdf0e10cSrcweir         if ( sValue.indexOf( nQuote ) >= 0 )
781cdf0e10cSrcweir             nQuote = '\'';
782cdf0e10cSrcweir         aBuffer.append( nQuote );
783cdf0e10cSrcweir         aBuffer.append( sValue );
784cdf0e10cSrcweir         aBuffer.append( nQuote );
785cdf0e10cSrcweir         aBuffer.append( (sal_Unicode)' ' );
786cdf0e10cSrcweir         sResult = aBuffer.makeStringAndClear();
787cdf0e10cSrcweir     }
788cdf0e10cSrcweir     return sResult;
789cdf0e10cSrcweir }
790cdf0e10cSrcweir 
lcl_serializeForDisplay(const Reference<XNodeList> & xNodes)791cdf0e10cSrcweir OUString lcl_serializeForDisplay( const Reference<XNodeList>& xNodes )
792cdf0e10cSrcweir {
793cdf0e10cSrcweir     ::rtl::OUString sResult;
794cdf0e10cSrcweir 
795cdf0e10cSrcweir     // create document fragment
796cdf0e10cSrcweir     Reference<XDocument> xDocument( getDocumentBuilder()->newDocument() );
797cdf0e10cSrcweir     Reference<XDocumentFragment> xFragment(
798cdf0e10cSrcweir         xDocument->createDocumentFragment() );
799cdf0e10cSrcweir     Reference<XNode> xNode( xFragment, UNO_QUERY );
800cdf0e10cSrcweir     OSL_ENSURE( xFragment.is(), "xFragment" );
801cdf0e10cSrcweir     OSL_ENSURE( xNode.is(), "xNode" );
802cdf0e10cSrcweir 
803cdf0e10cSrcweir     sal_Int32 nAttributeNodes = 0;
804cdf0e10cSrcweir 
805cdf0e10cSrcweir     // attach nodelist to fragment
806cdf0e10cSrcweir     sal_Int32 nLength = xNodes->getLength();
807cdf0e10cSrcweir     for( sal_Int32 i = 0; i < nLength; i++ )
808cdf0e10cSrcweir     {
809cdf0e10cSrcweir         Reference<XNode> xCurrent = xNodes->item( i );
810cdf0e10cSrcweir 
811cdf0e10cSrcweir         switch ( xCurrent->getNodeType() )
812cdf0e10cSrcweir         {
813cdf0e10cSrcweir         case NodeType_DOCUMENT_NODE:
814cdf0e10cSrcweir             // special-case documents: use top-level element instead
815cdf0e10cSrcweir             xCurrent = xCurrent->getFirstChild();
816cdf0e10cSrcweir             break;
817cdf0e10cSrcweir         case NodeType_ATTRIBUTE_NODE:
818cdf0e10cSrcweir         {
819cdf0e10cSrcweir             Reference< XAttr > xAttr( xCurrent, UNO_QUERY );
820cdf0e10cSrcweir             if ( xAttr.is() )
821cdf0e10cSrcweir             {
822cdf0e10cSrcweir                 sResult += lcl_serializeForDisplay( xAttr );
823cdf0e10cSrcweir                 ++nAttributeNodes;
824cdf0e10cSrcweir             }
825cdf0e10cSrcweir         }
826cdf0e10cSrcweir         continue;
827cdf0e10cSrcweir 
828cdf0e10cSrcweir         default:
829cdf0e10cSrcweir             break;
830cdf0e10cSrcweir         }
831cdf0e10cSrcweir 
832cdf0e10cSrcweir         // append node
833cdf0e10cSrcweir         xNode->appendChild( xDocument->importNode( xCurrent, sal_True ) );
834cdf0e10cSrcweir     }
835cdf0e10cSrcweir     OSL_ENSURE( ( nAttributeNodes == 0 ) || ( nAttributeNodes == nLength ),
836cdf0e10cSrcweir         "lcl_serializeForDisplay: mixed attribute and non-attribute nodes?" );
837cdf0e10cSrcweir     if ( nAttributeNodes )
838cdf0e10cSrcweir         // had only attribute nodes
839cdf0e10cSrcweir         return sResult;
840cdf0e10cSrcweir 
841cdf0e10cSrcweir     // serialize fragment
842cdf0e10cSrcweir     CSerializationAppXML aSerialization;
843cdf0e10cSrcweir     aSerialization.setSource( xFragment );
844cdf0e10cSrcweir     aSerialization.serialize();
845cdf0e10cSrcweir 
846cdf0e10cSrcweir     // copy stream into buffer
847cdf0e10cSrcweir     Reference<XTextInputStream> xTextInputStream(
848cdf0e10cSrcweir         createInstance( OUSTRING("com.sun.star.io.TextInputStream") ),
849cdf0e10cSrcweir         UNO_QUERY );
850cdf0e10cSrcweir     Reference<XActiveDataSink>( xTextInputStream, UNO_QUERY_THROW )
851cdf0e10cSrcweir         ->setInputStream( aSerialization.getInputStream() );
852cdf0e10cSrcweir 
853cdf0e10cSrcweir     /* WORK AROUND for problem in serialization: currently, multiple
854cdf0e10cSrcweir       XML delarations (<?xml...?>) are being written out and we don't
855cdf0e10cSrcweir       want them. When this is fixed, the code below is nice and
856cdf0e10cSrcweir       simple. The current code filters out the declarations.
857cdf0e10cSrcweir     OUString sResult = xTextInputStream->readString( Sequence<sal_Unicode>(),
858cdf0e10cSrcweir                                                      sal_True );
859cdf0e10cSrcweir     */
860cdf0e10cSrcweir 
861cdf0e10cSrcweir     // well, the serialization prepends XML header(s) that we need to
862cdf0e10cSrcweir     // remove first.
863cdf0e10cSrcweir     OUStringBuffer aBuffer;
864cdf0e10cSrcweir     while( ! xTextInputStream->isEOF() )
865cdf0e10cSrcweir     {
866cdf0e10cSrcweir         OUString sLine = xTextInputStream->readLine();
867cdf0e10cSrcweir         if( sLine.getLength() > 0
868cdf0e10cSrcweir             && sLine.compareToAscii( "<?xml", 5 ) != 0 )
869cdf0e10cSrcweir         {
870cdf0e10cSrcweir             aBuffer.append( sLine );
871cdf0e10cSrcweir             aBuffer.append( sal_Unicode('\n') );
872cdf0e10cSrcweir         }
873cdf0e10cSrcweir     }
874cdf0e10cSrcweir     sResult = aBuffer.makeStringAndClear();
875cdf0e10cSrcweir 
876cdf0e10cSrcweir     return sResult;
877cdf0e10cSrcweir }
878cdf0e10cSrcweir 
lcl_serializeForDisplay(const Reference<XXPathObject> & xResult)879cdf0e10cSrcweir OUString lcl_serializeForDisplay( const Reference<XXPathObject>& xResult )
880cdf0e10cSrcweir {
881cdf0e10cSrcweir     // error handling first
882cdf0e10cSrcweir     if( ! xResult.is() )
883cdf0e10cSrcweir         return getResource( RID_STR_XFORMS_CANT_EVALUATE );
884cdf0e10cSrcweir 
885cdf0e10cSrcweir 
886cdf0e10cSrcweir     // TODO: localize
887cdf0e10cSrcweir     OUStringBuffer aBuffer;
888cdf0e10cSrcweir 
889cdf0e10cSrcweir     switch( xResult->getObjectType() )
890cdf0e10cSrcweir     {
891cdf0e10cSrcweir     case XPathObjectType_XPATH_BOOLEAN:
892cdf0e10cSrcweir         aBuffer.append( xResult->getBoolean()
893cdf0e10cSrcweir                         ? OUSTRING("true")
894cdf0e10cSrcweir                         : OUSTRING("false") );
895cdf0e10cSrcweir         break;
896cdf0e10cSrcweir 
897cdf0e10cSrcweir     case XPathObjectType_XPATH_STRING:
898cdf0e10cSrcweir         aBuffer.append( sal_Unicode('"') );
899cdf0e10cSrcweir         aBuffer.append( xResult->getString() );
900cdf0e10cSrcweir         aBuffer.append( sal_Unicode('"') );
901cdf0e10cSrcweir         break;
902cdf0e10cSrcweir 
903cdf0e10cSrcweir     case XPathObjectType_XPATH_NODESET:
904cdf0e10cSrcweir         aBuffer.append( lcl_serializeForDisplay( xResult->getNodeList() ) );
905cdf0e10cSrcweir         break;
906cdf0e10cSrcweir 
907cdf0e10cSrcweir     case XPathObjectType_XPATH_NUMBER:
908cdf0e10cSrcweir         aBuffer.append( xResult->getDouble() );
909cdf0e10cSrcweir         break;
910cdf0e10cSrcweir 
911cdf0e10cSrcweir     case XPathObjectType_XPATH_UNDEFINED:
912cdf0e10cSrcweir     case XPathObjectType_XPATH_POINT:
913cdf0e10cSrcweir     case XPathObjectType_XPATH_RANGE:
914cdf0e10cSrcweir     case XPathObjectType_XPATH_LOCATIONSET:
915cdf0e10cSrcweir     case XPathObjectType_XPATH_USERS:
916cdf0e10cSrcweir     case XPathObjectType_XPATH_XSLT_TREE:
917cdf0e10cSrcweir     default:
918cdf0e10cSrcweir         // TODO: localized error message?
919cdf0e10cSrcweir         break;
920cdf0e10cSrcweir     }
921cdf0e10cSrcweir 
922cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
923cdf0e10cSrcweir }
924cdf0e10cSrcweir 
getResultForExpression(const XPropertySet_t & xBinding,sal_Bool bIsBindingExpression,const OUString & sExpression)925cdf0e10cSrcweir OUString Model::getResultForExpression(
926cdf0e10cSrcweir     const XPropertySet_t& xBinding,
927cdf0e10cSrcweir     sal_Bool bIsBindingExpression,
928cdf0e10cSrcweir     const OUString& sExpression )
929cdf0e10cSrcweir     throw( RuntimeException )
930cdf0e10cSrcweir {
931cdf0e10cSrcweir     Binding* pBinding = Binding::getBinding( xBinding );
932cdf0e10cSrcweir     if( pBinding == NULL )
933cdf0e10cSrcweir         throw RuntimeException();
934cdf0e10cSrcweir 
935cdf0e10cSrcweir     // prepare & evaluate expression
936cdf0e10cSrcweir     OUStringBuffer aBuffer;
937cdf0e10cSrcweir     ComputedExpression aExpression;
938cdf0e10cSrcweir     aExpression.setExpression( sExpression );
939cdf0e10cSrcweir     if( bIsBindingExpression )
940cdf0e10cSrcweir     {
941cdf0e10cSrcweir         // binding: use binding context and evaluation
942cdf0e10cSrcweir         aExpression.evaluate( pBinding->getEvaluationContext() );
943cdf0e10cSrcweir         aBuffer.append( lcl_serializeForDisplay( aExpression.getXPath() ) );
944cdf0e10cSrcweir     }
945cdf0e10cSrcweir     else
946cdf0e10cSrcweir     {
947cdf0e10cSrcweir         // MIP (not binding): iterate over bindings contexts
948cdf0e10cSrcweir         std::vector<EvaluationContext> aContext =
949cdf0e10cSrcweir             pBinding->getMIPEvaluationContexts();
950cdf0e10cSrcweir         for( std::vector<EvaluationContext>::iterator aIter = aContext.begin();
951cdf0e10cSrcweir              aIter != aContext.end();
952cdf0e10cSrcweir              aIter ++ )
953cdf0e10cSrcweir         {
954cdf0e10cSrcweir             aExpression.evaluate( *aIter );
955cdf0e10cSrcweir             aBuffer.append( lcl_serializeForDisplay(aExpression.getXPath()) );
956cdf0e10cSrcweir             aBuffer.append( sal_Unicode('\n') );
957cdf0e10cSrcweir         }
958cdf0e10cSrcweir     }
959cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
960cdf0e10cSrcweir }
961cdf0e10cSrcweir 
isValidXMLName(const OUString & sName)962cdf0e10cSrcweir sal_Bool Model::isValidXMLName( const OUString& sName )
963cdf0e10cSrcweir     throw( RuntimeException )
964cdf0e10cSrcweir {
965cdf0e10cSrcweir     return isValidQName( sName, NULL );
966cdf0e10cSrcweir }
967cdf0e10cSrcweir 
isValidPrefixName(const OUString & sName)968cdf0e10cSrcweir sal_Bool Model::isValidPrefixName( const OUString& sName )
969cdf0e10cSrcweir     throw( RuntimeException )
970cdf0e10cSrcweir {
971cdf0e10cSrcweir     return ::isValidPrefixName( sName, NULL );
972cdf0e10cSrcweir }
973cdf0e10cSrcweir 
setNodeValue(const XNode_t & xNode,const rtl::OUString & sValue)974cdf0e10cSrcweir void Model::setNodeValue(
975cdf0e10cSrcweir     const XNode_t& xNode,
976cdf0e10cSrcweir     const rtl::OUString& sValue )
977cdf0e10cSrcweir     throw( RuntimeException )
978cdf0e10cSrcweir {
979cdf0e10cSrcweir     setSimpleContent( xNode, sValue );
980cdf0e10cSrcweir }
981cdf0e10cSrcweir 
982cdf0e10cSrcweir 
983cdf0e10cSrcweir //
984cdf0e10cSrcweir // helper functions from model_helper.hxx
985cdf0e10cSrcweir //
986cdf0e10cSrcweir 
getInstanceData(const Sequence<PropertyValue> & aValues,OUString * pID,Reference<XDocument> * pInstance,OUString * pURL,bool * pURLOnce)987cdf0e10cSrcweir void xforms::getInstanceData(
988cdf0e10cSrcweir     const Sequence<PropertyValue>& aValues,
989cdf0e10cSrcweir     OUString* pID,
990cdf0e10cSrcweir     Reference<XDocument>* pInstance,
991cdf0e10cSrcweir     OUString* pURL,
992cdf0e10cSrcweir     bool* pURLOnce )
993cdf0e10cSrcweir {
994cdf0e10cSrcweir     sal_Int32 nValues = aValues.getLength();
995cdf0e10cSrcweir     const PropertyValue* pValues = aValues.getConstArray();
996cdf0e10cSrcweir     for( sal_Int32 n = 0; n < nValues; n++ )
997cdf0e10cSrcweir     {
998cdf0e10cSrcweir         const PropertyValue& rValue = pValues[n];
999cdf0e10cSrcweir #define PROP(NAME) \
1000cdf0e10cSrcweir         if( p##NAME != NULL && \
1001cdf0e10cSrcweir             rValue.Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(#NAME)) ) \
1002cdf0e10cSrcweir             rValue.Value >>= (*p##NAME)
1003cdf0e10cSrcweir         PROP(ID);
1004cdf0e10cSrcweir         PROP(Instance);
1005cdf0e10cSrcweir         PROP(URL);
1006cdf0e10cSrcweir         PROP(URLOnce);
1007cdf0e10cSrcweir #undef PROP
1008cdf0e10cSrcweir     }
1009cdf0e10cSrcweir }
1010cdf0e10cSrcweir 
setInstanceData(Sequence<PropertyValue> & aSequence,const OUString * _pID,const Reference<XDocument> * _pInstance,const OUString * _pURL,const bool * _pURLOnce)1011cdf0e10cSrcweir void xforms::setInstanceData(
1012cdf0e10cSrcweir     Sequence<PropertyValue>& aSequence,
1013cdf0e10cSrcweir     const OUString* _pID,
1014cdf0e10cSrcweir     const Reference<XDocument>* _pInstance,
1015cdf0e10cSrcweir     const OUString* _pURL,
1016cdf0e10cSrcweir     const bool* _pURLOnce )
1017cdf0e10cSrcweir {
1018cdf0e10cSrcweir     // get old instance data
1019cdf0e10cSrcweir     OUString sID;
1020cdf0e10cSrcweir     Reference<XDocument> xInstance;
1021cdf0e10cSrcweir     OUString sURL;
1022cdf0e10cSrcweir     bool bURLOnce = false;
1023cdf0e10cSrcweir     getInstanceData( aSequence, &sID, &xInstance, &sURL, &bURLOnce );
1024cdf0e10cSrcweir     const OUString* pID = ( sID.getLength() > 0 ) ? &sID : NULL;
1025cdf0e10cSrcweir     const Reference<XDocument>* pInstance = xInstance.is() ? &xInstance : NULL;
1026cdf0e10cSrcweir     const OUString* pURL = ( sURL.getLength() > 0 ) ? &sURL : NULL;
1027cdf0e10cSrcweir     const bool* pURLOnce = ( bURLOnce && pURL != NULL ) ? &bURLOnce : NULL;
1028cdf0e10cSrcweir 
1029cdf0e10cSrcweir     // determine new instance data
1030cdf0e10cSrcweir #define PROP(NAME) if( _p##NAME != NULL ) p##NAME = _p##NAME
1031cdf0e10cSrcweir     PROP(ID);
1032cdf0e10cSrcweir     PROP(Instance);
1033cdf0e10cSrcweir     PROP(URL);
1034cdf0e10cSrcweir     PROP(URLOnce);
1035cdf0e10cSrcweir #undef PROP
1036cdf0e10cSrcweir 
1037cdf0e10cSrcweir     // count # of values we want to set
1038cdf0e10cSrcweir     sal_Int32 nCount = 0;
1039cdf0e10cSrcweir #define PROP(NAME) if( p##NAME != NULL ) nCount++
1040cdf0e10cSrcweir     PROP(ID);
1041cdf0e10cSrcweir     PROP(Instance);
1042cdf0e10cSrcweir     PROP(URL);
1043cdf0e10cSrcweir     PROP(URLOnce);
1044cdf0e10cSrcweir #undef PROP
1045cdf0e10cSrcweir 
1046cdf0e10cSrcweir     // realloc sequence and enter values;
1047cdf0e10cSrcweir     aSequence.realloc( nCount );
1048cdf0e10cSrcweir     PropertyValue* pSequence = aSequence.getArray();
1049cdf0e10cSrcweir     sal_Int32 nIndex = 0;
1050cdf0e10cSrcweir #define PROP(NAME) \
1051cdf0e10cSrcweir     if( p##NAME != NULL ) \
1052cdf0e10cSrcweir     { \
1053cdf0e10cSrcweir         pSequence[ nIndex ].Name = OUSTRING(#NAME); \
1054cdf0e10cSrcweir         pSequence[ nIndex ].Value <<= *p##NAME; \
1055cdf0e10cSrcweir         nIndex++; \
1056cdf0e10cSrcweir     }
1057cdf0e10cSrcweir     PROP(ID);
1058cdf0e10cSrcweir     PROP(Instance);
1059cdf0e10cSrcweir     PROP(URL);
1060cdf0e10cSrcweir     PROP(URLOnce);
1061cdf0e10cSrcweir #undef PROP
1062cdf0e10cSrcweir }
1063