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_scripting.hxx"
26 #include <osl/diagnose.h>
27 #include <osl/file.h>
28
29 #ifdef _DEBUG
30 #include <stdio.h>
31 #endif
32
33 //Local headers
34 #include "ScriptURI.hxx"
35 #include <util/util.hxx>
36
37 using namespace ::rtl;
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::lang;
41
42
43 namespace scripting_impl {
44
45 static const OUString schema = OUString::createFromAscii( "vnd.sun.star.script://" );
46
47 /**
48 * Constructor
49 *
50 * @param scriptURI the string script URI
51 */
ScriptURI(const::rtl::OUString & scriptURI)52 ScriptURI::ScriptURI( const ::rtl::OUString& scriptURI ) : m_uri( scriptURI )
53 {
54 OSL_TRACE( "received uri: %s\n",::rtl::OUStringToOString( m_uri, RTL_TEXTENCODING_ASCII_US).pData->buffer );
55 set_values( parseIt() );
56 if( !isValid() )
57 {
58 OSL_TRACE( "ScriptURI ctor: throwing IllegalArgException" );
59 throw IllegalArgumentException(
60 OUSTR( "Failed to parse invalid URI: " ).concat( scriptURI ),
61 Reference < XInterface > (), 1 );
62 }
63 }
64
65 /**
66 * Destuctor
67 *
68 */
69 // dtor should never throw exceptions, so ensure this by specifying it
~ScriptURI()70 ScriptURI::~ScriptURI() SAL_THROW( () )
71 {
72 OSL_TRACE( "< ScriptURI dtor called >\n" );
73 }
74
75 /**
76 * This function is used to determine if this object represents a valid URI
77 *
78 */
isValid()79 bool ScriptURI::isValid( ) {
80 return ( m_valid == sal_True );
81 }
82
83 /**
84 * This function returns the location of the script
85 *
86 */
getLocation()87 ::rtl::OUString ScriptURI::getLocation( )
88 {
89 return m_location;
90 }
91
92 /**
93 * This function returns the language of the script, eg. java, StarBasic,...
94 *
95 */
getLanguage()96 ::rtl::OUString ScriptURI::getLanguage( )
97 {
98 return m_language;
99 }
100
101 /**
102 * This function returns the language dependent function name of the script
103 *
104 */
getFunctionName()105 ::rtl::OUString ScriptURI::getFunctionName( )
106 {
107 return m_functionName;
108 }
109
110 /**
111 * This function returns the language independent logical name of the script
112 *
113 */
getLogicalName()114 ::rtl::OUString ScriptURI::getLogicalName( )
115 {
116 return m_logicalName;
117 }
118
119 /**
120 * This function returns the full URI
121 *
122 */
getURI()123 ::rtl::OUString ScriptURI::getURI( )
124 {
125 return m_uri;
126 }
127
128 //Private mutex guarded method for setting the members
set_values(scripting_impl::Uri values)129 void ScriptURI::set_values( scripting_impl::Uri values )
130 {
131 osl::Guard< ::osl::Mutex > aGuard( m_mutex );
132 m_valid = values.valid;
133 m_location = values.location;
134 m_language = values.language;
135 // format is vnd.sun.star.script://[function_name]?language=[language]&location=[location]
136 // LogicalName is now not used anymore, further more the ScriptURI class
137 // will be retired also and a new UNO service will be used. Additionally the
138 // parcel-description will also need to be modified to remove logical name
139 // In order to temporarily support the existing code functionname is
140 // set to the logica name parsed by this class. So getLogicalName() and
141 // getFunctionName() return identical string.
142 //
143
144 m_functionName = values.logicalName;
145 m_logicalName = values.logicalName;
146
147 }
148 /**
149 * This is a private method used for parsing the URI received by the
150 * initialization.
151 *
152 */
153 // rather naive parsing?
parseIt()154 Uri ScriptURI::parseIt()
155 {
156 sal_Int32 schemaLen = schema.getLength();
157 scripting_impl::Uri results;
158 results.valid = sal_True;
159 //attempt to parse
160 // check that it starts vnd.sun.star.script
161 // better check for OBO errors here
162 if( m_uri.indexOf( schema ) != 0 )
163 {
164 OSL_TRACE( "wrong schema" );
165 results.valid=sal_False;
166 return results;
167 }
168
169 // substr from here to the '?' and set the logical name
170 sal_Int32 len = m_uri.indexOf( '?' );
171 if( len == -1 )
172 {
173 // no queries so just set the logical name
174 results.logicalName = m_uri.copy( schemaLen );
175 return results;
176 }
177 results.logicalName = m_uri.copy( schemaLen, len-schemaLen );
178 OSL_TRACE( "log name: %s\n", ::rtl::OUStringToOString( results.logicalName,
179 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
180
181 len++;
182 // now get the attributes
183 OUString attr;
184 do
185 {
186 attr = m_uri.getToken( 0, '&', len );
187 OSL_TRACE( "chunk: %s\n", ::rtl::OUStringToOString( attr,
188 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
189
190 if( attr.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "language" ) )
191 == sal_True )
192 {
193 sal_Int32 len2 = attr.indexOf('=');
194 results.language = attr.copy( len2 + 1 );
195 continue;
196 }
197 else if ( attr.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "location" ) )
198 == sal_True )
199 {
200 sal_Int32 len2 = attr.indexOf( '=' );
201 results.location = attr.copy( len2 + 1 );
202 continue;
203 }
204 else if ( attr.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "function" ) )
205 == sal_True )
206 {
207 sal_Int32 len2 = attr.indexOf( '=' );
208 results.functionName = attr.copy( len2 + 1 );
209 continue;
210 }
211 else
212 {
213 OSL_TRACE( "Unknown attribute?\n" );
214 }
215 }
216 while ( len >= 0 );
217
218 // parse is good
219 return results;
220 }
221
222 } // namespace script_uri
223