1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: LinkSequence.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
37 #ifndef _LINKSEQUENCE_HXX_
38 #include "LinkSequence.hxx"
41 using namespace webdav_ucp
;
42 using namespace com::sun::star
;
44 //////////////////////////////////////////////////////////////////////////
46 struct LinkSequenceParseContext
52 LinkSequenceParseContext()
53 : pLink( 0 ), hasSource( false ), hasDestination( false ) {}
54 ~LinkSequenceParseContext() { delete pLink
; }
59 #define STATE_LINK (STATE_TOP)
60 #define STATE_DST (STATE_TOP + 1)
61 #define STATE_SRC (STATE_TOP + 2)
63 //////////////////////////////////////////////////////////////////////////
64 extern "C" int LinkSequence_startelement_callback(
72 ( ( nspace
== 0 ) || ( strcmp( nspace
, "" ) == 0 ) ) )
76 case NE_XML_STATEROOT
:
77 if ( strcmp( name
, "link" ) == 0 )
82 if ( strcmp( name
, "dst" ) == 0 )
84 else if ( strcmp( name
, "src" ) == 0 )
89 return NE_XML_DECLINE
;
92 //////////////////////////////////////////////////////////////////////////
93 extern "C" int LinkSequence_chardata_callback(
99 LinkSequenceParseContext
* pCtx
100 = static_cast< LinkSequenceParseContext
* >( userdata
);
102 pCtx
->pLink
= new ucb::Link
;
107 pCtx
->pLink
->Destination
108 = rtl::OUString( buf
, len
, RTL_TEXTENCODING_ASCII_US
);
109 pCtx
->hasDestination
= true;
114 = rtl::OUString( buf
, len
, RTL_TEXTENCODING_ASCII_US
);
115 pCtx
->hasSource
= true;
118 return 0; // zero to continue, non-zero to abort parsing
121 //////////////////////////////////////////////////////////////////////////
122 extern "C" int LinkSequence_endelement_callback(
128 LinkSequenceParseContext
* pCtx
129 = static_cast< LinkSequenceParseContext
* >( userdata
);
131 pCtx
->pLink
= new ucb::Link
;
136 if ( !pCtx
->hasDestination
|| !pCtx
->hasSource
)
140 return 0; // zero to continue, non-zero to abort parsing
143 //////////////////////////////////////////////////////////////////////////
145 bool LinkSequence::createFromXML( const rtl::OString
& rInData
,
146 uno::Sequence
< ucb::Link
> & rOutData
)
148 const sal_Int32 TOKEN_LENGTH
= 7; // </link>
151 // rInData may contain multiple <link>...</link> tags.
152 sal_Int32 nCount
= 0;
153 sal_Int32 nStart
= 0;
154 sal_Int32 nEnd
= rInData
.indexOf( "</link>" );
157 ne_xml_parser
* parser
= ne_xml_create();
164 LinkSequenceParseContext aCtx
;
165 ne_xml_push_handler( parser
,
166 LinkSequence_startelement_callback
,
167 LinkSequence_chardata_callback
,
168 LinkSequence_endelement_callback
,
171 ne_xml_parse( parser
,
172 rInData
.getStr() + nStart
,
173 nEnd
- nStart
+ TOKEN_LENGTH
);
175 #if NEON_VERSION >= 0x0250
176 success
= !ne_xml_failed( parser
);
178 success
= !!ne_xml_valid( parser
);
181 ne_xml_destroy( parser
);
189 if ( nCount
> rOutData
.getLength() )
190 rOutData
.realloc( rOutData
.getLength() + 1 );
192 rOutData
[ nCount
- 1 ] = *aCtx
.pLink
;
195 nStart
= nEnd
+ TOKEN_LENGTH
+ 1;
196 nEnd
= rInData
.indexOf( "</link>", nStart
);
202 //////////////////////////////////////////////////////////////////////////
204 bool LinkSequence::toXML( const uno::Sequence
< ucb::Link
> & rInData
,
205 rtl::OUString
& rOutData
)
207 // <link><src>value</src><dst>value</dst></link><link><src>....
209 sal_Int32 nCount
= rInData
.getLength();
212 rtl::OUString
aPre( rtl::OUString::createFromAscii( "<link><src>" ) );
213 rtl::OUString
aMid( rtl::OUString::createFromAscii( "</src><dst>" ) );
214 rtl::OUString
aEnd( rtl::OUString::createFromAscii( "</dst></link>" ) );
216 for ( sal_Int32 n
= 0; n
< nCount
; ++n
)
219 rOutData
+= rInData
[ n
].Source
;
221 rOutData
+= rInData
[ n
].Destination
;