update dev300-m58
[ooovba.git] / dtrans / source / cnttype / mcnttype.cxx
blob60175182b04dd4eec263c567716b37d42c628324
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mcnttype.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dtrans.hxx"
34 //------------------------------------------------------------------------
35 // includes
36 //------------------------------------------------------------------------
37 #include "mcnttype.hxx"
39 //------------------------------------------------------------------------
40 // namespace directives
41 //------------------------------------------------------------------------
43 using namespace com::sun::star::uno;
44 using namespace com::sun::star::lang;
45 using namespace com::sun::star::container;
46 using namespace rtl;
47 using namespace std;
48 using namespace osl;
50 //------------------------------------------------------------------------
51 // constants
52 //------------------------------------------------------------------------
54 const OUString TSPECIALS = OUString::createFromAscii( "()<>@,;:\\\"/[]?=" );
55 const OUString TOKEN = OUString::createFromAscii( "!#$%&'*+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~." );
56 const OUString SPACE = OUString::createFromAscii( " " );
57 const OUString SEMICOLON = OUString::createFromAscii( ";" );
59 //------------------------------------------------------------------------
60 // ctor
61 //------------------------------------------------------------------------
63 CMimeContentType::CMimeContentType( const OUString& aCntType )
65 init( aCntType );
68 //------------------------------------------------------------------------
69 //
70 //------------------------------------------------------------------------
72 OUString SAL_CALL CMimeContentType::getMediaType( ) throw(RuntimeException)
74 return m_MediaType;
77 //------------------------------------------------------------------------
78 //
79 //------------------------------------------------------------------------
81 OUString SAL_CALL CMimeContentType::getMediaSubtype( ) throw(RuntimeException)
83 return m_MediaSubtype;
86 //------------------------------------------------------------------------
87 //
88 //------------------------------------------------------------------------
90 OUString SAL_CALL CMimeContentType::getFullMediaType( ) throw(RuntimeException)
92 return m_MediaType + OUString::createFromAscii( "/" ) + m_MediaSubtype;
95 //------------------------------------------------------------------------
96 //
97 //------------------------------------------------------------------------
99 Sequence< OUString > SAL_CALL CMimeContentType::getParameters( ) throw(RuntimeException)
101 MutexGuard aGuard( m_aMutex );
103 Sequence< OUString > seqParams;
105 map< OUString, OUString >::iterator iter;
106 map< OUString, OUString >::iterator iter_end = m_ParameterMap.end( );
108 for ( iter = m_ParameterMap.begin( ); iter != iter_end; ++iter )
110 seqParams.realloc( seqParams.getLength( ) + 1 );
111 seqParams[seqParams.getLength( ) - 1] = iter->first;
114 return seqParams;
117 //------------------------------------------------------------------------
119 //------------------------------------------------------------------------
121 sal_Bool SAL_CALL CMimeContentType::hasParameter( const OUString& aName ) throw(RuntimeException)
123 MutexGuard aGuard( m_aMutex );
124 return ( m_ParameterMap.end( ) != m_ParameterMap.find( aName ) );
127 //------------------------------------------------------------------------
129 //------------------------------------------------------------------------
131 OUString SAL_CALL CMimeContentType::getParameterValue( const OUString& aName ) throw(NoSuchElementException, RuntimeException)
133 MutexGuard aGuard( m_aMutex );
135 if ( !hasParameter( aName ) )
136 throw NoSuchElementException( );
138 return m_ParameterMap.find( aName )->second;
141 //------------------------------------------------------------------------
143 //------------------------------------------------------------------------
145 void SAL_CALL CMimeContentType::init( const OUString& aCntType ) throw( IllegalArgumentException )
147 if ( !aCntType.getLength( ) )
148 throw IllegalArgumentException( );
150 m_nPos = 0;
151 m_ContentType = aCntType;
152 getSym( );
153 type();
156 //------------------------------------------------------------------------
158 //------------------------------------------------------------------------
160 void SAL_CALL CMimeContentType::getSym( void )
162 if ( m_nPos < m_ContentType.getLength( ) )
164 m_nxtSym = OUString( &m_ContentType[m_nPos], 1 );
165 ++m_nPos;
166 return;
169 m_nxtSym = OUString( );
172 //------------------------------------------------------------------------
174 //------------------------------------------------------------------------
176 void SAL_CALL CMimeContentType::acceptSym( const OUString& pSymTlb )
178 if ( pSymTlb.indexOf( m_nxtSym ) < 0 )
179 throw IllegalArgumentException( );
181 getSym();
184 //------------------------------------------------------------------------
186 //------------------------------------------------------------------------
188 void SAL_CALL CMimeContentType::skipSpaces( void )
190 while ( SPACE == m_nxtSym )
191 getSym( );
194 //------------------------------------------------------------------------
196 //------------------------------------------------------------------------
198 void SAL_CALL CMimeContentType::type( void )
200 skipSpaces( );
202 // check FIRST( type )
203 if ( !isInRange( m_nxtSym, TOKEN ) )
204 throw IllegalArgumentException( );
206 // parse
207 while( m_nxtSym.getLength( ) )
209 if ( isInRange( m_nxtSym, TOKEN ) )
210 m_MediaType += m_nxtSym;
211 else if ( isInRange( m_nxtSym, OUString::createFromAscii( "/ " ) ) )
212 break;
213 else
214 throw IllegalArgumentException( );
215 getSym( );
218 // check FOLLOW( type )
219 skipSpaces( );
220 acceptSym( OUString::createFromAscii( "/" ) );
222 subtype( );
225 //------------------------------------------------------------------------
227 //------------------------------------------------------------------------
229 void SAL_CALL CMimeContentType::subtype( void )
231 skipSpaces( );
233 // check FIRST( subtype )
234 if ( !isInRange( m_nxtSym, TOKEN ) )
235 throw IllegalArgumentException( );
237 while( m_nxtSym.getLength( ) )
239 if ( isInRange( m_nxtSym, TOKEN ) )
240 m_MediaSubtype += m_nxtSym;
241 else if ( isInRange( m_nxtSym, OUString::createFromAscii( "; " ) ) )
242 break;
243 else
244 throw IllegalArgumentException( );
245 getSym( );
248 // parse the rest
249 skipSpaces( );
250 trailer();
253 //------------------------------------------------------------------------
255 //------------------------------------------------------------------------
257 void SAL_CALL CMimeContentType::trailer( void )
259 while( m_nxtSym.getLength( ) )
261 if ( m_nxtSym == OUString::createFromAscii( "(" ) )
263 getSym( );
264 comment( );
265 acceptSym( OUString::createFromAscii( ")" ) );
267 else if ( m_nxtSym == OUString::createFromAscii( ";" ) )
269 // get the parameter name
270 getSym( );
271 skipSpaces( );
273 if ( !isInRange( m_nxtSym, TOKEN ) )
274 throw IllegalArgumentException( );
276 OUString pname = pName( );
278 skipSpaces();
279 acceptSym( OUString::createFromAscii( "=" ) );
281 // get the parameter value
282 skipSpaces( );
284 OUString pvalue = pValue( );
286 // insert into map
287 if ( !m_ParameterMap.insert( pair < const OUString, OUString > ( pname, pvalue ) ).second )
288 throw IllegalArgumentException( );
290 else
291 throw IllegalArgumentException( );
293 skipSpaces( );
297 //------------------------------------------------------------------------
299 //------------------------------------------------------------------------
301 OUString SAL_CALL CMimeContentType::pName( )
303 OUString pname;
305 while( m_nxtSym.getLength( ) )
307 if ( isInRange( m_nxtSym, TOKEN ) )
308 pname += m_nxtSym;
309 else if ( isInRange( m_nxtSym, OUString::createFromAscii( "= " ) ) )
310 break;
311 else
312 throw IllegalArgumentException( );
313 getSym( );
316 return pname;
319 //------------------------------------------------------------------------
321 //------------------------------------------------------------------------
323 OUString SAL_CALL CMimeContentType::pValue( )
325 OUString pvalue;
327 // quoted pvalue
328 if ( m_nxtSym == OUString::createFromAscii( "\"" ) )
330 getSym( );
331 pvalue = quotedPValue( );
333 if ( OUString( &pvalue[pvalue.getLength() - 1], 1 ) != OUString::createFromAscii( "\"" ) )
334 throw IllegalArgumentException( );
336 // remove the last quote-sign
337 OUString qpvalue( pvalue, pvalue.getLength( ) - 1 );
338 pvalue = qpvalue;
340 if ( !pvalue.getLength( ) )
341 throw IllegalArgumentException( );
343 else if ( isInRange( m_nxtSym, TOKEN ) ) // unquoted pvalue
345 pvalue = nonquotedPValue( );
347 else
348 throw IllegalArgumentException( );
350 return pvalue;
353 //------------------------------------------------------------------------
354 // the following combinations within a quoted value are not allowed:
355 // '";' (quote sign followed by semicolon) and '" ' (quote sign followed
356 // by space)
357 //------------------------------------------------------------------------
359 OUString SAL_CALL CMimeContentType::quotedPValue( )
361 OUString pvalue;
362 sal_Bool bAfterQuoteSign = sal_False;
364 while ( m_nxtSym.getLength( ) )
366 if ( bAfterQuoteSign && ((m_nxtSym == SPACE)||(m_nxtSym == SEMICOLON) ) )
367 break;
368 else if ( isInRange( m_nxtSym, TOKEN + TSPECIALS + SPACE ) )
370 pvalue += m_nxtSym;
371 if ( m_nxtSym == OUString::createFromAscii( "\"" ) )
372 bAfterQuoteSign = sal_True;
373 else
374 bAfterQuoteSign = sal_False;
376 else
377 throw IllegalArgumentException( );
378 getSym( );
381 return pvalue;
384 //------------------------------------------------------------------------
386 //------------------------------------------------------------------------
388 OUString SAL_CALL CMimeContentType::nonquotedPValue( )
390 OUString pvalue;
392 while ( m_nxtSym.getLength( ) )
394 if ( isInRange( m_nxtSym, TOKEN ) )
395 pvalue += m_nxtSym;
396 else if ( isInRange( m_nxtSym, OUString::createFromAscii( "; " ) ) )
397 break;
398 else
399 throw IllegalArgumentException( );
400 getSym( );
403 return pvalue;
406 //------------------------------------------------------------------------
408 //------------------------------------------------------------------------
410 void SAL_CALL CMimeContentType::comment( void )
412 while ( m_nxtSym.getLength( ) )
414 if ( isInRange( m_nxtSym, TOKEN + SPACE ) )
415 getSym( );
416 else if ( m_nxtSym == OUString::createFromAscii( ")" ) )
417 break;
418 else
419 throw IllegalArgumentException( );
423 //------------------------------------------------------------------------
425 //------------------------------------------------------------------------
427 sal_Bool SAL_CALL CMimeContentType::isInRange( const rtl::OUString& aChr, const rtl::OUString& aRange )
429 return ( aRange.indexOf( aChr ) > -1 );