Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "mcnttype.hxx"
21 :
22 : // namespace directives
23 :
24 : using namespace com::sun::star::uno;
25 : using namespace com::sun::star::lang;
26 : using namespace com::sun::star::container;
27 : using namespace std;
28 : using namespace osl;
29 :
30 : // constants
31 :
32 : const char TSPECIALS[] = "()<>@,;:\\\"/[]?=";
33 : const char TOKEN[] = "!#$%&'*+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~.";
34 : const char SPACE[] = " ";
35 : const char SEMICOLON[] = ";";
36 :
37 : // ctor
38 :
39 0 : CMimeContentType::CMimeContentType( const OUString& aCntType )
40 : {
41 0 : init( aCntType );
42 0 : }
43 :
44 0 : OUString SAL_CALL CMimeContentType::getMediaType( ) throw(RuntimeException, std::exception)
45 : {
46 0 : return m_MediaType;
47 : }
48 :
49 0 : OUString SAL_CALL CMimeContentType::getMediaSubtype( ) throw(RuntimeException, std::exception)
50 : {
51 0 : return m_MediaSubtype;
52 : }
53 :
54 0 : OUString SAL_CALL CMimeContentType::getFullMediaType( ) throw(RuntimeException, std::exception)
55 : {
56 0 : return m_MediaType + "/" + m_MediaSubtype;
57 : }
58 :
59 0 : Sequence< OUString > SAL_CALL CMimeContentType::getParameters( ) throw(RuntimeException, std::exception)
60 : {
61 0 : MutexGuard aGuard( m_aMutex );
62 :
63 0 : Sequence< OUString > seqParams;
64 :
65 0 : map< OUString, OUString >::iterator iter;
66 0 : map< OUString, OUString >::iterator iter_end = m_ParameterMap.end( );
67 :
68 0 : for ( iter = m_ParameterMap.begin( ); iter != iter_end; ++iter )
69 : {
70 0 : seqParams.realloc( seqParams.getLength( ) + 1 );
71 0 : seqParams[seqParams.getLength( ) - 1] = iter->first;
72 : }
73 :
74 0 : return seqParams;
75 : }
76 :
77 0 : sal_Bool SAL_CALL CMimeContentType::hasParameter( const OUString& aName ) throw(RuntimeException, std::exception)
78 : {
79 0 : MutexGuard aGuard( m_aMutex );
80 0 : return ( m_ParameterMap.end( ) != m_ParameterMap.find( aName ) );
81 : }
82 :
83 0 : OUString SAL_CALL CMimeContentType::getParameterValue( const OUString& aName ) throw(NoSuchElementException, RuntimeException, std::exception)
84 : {
85 0 : MutexGuard aGuard( m_aMutex );
86 :
87 0 : if ( !hasParameter( aName ) )
88 0 : throw NoSuchElementException( );
89 :
90 0 : return m_ParameterMap.find( aName )->second;
91 : }
92 :
93 0 : void SAL_CALL CMimeContentType::init( const OUString& aCntType ) throw( IllegalArgumentException )
94 : {
95 0 : if ( aCntType.isEmpty( ) )
96 0 : throw IllegalArgumentException( );
97 :
98 0 : m_nPos = 0;
99 0 : m_ContentType = aCntType;
100 0 : getSym( );
101 0 : type();
102 0 : }
103 :
104 0 : void SAL_CALL CMimeContentType::getSym( void )
105 : {
106 0 : if ( m_nPos < m_ContentType.getLength( ) )
107 : {
108 0 : m_nxtSym = m_ContentType.copy(m_nPos, 1);
109 0 : ++m_nPos;
110 0 : return;
111 : }
112 :
113 0 : m_nxtSym = OUString( );
114 : }
115 :
116 0 : void SAL_CALL CMimeContentType::acceptSym( const OUString& pSymTlb )
117 : {
118 0 : if ( pSymTlb.indexOf( m_nxtSym ) < 0 )
119 0 : throw IllegalArgumentException( );
120 :
121 0 : getSym();
122 0 : }
123 :
124 0 : void SAL_CALL CMimeContentType::skipSpaces( void )
125 : {
126 0 : while (m_nxtSym == SPACE)
127 0 : getSym( );
128 0 : }
129 :
130 0 : void SAL_CALL CMimeContentType::type( void )
131 : {
132 0 : skipSpaces( );
133 :
134 0 : OUString sToken(TOKEN);
135 :
136 : // check FIRST( type )
137 0 : if ( !isInRange( m_nxtSym, sToken ) )
138 0 : throw IllegalArgumentException( );
139 :
140 : // parse
141 0 : while( !m_nxtSym.isEmpty( ) )
142 : {
143 0 : if ( isInRange( m_nxtSym, sToken ) )
144 0 : m_MediaType += m_nxtSym;
145 0 : else if ( isInRange( m_nxtSym, OUString("/ ") ) )
146 0 : break;
147 : else
148 0 : throw IllegalArgumentException( );
149 0 : getSym( );
150 : }
151 :
152 : // check FOLLOW( type )
153 0 : skipSpaces( );
154 0 : acceptSym( OUString("/") );
155 :
156 0 : subtype( );
157 0 : }
158 :
159 0 : void SAL_CALL CMimeContentType::subtype( void )
160 : {
161 0 : skipSpaces( );
162 :
163 0 : OUString sToken(TOKEN);
164 :
165 : // check FIRST( subtype )
166 0 : if ( !isInRange( m_nxtSym, sToken ) )
167 0 : throw IllegalArgumentException( );
168 :
169 0 : while( !m_nxtSym.isEmpty( ) )
170 : {
171 0 : if ( isInRange( m_nxtSym, sToken ) )
172 0 : m_MediaSubtype += m_nxtSym;
173 0 : else if ( isInRange( m_nxtSym, OUString("; ") ) )
174 0 : break;
175 : else
176 0 : throw IllegalArgumentException( );
177 0 : getSym( );
178 : }
179 :
180 : // parse the rest
181 0 : skipSpaces( );
182 0 : trailer();
183 0 : }
184 :
185 0 : void SAL_CALL CMimeContentType::trailer( void )
186 : {
187 0 : OUString sToken(TOKEN);
188 0 : while( !m_nxtSym.isEmpty( ) )
189 : {
190 0 : if ( m_nxtSym == "(" )
191 : {
192 0 : getSym( );
193 0 : comment( );
194 0 : acceptSym( OUString(")") );
195 : }
196 0 : else if ( m_nxtSym == ";" )
197 : {
198 : // get the parameter name
199 0 : getSym( );
200 0 : skipSpaces( );
201 :
202 0 : if ( !isInRange( m_nxtSym, sToken ) )
203 0 : throw IllegalArgumentException( );
204 :
205 0 : OUString pname = pName( );
206 :
207 0 : skipSpaces();
208 0 : acceptSym( OUString("=") );
209 :
210 : // get the parameter value
211 0 : skipSpaces( );
212 :
213 0 : OUString pvalue = pValue( );
214 :
215 : // insert into map
216 0 : if ( !m_ParameterMap.insert( pair < const OUString, OUString > ( pname, pvalue ) ).second )
217 0 : throw IllegalArgumentException( );
218 : }
219 : else
220 0 : throw IllegalArgumentException( );
221 :
222 0 : skipSpaces( );
223 0 : }
224 0 : }
225 :
226 0 : OUString SAL_CALL CMimeContentType::pName( )
227 : {
228 0 : OUString pname;
229 :
230 0 : OUString sToken(TOKEN);
231 0 : while( !m_nxtSym.isEmpty( ) )
232 : {
233 0 : if ( isInRange( m_nxtSym, sToken ) )
234 0 : pname += m_nxtSym;
235 0 : else if ( isInRange( m_nxtSym, OUString("= ") ) )
236 0 : break;
237 : else
238 0 : throw IllegalArgumentException( );
239 0 : getSym( );
240 : }
241 :
242 0 : return pname;
243 : }
244 :
245 0 : OUString SAL_CALL CMimeContentType::pValue( )
246 : {
247 0 : OUString pvalue;
248 :
249 0 : OUString sToken(TOKEN);
250 : // quoted pvalue
251 0 : if ( m_nxtSym == "\"" )
252 : {
253 0 : getSym( );
254 0 : pvalue = quotedPValue( );
255 :
256 0 : if ( pvalue[pvalue.getLength() - 1] != '"' )
257 0 : throw IllegalArgumentException( );
258 :
259 : // remove the last quote-sign
260 0 : pvalue = pvalue.copy(0, pvalue.getLength() - 1);
261 :
262 0 : if ( pvalue.isEmpty( ) )
263 0 : throw IllegalArgumentException( );
264 : }
265 0 : else if ( isInRange( m_nxtSym, sToken ) ) // unquoted pvalue
266 : {
267 0 : pvalue = nonquotedPValue( );
268 : }
269 : else
270 0 : throw IllegalArgumentException( );
271 :
272 0 : return pvalue;
273 : }
274 :
275 : // the following combinations within a quoted value are not allowed:
276 : // '";' (quote sign followed by semicolon) and '" ' (quote sign followed
277 : // by space)
278 :
279 0 : OUString SAL_CALL CMimeContentType::quotedPValue( )
280 : {
281 0 : OUString pvalue;
282 0 : bool bAfterQuoteSign = false;
283 :
284 0 : while ( !m_nxtSym.isEmpty( ) )
285 : {
286 0 : if ( bAfterQuoteSign && (
287 0 : (m_nxtSym == SPACE) ||
288 0 : (m_nxtSym == SEMICOLON))
289 : )
290 : {
291 0 : break;
292 : }
293 0 : else if ( isInRange( m_nxtSym, OUString(TOKEN) + OUString(TSPECIALS) + OUString(SPACE) ) )
294 : {
295 0 : pvalue += m_nxtSym;
296 0 : if ( m_nxtSym == "\"" )
297 0 : bAfterQuoteSign = true;
298 : else
299 0 : bAfterQuoteSign = false;
300 : }
301 : else
302 0 : throw IllegalArgumentException( );
303 0 : getSym( );
304 : }
305 :
306 0 : return pvalue;
307 : }
308 :
309 0 : OUString SAL_CALL CMimeContentType::nonquotedPValue( )
310 : {
311 0 : OUString pvalue;
312 :
313 0 : OUString sToken(TOKEN);
314 0 : while ( !m_nxtSym.isEmpty( ) )
315 : {
316 0 : if ( isInRange( m_nxtSym, sToken ) )
317 0 : pvalue += m_nxtSym;
318 0 : else if ( isInRange( m_nxtSym, OUString("; ") ) )
319 0 : break;
320 : else
321 0 : throw IllegalArgumentException( );
322 0 : getSym( );
323 : }
324 :
325 0 : return pvalue;
326 : }
327 :
328 0 : void SAL_CALL CMimeContentType::comment( void )
329 : {
330 0 : while ( !m_nxtSym.isEmpty( ) )
331 : {
332 0 : if ( isInRange( m_nxtSym, OUString(TOKEN) + OUString(SPACE) ) )
333 0 : getSym( );
334 0 : else if ( m_nxtSym == ")" )
335 0 : break;
336 : else
337 0 : throw IllegalArgumentException( );
338 : }
339 0 : }
340 :
341 0 : bool SAL_CALL CMimeContentType::isInRange( const OUString& aChr, const OUString& aRange )
342 : {
343 0 : return ( aRange.indexOf( aChr ) > -1 );
344 : }
345 :
346 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|