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