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>
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
51 LockSequenceParseContext()
52 : pLock( 0 ), hasLockScope( false ), hasLockType( false ),
53 hasDepth( false ), hasHREF( false ), hasTimeout( false ) {}
55 ~LockSequenceParseContext() { delete pLock
; }
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(
76 const char * /*nspace*/,
84 case NE_XML_STATEROOT
:
85 if ( strcmp( name
, "activelock" ) == 0 )
86 return STATE_ACTIVELOCK
;
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 )
96 else if ( strcmp( name
, "owner" ) == 0 )
98 else if ( strcmp( name
, "timeout" ) == 0 )
100 else if ( strcmp( name
, "locktoken" ) == 0 )
101 return STATE_LOCKTOKEN
;
104 case STATE_LOCKSCOPE
:
105 if ( strcmp( name
, "exclusive" ) == 0 )
106 return STATE_EXCLUSIVE
;
107 else if ( strcmp( name
, "shared" ) == 0 )
112 if ( strcmp( name
, "write" ) == 0 )
116 case STATE_LOCKTOKEN
:
117 if ( strcmp( name
, "href" ) == 0 )
122 // owner elem contains ANY. Accept anything; no state change.
126 return NE_XML_DECLINE
;
130 extern "C" int LockSequence_chardata_callback(
133 #ifdef BEEHIVE_BUGS_WORKAROUND
140 LockSequenceParseContext
* pCtx
141 = static_cast< LockSequenceParseContext
* >( userdata
);
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 )
150 char * buf
= new char[ len
+ 1 ]();
151 strncpy( buf
, buf1
, len
);
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;
176 OSL_FAIL( "LockSequence_chardata_callback - Unknown depth!" );
181 // collect raw XML data... (owner contains ANY)
183 pCtx
->pLock
->Owner
>>= aValue
;
184 aValue
+= OUString( buf
, len
, RTL_TEXTENCODING_ASCII_US
);
185 pCtx
->pLock
->Owner
<<= aValue
;
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 )
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 )
221 pCtx
->pLock
->Timeout
= sal_Int64( -1 );
222 pCtx
->hasTimeout
= true;
223 OSL_FAIL( "LockSequence_chardata_callback - Unknown timeout!" );
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;
240 #ifdef BEEHIVE_BUGS_WORKAROUND
244 return 0; // zero to continue, non-zero to abort parsing
248 extern "C" int LockSequence_endelement_callback(
254 LockSequenceParseContext
* pCtx
255 = static_cast< LockSequenceParseContext
* >( userdata
);
257 pCtx
->pLock
= new ucb::Lock
;
261 case STATE_EXCLUSIVE
:
262 pCtx
->pLock
->Scope
= ucb::LockScope_EXCLUSIVE
;
263 pCtx
->hasLockScope
= true;
267 pCtx
->pLock
->Scope
= ucb::LockScope_SHARED
;
268 pCtx
->hasLockScope
= true;
272 pCtx
->pLock
->Type
= ucb::LockType_WRITE
;
273 pCtx
->hasLockType
= true;
277 if ( !pCtx
->hasDepth
)
282 if ( !pCtx
->hasHREF
)
287 if ( !pCtx
->hasTimeout
)
291 case STATE_LOCKSCOPE
:
292 if ( !pCtx
->hasLockScope
)
297 if ( !pCtx
->hasLockType
)
301 case STATE_ACTIVELOCK
:
302 if ( !pCtx
->hasLockType
|| !pCtx
->hasDepth
)
309 return 0; // zero to continue, non-zero to abort parsing
314 bool LockSequence::createFromXML( const OString
& rInData
,
315 uno::Sequence
< ucb::Lock
> & rOutData
)
317 const sal_Int32 TOKEN_LENGTH
= 13; // </activelock>
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>" );
326 ne_xml_parser
* parser
= ne_xml_create();
333 LockSequenceParseContext aCtx
;
334 ne_xml_push_handler( parser
,
335 LockSequence_startelement_callback
,
336 LockSequence_chardata_callback
,
337 LockSequence_endelement_callback
,
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
);
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
);
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */