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