1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mcnttype.cxx,v $
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 //------------------------------------------------------------------------
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
;
50 //------------------------------------------------------------------------
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 //------------------------------------------------------------------------
61 //------------------------------------------------------------------------
63 CMimeContentType::CMimeContentType( const OUString
& aCntType
)
68 //------------------------------------------------------------------------
70 //------------------------------------------------------------------------
72 OUString SAL_CALL
CMimeContentType::getMediaType( ) throw(RuntimeException
)
77 //------------------------------------------------------------------------
79 //------------------------------------------------------------------------
81 OUString SAL_CALL
CMimeContentType::getMediaSubtype( ) throw(RuntimeException
)
83 return m_MediaSubtype
;
86 //------------------------------------------------------------------------
88 //------------------------------------------------------------------------
90 OUString SAL_CALL
CMimeContentType::getFullMediaType( ) throw(RuntimeException
)
92 return m_MediaType
+ OUString::createFromAscii( "/" ) + m_MediaSubtype
;
95 //------------------------------------------------------------------------
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
;
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( );
151 m_ContentType
= aCntType
;
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 );
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( );
184 //------------------------------------------------------------------------
186 //------------------------------------------------------------------------
188 void SAL_CALL
CMimeContentType::skipSpaces( void )
190 while ( SPACE
== m_nxtSym
)
194 //------------------------------------------------------------------------
196 //------------------------------------------------------------------------
198 void SAL_CALL
CMimeContentType::type( void )
202 // check FIRST( type )
203 if ( !isInRange( m_nxtSym
, TOKEN
) )
204 throw IllegalArgumentException( );
207 while( m_nxtSym
.getLength( ) )
209 if ( isInRange( m_nxtSym
, TOKEN
) )
210 m_MediaType
+= m_nxtSym
;
211 else if ( isInRange( m_nxtSym
, OUString::createFromAscii( "/ " ) ) )
214 throw IllegalArgumentException( );
218 // check FOLLOW( type )
220 acceptSym( OUString::createFromAscii( "/" ) );
225 //------------------------------------------------------------------------
227 //------------------------------------------------------------------------
229 void SAL_CALL
CMimeContentType::subtype( void )
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( "; " ) ) )
244 throw IllegalArgumentException( );
253 //------------------------------------------------------------------------
255 //------------------------------------------------------------------------
257 void SAL_CALL
CMimeContentType::trailer( void )
259 while( m_nxtSym
.getLength( ) )
261 if ( m_nxtSym
== OUString::createFromAscii( "(" ) )
265 acceptSym( OUString::createFromAscii( ")" ) );
267 else if ( m_nxtSym
== OUString::createFromAscii( ";" ) )
269 // get the parameter name
273 if ( !isInRange( m_nxtSym
, TOKEN
) )
274 throw IllegalArgumentException( );
276 OUString pname
= pName( );
279 acceptSym( OUString::createFromAscii( "=" ) );
281 // get the parameter value
284 OUString pvalue
= pValue( );
287 if ( !m_ParameterMap
.insert( pair
< const OUString
, OUString
> ( pname
, pvalue
) ).second
)
288 throw IllegalArgumentException( );
291 throw IllegalArgumentException( );
297 //------------------------------------------------------------------------
299 //------------------------------------------------------------------------
301 OUString SAL_CALL
CMimeContentType::pName( )
305 while( m_nxtSym
.getLength( ) )
307 if ( isInRange( m_nxtSym
, TOKEN
) )
309 else if ( isInRange( m_nxtSym
, OUString::createFromAscii( "= " ) ) )
312 throw IllegalArgumentException( );
319 //------------------------------------------------------------------------
321 //------------------------------------------------------------------------
323 OUString SAL_CALL
CMimeContentType::pValue( )
328 if ( m_nxtSym
== OUString::createFromAscii( "\"" ) )
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 );
340 if ( !pvalue
.getLength( ) )
341 throw IllegalArgumentException( );
343 else if ( isInRange( m_nxtSym
, TOKEN
) ) // unquoted pvalue
345 pvalue
= nonquotedPValue( );
348 throw IllegalArgumentException( );
353 //------------------------------------------------------------------------
354 // the following combinations within a quoted value are not allowed:
355 // '";' (quote sign followed by semicolon) and '" ' (quote sign followed
357 //------------------------------------------------------------------------
359 OUString SAL_CALL
CMimeContentType::quotedPValue( )
362 sal_Bool bAfterQuoteSign
= sal_False
;
364 while ( m_nxtSym
.getLength( ) )
366 if ( bAfterQuoteSign
&& ((m_nxtSym
== SPACE
)||(m_nxtSym
== SEMICOLON
) ) )
368 else if ( isInRange( m_nxtSym
, TOKEN
+ TSPECIALS
+ SPACE
) )
371 if ( m_nxtSym
== OUString::createFromAscii( "\"" ) )
372 bAfterQuoteSign
= sal_True
;
374 bAfterQuoteSign
= sal_False
;
377 throw IllegalArgumentException( );
384 //------------------------------------------------------------------------
386 //------------------------------------------------------------------------
388 OUString SAL_CALL
CMimeContentType::nonquotedPValue( )
392 while ( m_nxtSym
.getLength( ) )
394 if ( isInRange( m_nxtSym
, TOKEN
) )
396 else if ( isInRange( m_nxtSym
, OUString::createFromAscii( "; " ) ) )
399 throw IllegalArgumentException( );
406 //------------------------------------------------------------------------
408 //------------------------------------------------------------------------
410 void SAL_CALL
CMimeContentType::comment( void )
412 while ( m_nxtSym
.getLength( ) )
414 if ( isInRange( m_nxtSym
, TOKEN
+ SPACE
) )
416 else if ( m_nxtSym
== OUString::createFromAscii( ")" ) )
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 );