1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 
31 #include <xmloff/xmlscripti.hxx>
32 #include "xmloff/xmlnmspe.hxx"
33 #include <xmloff/xmltoken.hxx>
34 #include <xmloff/xmlimp.hxx>
35 #include <xmloff/nmspmap.hxx>
36 #include <xmloff/XMLEventsImportContext.hxx>
37 #include "xmlbasici.hxx"
38 
39 #include <com/sun/star/document/XEventsSupplier.hpp>
40 #include <com/sun/star/document/XEmbeddedScripts.hpp>
41 
42 using ::rtl::OUString;
43 using namespace com::sun::star;
44 using namespace com::sun::star::uno;
45 using namespace com::sun::star::lang;
46 using namespace com::sun::star::frame;
47 using namespace com::sun::star::document;
48 using namespace com::sun::star::xml::sax;
49 using namespace ::xmloff::token;
50 
51 
52 // =============================================================================
53 // XMLScriptChildContext: context for <office:script> element
54 // =============================================================================
55 
56 class XMLScriptChildContext : public SvXMLImportContext
57 {
58 private:
59     ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >                 m_xModel;
60     ::com::sun::star::uno::Reference< ::com::sun::star::document::XEmbeddedScripts >    m_xDocumentScripts;
61     ::rtl::OUString m_aLanguage;
62 
63 public:
64     XMLScriptChildContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const ::rtl::OUString& rLName,
65         const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel>& rxModel,
66         const ::rtl::OUString& rLanguage );
67     virtual ~XMLScriptChildContext();
68 
69     virtual SvXMLImportContext* CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName,
70         const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
71 
72 	virtual void EndElement();
73 };
74 
75 // -----------------------------------------------------------------------------
76 
77 XMLScriptChildContext::XMLScriptChildContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const ::rtl::OUString& rLName,
78         const Reference< frame::XModel >& rxModel, const ::rtl::OUString& rLanguage )
79     :SvXMLImportContext( rImport, nPrfx, rLName )
80     ,m_xModel( rxModel )
81     ,m_xDocumentScripts( rxModel, UNO_QUERY )
82     ,m_aLanguage( rLanguage )
83 {
84 }
85 
86 // -----------------------------------------------------------------------------
87 
88 XMLScriptChildContext::~XMLScriptChildContext()
89 {
90 }
91 
92 // -----------------------------------------------------------------------------
93 
94 SvXMLImportContext* XMLScriptChildContext::CreateChildContext(
95     sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName,
96     const Reference< xml::sax::XAttributeList >& xAttrList )
97 {
98     SvXMLImportContext* pContext = NULL;
99 
100     if ( m_xDocumentScripts.is() )
101     {   // document supports embedding scripts/macros
102         ::rtl::OUString aBasic( GetImport().GetNamespaceMap().GetPrefixByKey( XML_NAMESPACE_OOO ) );
103         aBasic += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":Basic" ) );
104 
105         if ( m_aLanguage == aBasic && nPrefix == XML_NAMESPACE_OOO && IsXMLToken( rLocalName, XML_LIBRARIES ) )
106             pContext = new XMLBasicImportContext( GetImport(), nPrefix, rLocalName, m_xModel );
107     }
108 
109     if ( !pContext )
110         pContext = SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList );
111 
112     return pContext;
113 }
114 
115 // -----------------------------------------------------------------------------
116 
117 void XMLScriptChildContext::EndElement()
118 {
119 }
120 
121 // =============================================================================
122 // XMLScriptContext: context for <office:scripts> element
123 // =============================================================================
124 
125 XMLScriptContext::XMLScriptContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
126         const Reference<XModel>& rDocModel )
127     :SvXMLImportContext( rImport, nPrfx, rLName )
128     ,m_xModel( rDocModel )
129 {
130 }
131 
132 // -----------------------------------------------------------------------------
133 
134 XMLScriptContext::~XMLScriptContext()
135 {
136 }
137 
138 // -----------------------------------------------------------------------------
139 
140 SvXMLImportContext* XMLScriptContext::CreateChildContext(
141     sal_uInt16 nPrefix, const OUString& rLName,
142     const Reference<XAttributeList>& xAttrList )
143 {
144 	SvXMLImportContext* pContext = NULL;
145 
146     if ( nPrefix == XML_NAMESPACE_OFFICE )
147     {
148         if ( IsXMLToken( rLName, XML_EVENT_LISTENERS ) )
149         {
150             Reference< XEventsSupplier> xSupplier( GetImport().GetModel(), UNO_QUERY );
151             pContext = new XMLEventsImportContext( GetImport(), nPrefix, rLName, xSupplier );
152         }
153         else if ( IsXMLToken( rLName, XML_SCRIPT ) )
154         {
155             ::rtl::OUString aAttrName( GetImport().GetNamespaceMap().GetPrefixByKey( XML_NAMESPACE_SCRIPT ) );
156             aAttrName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":language" ) );
157             if ( xAttrList.is() )
158             {
159                 ::rtl::OUString aLanguage = xAttrList->getValueByName( aAttrName );
160 
161 				if ( m_xModel.is() )
162 				{
163 					uno::Sequence< beans::PropertyValue > aMedDescr = m_xModel->getArgs();
164 					sal_Int32 nNewLen = aMedDescr.getLength() + 1;
165 					aMedDescr.realloc( nNewLen );
166 					aMedDescr[nNewLen-1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BreakMacroSignature" ) );
167 					aMedDescr[nNewLen-1].Value <<= (sal_Bool)sal_True;
168 					m_xModel->attachResource( m_xModel->getURL(), aMedDescr );
169 
170                 	pContext = new XMLScriptChildContext( GetImport(), nPrefix, rLName, m_xModel, aLanguage );
171 				}
172             }
173         }
174 	}
175 
176     if ( !pContext )
177         pContext = SvXMLImportContext::CreateChildContext( nPrefix, rLName, xAttrList);
178 
179 	return pContext;
180 }
181 
182 // -----------------------------------------------------------------------------
183 
184 void XMLScriptContext::EndElement()
185 {
186 }
187 
188 // -----------------------------------------------------------------------------
189