Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / ucb / source / ucp / webdav-neon / LinkSequence.cxx
blob361e1a27fbd719cfacdff89cd586a86cd635d238
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
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.
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).
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.
27 ************************************************************************/
29 #include <config_lgpl.h>
30 #include <string.h>
31 #include <ne_xml.h>
32 #include <memory>
34 #include "LinkSequence.hxx"
36 using namespace webdav_ucp;
37 using namespace com::sun::star;
40 struct LinkSequenceParseContext
42 std::unique_ptr<ucb::Link> pLink;
43 bool hasSource;
44 bool hasDestination;
46 LinkSequenceParseContext()
47 : hasSource( false ), hasDestination( false ) {}
50 #define STATE_TOP (1)
52 #define STATE_LINK (STATE_TOP)
53 #define STATE_DST (STATE_TOP + 1)
54 #define STATE_SRC (STATE_TOP + 2)
57 extern "C" {
59 static int LinkSequence_startelement_callback(
60 void *,
61 int parent,
62 const char * /*nspace*/,
63 const char *name,
64 const char ** )
66 if ( name != nullptr )
68 switch ( parent )
70 case NE_XML_STATEROOT:
71 if ( strcmp( name, "link" ) == 0 )
72 return STATE_LINK;
73 break;
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;
83 return NE_XML_DECLINE;
87 static int LinkSequence_chardata_callback(
88 void *userdata,
89 int state,
90 const char *buf,
91 size_t len )
93 LinkSequenceParseContext * pCtx
94 = static_cast< LinkSequenceParseContext * >( userdata );
95 if ( !pCtx->pLink )
96 pCtx->pLink.reset( new ucb::Link );
98 switch ( state )
100 case STATE_DST:
101 pCtx->pLink->Destination
102 = OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
103 pCtx->hasDestination = true;
104 break;
106 case STATE_SRC:
107 pCtx->pLink->Source
108 = OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
109 pCtx->hasSource = true;
110 break;
112 return 0; // zero to continue, non-zero to abort parsing
116 static int LinkSequence_endelement_callback(
117 void *userdata,
118 int state,
119 const char *,
120 const char * )
122 LinkSequenceParseContext * pCtx
123 = static_cast< LinkSequenceParseContext * >( userdata );
124 if ( !pCtx->pLink )
125 pCtx->pLink.reset( new ucb::Link );
127 switch ( state )
129 case STATE_LINK:
130 if ( !pCtx->hasDestination || !pCtx->hasSource )
131 return 1; // abort
132 break;
134 return 0; // zero to continue, non-zero to abort parsing
139 // static
140 bool LinkSequence::createFromXML( const OString & rInData,
141 uno::Sequence< ucb::Link > & rOutData )
143 const sal_Int32 TOKEN_LENGTH = 7; // </link>
144 bool success = true;
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 )
152 ne_xml_parser * parser = ne_xml_create();
153 if ( !parser )
155 success = false;
156 break;
159 LinkSequenceParseContext aCtx;
160 ne_xml_push_handler( parser,
161 LinkSequence_startelement_callback,
162 LinkSequence_chardata_callback,
163 LinkSequence_endelement_callback,
164 &aCtx );
166 ne_xml_parse( parser,
167 rInData.getStr() + nStart,
168 nEnd - nStart + TOKEN_LENGTH );
170 success = !ne_xml_failed( parser );
172 ne_xml_destroy( parser );
174 if ( !success )
175 break;
177 if ( aCtx.pLink )
179 nCount++;
180 if ( nCount > rOutData.getLength() )
181 rOutData.realloc( rOutData.getLength() + 1 );
183 rOutData[ nCount - 1 ] = *aCtx.pLink;
186 nStart = nEnd + TOKEN_LENGTH;
187 nEnd = rInData.indexOf( "</link>", nStart );
190 return success;
194 // static
195 bool LinkSequence::toXML( const uno::Sequence< ucb::Link > & rInData,
196 OUString & rOutData )
198 // <link><src>value</src><dst>value</dst></link><link><src>...
200 for ( const auto& rLink : rInData )
202 rOutData += "<link><src>";
203 rOutData += rLink.Source;
204 rOutData += "</src><dst>";
205 rOutData += rLink.Destination;
206 rOutData += "</dst></link>";
208 return rInData.hasElements();
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */