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