1*2f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2f86921cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2f86921cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2f86921cSAndrew Rist  * distributed with this work for additional information
6*2f86921cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2f86921cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2f86921cSAndrew Rist  * "License"); you may not use this file except in compliance
9*2f86921cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2f86921cSAndrew Rist  *
11*2f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2f86921cSAndrew Rist  *
13*2f86921cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2f86921cSAndrew Rist  * software distributed under the License is distributed on an
15*2f86921cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2f86921cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2f86921cSAndrew Rist  * specific language governing permissions and limitations
18*2f86921cSAndrew Rist  * under the License.
19*2f86921cSAndrew Rist  *
20*2f86921cSAndrew Rist  *************************************************************/
21*2f86921cSAndrew Rist 
22*2f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_ucb.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "ucpext_provider.hxx"
27cdf0e10cSrcweir #include "ucpext_content.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir /** === begin UNO includes === **/
30cdf0e10cSrcweir /** === end UNO includes === **/
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <ucbhelper/contentidentifier.hxx>
33cdf0e10cSrcweir #include <osl/diagnose.h>
34cdf0e10cSrcweir #include <osl/mutex.hxx>
35cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
36cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir //......................................................................................................................
39cdf0e10cSrcweir namespace ucb { namespace ucp { namespace ext
40cdf0e10cSrcweir {
41cdf0e10cSrcweir //......................................................................................................................
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 	/** === begin UNO using === **/
44cdf0e10cSrcweir 	using ::com::sun::star::uno::Reference;
45cdf0e10cSrcweir 	using ::com::sun::star::uno::XInterface;
46cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY;
47cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY_THROW;
48cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_SET_THROW;
49cdf0e10cSrcweir 	using ::com::sun::star::uno::Exception;
50cdf0e10cSrcweir 	using ::com::sun::star::uno::RuntimeException;
51cdf0e10cSrcweir 	using ::com::sun::star::uno::Any;
52cdf0e10cSrcweir 	using ::com::sun::star::uno::makeAny;
53cdf0e10cSrcweir 	using ::com::sun::star::uno::Sequence;
54cdf0e10cSrcweir 	using ::com::sun::star::uno::Type;
55cdf0e10cSrcweir     using ::com::sun::star::lang::XMultiServiceFactory;
56cdf0e10cSrcweir     using ::com::sun::star::ucb::XContentIdentifier;
57cdf0e10cSrcweir     using ::com::sun::star::ucb::IllegalIdentifierException;
58cdf0e10cSrcweir     using ::com::sun::star::ucb::XContent;
59cdf0e10cSrcweir     using ::com::sun::star::uno::XComponentContext;
60cdf0e10cSrcweir 	/** === end UNO using === **/
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     //==================================================================================================================
63cdf0e10cSrcweir     //= ContentProvider
64cdf0e10cSrcweir     //==================================================================================================================
65cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
ContentProvider(const Reference<XMultiServiceFactory> & i_rServiceManager)66cdf0e10cSrcweir     ContentProvider::ContentProvider( const Reference< XMultiServiceFactory >& i_rServiceManager )
67cdf0e10cSrcweir         :ContentProvider_Base( i_rServiceManager )
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
~ContentProvider()72cdf0e10cSrcweir     ContentProvider::~ContentProvider()
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
getImplementationName_static()77cdf0e10cSrcweir     ::rtl::OUString SAL_CALL ContentProvider::getImplementationName_static() throw (RuntimeException)
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.ucp.ext.ContentProvider" ) );
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
getImplementationName()83cdf0e10cSrcweir     ::rtl::OUString SAL_CALL ContentProvider::getImplementationName() throw (RuntimeException)
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         return getImplementationName_static();
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
getSupportedServiceNames_static()89cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL ContentProvider::getSupportedServiceNames_static(  ) throw (RuntimeException)
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir         Sequence< ::rtl::OUString > aServiceNames(2);
92cdf0e10cSrcweir         aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.ContentProvider" ) );
93cdf0e10cSrcweir         aServiceNames[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.ExtensionContentProvider" ) );
94cdf0e10cSrcweir         return aServiceNames;
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
getSupportedServiceNames()98cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL ContentProvider::getSupportedServiceNames(  ) throw (RuntimeException)
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir         return getSupportedServiceNames_static();
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
Create(const Reference<XComponentContext> & i_rContext)104cdf0e10cSrcweir     Reference< XInterface > ContentProvider::Create( const Reference< XComponentContext >& i_rContext )
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         const ::comphelper::ComponentContext aContext( i_rContext );
107cdf0e10cSrcweir         return *( new ContentProvider( aContext.getLegacyServiceFactory() ) );
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
getRootURL()111cdf0e10cSrcweir     ::rtl::OUString ContentProvider::getRootURL()
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.extension://" ) );
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
getArtificialNodeContentType()117cdf0e10cSrcweir     ::rtl::OUString ContentProvider::getArtificialNodeContentType()
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/vnd.sun.star.extension-content" ) );
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
123cdf0e10cSrcweir     namespace
124cdf0e10cSrcweir     {
lcl_ensureAndTransfer(::rtl::OUString & io_rIdentifierFragment,::rtl::OUStringBuffer & o_rNormalization,const sal_Unicode i_nLeadingChar)125cdf0e10cSrcweir         void lcl_ensureAndTransfer( ::rtl::OUString& io_rIdentifierFragment, ::rtl::OUStringBuffer& o_rNormalization, const sal_Unicode i_nLeadingChar )
126cdf0e10cSrcweir         {
127cdf0e10cSrcweir             if ( ( io_rIdentifierFragment.getLength() == 0 ) || ( io_rIdentifierFragment[0] != i_nLeadingChar ) )
128cdf0e10cSrcweir                 throw IllegalIdentifierException();
129cdf0e10cSrcweir             io_rIdentifierFragment = io_rIdentifierFragment.copy( 1 );
130cdf0e10cSrcweir             o_rNormalization.append( i_nLeadingChar );
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
queryContent(const Reference<XContentIdentifier> & i_rIdentifier)135cdf0e10cSrcweir     Reference< XContent > SAL_CALL ContentProvider::queryContent( const Reference< XContentIdentifier  >& i_rIdentifier )
136cdf0e10cSrcweir         throw( IllegalIdentifierException, RuntimeException )
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         // Check URL scheme...
139cdf0e10cSrcweir         const ::rtl::OUString sScheme( rtl::OUString::createFromAscii( "vnd.sun.star.extension" ) );
140cdf0e10cSrcweir         if ( !i_rIdentifier->getContentProviderScheme().equalsIgnoreAsciiCase( sScheme ) )
141cdf0e10cSrcweir             throw IllegalIdentifierException();
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         // normalize the identifier
144cdf0e10cSrcweir         const ::rtl::OUString sIdentifier( i_rIdentifier->getContentIdentifier() );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         // the scheme needs to be lower-case
147cdf0e10cSrcweir         ::rtl::OUStringBuffer aComposer;
148cdf0e10cSrcweir         aComposer.append( sIdentifier.copy( 0, sScheme.getLength() ).toAsciiLowerCase() );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         // one : is required after the scheme
151cdf0e10cSrcweir         ::rtl::OUString sRemaining( sIdentifier.copy( sScheme.getLength() ) );
152cdf0e10cSrcweir         lcl_ensureAndTransfer( sRemaining, aComposer, ':' );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         // and at least one /
155cdf0e10cSrcweir         lcl_ensureAndTransfer( sRemaining, aComposer, '/' );
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         // the normalized form requires one additional /, but we also accept identifiers which don't have it
158cdf0e10cSrcweir         if ( sRemaining.getLength() == 0 )
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             // the root content is a special case, it requires ///
161cdf0e10cSrcweir             aComposer.appendAscii( "//" );
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         else
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             if ( sRemaining[0] != '/' )
166cdf0e10cSrcweir             {
167cdf0e10cSrcweir                 aComposer.append( sal_Unicode( '/' ) );
168cdf0e10cSrcweir                 aComposer.append( sRemaining );
169cdf0e10cSrcweir             }
170cdf0e10cSrcweir             else
171cdf0e10cSrcweir             {
172cdf0e10cSrcweir                 lcl_ensureAndTransfer( sRemaining, aComposer, '/' );
173cdf0e10cSrcweir                 // by now, we moved "vnd.sun.star.extension://" from the URL to aComposer
174cdf0e10cSrcweir                 if ( sRemaining.getLength() == 0 )
175cdf0e10cSrcweir                 {
176cdf0e10cSrcweir                     // again, it's the root content, but one / is missing
177cdf0e10cSrcweir                     aComposer.append( sal_Unicode( '/' ) );
178cdf0e10cSrcweir                 }
179cdf0e10cSrcweir                 else
180cdf0e10cSrcweir                 {
181cdf0e10cSrcweir                     aComposer.append( sRemaining );
182cdf0e10cSrcweir                 }
183cdf0e10cSrcweir             }
184cdf0e10cSrcweir         }
185cdf0e10cSrcweir         const Reference< XContentIdentifier > xNormalizedIdentifier( new ::ucbhelper::ContentIdentifier( m_xSMgr, aComposer.makeStringAndClear() ) );
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         // check if a content with given id already exists...
190cdf0e10cSrcweir         Reference< XContent > xContent( queryExistingContent( xNormalizedIdentifier ).get() );
191cdf0e10cSrcweir         if ( xContent.is() )
192cdf0e10cSrcweir             return xContent;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         // create a new content
195cdf0e10cSrcweir         xContent = new Content( m_xSMgr, this, xNormalizedIdentifier );
196cdf0e10cSrcweir         if ( !xContent->getIdentifier().is() )
197cdf0e10cSrcweir             throw IllegalIdentifierException();
198cdf0e10cSrcweir 
199cdf0e10cSrcweir         registerNewContent( xContent );
200cdf0e10cSrcweir         return xContent;
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir //......................................................................................................................
204cdf0e10cSrcweir } } }   // namespace ucb::ucp::ext
205cdf0e10cSrcweir //......................................................................................................................
206