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>
33 #include "LinkSequence.hxx"
35 using namespace webdav_ucp
;
36 using namespace com::sun::star
;
40 struct LinkSequenceParseContext
46 LinkSequenceParseContext()
47 : pLink( 0 ), hasSource( false ), hasDestination( false ) {}
48 ~LinkSequenceParseContext() { delete pLink
; }
53 #define STATE_LINK (STATE_TOP)
54 #define STATE_DST (STATE_TOP + 1)
55 #define STATE_SRC (STATE_TOP + 2)
58 extern "C" int LinkSequence_startelement_callback(
61 const char * /*nspace*/,
69 case NE_XML_STATEROOT
:
70 if ( strcmp( name
, "link" ) == 0 )
75 if ( strcmp( name
, "dst" ) == 0 )
77 else if ( strcmp( name
, "src" ) == 0 )
82 return NE_XML_DECLINE
;
86 extern "C" int LinkSequence_chardata_callback(
92 LinkSequenceParseContext
* pCtx
93 = static_cast< LinkSequenceParseContext
* >( userdata
);
95 pCtx
->pLink
= new ucb::Link
;
100 pCtx
->pLink
->Destination
101 = OUString( buf
, len
, RTL_TEXTENCODING_ASCII_US
);
102 pCtx
->hasDestination
= true;
107 = OUString( buf
, len
, RTL_TEXTENCODING_ASCII_US
);
108 pCtx
->hasSource
= true;
111 return 0; // zero to continue, non-zero to abort parsing
115 extern "C" int LinkSequence_endelement_callback(
121 LinkSequenceParseContext
* pCtx
122 = static_cast< LinkSequenceParseContext
* >( userdata
);
124 pCtx
->pLink
= new ucb::Link
;
129 if ( !pCtx
->hasDestination
|| !pCtx
->hasSource
)
133 return 0; // zero to continue, non-zero to abort parsing
138 bool LinkSequence::createFromXML( const OString
& rInData
,
139 uno::Sequence
< ucb::Link
> & rOutData
)
141 const sal_Int32 TOKEN_LENGTH
= 7; // </link>
144 // rInData may contain multiple <link>...</link> tags.
145 sal_Int32 nCount
= 0;
146 sal_Int32 nStart
= 0;
147 sal_Int32 nEnd
= rInData
.indexOf( "</link>" );
150 ne_xml_parser
* parser
= ne_xml_create();
157 LinkSequenceParseContext aCtx
;
158 ne_xml_push_handler( parser
,
159 LinkSequence_startelement_callback
,
160 LinkSequence_chardata_callback
,
161 LinkSequence_endelement_callback
,
164 ne_xml_parse( parser
,
165 rInData
.getStr() + nStart
,
166 nEnd
- nStart
+ TOKEN_LENGTH
);
168 success
= !ne_xml_failed( parser
);
170 ne_xml_destroy( parser
);
178 if ( nCount
> rOutData
.getLength() )
179 rOutData
.realloc( rOutData
.getLength() + 1 );
181 rOutData
[ nCount
- 1 ] = *aCtx
.pLink
;
184 nStart
= nEnd
+ TOKEN_LENGTH
;
185 nEnd
= rInData
.indexOf( "</link>", nStart
);
193 bool LinkSequence::toXML( const uno::Sequence
< ucb::Link
> & rInData
,
194 OUString
& rOutData
)
196 // <link><src>value</src><dst>value</dst></link><link><src>....
198 sal_Int32 nCount
= rInData
.getLength();
201 OUString
aPre( "<link><src>" );
202 OUString
aMid( "</src><dst>" );
203 OUString
aEnd( "</dst></link>" );
205 for ( sal_Int32 n
= 0; n
< nCount
; ++n
)
208 rOutData
+= rInData
[ n
].Source
;
210 rOutData
+= rInData
[ n
].Destination
;
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */