LibreOffice Module ucb (master)  1
LockEntrySequence.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
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.
15  *
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).
21  *
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.
26  *
27  ************************************************************************/
28 
29 #include <config_lgpl.h>
30 #include <string.h>
31 #include <ne_xml.h>
32 #include "LockEntrySequence.hxx"
33 #include <memory>
34 
35 using namespace webdav_ucp;
36 using namespace com::sun::star;
37 
38 
40 {
41  std::unique_ptr<ucb::LockEntry> pEntry;
42  bool hasScope;
43  bool hasType;
44 
46  : hasScope( false ), hasType( false ) {}
47 };
48 
49 #define STATE_TOP (1)
50 
51 #define STATE_LOCKENTRY (STATE_TOP)
52 #define STATE_LOCKSCOPE (STATE_TOP + 1)
53 #define STATE_EXCLUSIVE (STATE_TOP + 2)
54 #define STATE_SHARED (STATE_TOP + 3)
55 #define STATE_LOCKTYPE (STATE_TOP + 4)
56 #define STATE_WRITE (STATE_TOP + 5)
57 
58 
59 extern "C" {
60 
62  void *,
63  int parent,
64  const char * /*nspace*/,
65  const char *name,
66  const char ** )
67 {
68  if ( name != nullptr )
69  {
70  switch ( parent )
71  {
72  case NE_XML_STATEROOT:
73  if ( strcmp( name, "lockentry" ) == 0 )
74  return STATE_LOCKENTRY;
75  break;
76 
77  case STATE_LOCKENTRY:
78  if ( strcmp( name, "lockscope" ) == 0 )
79  return STATE_LOCKSCOPE;
80  else if ( strcmp( name, "locktype" ) == 0 )
81  return STATE_LOCKTYPE;
82 
83 #define IIS_BUGS_WORKAROUND
84 
85 #ifdef IIS_BUGS_WORKAROUND
86  /* IIS (6) returns XML violating RFC 4918
87  for DAV:supportedlock property value.
88 
89  <lockentry>
90  <write></write>
91  <shared></shared>
92  </lockentry>
93  <lockentry>
94  <write></write>
95  <exclusive></exclusive>
96  </lockentry>
97 
98  Bother...
99  */
100  else if ( strcmp( name, "exclusive" ) == 0 )
101  return STATE_EXCLUSIVE;
102  else if ( strcmp( name, "shared" ) == 0 )
103  return STATE_SHARED;
104  else if ( strcmp( name, "write" ) == 0 )
105  return STATE_WRITE;
106 #endif
107  break;
108 
109  case STATE_LOCKSCOPE:
110  if ( strcmp( name, "exclusive" ) == 0 )
111  return STATE_EXCLUSIVE;
112  else if ( strcmp( name, "shared" ) == 0 )
113  return STATE_SHARED;
114  break;
115 
116  case STATE_LOCKTYPE:
117  if ( strcmp( name, "write" ) == 0 )
118  return STATE_WRITE;
119  break;
120  }
121  }
122  return NE_XML_DECLINE;
123 }
124 
125 
127  void *,
128  int,
129  const char *,
130  size_t )
131 {
132  return 0; // zero to continue, non-zero to abort parsing
133 }
134 
135 
137  void *userdata,
138  int state,
139  const char *,
140  const char * )
141 {
143  = static_cast< LockEntrySequenceParseContext * >( userdata );
144  if ( !pCtx->pEntry )
145  pCtx->pEntry.reset( new ucb::LockEntry );
146 
147  switch ( state )
148  {
149  case STATE_EXCLUSIVE:
150  pCtx->pEntry->Scope = ucb::LockScope_EXCLUSIVE;
151  pCtx->hasScope = true;
152  break;
153 
154  case STATE_SHARED:
155  pCtx->pEntry->Scope = ucb::LockScope_SHARED;
156  pCtx->hasScope = true;
157  break;
158 
159  case STATE_WRITE:
160  pCtx->pEntry->Type = ucb::LockType_WRITE;
161  pCtx->hasType = true;
162  break;
163 
164  case STATE_LOCKSCOPE:
165  if ( !pCtx->hasScope )
166  return 1; // abort
167  break;
168 
169  case STATE_LOCKTYPE:
170  if ( !pCtx->hasType )
171  return 1; // abort
172  break;
173 
174  case STATE_LOCKENTRY:
175  if ( !pCtx->hasType || !pCtx->hasScope )
176  return 1; // abort
177  break;
178 
179  default:
180  break;
181  }
182  return 0; // zero to continue, non-zero to abort parsing
183 }
184 
185 }
186 
187 // static
188 bool LockEntrySequence::createFromXML( const OString & rInData,
189  uno::Sequence<
190  ucb::LockEntry > & rOutData )
191 {
192  const sal_Int32 TOKEN_LENGTH = 12; // </lockentry>
193  bool success = true;
194 
195  // rInData may contain multiple <lockentry>...</lockentry> tags.
196  sal_Int32 nCount = 0;
197  sal_Int32 nStart = 0;
198  sal_Int32 nEnd = rInData.indexOf( "</lockentry>" );
199  while ( nEnd > -1 )
200  {
201  ne_xml_parser * parser = ne_xml_create();
202  if ( !parser )
203  {
204  success = false;
205  break;
206  }
207 
209  ne_xml_push_handler( parser,
213  &aCtx );
214 
215  ne_xml_parse( parser,
216  rInData.getStr() + nStart,
217  nEnd - nStart + TOKEN_LENGTH );
218 
219  success = !ne_xml_failed( parser );
220 
221  ne_xml_destroy( parser );
222 
223  if ( !success )
224  break;
225 
226  if ( aCtx.pEntry )
227  {
228  nCount++;
229  if ( nCount > rOutData.getLength() )
230  rOutData.realloc( rOutData.getLength() + 2 );
231 
232  rOutData[ nCount - 1 ] = *aCtx.pEntry;
233  }
234 
235  nStart = nEnd + TOKEN_LENGTH;
236  nEnd = rInData.indexOf( "</lockentry>", nStart );
237  }
238 
239  rOutData.realloc( nCount );
240  return success;
241 }
242 
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define STATE_LOCKTYPE
tuple parser
#define STATE_LOCKENTRY
#define STATE_EXCLUSIVE
static int LockEntrySequence_endelement_callback(void *userdata, int state, const char *, const char *)
static int LockEntrySequence_startelement_callback(void *, int parent, const char *, const char *name, const char **)
static bool createFromXML(const OString &rInData, css::uno::Sequence< css::ucb::LockEntry > &rOutData)
std::unique_ptr< ucb::LockEntry > pEntry
static int LockEntrySequence_chardata_callback(void *, int, const char *, size_t)
#define STATE_LOCKSCOPE
#define STATE_SHARED
#define STATE_WRITE