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