1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 
27 #include "XFormsModelContext.hxx"
28 
29 #include "XFormsBindContext.hxx"
30 #include "XFormsSubmissionContext.hxx"
31 #include "XFormsInstanceContext.hxx"
32 #include "SchemaContext.hxx"
33 #include "xformsapi.hxx"
34 
35 #include <xmloff/xmlimp.hxx>
36 #include "xmloff/xmlnmspe.hxx"
37 #include <xmloff/nmspmap.hxx>
38 #include <xmloff/xmltoken.hxx>
39 #include "xmloff/xmlerror.hxx"
40 
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/xml/dom/XDocument.hpp>
43 #include <com/sun/star/util/XUpdatable.hpp>
44 #include <com/sun/star/xforms/XModel.hpp>
45 
46 #include <tools/debug.hxx>
47 
48 
49 using rtl::OUString;
50 using com::sun::star::xml::sax::XAttributeList;
51 using com::sun::star::beans::XPropertySet;
52 using com::sun::star::util::XUpdatable;
53 using namespace com::sun::star::uno;
54 using namespace xmloff::token;
55 
56 
57 
58 
59 static SvXMLTokenMapEntry aAttributes[] =
60 {
61     TOKEN_MAP_ENTRY( NONE, ID ),
62     TOKEN_MAP_ENTRY( NONE, SCHEMA ),
63     XML_TOKEN_MAP_END
64 };
65 
66 static SvXMLTokenMapEntry aChildren[] =
67 {
68     TOKEN_MAP_ENTRY( XFORMS, INSTANCE ),
69     TOKEN_MAP_ENTRY( XFORMS, BIND ),
70     TOKEN_MAP_ENTRY( XFORMS, SUBMISSION ),
71     TOKEN_MAP_ENTRY( XSD,    SCHEMA ),
72     XML_TOKEN_MAP_END
73 };
74 
75 
XFormsModelContext(SvXMLImport & rImport,sal_uInt16 nPrefix,const OUString & rLocalName)76 XFormsModelContext::XFormsModelContext( SvXMLImport& rImport,
77                                         sal_uInt16 nPrefix,
78                                         const OUString& rLocalName ) :
79     TokenContext( rImport, nPrefix, rLocalName, aAttributes, aChildren ),
80     mxModel( lcl_createXFormsModel() )
81 {
82 }
83 
~XFormsModelContext()84 XFormsModelContext::~XFormsModelContext()
85 {
86 }
87 
88 
getModel()89 Reference<XPropertySet> XFormsModelContext::getModel()
90 {
91     return mxModel;
92 }
93 
94 
HandleAttribute(sal_uInt16 nToken,const OUString & rValue)95 void XFormsModelContext::HandleAttribute(
96     sal_uInt16 nToken,
97     const OUString& rValue )
98 {
99     switch( nToken )
100     {
101     case XML_ID:
102         mxModel->setPropertyValue( OUSTRING("ID"), makeAny( rValue ) );
103         break;
104     case XML_SCHEMA:
105         GetImport().SetError( XMLERROR_XFORMS_NO_SCHEMA_SUPPORT );
106         break;
107     default:
108         DBG_ERROR( "this should not happen" );
109         break;
110     }
111 }
112 
HandleChild(sal_uInt16 nToken,sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<XAttributeList> &)113 SvXMLImportContext* XFormsModelContext::HandleChild(
114     sal_uInt16 nToken,
115     sal_uInt16 nPrefix,
116     const OUString& rLocalName,
117     const Reference<XAttributeList>& )
118 {
119     SvXMLImportContext* pContext = NULL;
120 
121     switch( nToken )
122     {
123     case XML_INSTANCE:
124         pContext = new XFormsInstanceContext( GetImport(), nPrefix, rLocalName,
125                                               mxModel );
126         break;
127     case XML_BIND:
128         pContext = new XFormsBindContext( GetImport(), nPrefix, rLocalName,
129                                           mxModel );
130         break;
131     case XML_SUBMISSION:
132         pContext = new XFormsSubmissionContext( GetImport(), nPrefix,
133                                                 rLocalName, mxModel );
134         break;
135     case XML_SCHEMA:
136         pContext = new SchemaContext(
137             GetImport(), nPrefix, rLocalName,
138             Reference<com::sun::star::xforms::XModel>( mxModel,
139                                                        UNO_QUERY_THROW )
140                 ->getDataTypeRepository() );
141         break;
142     default:
143         DBG_ERROR( "Boooo!" );
144         break;
145     }
146 
147     return pContext;
148 }
149 
EndElement()150 void XFormsModelContext::EndElement()
151 {
152     // update before putting model into document
153     Reference<XUpdatable> xUpdate( mxModel, UNO_QUERY );
154     if( xUpdate.is() )
155         xUpdate->update();
156 
157     GetImport().initXForms();
158     lcl_addXFormsModel( GetImport().GetModel(), getModel() );
159 }
160