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_xmlscript.hxx"
26 #include "imp_share.hxx"
27 
28 #include <osl/diagnose.h>
29 
30 #include <rtl/ustrbuf.hxx>
31 
32 #include <xml_import.hxx>
33 #include <comphelper/processfactory.hxx>
34 
35 
36 namespace xmlscript
37 {
38 
39 //##################################################################################################
40 
41 //__________________________________________________________________________________________________
getParent()42 Reference< xml::input::XElement > ModuleElement::getParent()
43     throw (RuntimeException)
44 {
45     return static_cast< xml::input::XElement * >( _pParent );
46 }
47 //__________________________________________________________________________________________________
getLocalName()48 OUString ModuleElement::getLocalName()
49     throw (RuntimeException)
50 {
51     return _aLocalName;
52 }
53 //__________________________________________________________________________________________________
getUid()54 sal_Int32 ModuleElement::getUid()
55     throw (RuntimeException)
56 {
57     return _pImport->XMLNS_SCRIPT_UID;
58 }
59 //__________________________________________________________________________________________________
getAttributes()60 Reference< xml::input::XAttributes > ModuleElement::getAttributes()
61     throw (RuntimeException)
62 {
63     return _xAttributes;
64 }
65 //__________________________________________________________________________________________________
ignorableWhitespace(OUString const &)66 void ModuleElement::ignorableWhitespace(
67     OUString const & /*rWhitespaces*/ )
68     throw (xml::sax::SAXException, RuntimeException)
69 {
70     // not used
71 }
72 //__________________________________________________________________________________________________
characters(OUString const & rChars)73 void ModuleElement::characters( OUString const & rChars )
74     throw (xml::sax::SAXException, RuntimeException)
75 {
76     _StrBuffer.append( rChars );
77 }
78 //__________________________________________________________________________________________________
processingInstruction(OUString const &,OUString const &)79 void ModuleElement::processingInstruction(
80     OUString const & /*rTarget*/, OUString const & /*rData*/ )
81     throw (xml::sax::SAXException, RuntimeException)
82 {
83 }
84 //__________________________________________________________________________________________________
endElement()85 void ModuleElement::endElement()
86     throw (xml::sax::SAXException, RuntimeException)
87 {
88     _pImport->mrModuleDesc.aCode = _StrBuffer.makeStringAndClear();
89 }
90 //__________________________________________________________________________________________________
startChildElement(sal_Int32,OUString const &,Reference<xml::input::XAttributes> const &)91 Reference< xml::input::XElement > ModuleElement::startChildElement(
92     sal_Int32 /*nUid*/, OUString const & /*rLocalName*/,
93     Reference< xml::input::XAttributes > const & /*xAttributes*/ )
94     throw (xml::sax::SAXException, RuntimeException)
95 {
96     throw xml::sax::SAXException(
97         OUString( RTL_CONSTASCII_USTRINGPARAM("unexpected element!") ),
98         Reference< XInterface >(), Any() );
99 }
100 
101 //__________________________________________________________________________________________________
ModuleElement(OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes,ModuleElement * pParent,ModuleImport * pImport)102 ModuleElement::ModuleElement(
103     OUString const & rLocalName,
104     Reference< xml::input::XAttributes > const & xAttributes,
105     ModuleElement * pParent, ModuleImport * pImport )
106     SAL_THROW( () )
107     : _pImport( pImport )
108     , _pParent( pParent )
109     , _aLocalName( rLocalName )
110     , _xAttributes( xAttributes )
111 {
112     _pImport->acquire();
113 
114     if (_pParent)
115     {
116         _pParent->acquire();
117     }
118 }
119 //__________________________________________________________________________________________________
~ModuleElement()120 ModuleElement::~ModuleElement()
121     SAL_THROW( () )
122 {
123     _pImport->release();
124 
125     if (_pParent)
126     {
127         _pParent->release();
128     }
129 
130 #if OSL_DEBUG_LEVEL > 1
131     OString aStr( OUStringToOString( _aLocalName, RTL_TEXTENCODING_ASCII_US ) );
132     OSL_TRACE( "ModuleElement::~ModuleElement(): %s\n", aStr.getStr() );
133 #endif
134 }
135 
136 //##################################################################################################
137 
138 // XRoot
139 
140 //______________________________________________________________________________
startDocument(Reference<xml::input::XNamespaceMapping> const & xNamespaceMapping)141 void ModuleImport::startDocument(
142     Reference< xml::input::XNamespaceMapping > const & xNamespaceMapping )
143     throw (xml::sax::SAXException, RuntimeException)
144 {
145     XMLNS_SCRIPT_UID = xNamespaceMapping->getUidByUri(
146         OUSTR(XMLNS_SCRIPT_URI) );
147     XMLNS_LIBRARY_UID = xNamespaceMapping->getUidByUri(
148         OUSTR(XMLNS_LIBRARY_URI) );
149     XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri(
150         OUSTR(XMLNS_XLINK_URI) );
151 }
152 
153 //__________________________________________________________________________________________________
endDocument()154 void ModuleImport::endDocument()
155     throw (xml::sax::SAXException, RuntimeException)
156 {
157     // ignored
158 }
159 //__________________________________________________________________________________________________
processingInstruction(OUString const &,OUString const &)160 void ModuleImport::processingInstruction(
161     OUString const & /*rTarget*/, OUString const & /*rData*/ )
162     throw (xml::sax::SAXException, RuntimeException)
163 {
164 }
165 //__________________________________________________________________________________________________
setDocumentLocator(Reference<xml::sax::XLocator> const &)166 void ModuleImport::setDocumentLocator(
167     Reference< xml::sax::XLocator > const & /*xLocator*/ )
168     throw (xml::sax::SAXException, RuntimeException)
169 {
170 }
171 //__________________________________________________________________________________________________
startRootElement(sal_Int32 nUid,OUString const & rLocalName,Reference<xml::input::XAttributes> const & xAttributes)172 Reference< xml::input::XElement > ModuleImport::startRootElement(
173     sal_Int32 nUid, OUString const & rLocalName,
174     Reference< xml::input::XAttributes > const & xAttributes )
175     throw (xml::sax::SAXException, RuntimeException)
176 {
177     if (XMLNS_SCRIPT_UID != nUid)
178     {
179         throw xml::sax::SAXException(
180             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal namespace!") ),
181             Reference< XInterface >(), Any() );
182     }
183     // window
184     else if (rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("module") ))
185     {
186         mrModuleDesc.aName = xAttributes->getValueByUidName(
187             XMLNS_SCRIPT_UID,
188             OUString( RTL_CONSTASCII_USTRINGPARAM("name") ) );
189         mrModuleDesc.aLanguage = xAttributes->getValueByUidName(
190             XMLNS_SCRIPT_UID,
191             OUString( RTL_CONSTASCII_USTRINGPARAM("language") ) );
192         mrModuleDesc.aModuleType = xAttributes->getValueByUidName(
193             XMLNS_SCRIPT_UID,
194             OUString( RTL_CONSTASCII_USTRINGPARAM("moduleType") ) );
195 
196         return new ModuleElement( rLocalName, xAttributes, 0, this );
197     }
198     else
199     {
200         throw xml::sax::SAXException(
201             OUString( RTL_CONSTASCII_USTRINGPARAM(
202                           "illegal root element (expected module) given: ") ) +
203             rLocalName, Reference< XInterface >(), Any() );
204     }
205 }
206 //__________________________________________________________________________________________________
~ModuleImport()207 ModuleImport::~ModuleImport()
208     SAL_THROW( () )
209 {
210 #if OSL_DEBUG_LEVEL > 1
211     OSL_TRACE( "ModuleImport::~ModuleImport().\n" );
212 #endif
213 }
214 
215 //##################################################################################################
216 
217 Reference< xml::sax::XDocumentHandler >
importScriptModule(ModuleDescriptor & rMod)218 SAL_CALL importScriptModule( ModuleDescriptor& rMod )
219     SAL_THROW( (Exception) )
220 {
221     return ::xmlscript::createDocumentHandler(
222         static_cast< xml::input::XRoot * >( new ModuleImport( rMod ) ) );
223 }
224 
225 }
226 
227