update dev300-m58
[ooovba.git] / ucb / source / ucp / webdav / LinkSequence.cxx
blobf890122dec3deb5352ed8b0ed250d19bef70f6b2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: LinkSequence.cxx,v $
10 * $Revision: 1.16 $
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"
34 #include <string.h>
35 #include <ne_xml.h>
37 #ifndef _LINKSEQUENCE_HXX_
38 #include "LinkSequence.hxx"
39 #endif
41 using namespace webdav_ucp;
42 using namespace com::sun::star;
44 //////////////////////////////////////////////////////////////////////////
46 struct LinkSequenceParseContext
48 ucb::Link * pLink;
49 bool hasSource;
50 bool hasDestination;
52 LinkSequenceParseContext()
53 : pLink( 0 ), hasSource( false ), hasDestination( false ) {}
54 ~LinkSequenceParseContext() { delete pLink; }
57 #define STATE_TOP (1)
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(
65 void *,
66 int parent,
67 const char *nspace,
68 const char *name,
69 const char ** )
71 if ( ( name != 0 ) &&
72 ( ( nspace == 0 ) || ( strcmp( nspace, "" ) == 0 ) ) )
74 switch ( parent )
76 case NE_XML_STATEROOT:
77 if ( strcmp( name, "link" ) == 0 )
78 return STATE_LINK;
79 break;
81 case STATE_LINK:
82 if ( strcmp( name, "dst" ) == 0 )
83 return STATE_DST;
84 else if ( strcmp( name, "src" ) == 0 )
85 return STATE_SRC;
86 break;
89 return NE_XML_DECLINE;
92 //////////////////////////////////////////////////////////////////////////
93 extern "C" int LinkSequence_chardata_callback(
94 void *userdata,
95 int state,
96 const char *buf,
97 size_t len )
99 LinkSequenceParseContext * pCtx
100 = static_cast< LinkSequenceParseContext * >( userdata );
101 if ( !pCtx->pLink )
102 pCtx->pLink = new ucb::Link;
104 switch ( state )
106 case STATE_DST:
107 pCtx->pLink->Destination
108 = rtl::OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
109 pCtx->hasDestination = true;
110 break;
112 case STATE_SRC:
113 pCtx->pLink->Source
114 = rtl::OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
115 pCtx->hasSource = true;
116 break;
118 return 0; // zero to continue, non-zero to abort parsing
121 //////////////////////////////////////////////////////////////////////////
122 extern "C" int LinkSequence_endelement_callback(
123 void *userdata,
124 int state,
125 const char *,
126 const char * )
128 LinkSequenceParseContext * pCtx
129 = static_cast< LinkSequenceParseContext * >( userdata );
130 if ( !pCtx->pLink )
131 pCtx->pLink = new ucb::Link;
133 switch ( state )
135 case STATE_LINK:
136 if ( !pCtx->hasDestination || !pCtx->hasSource )
137 return 1; // abort
138 break;
140 return 0; // zero to continue, non-zero to abort parsing
143 //////////////////////////////////////////////////////////////////////////
144 // static
145 bool LinkSequence::createFromXML( const rtl::OString & rInData,
146 uno::Sequence< ucb::Link > & rOutData )
148 const sal_Int32 TOKEN_LENGTH = 7; // </link>
149 bool success = true;
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>" );
155 while ( nEnd > -1 )
157 ne_xml_parser * parser = ne_xml_create();
158 if ( !parser )
160 success = false;
161 break;
164 LinkSequenceParseContext aCtx;
165 ne_xml_push_handler( parser,
166 LinkSequence_startelement_callback,
167 LinkSequence_chardata_callback,
168 LinkSequence_endelement_callback,
169 &aCtx );
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 );
177 #else
178 success = !!ne_xml_valid( parser );
179 #endif
181 ne_xml_destroy( parser );
183 if ( !success )
184 break;
186 if ( aCtx.pLink )
188 nCount++;
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 );
199 return success;
202 //////////////////////////////////////////////////////////////////////////
203 // static
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();
210 if ( nCount )
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 )
218 rOutData += aPre;
219 rOutData += rInData[ n ].Source;
220 rOutData += aMid;
221 rOutData += rInData[ n ].Destination;
222 rOutData += aEnd;
224 return true;
226 return false;