update dev300-m58
[ooovba.git] / ucb / source / ucp / webdav / LockSequence.cxx
blobb959ddc109da7d891f4318bc692c6432e5f1dba2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: LockSequence.cxx,v $
10 * $Revision: 1.17 $
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_ucb.hxx"
34 #include <string.h>
35 #include <ne_xml.h>
36 #include "LockSequence.hxx"
38 using namespace webdav_ucp;
39 using namespace com::sun::star;
41 //////////////////////////////////////////////////////////////////////////
43 struct LockSequenceParseContext
45 ucb::Lock * pLock;
46 bool hasLockScope;
47 bool hasLockType;
48 bool hasDepth;
49 bool hasHREF;
50 bool hasTimeout;
52 LockSequenceParseContext()
53 : pLock( 0 ), hasLockScope( false ), hasLockType( false ),
54 hasDepth( false ), hasHREF( false ), hasTimeout( false ) {}
56 ~LockSequenceParseContext() { delete pLock; }
59 #define STATE_TOP (1)
61 #define STATE_ACTIVELOCK (STATE_TOP)
62 #define STATE_LOCKSCOPE (STATE_TOP + 1)
63 #define STATE_LOCKTYPE (STATE_TOP + 2)
64 #define STATE_DEPTH (STATE_TOP + 3)
65 #define STATE_OWNER (STATE_TOP + 4)
66 #define STATE_TIMEOUT (STATE_TOP + 5)
67 #define STATE_LOCKTOKEN (STATE_TOP + 6)
68 #define STATE_EXCLUSIVE (STATE_TOP + 7)
69 #define STATE_SHARED (STATE_TOP + 8)
70 #define STATE_WRITE (STATE_TOP + 9)
71 #define STATE_HREF (STATE_TOP + 10)
73 //////////////////////////////////////////////////////////////////////////
74 extern "C" int LockSequence_startelement_callback(
75 void *,
76 int parent,
77 const char *nspace,
78 const char *name,
79 const char ** )
81 if ( ( name != 0 ) &&
82 ( ( nspace == 0 ) || ( strcmp( nspace, "" ) == 0 ) ) )
84 switch ( parent )
86 case NE_XML_STATEROOT:
87 if ( strcmp( name, "activelock" ) == 0 )
88 return STATE_ACTIVELOCK;
89 break;
91 case STATE_ACTIVELOCK:
92 if ( strcmp( name, "lockscope" ) == 0 )
93 return STATE_LOCKSCOPE;
94 else if ( strcmp( name, "locktype" ) == 0 )
95 return STATE_LOCKTYPE;
96 else if ( strcmp( name, "depth" ) == 0 )
97 return STATE_DEPTH;
98 else if ( strcmp( name, "owner" ) == 0 )
99 return STATE_OWNER;
100 else if ( strcmp( name, "timeout" ) == 0 )
101 return STATE_TIMEOUT;
102 else if ( strcmp( name, "locktoken" ) == 0 )
103 return STATE_LOCKTOKEN;
104 break;
106 case STATE_LOCKSCOPE:
107 if ( strcmp( name, "exclusive" ) == 0 )
108 return STATE_EXCLUSIVE;
109 else if ( strcmp( name, "shared" ) == 0 )
110 return STATE_SHARED;
111 break;
113 case STATE_LOCKTYPE:
114 if ( strcmp( name, "write" ) == 0 )
115 return STATE_WRITE;
116 break;
118 case STATE_LOCKTOKEN:
119 if ( strcmp( name, "href" ) == 0 )
120 return STATE_HREF;
121 break;
123 case STATE_OWNER:
124 // owner elem contains ANY. Accept anything; no state change.
125 return STATE_OWNER;
128 return NE_XML_DECLINE;
131 //////////////////////////////////////////////////////////////////////////
132 extern "C" int LockSequence_chardata_callback(
133 void *userdata,
134 int state,
135 const char *buf,
136 size_t len )
138 LockSequenceParseContext * pCtx
139 = static_cast< LockSequenceParseContext * >( userdata );
140 if ( !pCtx->pLock )
141 pCtx->pLock = new ucb::Lock;
143 switch ( state )
145 case STATE_DEPTH:
146 if ( rtl_str_compareIgnoreAsciiCase_WithLength(
147 buf, len, "0", 1 ) == 0 )
149 pCtx->pLock->Depth = ucb::LockDepth_ZERO;
150 pCtx->hasDepth = true;
152 else if ( rtl_str_compareIgnoreAsciiCase_WithLength(
153 buf, len, "1", 1 ) == 0 )
155 pCtx->pLock->Depth = ucb::LockDepth_ONE;
156 pCtx->hasDepth = true;
158 else if ( rtl_str_compareIgnoreAsciiCase_WithLength(
159 buf, len, "infinity", 8 ) == 0 )
161 pCtx->pLock->Depth = ucb::LockDepth_INFINITY;
162 pCtx->hasDepth = true;
164 else
165 OSL_ENSURE( sal_False,
166 "LockSequence_chardata_callback - Unknown depth!" );
167 break;
169 case STATE_OWNER:
171 // collect raw XML data... (owner contains ANY)
172 rtl::OUString aValue;
173 pCtx->pLock->Owner >>= aValue;
174 aValue += rtl::OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
175 pCtx->pLock->Owner <<= aValue;
176 break;
179 case STATE_TIMEOUT:
181 // RFC2518, RFC2616:
183 // TimeType = ("Second-" DAVTimeOutVal | "Infinite" | Other)
184 // DAVTimeOutVal = 1*digit
185 // Other = "Extend" field-value
186 // field-value = *( field-content | LWS )
187 // field-content = <the OCTETs making up the field-value
188 // and consisting of either *TEXT or combinations
189 // of token, separators, and quoted-string>
191 if ( rtl_str_compareIgnoreAsciiCase_WithLength(
192 buf, len, "Infinite", 8 ) == 0 )
194 pCtx->pLock->Timeout = sal_Int64( -1 );
195 pCtx->hasTimeout = true;
197 else if ( rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
198 buf, len, "Second-", 7, 7 ) == 0 )
200 pCtx->pLock->Timeout
201 = rtl::OString( buf + 7, len - 7 ).toInt64();
202 pCtx->hasTimeout = true;
204 // else if ( rtl_str_shortenedCompareIgnoreCase_WithLength(
205 // buf, len, "Extend", 6, 6 ) == 0 )
206 // {
207 // @@@
208 // }
209 else
211 pCtx->pLock->Timeout = sal_Int64( -1 );
212 pCtx->hasTimeout = true;
213 OSL_ENSURE( sal_False,
214 "LockSequence_chardata_callback - Unknown timeout!" );
216 break;
218 case STATE_HREF:
220 // collect hrefs.
221 sal_Int32 nPos = pCtx->pLock->LockTokens.getLength();
222 pCtx->pLock->LockTokens.realloc( nPos + 1 );
223 pCtx->pLock->LockTokens[ nPos ]
224 = rtl::OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
225 pCtx->hasHREF = true;
226 break;
230 return 0; // zero to continue, non-zero to abort parsing
233 //////////////////////////////////////////////////////////////////////////
234 extern "C" int LockSequence_endelement_callback(
235 void *userdata,
236 int state,
237 const char *,
238 const char * )
240 LockSequenceParseContext * pCtx
241 = static_cast< LockSequenceParseContext * >( userdata );
242 if ( !pCtx->pLock )
243 pCtx->pLock = new ucb::Lock;
245 switch ( state )
247 case STATE_EXCLUSIVE:
248 pCtx->pLock->Scope = ucb::LockScope_EXCLUSIVE;
249 pCtx->hasLockScope = true;
250 break;
252 case STATE_SHARED:
253 pCtx->pLock->Scope = ucb::LockScope_SHARED;
254 pCtx->hasLockScope = true;
255 break;
257 case STATE_WRITE:
258 pCtx->pLock->Type = ucb::LockType_WRITE;
259 pCtx->hasLockType = true;
260 break;
262 case STATE_DEPTH:
263 if ( !pCtx->hasDepth )
264 return 1; // abort
265 break;
267 case STATE_HREF:
268 if ( !pCtx->hasHREF )
269 return 1; // abort
270 break;
272 case STATE_TIMEOUT:
273 if ( !pCtx->hasTimeout )
274 return 1; // abort
275 break;
277 case STATE_LOCKSCOPE:
278 if ( !pCtx->hasLockScope )
279 return 1; // abort
280 break;
282 case STATE_LOCKTYPE:
283 if ( !pCtx->hasLockType )
284 return 1; // abort
285 break;
287 case STATE_ACTIVELOCK:
288 if ( !pCtx->hasLockType || !pCtx->hasLockType || !pCtx->hasDepth )
289 return 1; // abort
290 break;
292 default:
293 break;
295 return 0; // zero to continue, non-zero to abort parsing
298 //////////////////////////////////////////////////////////////////////////
299 // static
300 bool LockSequence::createFromXML( const rtl::OString & rInData,
301 uno::Sequence< ucb::Lock > & rOutData )
303 const sal_Int32 TOKEN_LENGTH = 13; // </activelock>
304 bool success = true;
306 // rInData may contain multiple <activelock>...</activelock> tags.
307 sal_Int32 nCount = 0;
308 sal_Int32 nStart = 0;
309 sal_Int32 nEnd = rInData.indexOf( "</activelock>" );
310 while ( nEnd > -1 )
312 ne_xml_parser * parser = ne_xml_create();
313 if ( !parser )
315 success = false;
316 break;
319 LockSequenceParseContext aCtx;
320 ne_xml_push_handler( parser,
321 LockSequence_startelement_callback,
322 LockSequence_chardata_callback,
323 LockSequence_endelement_callback,
324 &aCtx );
326 ne_xml_parse( parser,
327 rInData.getStr() + nStart,
328 nEnd - nStart + TOKEN_LENGTH );
330 #if NEON_VERSION >= 0x0250
331 success = !ne_xml_failed( parser );
332 #else
333 success = !!ne_xml_valid( parser );
334 #endif
336 ne_xml_destroy( parser );
338 if ( !success )
339 break;
341 if ( aCtx.pLock )
343 nCount++;
344 if ( nCount > rOutData.getLength() )
345 rOutData.realloc( rOutData.getLength() + 1 );
347 rOutData[ nCount - 1 ] = *aCtx.pLock;
350 nStart = nEnd + TOKEN_LENGTH + 1;
351 nEnd = rInData.indexOf( "</activelock>", nStart );
354 return success;