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>
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
;
46 LinkSequenceParseContext()
47 : hasSource( false ), hasDestination( false ) {}
52 #define STATE_LINK (STATE_TOP)
53 #define STATE_DST (STATE_TOP + 1)
54 #define STATE_SRC (STATE_TOP + 2)
59 static int LinkSequence_startelement_callback(
62 const char * /*nspace*/,
66 if ( name
!= nullptr )
70 case NE_XML_STATEROOT
:
71 if ( strcmp( name
, "link" ) == 0 )
76 if ( strcmp( name
, "dst" ) == 0 )
78 else if ( strcmp( name
, "src" ) == 0 )
83 return NE_XML_DECLINE
;
87 static int LinkSequence_chardata_callback(
93 LinkSequenceParseContext
* pCtx
94 = static_cast< LinkSequenceParseContext
* >( userdata
);
96 pCtx
->pLink
.reset( new ucb::Link
);
101 pCtx
->pLink
->Destination
102 = OUString( buf
, len
, RTL_TEXTENCODING_ASCII_US
);
103 pCtx
->hasDestination
= true;
108 = OUString( buf
, len
, RTL_TEXTENCODING_ASCII_US
);
109 pCtx
->hasSource
= true;
112 return 0; // zero to continue, non-zero to abort parsing
116 static int LinkSequence_endelement_callback(
122 LinkSequenceParseContext
* pCtx
123 = static_cast< LinkSequenceParseContext
* >( userdata
);
125 pCtx
->pLink
.reset( new ucb::Link
);
130 if ( !pCtx
->hasDestination
|| !pCtx
->hasSource
)
134 return 0; // zero to continue, non-zero to abort parsing
140 bool LinkSequence::createFromXML( const OString
& rInData
,
141 uno::Sequence
< ucb::Link
> & rOutData
)
143 const sal_Int32 TOKEN_LENGTH
= 7; // </link>
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>" );
152 ne_xml_parser
* parser
= ne_xml_create();
159 LinkSequenceParseContext aCtx
;
160 ne_xml_push_handler( parser
,
161 LinkSequence_startelement_callback
,
162 LinkSequence_chardata_callback
,
163 LinkSequence_endelement_callback
,
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
);
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
);
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: */