1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
33 //------------------------------------------------------------------------
35 //------------------------------------------------------------------------
37 const char TSPECIALS
[] = "()<>@,;:\\\"/[]?=";
38 const char TOKEN
[] = "!#$%&'*+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~.";
39 const char SPACE
[] = " ";
40 const char SEMICOLON
[] = ";";
42 //------------------------------------------------------------------------
44 //------------------------------------------------------------------------
46 CMimeContentType::CMimeContentType( const OUString
& aCntType
)
51 //------------------------------------------------------------------------
53 //------------------------------------------------------------------------
55 OUString SAL_CALL
CMimeContentType::getMediaType( ) throw(RuntimeException
)
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
;
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( );
134 m_ContentType
= aCntType
;
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);
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( );
167 //------------------------------------------------------------------------
169 //------------------------------------------------------------------------
171 void SAL_CALL
CMimeContentType::skipSpaces( void )
173 while (m_nxtSym
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(SPACE
)))
177 //------------------------------------------------------------------------
179 //------------------------------------------------------------------------
181 void SAL_CALL
CMimeContentType::type( void )
185 OUString
sToken(TOKEN
);
187 // check FIRST( type )
188 if ( !isInRange( m_nxtSym
, sToken
) )
189 throw IllegalArgumentException( );
192 while( !m_nxtSym
.isEmpty( ) )
194 if ( isInRange( m_nxtSym
, sToken
) )
195 m_MediaType
+= m_nxtSym
;
196 else if ( isInRange( m_nxtSym
, OUString("/ ") ) )
199 throw IllegalArgumentException( );
203 // check FOLLOW( type )
205 acceptSym( OUString("/") );
210 //------------------------------------------------------------------------
212 //------------------------------------------------------------------------
214 void SAL_CALL
CMimeContentType::subtype( void )
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("; ") ) )
231 throw IllegalArgumentException( );
240 //------------------------------------------------------------------------
242 //------------------------------------------------------------------------
244 void SAL_CALL
CMimeContentType::trailer( void )
246 OUString
sToken(TOKEN
);
247 while( !m_nxtSym
.isEmpty( ) )
249 if ( m_nxtSym
== OUString("(") )
253 acceptSym( OUString(")") );
255 else if ( m_nxtSym
== OUString(";") )
257 // get the parameter name
261 if ( !isInRange( m_nxtSym
, sToken
) )
262 throw IllegalArgumentException( );
264 OUString pname
= pName( );
267 acceptSym( OUString("=") );
269 // get the parameter value
272 OUString pvalue
= pValue( );
275 if ( !m_ParameterMap
.insert( pair
< const OUString
, OUString
> ( pname
, pvalue
) ).second
)
276 throw IllegalArgumentException( );
279 throw IllegalArgumentException( );
285 //------------------------------------------------------------------------
287 //------------------------------------------------------------------------
289 OUString SAL_CALL
CMimeContentType::pName( )
293 OUString
sToken(TOKEN
);
294 while( !m_nxtSym
.isEmpty( ) )
296 if ( isInRange( m_nxtSym
, sToken
) )
298 else if ( isInRange( m_nxtSym
, OUString("= ") ) )
301 throw IllegalArgumentException( );
308 //------------------------------------------------------------------------
310 //------------------------------------------------------------------------
312 OUString SAL_CALL
CMimeContentType::pValue( )
316 OUString
sToken(TOKEN
);
318 if ( m_nxtSym
== OUString( "\"" ) )
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( );
337 throw IllegalArgumentException( );
342 //------------------------------------------------------------------------
343 // the following combinations within a quoted value are not allowed:
344 // '";' (quote sign followed by semicolon) and '" ' (quote sign followed
346 //------------------------------------------------------------------------
348 OUString SAL_CALL
CMimeContentType::quotedPValue( )
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
))))
362 else if ( isInRange( m_nxtSym
, OUString(TOKEN
) + OUString(TSPECIALS
) + OUString(SPACE
) ) )
365 if ( m_nxtSym
== OUString( "\"" ) )
366 bAfterQuoteSign
= sal_True
;
368 bAfterQuoteSign
= sal_False
;
371 throw IllegalArgumentException( );
378 //------------------------------------------------------------------------
380 //------------------------------------------------------------------------
382 OUString SAL_CALL
CMimeContentType::nonquotedPValue( )
386 OUString
sToken(TOKEN
);
387 while ( !m_nxtSym
.isEmpty( ) )
389 if ( isInRange( m_nxtSym
, sToken
) )
391 else if ( isInRange( m_nxtSym
, OUString("; ") ) )
394 throw IllegalArgumentException( );
401 //------------------------------------------------------------------------
403 //------------------------------------------------------------------------
405 void SAL_CALL
CMimeContentType::comment( void )
407 while ( !m_nxtSym
.isEmpty( ) )
409 if ( isInRange( m_nxtSym
, OUString(TOKEN
) + OUString(SPACE
) ) )
411 else if ( m_nxtSym
== OUString(")") )
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: */