fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / ucb / source / ucp / webdav-neon / LockSequence.cxx
blobd5ce74e160460f0bbb0b755371648ceb252cbc26
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <config_lgpl.h>
30 #include <string.h>
31 #include <ne_xml.h>
32 #include <osl/diagnose.h>
33 #include "LockSequence.hxx"
35 using namespace webdav_ucp;
36 using namespace com::sun::star;
38 #define BEEHIVE_BUGS_WORKAROUND
42 struct LockSequenceParseContext
44 ucb::Lock * pLock;
45 bool hasLockScope;
46 bool hasLockType;
47 bool hasDepth;
48 bool hasHREF;
49 bool hasTimeout;
51 LockSequenceParseContext()
52 : pLock( 0 ), hasLockScope( false ), hasLockType( false ),
53 hasDepth( false ), hasHREF( false ), hasTimeout( false ) {}
55 ~LockSequenceParseContext() { delete pLock; }
58 #define STATE_TOP (1)
60 #define STATE_ACTIVELOCK (STATE_TOP)
61 #define STATE_LOCKSCOPE (STATE_TOP + 1)
62 #define STATE_LOCKTYPE (STATE_TOP + 2)
63 #define STATE_DEPTH (STATE_TOP + 3)
64 #define STATE_OWNER (STATE_TOP + 4)
65 #define STATE_TIMEOUT (STATE_TOP + 5)
66 #define STATE_LOCKTOKEN (STATE_TOP + 6)
67 #define STATE_EXCLUSIVE (STATE_TOP + 7)
68 #define STATE_SHARED (STATE_TOP + 8)
69 #define STATE_WRITE (STATE_TOP + 9)
70 #define STATE_HREF (STATE_TOP + 10)
73 extern "C" int LockSequence_startelement_callback(
74 void *,
75 int parent,
76 const char * /*nspace*/,
77 const char *name,
78 const char ** )
80 if ( name != 0 )
82 switch ( parent )
84 case NE_XML_STATEROOT:
85 if ( strcmp( name, "activelock" ) == 0 )
86 return STATE_ACTIVELOCK;
87 break;
89 case STATE_ACTIVELOCK:
90 if ( strcmp( name, "lockscope" ) == 0 )
91 return STATE_LOCKSCOPE;
92 else if ( strcmp( name, "locktype" ) == 0 )
93 return STATE_LOCKTYPE;
94 else if ( strcmp( name, "depth" ) == 0 )
95 return STATE_DEPTH;
96 else if ( strcmp( name, "owner" ) == 0 )
97 return STATE_OWNER;
98 else if ( strcmp( name, "timeout" ) == 0 )
99 return STATE_TIMEOUT;
100 else if ( strcmp( name, "locktoken" ) == 0 )
101 return STATE_LOCKTOKEN;
102 break;
104 case STATE_LOCKSCOPE:
105 if ( strcmp( name, "exclusive" ) == 0 )
106 return STATE_EXCLUSIVE;
107 else if ( strcmp( name, "shared" ) == 0 )
108 return STATE_SHARED;
109 break;
111 case STATE_LOCKTYPE:
112 if ( strcmp( name, "write" ) == 0 )
113 return STATE_WRITE;
114 break;
116 case STATE_LOCKTOKEN:
117 if ( strcmp( name, "href" ) == 0 )
118 return STATE_HREF;
119 break;
121 case STATE_OWNER:
122 // owner elem contains ANY. Accept anything; no state change.
123 return STATE_OWNER;
126 return NE_XML_DECLINE;
130 extern "C" int LockSequence_chardata_callback(
131 void *userdata,
132 int state,
133 #ifdef BEEHIVE_BUGS_WORKAROUND
134 const char *buf1,
135 #else
136 const char *buf,
137 #endif
138 size_t len )
140 LockSequenceParseContext * pCtx
141 = static_cast< LockSequenceParseContext * >( userdata );
142 if ( !pCtx->pLock )
143 pCtx->pLock = new ucb::Lock;
145 #ifdef BEEHIVE_BUGS_WORKAROUND
146 // Beehive sends XML values containing trailing newlines.
147 if ( buf1[ len - 1 ] == 0x0a )
148 len--;
150 char * buf = new char[ len + 1 ]();
151 strncpy( buf, buf1, len );
152 #endif
154 switch ( state )
156 case STATE_DEPTH:
157 if ( rtl_str_compareIgnoreAsciiCase_WithLength(
158 buf, len, "0", 1 ) == 0 )
160 pCtx->pLock->Depth = ucb::LockDepth_ZERO;
161 pCtx->hasDepth = true;
163 else if ( rtl_str_compareIgnoreAsciiCase_WithLength(
164 buf, len, "1", 1 ) == 0 )
166 pCtx->pLock->Depth = ucb::LockDepth_ONE;
167 pCtx->hasDepth = true;
169 else if ( rtl_str_compareIgnoreAsciiCase_WithLength(
170 buf, len, "infinity", 8 ) == 0 )
172 pCtx->pLock->Depth = ucb::LockDepth_INFINITY;
173 pCtx->hasDepth = true;
175 else
176 OSL_FAIL( "LockSequence_chardata_callback - Unknown depth!" );
177 break;
179 case STATE_OWNER:
181 // collect raw XML data... (owner contains ANY)
182 OUString aValue;
183 pCtx->pLock->Owner >>= aValue;
184 aValue += OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
185 pCtx->pLock->Owner <<= aValue;
186 break;
189 case STATE_TIMEOUT:
191 // RFC2518, RFC2616:
193 // TimeType = ("Second-" DAVTimeOutVal | "Infinite" | Other)
194 // DAVTimeOutVal = 1*digit
195 // Other = "Extend" field-value
196 // field-value = *( field-content | LWS )
197 // field-content = <the OCTETs making up the field-value
198 // and consisting of either *TEXT or combinations
199 // of token, separators, and quoted-string>
201 if ( rtl_str_compareIgnoreAsciiCase_WithLength(
202 buf, len, "Infinite", 8 ) == 0 )
204 pCtx->pLock->Timeout = sal_Int64( -1 );
205 pCtx->hasTimeout = true;
207 else if ( rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
208 buf, len, "Second-", 7, 7 ) == 0 )
210 pCtx->pLock->Timeout
211 = OString( buf + 7, len - 7 ).toInt64();
212 pCtx->hasTimeout = true;
214 // else if ( rtl_str_shortenedCompareIgnoreCase_WithLength(
215 // buf, len, "Extend", 6, 6 ) == 0 )
216 // {
217 // @@@
218 // }
219 else
221 pCtx->pLock->Timeout = sal_Int64( -1 );
222 pCtx->hasTimeout = true;
223 OSL_FAIL( "LockSequence_chardata_callback - Unknown timeout!" );
225 break;
227 case STATE_HREF:
229 // collect hrefs.
230 sal_Int32 nPos = pCtx->pLock->LockTokens.getLength();
231 pCtx->pLock->LockTokens.realloc( nPos + 1 );
232 pCtx->pLock->LockTokens[ nPos ]
233 = OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
234 pCtx->hasHREF = true;
235 break;
240 #ifdef BEEHIVE_BUGS_WORKAROUND
241 delete [] buf;
242 #endif
244 return 0; // zero to continue, non-zero to abort parsing
248 extern "C" int LockSequence_endelement_callback(
249 void *userdata,
250 int state,
251 const char *,
252 const char * )
254 LockSequenceParseContext * pCtx
255 = static_cast< LockSequenceParseContext * >( userdata );
256 if ( !pCtx->pLock )
257 pCtx->pLock = new ucb::Lock;
259 switch ( state )
261 case STATE_EXCLUSIVE:
262 pCtx->pLock->Scope = ucb::LockScope_EXCLUSIVE;
263 pCtx->hasLockScope = true;
264 break;
266 case STATE_SHARED:
267 pCtx->pLock->Scope = ucb::LockScope_SHARED;
268 pCtx->hasLockScope = true;
269 break;
271 case STATE_WRITE:
272 pCtx->pLock->Type = ucb::LockType_WRITE;
273 pCtx->hasLockType = true;
274 break;
276 case STATE_DEPTH:
277 if ( !pCtx->hasDepth )
278 return 1; // abort
279 break;
281 case STATE_HREF:
282 if ( !pCtx->hasHREF )
283 return 1; // abort
284 break;
286 case STATE_TIMEOUT:
287 if ( !pCtx->hasTimeout )
288 return 1; // abort
289 break;
291 case STATE_LOCKSCOPE:
292 if ( !pCtx->hasLockScope )
293 return 1; // abort
294 break;
296 case STATE_LOCKTYPE:
297 if ( !pCtx->hasLockType )
298 return 1; // abort
299 break;
301 case STATE_ACTIVELOCK:
302 if ( !pCtx->hasLockType || !pCtx->hasDepth )
303 return 1; // abort
304 break;
306 default:
307 break;
309 return 0; // zero to continue, non-zero to abort parsing
313 // static
314 bool LockSequence::createFromXML( const OString & rInData,
315 uno::Sequence< ucb::Lock > & rOutData )
317 const sal_Int32 TOKEN_LENGTH = 13; // </activelock>
318 bool success = true;
320 // rInData may contain multiple <activelock>...</activelock> tags.
321 sal_Int32 nCount = 0;
322 sal_Int32 nStart = 0;
323 sal_Int32 nEnd = rInData.indexOf( "</activelock>" );
324 while ( nEnd > -1 )
326 ne_xml_parser * parser = ne_xml_create();
327 if ( !parser )
329 success = false;
330 break;
333 LockSequenceParseContext aCtx;
334 ne_xml_push_handler( parser,
335 LockSequence_startelement_callback,
336 LockSequence_chardata_callback,
337 LockSequence_endelement_callback,
338 &aCtx );
340 ne_xml_parse( parser,
341 rInData.getStr() + nStart,
342 nEnd - nStart + TOKEN_LENGTH );
344 success = !ne_xml_failed( parser );
346 ne_xml_destroy( parser );
348 if ( !success )
349 break;
351 if ( aCtx.pLock )
353 nCount++;
354 if ( nCount > rOutData.getLength() )
355 rOutData.realloc( rOutData.getLength() + 1 );
357 rOutData[ nCount - 1 ] = *aCtx.pLock;
360 nStart = nEnd + TOKEN_LENGTH;
361 nEnd = rInData.indexOf( "</activelock>", nStart );
364 return success;
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */