fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / dtrans / source / cnttype / mcnttype.cxx
blob129370196fd308717024d6fe793c73e2fb5939da
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "mcnttype.hxx"
22 //------------------------------------------------------------------------
23 // namespace directives
24 //------------------------------------------------------------------------
26 using namespace com::sun::star::uno;
27 using namespace com::sun::star::lang;
28 using namespace com::sun::star::container;
29 using namespace std;
30 using namespace osl;
33 //------------------------------------------------------------------------
34 // constants
35 //------------------------------------------------------------------------
37 const char TSPECIALS[] = "()<>@,;:\\\"/[]?=";
38 const char TOKEN[] = "!#$%&'*+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~.";
39 const char SPACE[] = " ";
40 const char SEMICOLON[] = ";";
42 //------------------------------------------------------------------------
43 // ctor
44 //------------------------------------------------------------------------
46 CMimeContentType::CMimeContentType( const OUString& aCntType )
48 init( aCntType );
51 //------------------------------------------------------------------------
53 //------------------------------------------------------------------------
55 OUString SAL_CALL CMimeContentType::getMediaType( ) throw(RuntimeException)
57 return m_MediaType;
60 //------------------------------------------------------------------------
62 //------------------------------------------------------------------------
64 OUString SAL_CALL CMimeContentType::getMediaSubtype( ) throw(RuntimeException)
66 return m_MediaSubtype;
69 //------------------------------------------------------------------------
71 //------------------------------------------------------------------------
73 OUString SAL_CALL CMimeContentType::getFullMediaType( ) throw(RuntimeException)
75 return m_MediaType + OUString("/") + m_MediaSubtype;
78 //------------------------------------------------------------------------
80 //------------------------------------------------------------------------
82 Sequence< OUString > SAL_CALL CMimeContentType::getParameters( ) throw(RuntimeException)
84 MutexGuard aGuard( m_aMutex );
86 Sequence< OUString > seqParams;
88 map< OUString, OUString >::iterator iter;
89 map< OUString, OUString >::iterator iter_end = m_ParameterMap.end( );
91 for ( iter = m_ParameterMap.begin( ); iter != iter_end; ++iter )
93 seqParams.realloc( seqParams.getLength( ) + 1 );
94 seqParams[seqParams.getLength( ) - 1] = iter->first;
97 return seqParams;
100 //------------------------------------------------------------------------
102 //------------------------------------------------------------------------
104 sal_Bool SAL_CALL CMimeContentType::hasParameter( const OUString& aName ) throw(RuntimeException)
106 MutexGuard aGuard( m_aMutex );
107 return ( m_ParameterMap.end( ) != m_ParameterMap.find( aName ) );
110 //------------------------------------------------------------------------
112 //------------------------------------------------------------------------
114 OUString SAL_CALL CMimeContentType::getParameterValue( const OUString& aName ) throw(NoSuchElementException, RuntimeException)
116 MutexGuard aGuard( m_aMutex );
118 if ( !hasParameter( aName ) )
119 throw NoSuchElementException( );
121 return m_ParameterMap.find( aName )->second;
124 //------------------------------------------------------------------------
126 //------------------------------------------------------------------------
128 void SAL_CALL CMimeContentType::init( const OUString& aCntType ) throw( IllegalArgumentException )
130 if ( aCntType.isEmpty( ) )
131 throw IllegalArgumentException( );
133 m_nPos = 0;
134 m_ContentType = aCntType;
135 getSym( );
136 type();
139 //------------------------------------------------------------------------
141 //------------------------------------------------------------------------
143 void SAL_CALL CMimeContentType::getSym( void )
145 if ( m_nPos < m_ContentType.getLength( ) )
147 m_nxtSym = m_ContentType.copy(m_nPos, 1);
148 ++m_nPos;
149 return;
152 m_nxtSym = OUString( );
155 //------------------------------------------------------------------------
157 //------------------------------------------------------------------------
159 void SAL_CALL CMimeContentType::acceptSym( const OUString& pSymTlb )
161 if ( pSymTlb.indexOf( m_nxtSym ) < 0 )
162 throw IllegalArgumentException( );
164 getSym();
167 //------------------------------------------------------------------------
169 //------------------------------------------------------------------------
171 void SAL_CALL CMimeContentType::skipSpaces( void )
173 while (m_nxtSym.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(SPACE)))
174 getSym( );
177 //------------------------------------------------------------------------
179 //------------------------------------------------------------------------
181 void SAL_CALL CMimeContentType::type( void )
183 skipSpaces( );
185 OUString sToken(TOKEN);
187 // check FIRST( type )
188 if ( !isInRange( m_nxtSym, sToken ) )
189 throw IllegalArgumentException( );
191 // parse
192 while( !m_nxtSym.isEmpty( ) )
194 if ( isInRange( m_nxtSym, sToken ) )
195 m_MediaType += m_nxtSym;
196 else if ( isInRange( m_nxtSym, OUString("/ ") ) )
197 break;
198 else
199 throw IllegalArgumentException( );
200 getSym( );
203 // check FOLLOW( type )
204 skipSpaces( );
205 acceptSym( OUString("/") );
207 subtype( );
210 //------------------------------------------------------------------------
212 //------------------------------------------------------------------------
214 void SAL_CALL CMimeContentType::subtype( void )
216 skipSpaces( );
218 OUString sToken(TOKEN);
220 // check FIRST( subtype )
221 if ( !isInRange( m_nxtSym, sToken ) )
222 throw IllegalArgumentException( );
224 while( !m_nxtSym.isEmpty( ) )
226 if ( isInRange( m_nxtSym, sToken ) )
227 m_MediaSubtype += m_nxtSym;
228 else if ( isInRange( m_nxtSym, OUString("; ") ) )
229 break;
230 else
231 throw IllegalArgumentException( );
232 getSym( );
235 // parse the rest
236 skipSpaces( );
237 trailer();
240 //------------------------------------------------------------------------
242 //------------------------------------------------------------------------
244 void SAL_CALL CMimeContentType::trailer( void )
246 OUString sToken(TOKEN);
247 while( !m_nxtSym.isEmpty( ) )
249 if ( m_nxtSym == OUString("(") )
251 getSym( );
252 comment( );
253 acceptSym( OUString(")") );
255 else if ( m_nxtSym == OUString(";") )
257 // get the parameter name
258 getSym( );
259 skipSpaces( );
261 if ( !isInRange( m_nxtSym, sToken ) )
262 throw IllegalArgumentException( );
264 OUString pname = pName( );
266 skipSpaces();
267 acceptSym( OUString("=") );
269 // get the parameter value
270 skipSpaces( );
272 OUString pvalue = pValue( );
274 // insert into map
275 if ( !m_ParameterMap.insert( pair < const OUString, OUString > ( pname, pvalue ) ).second )
276 throw IllegalArgumentException( );
278 else
279 throw IllegalArgumentException( );
281 skipSpaces( );
285 //------------------------------------------------------------------------
287 //------------------------------------------------------------------------
289 OUString SAL_CALL CMimeContentType::pName( )
291 OUString pname;
293 OUString sToken(TOKEN);
294 while( !m_nxtSym.isEmpty( ) )
296 if ( isInRange( m_nxtSym, sToken ) )
297 pname += m_nxtSym;
298 else if ( isInRange( m_nxtSym, OUString("= ") ) )
299 break;
300 else
301 throw IllegalArgumentException( );
302 getSym( );
305 return pname;
308 //------------------------------------------------------------------------
310 //------------------------------------------------------------------------
312 OUString SAL_CALL CMimeContentType::pValue( )
314 OUString pvalue;
316 OUString sToken(TOKEN);
317 // quoted pvalue
318 if ( m_nxtSym == OUString( "\"" ) )
320 getSym( );
321 pvalue = quotedPValue( );
323 if ( pvalue[pvalue.getLength() - 1] != '"' )
324 throw IllegalArgumentException( );
326 // remove the last quote-sign
327 pvalue = pvalue.copy(0, pvalue.getLength() - 1);
329 if ( pvalue.isEmpty( ) )
330 throw IllegalArgumentException( );
332 else if ( isInRange( m_nxtSym, sToken ) ) // unquoted pvalue
334 pvalue = nonquotedPValue( );
336 else
337 throw IllegalArgumentException( );
339 return pvalue;
342 //------------------------------------------------------------------------
343 // the following combinations within a quoted value are not allowed:
344 // '";' (quote sign followed by semicolon) and '" ' (quote sign followed
345 // by space)
346 //------------------------------------------------------------------------
348 OUString SAL_CALL CMimeContentType::quotedPValue( )
350 OUString pvalue;
351 sal_Bool bAfterQuoteSign = sal_False;
353 while ( !m_nxtSym.isEmpty( ) )
355 if ( bAfterQuoteSign && (
356 (m_nxtSym.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(SPACE))) ||
357 (m_nxtSym.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(SEMICOLON))))
360 break;
362 else if ( isInRange( m_nxtSym, OUString(TOKEN) + OUString(TSPECIALS) + OUString(SPACE) ) )
364 pvalue += m_nxtSym;
365 if ( m_nxtSym == OUString( "\"" ) )
366 bAfterQuoteSign = sal_True;
367 else
368 bAfterQuoteSign = sal_False;
370 else
371 throw IllegalArgumentException( );
372 getSym( );
375 return pvalue;
378 //------------------------------------------------------------------------
380 //------------------------------------------------------------------------
382 OUString SAL_CALL CMimeContentType::nonquotedPValue( )
384 OUString pvalue;
386 OUString sToken(TOKEN);
387 while ( !m_nxtSym.isEmpty( ) )
389 if ( isInRange( m_nxtSym, sToken ) )
390 pvalue += m_nxtSym;
391 else if ( isInRange( m_nxtSym, OUString("; ") ) )
392 break;
393 else
394 throw IllegalArgumentException( );
395 getSym( );
398 return pvalue;
401 //------------------------------------------------------------------------
403 //------------------------------------------------------------------------
405 void SAL_CALL CMimeContentType::comment( void )
407 while ( !m_nxtSym.isEmpty( ) )
409 if ( isInRange( m_nxtSym, OUString(TOKEN) + OUString(SPACE) ) )
410 getSym( );
411 else if ( m_nxtSym == OUString(")") )
412 break;
413 else
414 throw IllegalArgumentException( );
418 //------------------------------------------------------------------------
420 //------------------------------------------------------------------------
422 sal_Bool SAL_CALL CMimeContentType::isInRange( const OUString& aChr, const OUString& aRange )
424 return ( aRange.indexOf( aChr ) > -1 );
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */