update dev300-m58
[ooovba.git] / ucb / source / ucp / webdav / LockEntrySequence.cxx
blob6d997bd0a6ec4781326a1aab54bb05797de68874
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: LockEntrySequence.cxx,v $
10 * $Revision: 1.16 $
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 "LockEntrySequence.hxx"
38 using namespace webdav_ucp;
39 using namespace com::sun::star;
41 //////////////////////////////////////////////////////////////////////////
43 struct LockEntrySequenceParseContext
45 ucb::LockEntry * pEntry;
46 bool hasScope;
47 bool hasType;
49 LockEntrySequenceParseContext()
50 : pEntry( 0 ), hasScope( false ), hasType( false ) {}
51 ~LockEntrySequenceParseContext() { delete pEntry; }
54 #define STATE_TOP (1)
56 #define STATE_LOCKENTRY (STATE_TOP)
57 #define STATE_LOCKSCOPE (STATE_TOP + 1)
58 #define STATE_EXCLUSIVE (STATE_TOP + 2)
59 #define STATE_SHARED (STATE_TOP + 3)
60 #define STATE_LOCKTYPE (STATE_TOP + 4)
61 #define STATE_WRITE (STATE_TOP + 5)
63 //////////////////////////////////////////////////////////////////////////
64 extern "C" int LockEntrySequence_startelement_callback(
65 void *,
66 int parent,
67 const char *nspace,
68 const char *name,
69 const char ** )
71 if ( ( name != 0 ) &&
72 ( ( nspace == 0 ) || ( strcmp( nspace, "" ) == 0 ) ) )
74 switch ( parent )
76 case NE_XML_STATEROOT:
77 if ( strcmp( name, "lockentry" ) == 0 )
78 return STATE_LOCKENTRY;
79 break;
81 case STATE_LOCKENTRY:
82 if ( strcmp( name, "lockscope" ) == 0 )
83 return STATE_LOCKSCOPE;
84 else if ( strcmp( name, "locktype" ) == 0 )
85 return STATE_LOCKTYPE;
86 break;
88 case STATE_LOCKSCOPE:
89 if ( strcmp( name, "exclusive" ) == 0 )
90 return STATE_EXCLUSIVE;
91 else if ( strcmp( name, "shared" ) == 0 )
92 return STATE_SHARED;
93 break;
95 case STATE_LOCKTYPE:
96 if ( strcmp( name, "write" ) == 0 )
97 return STATE_WRITE;
98 break;
101 return NE_XML_DECLINE;
104 //////////////////////////////////////////////////////////////////////////
105 extern "C" int LockEntrySequence_chardata_callback(
106 void *,
107 int,
108 const char *,
109 size_t )
111 return 0; // zero to continue, non-zero to abort parsing
114 //////////////////////////////////////////////////////////////////////////
115 extern "C" int LockEntrySequence_endelement_callback(
116 void *userdata,
117 int state,
118 const char *,
119 const char * )
121 LockEntrySequenceParseContext * pCtx
122 = static_cast< LockEntrySequenceParseContext * >( userdata );
123 if ( !pCtx->pEntry )
124 pCtx->pEntry = new ucb::LockEntry;
126 switch ( state )
128 case STATE_EXCLUSIVE:
129 pCtx->pEntry->Scope = ucb::LockScope_EXCLUSIVE;
130 pCtx->hasScope = true;
131 break;
133 case STATE_SHARED:
134 pCtx->pEntry->Scope = ucb::LockScope_SHARED;
135 pCtx->hasScope = true;
136 break;
138 case STATE_WRITE:
139 pCtx->pEntry->Type = ucb::LockType_WRITE;
140 pCtx->hasType = true;
141 break;
143 case STATE_LOCKSCOPE:
144 if ( !pCtx->hasScope )
145 return 1; // abort
146 break;
148 case STATE_LOCKTYPE:
149 if ( !pCtx->hasType )
150 return 1; // abort
151 break;
153 case STATE_LOCKENTRY:
154 if ( !pCtx->hasType || !pCtx->hasType )
155 return 1; // abort
156 break;
158 default:
159 break;
161 return 0; // zero to continue, non-zero to abort parsing
164 //////////////////////////////////////////////////////////////////////////
165 // static
166 bool LockEntrySequence::createFromXML( const rtl::OString & rInData,
167 uno::Sequence<
168 ucb::LockEntry > & rOutData )
170 const sal_Int32 TOKEN_LENGTH = 12; // </lockentry>
171 bool success = true;
173 // rInData may contain multiple <lockentry>...</lockentry> tags.
174 sal_Int32 nCount = 0;
175 sal_Int32 nStart = 0;
176 sal_Int32 nEnd = rInData.indexOf( "</lockentry>" );
177 while ( nEnd > -1 )
179 ne_xml_parser * parser = ne_xml_create();
180 if ( !parser )
182 success = false;
183 break;
186 LockEntrySequenceParseContext aCtx;
187 ne_xml_push_handler( parser,
188 LockEntrySequence_startelement_callback,
189 LockEntrySequence_chardata_callback,
190 LockEntrySequence_endelement_callback,
191 &aCtx );
193 ne_xml_parse( parser,
194 rInData.getStr() + nStart,
195 nEnd - nStart + TOKEN_LENGTH );
197 #if NEON_VERSION >= 0x0250
198 success = !ne_xml_failed( parser );
199 #else
200 success = !!ne_xml_valid( parser );
201 #endif
203 ne_xml_destroy( parser );
205 if ( !success )
206 break;
208 if ( aCtx.pEntry )
210 nCount++;
211 if ( nCount > rOutData.getLength() )
212 rOutData.realloc( rOutData.getLength() + 2 );
214 rOutData[ nCount - 1 ] = *aCtx.pEntry;
217 nStart = nEnd + TOKEN_LENGTH + 1;
218 nEnd = rInData.indexOf( "</lockentry>", nStart );
221 rOutData.realloc( nCount );
222 return success;