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