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