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: xml_cdff.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 #include "xml_cdff.hxx"
34 #include <tools/stream.hxx>
35 #include "xml_cdim.hxx"
39 typedef ComponentDescriptionImpl::ValueList CdiValueList
;
44 dyn_buffer() : s(0) {}
45 ~dyn_buffer() { if (s
) delete [] s
; }
46 operator const char *() const { return s
; }
47 char * operator->() { return s
; }
49 INT32 ix
) { return s
[ix
]; }
51 INT32 i_size
) { if (s
) delete [] s
; s
= new char [i_size
]; }
58 LoadXmlFile( dyn_buffer
& o_rBuffer
,
59 const UniString
& i_sXmlFilePath
)
62 SvFileStream aXmlFile
;
64 aXmlFile
.Open(i_sXmlFilePath
, STREAM_READ
);
65 if (aXmlFile
.GetErrorCode() != FSYS_ERR_OK
)
69 aXmlFile
.Seek(STREAM_SEEK_TO_END
);
70 INT32 nBufferSize
= aXmlFile
.Tell();
71 o_rBuffer
.SetSize(nBufferSize
+ 1);
72 o_rBuffer
[nBufferSize
] = '\0';
74 if (aXmlFile
.Read(o_rBuffer
.operator->(), nBufferSize
) == 0)
84 CompDescrsFromAnXmlFile::CompDescrsFromAnXmlFile()
85 : dpDescriptions(new std::vector
< ComponentDescriptionImpl
* >),
86 eStatus(not_yet_parsed
)
88 dpDescriptions
->reserve(3);
91 CompDescrsFromAnXmlFile::~CompDescrsFromAnXmlFile()
94 delete dpDescriptions
;
99 CompDescrsFromAnXmlFile::Parse( const UniString
& i_sXmlFilePath
)
103 if (! LoadXmlFile(dpBuffer
,i_sXmlFilePath
) )
105 eStatus
= cant_read_file
;
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
)
131 if (! bWithinElement
)
133 pCurTagData
= pCurCD
->GetBeginTag(sStatusValue
, pBufferPosition
);
134 if (pCurTagData
!= 0)
136 if (sStatusValue
.Len () == 0)
139 pTokenStart
= pBufferPosition
;
141 bWithinElement
= TRUE
;;
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();
156 eStatus
= inconsistent_file
;
158 } // endif (pCurTagData != 0) elseif() else
160 else if ( pCurTagData
->MatchesEndTag(pBufferPosition
) )
163 pBufferPosition
+= pCurTagData
->EndTagLength();
164 bWithinElement
= FALSE
;
166 // Remove leading and trailing spaces:
167 while ( isspace(*pTokenStart
) )
172 while ( nTokenLength
> 0
173 && isspace(pTokenStart
[nTokenLength
-1]) )
177 // Add token to tag values list.
178 pCurTagData
->push_back(ByteString(pTokenStart
,nTokenLength
));
184 } // endif (!bWithinElement) else if () else
197 eStatus
= inconsistent_file
;
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
]
221 CompDescrsFromAnXmlFile::Empty()
223 for ( std::vector
< ComponentDescriptionImpl
* >::iterator aIter
= dpDescriptions
->begin();
224 aIter
!= dpDescriptions
->end();
229 dpDescriptions
->erase( dpDescriptions
->begin(),
230 dpDescriptions
->end() );