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 // namespace directives
24 using namespace com::sun::star::uno
;
25 using namespace com::sun::star::lang
;
26 using namespace com::sun::star::container
;
32 const char TSPECIALS
[] = "()<>@,;:\\\"/[]?=";
33 const char TOKEN
[] = "!#$%&'*+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~.";
34 const char SPACE
[] = " ";
35 const char SEMICOLON
[] = ";";
39 CMimeContentType::CMimeContentType( const OUString
& aCntType
)
44 OUString SAL_CALL
CMimeContentType::getMediaType( ) throw(RuntimeException
, std::exception
)
49 OUString SAL_CALL
CMimeContentType::getMediaSubtype( ) throw(RuntimeException
, std::exception
)
51 return m_MediaSubtype
;
54 OUString SAL_CALL
CMimeContentType::getFullMediaType( ) throw(RuntimeException
, std::exception
)
56 return m_MediaType
+ "/" + m_MediaSubtype
;
59 Sequence
< OUString
> SAL_CALL
CMimeContentType::getParameters( ) throw(RuntimeException
, std::exception
)
61 MutexGuard
aGuard( m_aMutex
);
63 Sequence
< OUString
> seqParams
;
65 map
< OUString
, OUString
>::iterator iter
;
66 map
< OUString
, OUString
>::iterator iter_end
= m_ParameterMap
.end( );
68 for ( iter
= m_ParameterMap
.begin( ); iter
!= iter_end
; ++iter
)
70 seqParams
.realloc( seqParams
.getLength( ) + 1 );
71 seqParams
[seqParams
.getLength( ) - 1] = iter
->first
;
77 sal_Bool SAL_CALL
CMimeContentType::hasParameter( const OUString
& aName
) throw(RuntimeException
, std::exception
)
79 MutexGuard
aGuard( m_aMutex
);
80 return ( m_ParameterMap
.end( ) != m_ParameterMap
.find( aName
) );
83 OUString SAL_CALL
CMimeContentType::getParameterValue( const OUString
& aName
) throw(NoSuchElementException
, RuntimeException
, std::exception
)
85 MutexGuard
aGuard( m_aMutex
);
87 if ( !hasParameter( aName
) )
88 throw NoSuchElementException( );
90 return m_ParameterMap
.find( aName
)->second
;
93 void SAL_CALL
CMimeContentType::init( const OUString
& aCntType
) throw( IllegalArgumentException
)
95 if ( aCntType
.isEmpty( ) )
96 throw IllegalArgumentException( );
99 m_ContentType
= aCntType
;
104 void SAL_CALL
CMimeContentType::getSym()
106 if ( m_nPos
< m_ContentType
.getLength( ) )
108 m_nxtSym
= m_ContentType
.copy(m_nPos
, 1);
113 m_nxtSym
= OUString( );
116 void SAL_CALL
CMimeContentType::acceptSym( const OUString
& pSymTlb
)
118 if ( pSymTlb
.indexOf( m_nxtSym
) < 0 )
119 throw IllegalArgumentException( );
124 void SAL_CALL
CMimeContentType::skipSpaces()
126 while (m_nxtSym
== SPACE
)
130 void SAL_CALL
CMimeContentType::type()
134 OUString
sToken(TOKEN
);
136 // check FIRST( type )
137 if ( !isInRange( m_nxtSym
, sToken
) )
138 throw IllegalArgumentException( );
141 while( !m_nxtSym
.isEmpty( ) )
143 if ( isInRange( m_nxtSym
, sToken
) )
144 m_MediaType
+= m_nxtSym
;
145 else if ( isInRange( m_nxtSym
, OUString("/ ") ) )
148 throw IllegalArgumentException( );
152 // check FOLLOW( type )
154 acceptSym( OUString("/") );
159 void SAL_CALL
CMimeContentType::subtype()
163 OUString
sToken(TOKEN
);
165 // check FIRST( subtype )
166 if ( !isInRange( m_nxtSym
, sToken
) )
167 throw IllegalArgumentException( );
169 while( !m_nxtSym
.isEmpty( ) )
171 if ( isInRange( m_nxtSym
, sToken
) )
172 m_MediaSubtype
+= m_nxtSym
;
173 else if ( isInRange( m_nxtSym
, OUString("; ") ) )
176 throw IllegalArgumentException( );
185 void SAL_CALL
CMimeContentType::trailer()
187 OUString
sToken(TOKEN
);
188 while( !m_nxtSym
.isEmpty( ) )
190 if ( m_nxtSym
== "(" )
194 acceptSym( OUString(")") );
196 else if ( m_nxtSym
== ";" )
198 // get the parameter name
202 if ( !isInRange( m_nxtSym
, sToken
) )
203 throw IllegalArgumentException( );
205 OUString pname
= pName( );
208 acceptSym( OUString("=") );
210 // get the parameter value
213 OUString pvalue
= pValue( );
216 if ( !m_ParameterMap
.insert( pair
< const OUString
, OUString
> ( pname
, pvalue
) ).second
)
217 throw IllegalArgumentException( );
220 throw IllegalArgumentException( );
226 OUString SAL_CALL
CMimeContentType::pName( )
230 OUString
sToken(TOKEN
);
231 while( !m_nxtSym
.isEmpty( ) )
233 if ( isInRange( m_nxtSym
, sToken
) )
235 else if ( isInRange( m_nxtSym
, OUString("= ") ) )
238 throw IllegalArgumentException( );
245 OUString SAL_CALL
CMimeContentType::pValue( )
249 OUString
sToken(TOKEN
);
251 if ( m_nxtSym
== "\"" )
254 pvalue
= quotedPValue( );
256 if ( pvalue
[pvalue
.getLength() - 1] != '"' )
257 throw IllegalArgumentException( );
259 // remove the last quote-sign
260 pvalue
= pvalue
.copy(0, pvalue
.getLength() - 1);
262 if ( pvalue
.isEmpty( ) )
263 throw IllegalArgumentException( );
265 else if ( isInRange( m_nxtSym
, sToken
) ) // unquoted pvalue
267 pvalue
= nonquotedPValue( );
270 throw IllegalArgumentException( );
275 // the following combinations within a quoted value are not allowed:
276 // '";' (quote sign followed by semicolon) and '" ' (quote sign followed
279 OUString SAL_CALL
CMimeContentType::quotedPValue( )
282 bool bAfterQuoteSign
= false;
284 while ( !m_nxtSym
.isEmpty( ) )
286 if ( bAfterQuoteSign
&& (
287 (m_nxtSym
== SPACE
) ||
288 (m_nxtSym
== SEMICOLON
))
293 else if ( isInRange( m_nxtSym
, OUStringLiteral(TOKEN
) + TSPECIALS
+ SPACE
) )
296 if ( m_nxtSym
== "\"" )
297 bAfterQuoteSign
= true;
299 bAfterQuoteSign
= false;
302 throw IllegalArgumentException( );
309 OUString SAL_CALL
CMimeContentType::nonquotedPValue( )
313 OUString
sToken(TOKEN
);
314 while ( !m_nxtSym
.isEmpty( ) )
316 if ( isInRange( m_nxtSym
, sToken
) )
318 else if ( isInRange( m_nxtSym
, OUString("; ") ) )
321 throw IllegalArgumentException( );
328 void SAL_CALL
CMimeContentType::comment()
330 while ( !m_nxtSym
.isEmpty( ) )
332 if ( isInRange( m_nxtSym
, OUStringLiteral(TOKEN
) + SPACE
) )
334 else if ( m_nxtSym
== ")" )
337 throw IllegalArgumentException( );
341 bool SAL_CALL
CMimeContentType::isInRange( const OUString
& aChr
, const OUString
& aRange
)
343 return ( aRange
.indexOf( aChr
) > -1 );
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */