Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : *
6 : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : *
8 : * OpenOffice.org - a multi-platform office productivity suite
9 : *
10 : * This file is part of OpenOffice.org.
11 : *
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.
15 : *
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).
21 : *
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.
26 : *
27 : ************************************************************************/
28 :
29 :
30 : #include <string.h>
31 : #include <ne_xml.h>
32 :
33 : #include "LinkSequence.hxx"
34 :
35 : using namespace webdav_ucp;
36 : using namespace com::sun::star;
37 :
38 : //////////////////////////////////////////////////////////////////////////
39 :
40 : struct LinkSequenceParseContext
41 : {
42 : ucb::Link * pLink;
43 : bool hasSource;
44 : bool hasDestination;
45 :
46 0 : LinkSequenceParseContext()
47 0 : : pLink( 0 ), hasSource( false ), hasDestination( false ) {}
48 0 : ~LinkSequenceParseContext() { delete pLink; }
49 : };
50 :
51 : #define STATE_TOP (1)
52 :
53 : #define STATE_LINK (STATE_TOP)
54 : #define STATE_DST (STATE_TOP + 1)
55 : #define STATE_SRC (STATE_TOP + 2)
56 :
57 : //////////////////////////////////////////////////////////////////////////
58 0 : extern "C" int LinkSequence_startelement_callback(
59 : void *,
60 : int parent,
61 : const char * /*nspace*/,
62 : const char *name,
63 : const char ** )
64 : {
65 0 : if ( name != 0 )
66 : {
67 0 : switch ( parent )
68 : {
69 : case NE_XML_STATEROOT:
70 0 : if ( strcmp( name, "link" ) == 0 )
71 0 : return STATE_LINK;
72 0 : break;
73 :
74 : case STATE_LINK:
75 0 : if ( strcmp( name, "dst" ) == 0 )
76 0 : return STATE_DST;
77 0 : else if ( strcmp( name, "src" ) == 0 )
78 0 : return STATE_SRC;
79 0 : break;
80 : }
81 : }
82 0 : return NE_XML_DECLINE;
83 : }
84 :
85 : //////////////////////////////////////////////////////////////////////////
86 0 : extern "C" int LinkSequence_chardata_callback(
87 : void *userdata,
88 : int state,
89 : const char *buf,
90 : size_t len )
91 : {
92 : LinkSequenceParseContext * pCtx
93 0 : = static_cast< LinkSequenceParseContext * >( userdata );
94 0 : if ( !pCtx->pLink )
95 0 : pCtx->pLink = new ucb::Link;
96 :
97 0 : switch ( state )
98 : {
99 : case STATE_DST:
100 : pCtx->pLink->Destination
101 0 : = rtl::OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
102 0 : pCtx->hasDestination = true;
103 0 : break;
104 :
105 : case STATE_SRC:
106 : pCtx->pLink->Source
107 0 : = rtl::OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
108 0 : pCtx->hasSource = true;
109 0 : break;
110 : }
111 0 : return 0; // zero to continue, non-zero to abort parsing
112 : }
113 :
114 : //////////////////////////////////////////////////////////////////////////
115 0 : extern "C" int LinkSequence_endelement_callback(
116 : void *userdata,
117 : int state,
118 : const char *,
119 : const char * )
120 : {
121 : LinkSequenceParseContext * pCtx
122 0 : = static_cast< LinkSequenceParseContext * >( userdata );
123 0 : if ( !pCtx->pLink )
124 0 : pCtx->pLink = new ucb::Link;
125 :
126 0 : switch ( state )
127 : {
128 : case STATE_LINK:
129 0 : if ( !pCtx->hasDestination || !pCtx->hasSource )
130 0 : return 1; // abort
131 0 : break;
132 : }
133 0 : return 0; // zero to continue, non-zero to abort parsing
134 : }
135 :
136 : //////////////////////////////////////////////////////////////////////////
137 : // static
138 0 : bool LinkSequence::createFromXML( const rtl::OString & rInData,
139 : uno::Sequence< ucb::Link > & rOutData )
140 : {
141 0 : const sal_Int32 TOKEN_LENGTH = 7; // </link>
142 0 : bool success = true;
143 :
144 : // rInData may contain multiple <link>...</link> tags.
145 0 : sal_Int32 nCount = 0;
146 0 : sal_Int32 nStart = 0;
147 0 : sal_Int32 nEnd = rInData.indexOf( "</link>" );
148 0 : while ( nEnd > -1 )
149 : {
150 0 : ne_xml_parser * parser = ne_xml_create();
151 0 : if ( !parser )
152 : {
153 0 : success = false;
154 : break;
155 : }
156 :
157 0 : LinkSequenceParseContext aCtx;
158 : ne_xml_push_handler( parser,
159 : LinkSequence_startelement_callback,
160 : LinkSequence_chardata_callback,
161 : LinkSequence_endelement_callback,
162 0 : &aCtx );
163 :
164 : ne_xml_parse( parser,
165 0 : rInData.getStr() + nStart,
166 0 : nEnd - nStart + TOKEN_LENGTH );
167 :
168 0 : success = !ne_xml_failed( parser );
169 :
170 0 : ne_xml_destroy( parser );
171 :
172 0 : if ( !success )
173 : break;
174 :
175 0 : if ( aCtx.pLink )
176 : {
177 0 : nCount++;
178 0 : if ( nCount > rOutData.getLength() )
179 0 : rOutData.realloc( rOutData.getLength() + 1 );
180 :
181 0 : rOutData[ nCount - 1 ] = *aCtx.pLink;
182 : }
183 :
184 0 : nStart = nEnd + TOKEN_LENGTH;
185 0 : nEnd = rInData.indexOf( "</link>", nStart );
186 0 : }
187 :
188 0 : return success;
189 : }
190 :
191 : //////////////////////////////////////////////////////////////////////////
192 : // static
193 0 : bool LinkSequence::toXML( const uno::Sequence< ucb::Link > & rInData,
194 : rtl::OUString & rOutData )
195 : {
196 : // <link><src>value</src><dst>value</dst></link><link><src>....
197 :
198 0 : sal_Int32 nCount = rInData.getLength();
199 0 : if ( nCount )
200 : {
201 0 : rtl::OUString aPre( "<link><src>" );
202 0 : rtl::OUString aMid( "</src><dst>" );
203 0 : rtl::OUString aEnd( "</dst></link>" );
204 :
205 0 : for ( sal_Int32 n = 0; n < nCount; ++n )
206 : {
207 0 : rOutData += aPre;
208 0 : rOutData += rInData[ n ].Source;
209 0 : rOutData += aMid;
210 0 : rOutData += rInData[ n ].Destination;
211 0 : rOutData += aEnd;
212 : }
213 0 : return true;
214 : }
215 0 : return false;
216 : }
217 :
218 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|