1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: LockSequence.cxx,v $
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"
36 #include "LockSequence.hxx"
38 using namespace webdav_ucp
;
39 using namespace com::sun::star
;
41 //////////////////////////////////////////////////////////////////////////
43 struct LockSequenceParseContext
52 LockSequenceParseContext()
53 : pLock( 0 ), hasLockScope( false ), hasLockType( false ),
54 hasDepth( false ), hasHREF( false ), hasTimeout( false ) {}
56 ~LockSequenceParseContext() { delete pLock
; }
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(
82 ( ( nspace
== 0 ) || ( strcmp( nspace
, "" ) == 0 ) ) )
86 case NE_XML_STATEROOT
:
87 if ( strcmp( name
, "activelock" ) == 0 )
88 return STATE_ACTIVELOCK
;
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 )
98 else if ( strcmp( name
, "owner" ) == 0 )
100 else if ( strcmp( name
, "timeout" ) == 0 )
101 return STATE_TIMEOUT
;
102 else if ( strcmp( name
, "locktoken" ) == 0 )
103 return STATE_LOCKTOKEN
;
106 case STATE_LOCKSCOPE
:
107 if ( strcmp( name
, "exclusive" ) == 0 )
108 return STATE_EXCLUSIVE
;
109 else if ( strcmp( name
, "shared" ) == 0 )
114 if ( strcmp( name
, "write" ) == 0 )
118 case STATE_LOCKTOKEN
:
119 if ( strcmp( name
, "href" ) == 0 )
124 // owner elem contains ANY. Accept anything; no state change.
128 return NE_XML_DECLINE
;
131 //////////////////////////////////////////////////////////////////////////
132 extern "C" int LockSequence_chardata_callback(
138 LockSequenceParseContext
* pCtx
139 = static_cast< LockSequenceParseContext
* >( userdata
);
141 pCtx
->pLock
= new ucb::Lock
;
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;
165 OSL_ENSURE( sal_False
,
166 "LockSequence_chardata_callback - Unknown depth!" );
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
;
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 )
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 )
211 pCtx
->pLock
->Timeout
= sal_Int64( -1 );
212 pCtx
->hasTimeout
= true;
213 OSL_ENSURE( sal_False
,
214 "LockSequence_chardata_callback - Unknown timeout!" );
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;
230 return 0; // zero to continue, non-zero to abort parsing
233 //////////////////////////////////////////////////////////////////////////
234 extern "C" int LockSequence_endelement_callback(
240 LockSequenceParseContext
* pCtx
241 = static_cast< LockSequenceParseContext
* >( userdata
);
243 pCtx
->pLock
= new ucb::Lock
;
247 case STATE_EXCLUSIVE
:
248 pCtx
->pLock
->Scope
= ucb::LockScope_EXCLUSIVE
;
249 pCtx
->hasLockScope
= true;
253 pCtx
->pLock
->Scope
= ucb::LockScope_SHARED
;
254 pCtx
->hasLockScope
= true;
258 pCtx
->pLock
->Type
= ucb::LockType_WRITE
;
259 pCtx
->hasLockType
= true;
263 if ( !pCtx
->hasDepth
)
268 if ( !pCtx
->hasHREF
)
273 if ( !pCtx
->hasTimeout
)
277 case STATE_LOCKSCOPE
:
278 if ( !pCtx
->hasLockScope
)
283 if ( !pCtx
->hasLockType
)
287 case STATE_ACTIVELOCK
:
288 if ( !pCtx
->hasLockType
|| !pCtx
->hasLockType
|| !pCtx
->hasDepth
)
295 return 0; // zero to continue, non-zero to abort parsing
298 //////////////////////////////////////////////////////////////////////////
300 bool LockSequence::createFromXML( const rtl::OString
& rInData
,
301 uno::Sequence
< ucb::Lock
> & rOutData
)
303 const sal_Int32 TOKEN_LENGTH
= 13; // </activelock>
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>" );
312 ne_xml_parser
* parser
= ne_xml_create();
319 LockSequenceParseContext aCtx
;
320 ne_xml_push_handler( parser
,
321 LockSequence_startelement_callback
,
322 LockSequence_chardata_callback
,
323 LockSequence_endelement_callback
,
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
);
333 success
= !!ne_xml_valid( parser
);
336 ne_xml_destroy( parser
);
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
);