LibreOffice Module ucb (master)  1
LinkSequence.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 <memory>
33 
34 #include "LinkSequence.hxx"
35 
36 using namespace webdav_ucp;
37 using namespace com::sun::star;
38 
39 namespace {
40 
41 struct LinkSequenceParseContext
42 {
43  std::unique_ptr<ucb::Link> pLink;
44  bool hasSource;
45  bool hasDestination;
46 
47  LinkSequenceParseContext()
48  : hasSource( false ), hasDestination( false ) {}
49 };
50 
51 }
52 
53 #define STATE_TOP (1)
54 
55 #define STATE_LINK (STATE_TOP)
56 #define STATE_DST (STATE_TOP + 1)
57 #define STATE_SRC (STATE_TOP + 2)
58 
59 
60 extern "C" {
61 
63  void *,
64  int parent,
65  const char * /*nspace*/,
66  const char *name,
67  const char ** )
68 {
69  if ( name != nullptr )
70  {
71  switch ( parent )
72  {
73  case NE_XML_STATEROOT:
74  if ( strcmp( name, "link" ) == 0 )
75  return STATE_LINK;
76  break;
77 
78  case STATE_LINK:
79  if ( strcmp( name, "dst" ) == 0 )
80  return STATE_DST;
81  else if ( strcmp( name, "src" ) == 0 )
82  return STATE_SRC;
83  break;
84  }
85  }
86  return NE_XML_DECLINE;
87 }
88 
89 
91  void *userdata,
92  int state,
93  const char *buf,
94  size_t len )
95 {
96  LinkSequenceParseContext * pCtx
97  = static_cast< LinkSequenceParseContext * >( userdata );
98  if ( !pCtx->pLink )
99  pCtx->pLink.reset( new ucb::Link );
100 
101  switch ( state )
102  {
103  case STATE_DST:
104  pCtx->pLink->Destination
105  = OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
106  pCtx->hasDestination = true;
107  break;
108 
109  case STATE_SRC:
110  pCtx->pLink->Source
111  = OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
112  pCtx->hasSource = true;
113  break;
114  }
115  return 0; // zero to continue, non-zero to abort parsing
116 }
117 
118 
120  void *userdata,
121  int state,
122  const char *,
123  const char * )
124 {
125  LinkSequenceParseContext * pCtx
126  = static_cast< LinkSequenceParseContext * >( userdata );
127  if ( !pCtx->pLink )
128  pCtx->pLink.reset( new ucb::Link );
129 
130  switch ( state )
131  {
132  case STATE_LINK:
133  if ( !pCtx->hasDestination || !pCtx->hasSource )
134  return 1; // abort
135  break;
136  }
137  return 0; // zero to continue, non-zero to abort parsing
138 }
139 
140 }
141 
142 // static
143 bool LinkSequence::createFromXML( const OString & rInData,
144  uno::Sequence< ucb::Link > & rOutData )
145 {
146  const sal_Int32 TOKEN_LENGTH = 7; // </link>
147  bool success = true;
148 
149  // rInData may contain multiple <link>...</link> tags.
150  sal_Int32 nCount = 0;
151  sal_Int32 nStart = 0;
152  sal_Int32 nEnd = rInData.indexOf( "</link>" );
153  while ( nEnd > -1 )
154  {
155  ne_xml_parser * parser = ne_xml_create();
156  if ( !parser )
157  {
158  success = false;
159  break;
160  }
161 
162  LinkSequenceParseContext aCtx;
163  ne_xml_push_handler( parser,
167  &aCtx );
168 
169  ne_xml_parse( parser,
170  rInData.getStr() + nStart,
171  nEnd - nStart + TOKEN_LENGTH );
172 
173  success = !ne_xml_failed( parser );
174 
175  ne_xml_destroy( parser );
176 
177  if ( !success )
178  break;
179 
180  if ( aCtx.pLink )
181  {
182  nCount++;
183  if ( nCount > rOutData.getLength() )
184  rOutData.realloc( rOutData.getLength() + 1 );
185 
186  rOutData[ nCount - 1 ] = *aCtx.pLink;
187  }
188 
189  nStart = nEnd + TOKEN_LENGTH;
190  nEnd = rInData.indexOf( "</link>", nStart );
191  }
192 
193  return success;
194 }
195 
196 
197 // static
198 bool LinkSequence::toXML( const uno::Sequence< ucb::Link > & rInData,
199  OUString & rOutData )
200 {
201  // <link><src>value</src><dst>value</dst></link><link><src>...
202 
203  for ( const auto& rLink : rInData )
204  {
205  rOutData += "<link><src>";
206  rOutData += rLink.Source;
207  rOutData += "</src><dst>";
208  rOutData += rLink.Destination;
209  rOutData += "</dst></link>";
210  }
211  return rInData.hasElements();
212 }
213 
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static int LinkSequence_endelement_callback(void *userdata, int state, const char *, const char *)
tuple parser
static bool createFromXML(const OString &rInData, css::uno::Sequence< css::ucb::Link > &rOutData)
static bool toXML(const css::uno::Sequence< css::ucb::Link > &rInData, OUString &rOutData)
#define STATE_DST
int nCount
static int LinkSequence_startelement_callback(void *, int parent, const char *, const char *name, const char **)
SvBaseLink * pLink
#define STATE_SRC
#define STATE_LINK
static int LinkSequence_chardata_callback(void *userdata, int state, const char *buf, size_t len)