Update ooo320-m1
[ooovba.git] / xml2cmp / source / x2cclass / xml_cdff.cxx
blob2004149bf74dc65476d7535eccedd3bb6d187d9a
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: xml_cdff.cxx,v $
10 * $Revision: 1.3 $
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 #include "xml_cdff.hxx"
33 #include <string.h>
34 #include <tools/stream.hxx>
35 #include "xml_cdim.hxx"
36 #include <ctype.h>
39 typedef ComponentDescriptionImpl::ValueList CdiValueList;
41 class dyn_buffer
43 public:
44 dyn_buffer() : s(0) {}
45 ~dyn_buffer() { if (s) delete [] s; }
46 operator const char *() const { return s; }
47 char * operator->() { return s; }
48 char & operator[](
49 INT32 ix ) { return s[ix]; }
50 void SetSize(
51 INT32 i_size ) { if (s) delete [] s; s = new char [i_size]; }
52 private:
53 char * s;
57 inline BOOL
58 LoadXmlFile( dyn_buffer & o_rBuffer,
59 const UniString & i_sXmlFilePath )
61 BOOL ret = TRUE;
62 SvFileStream aXmlFile;
64 aXmlFile.Open(i_sXmlFilePath, STREAM_READ);
65 if (aXmlFile.GetErrorCode() != FSYS_ERR_OK)
66 ret = FALSE;
67 if (ret)
69 aXmlFile.Seek(STREAM_SEEK_TO_END);
70 INT32 nBufferSize = aXmlFile.Tell();
71 o_rBuffer.SetSize(nBufferSize + 1);
72 o_rBuffer[nBufferSize] = '\0';
73 aXmlFile.Seek(0);
74 if (aXmlFile.Read(o_rBuffer.operator->(), nBufferSize) == 0)
75 ret = FALSE;
78 aXmlFile.Close();
79 return ret;
84 CompDescrsFromAnXmlFile::CompDescrsFromAnXmlFile()
85 : dpDescriptions(new std::vector< ComponentDescriptionImpl* >),
86 eStatus(not_yet_parsed)
88 dpDescriptions->reserve(3);
91 CompDescrsFromAnXmlFile::~CompDescrsFromAnXmlFile()
93 Empty();
94 delete dpDescriptions;
98 BOOL
99 CompDescrsFromAnXmlFile::Parse( const UniString & i_sXmlFilePath )
101 dyn_buffer dpBuffer;
103 if (! LoadXmlFile(dpBuffer,i_sXmlFilePath) )
105 eStatus = cant_read_file;
106 return FALSE;
109 const char * pTokenStart = 0;
110 const char * pBufferPosition = dpBuffer;
111 INT32 nTokenLength = 0;
112 BOOL bWithinElement = FALSE;
114 CdiValueList * pCurTagData = 0;
115 ByteString sStatusValue; // Used only if a <Status ...> tag is found.
118 for ( ComponentDescriptionImpl::ParseUntilStartOfDescription(pBufferPosition);
119 pBufferPosition != 0;
120 ComponentDescriptionImpl::ParseUntilStartOfDescription(pBufferPosition) )
122 ComponentDescriptionImpl * pCurCD = 0;
123 pCurCD = new ComponentDescriptionImpl;
124 dpDescriptions->push_back(pCurCD);
126 for ( ; *pBufferPosition != '\0' && pCurCD != 0; )
128 switch (*pBufferPosition)
130 case '<' :
131 if (! bWithinElement)
133 pCurTagData = pCurCD->GetBeginTag(sStatusValue, pBufferPosition);
134 if (pCurTagData != 0)
136 if (sStatusValue.Len () == 0)
138 // Start new token:
139 pTokenStart = pBufferPosition;
140 nTokenLength = 0;
141 bWithinElement = TRUE;;
143 else
145 // Status tag is already parsed:
146 pCurTagData->push_back(sStatusValue);
147 } // endif (sStatusValue.Length () == 0)
149 else if ( ComponentDescriptionImpl::CheckEndOfDescription(pBufferPosition) )
151 pBufferPosition += ComponentDescriptionImpl::DescriptionEndTagSize();
152 pCurCD = 0;
154 else
156 eStatus = inconsistent_file;
157 return FALSE;
158 } // endif (pCurTagData != 0) elseif() else
160 else if ( pCurTagData->MatchesEndTag(pBufferPosition) )
162 // Finish token:
163 pBufferPosition += pCurTagData->EndTagLength();
164 bWithinElement = FALSE;
166 // Remove leading and trailing spaces:
167 while ( isspace(*pTokenStart) )
169 pTokenStart++;
170 nTokenLength--;
172 while ( nTokenLength > 0
173 && isspace(pTokenStart[nTokenLength-1]) )
175 nTokenLength--;
177 // Add token to tag values list.
178 pCurTagData->push_back(ByteString(pTokenStart,nTokenLength));
180 else
182 nTokenLength++;
183 ++pBufferPosition;
184 } // endif (!bWithinElement) else if () else
185 break;
186 default:
187 if (bWithinElement)
189 ++nTokenLength;
191 ++pBufferPosition;
192 } // end switch
193 } // end for
195 if (bWithinElement)
197 eStatus = inconsistent_file;
198 return FALSE;
200 } // end for
202 return TRUE;
205 INT32
206 CompDescrsFromAnXmlFile::NrOfDescriptions() const
208 return dpDescriptions->size();
211 const ComponentDescription &
212 CompDescrsFromAnXmlFile::operator[](INT32 i_nIndex) const
214 static const ComponentDescriptionImpl aNullDescr_;
215 return 0 <= i_nIndex && i_nIndex < dpDescriptions->size()
216 ? *(*dpDescriptions)[i_nIndex]
217 : aNullDescr_;
220 void
221 CompDescrsFromAnXmlFile::Empty()
223 for ( std::vector< ComponentDescriptionImpl* >::iterator aIter = dpDescriptions->begin();
224 aIter != dpDescriptions->end();
225 ++aIter )
227 delete *aIter;
229 dpDescriptions->erase( dpDescriptions->begin(),
230 dpDescriptions->end() );