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 "XFormsInstanceContext.hxx"
28 
29 #include "DomBuilderContext.hxx"
30 #include "xformsapi.hxx"
31 
32 #include <rtl/ustring.hxx>
33 #include <com/sun/star/uno/Reference.hxx>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <com/sun/star/xml/dom/XDocument.hpp>
37 #include <com/sun/star/xforms/XModel.hpp>
38 #include <tools/debug.hxx>
39 
40 #include <xmloff/xmlnmspe.hxx>
41 #include <xmloff/xmltoken.hxx>
42 #include <xmloff/xmlimp.hxx>
43 #include <xmloff/xmlerror.hxx>
44 #include <xmloff/nmspmap.hxx>
45 
46 
47 using rtl::OUString;
48 using com::sun::star::uno::Reference;
49 using com::sun::star::uno::makeAny;
50 using com::sun::star::uno::UNO_QUERY;
51 using com::sun::star::uno::Sequence;
52 using com::sun::star::xforms::XModel;
53 using com::sun::star::beans::XPropertySet;
54 using com::sun::star::beans::PropertyValue;
55 using com::sun::star::xml::sax::XAttributeList;
56 
57 using xmloff::token::IsXMLToken;
58 using xmloff::token::XML_INSTANCE;
59 using xmloff::token::XML_SRC;
60 using xmloff::token::XML_ID;
61 
62 static SvXMLTokenMapEntry aAttributes[] =
63 {
64     TOKEN_MAP_ENTRY( NONE, SRC ),
65     TOKEN_MAP_ENTRY( NONE, ID ),
66     XML_TOKEN_MAP_END
67 };
68 
XFormsInstanceContext(SvXMLImport & rImport,sal_uInt16 nPrefix,const OUString & rLocalName,Reference<XPropertySet> xModel)69 XFormsInstanceContext::XFormsInstanceContext(
70     SvXMLImport& rImport,
71     sal_uInt16 nPrefix,
72     const OUString& rLocalName,
73     Reference<XPropertySet> xModel ) :
74         TokenContext( rImport, nPrefix, rLocalName, aAttributes, aEmptyMap ),
75         mxModel( Reference<XModel>( xModel, UNO_QUERY ) )
76 {
77     DBG_ASSERT( mxModel.is(), "need model" );
78 }
79 
~XFormsInstanceContext()80 XFormsInstanceContext::~XFormsInstanceContext()
81 {
82 }
83 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<XAttributeList> &)84 SvXMLImportContext* XFormsInstanceContext::CreateChildContext(
85     sal_uInt16 nPrefix,
86     const OUString& rLocalName,
87     const Reference<XAttributeList>& )
88 {
89     SvXMLImportContext* pContext = NULL;
90 
91     // only the first element child of an xforms:instance element
92     // is used as an instance. The other children remainder must be
93     // ignored.
94     if( mxInstance.is() )
95     {
96         GetImport().SetError( XMLERROR_XFORMS_ONLY_ONE_INSTANCE_ELEMENT, rLocalName );
97         pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
98     }
99     else
100     {
101         // create new DomBuilderContext. Save reference to tree in Model.
102         DomBuilderContext* pInstance =
103             new DomBuilderContext( GetImport(), nPrefix, rLocalName );
104         mxInstance = pInstance->getTree();
105         pContext = pInstance;
106     }
107 
108     DBG_ASSERT( pContext != NULL, "no context!" );
109     return pContext;
110 
111 }
112 
EndElement()113 void XFormsInstanceContext::EndElement()
114 {
115     Sequence<PropertyValue> aSequence( 3 );
116     PropertyValue* pSequence = aSequence.getArray();
117     pSequence[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM("Instance") );
118     pSequence[0].Value <<= mxInstance;
119     pSequence[1].Name = OUString( RTL_CONSTASCII_USTRINGPARAM("ID") );
120     pSequence[1].Value <<= msId;
121     pSequence[2].Name = OUString( RTL_CONSTASCII_USTRINGPARAM("URL") );
122     pSequence[2].Value <<= msURL;
123 
124     mxModel->getInstances()->insert( makeAny( aSequence ) );
125 }
126 
127 
HandleAttribute(sal_uInt16 nToken,const rtl::OUString & rValue)128 void XFormsInstanceContext::HandleAttribute(
129     sal_uInt16 nToken,
130     const rtl::OUString& rValue )
131 {
132     switch( nToken )
133     {
134     case XML_SRC:
135         msURL = rValue;
136         break;
137     case XML_ID:
138         msId = rValue;
139         break;
140     default:
141         DBG_ERROR( "should not happen" );
142         break;
143     }
144 }
145 
HandleChild(sal_uInt16,sal_uInt16,const OUString &,const Reference<XAttributeList> &)146 SvXMLImportContext* XFormsInstanceContext::HandleChild(
147     sal_uInt16,
148     sal_uInt16,
149     const OUString&,
150     const Reference<XAttributeList>& )
151 {
152     DBG_ERROR( "to be handled by CreateChildContext" );
153     return NULL;
154 }
155