1*ab595ff6SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ab595ff6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ab595ff6SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ab595ff6SAndrew Rist * distributed with this work for additional information 6*ab595ff6SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ab595ff6SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ab595ff6SAndrew Rist * "License"); you may not use this file except in compliance 9*ab595ff6SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*ab595ff6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*ab595ff6SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ab595ff6SAndrew Rist * software distributed under the License is distributed on an 15*ab595ff6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ab595ff6SAndrew Rist * KIND, either express or implied. See the License for the 17*ab595ff6SAndrew Rist * specific language governing permissions and limitations 18*ab595ff6SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*ab595ff6SAndrew Rist *************************************************************/ 21*ab595ff6SAndrew Rist 22*ab595ff6SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "xml_cdff.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <string.h> 27cdf0e10cSrcweir #include <tools/stream.hxx> 28cdf0e10cSrcweir #include "xml_cdim.hxx" 29cdf0e10cSrcweir #include <ctype.h> 30cdf0e10cSrcweir 31cdf0e10cSrcweir 32cdf0e10cSrcweir typedef ComponentDescriptionImpl::ValueList CdiValueList; 33cdf0e10cSrcweir 34cdf0e10cSrcweir class dyn_buffer 35cdf0e10cSrcweir { 36cdf0e10cSrcweir public: 37cdf0e10cSrcweir dyn_buffer() : s(0) {} 38cdf0e10cSrcweir ~dyn_buffer() { if (s) delete [] s; } 39cdf0e10cSrcweir operator const char *() const { return s; } 40cdf0e10cSrcweir char * operator->() { return s; } 41cdf0e10cSrcweir char & operator[]( 42cdf0e10cSrcweir INT32 ix ) { return s[ix]; } 43cdf0e10cSrcweir void SetSize( 44cdf0e10cSrcweir INT32 i_size ) { if (s) delete [] s; s = new char [i_size]; } 45cdf0e10cSrcweir private: 46cdf0e10cSrcweir char * s; 47cdf0e10cSrcweir }; 48cdf0e10cSrcweir 49cdf0e10cSrcweir 50cdf0e10cSrcweir inline BOOL 51cdf0e10cSrcweir LoadXmlFile( dyn_buffer & o_rBuffer, 52cdf0e10cSrcweir const UniString & i_sXmlFilePath ) 53cdf0e10cSrcweir { 54cdf0e10cSrcweir BOOL ret = TRUE; 55cdf0e10cSrcweir SvFileStream aXmlFile; 56cdf0e10cSrcweir 57cdf0e10cSrcweir aXmlFile.Open(i_sXmlFilePath, STREAM_READ); 58cdf0e10cSrcweir if (aXmlFile.GetErrorCode() != FSYS_ERR_OK) 59cdf0e10cSrcweir ret = FALSE; 60cdf0e10cSrcweir if (ret) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir aXmlFile.Seek(STREAM_SEEK_TO_END); 63cdf0e10cSrcweir INT32 nBufferSize = aXmlFile.Tell(); 64cdf0e10cSrcweir o_rBuffer.SetSize(nBufferSize + 1); 65cdf0e10cSrcweir o_rBuffer[nBufferSize] = '\0'; 66cdf0e10cSrcweir aXmlFile.Seek(0); 67cdf0e10cSrcweir if (aXmlFile.Read(o_rBuffer.operator->(), nBufferSize) == 0) 68cdf0e10cSrcweir ret = FALSE; 69cdf0e10cSrcweir } 70cdf0e10cSrcweir 71cdf0e10cSrcweir aXmlFile.Close(); 72cdf0e10cSrcweir return ret; 73cdf0e10cSrcweir } 74cdf0e10cSrcweir 75cdf0e10cSrcweir 76cdf0e10cSrcweir 77cdf0e10cSrcweir CompDescrsFromAnXmlFile::CompDescrsFromAnXmlFile() 78cdf0e10cSrcweir : dpDescriptions(new std::vector< ComponentDescriptionImpl* >), 79cdf0e10cSrcweir eStatus(not_yet_parsed) 80cdf0e10cSrcweir { 81cdf0e10cSrcweir dpDescriptions->reserve(3); 82cdf0e10cSrcweir } 83cdf0e10cSrcweir 84cdf0e10cSrcweir CompDescrsFromAnXmlFile::~CompDescrsFromAnXmlFile() 85cdf0e10cSrcweir { 86cdf0e10cSrcweir Empty(); 87cdf0e10cSrcweir delete dpDescriptions; 88cdf0e10cSrcweir } 89cdf0e10cSrcweir 90cdf0e10cSrcweir 91cdf0e10cSrcweir BOOL 92cdf0e10cSrcweir CompDescrsFromAnXmlFile::Parse( const UniString & i_sXmlFilePath ) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir dyn_buffer dpBuffer; 95cdf0e10cSrcweir 96cdf0e10cSrcweir if (! LoadXmlFile(dpBuffer,i_sXmlFilePath) ) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir eStatus = cant_read_file; 99cdf0e10cSrcweir return FALSE; 100cdf0e10cSrcweir } 101cdf0e10cSrcweir 102cdf0e10cSrcweir const char * pTokenStart = 0; 103cdf0e10cSrcweir const char * pBufferPosition = dpBuffer; 104cdf0e10cSrcweir INT32 nTokenLength = 0; 105cdf0e10cSrcweir BOOL bWithinElement = FALSE; 106cdf0e10cSrcweir 107cdf0e10cSrcweir CdiValueList * pCurTagData = 0; 108cdf0e10cSrcweir ByteString sStatusValue; // Used only if a <Status ...> tag is found. 109cdf0e10cSrcweir 110cdf0e10cSrcweir 111cdf0e10cSrcweir for ( ComponentDescriptionImpl::ParseUntilStartOfDescription(pBufferPosition); 112cdf0e10cSrcweir pBufferPosition != 0; 113cdf0e10cSrcweir ComponentDescriptionImpl::ParseUntilStartOfDescription(pBufferPosition) ) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir ComponentDescriptionImpl * pCurCD = 0; 116cdf0e10cSrcweir pCurCD = new ComponentDescriptionImpl; 117cdf0e10cSrcweir dpDescriptions->push_back(pCurCD); 118cdf0e10cSrcweir 119cdf0e10cSrcweir for ( ; *pBufferPosition != '\0' && pCurCD != 0; ) 120cdf0e10cSrcweir { 121cdf0e10cSrcweir switch (*pBufferPosition) 122cdf0e10cSrcweir { 123cdf0e10cSrcweir case '<' : 124cdf0e10cSrcweir if (! bWithinElement) 125cdf0e10cSrcweir { 126cdf0e10cSrcweir pCurTagData = pCurCD->GetBeginTag(sStatusValue, pBufferPosition); 127cdf0e10cSrcweir if (pCurTagData != 0) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir if (sStatusValue.Len () == 0) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir // Start new token: 132cdf0e10cSrcweir pTokenStart = pBufferPosition; 133cdf0e10cSrcweir nTokenLength = 0; 134cdf0e10cSrcweir bWithinElement = TRUE;; 135cdf0e10cSrcweir } 136cdf0e10cSrcweir else 137cdf0e10cSrcweir { 138cdf0e10cSrcweir // Status tag is already parsed: 139cdf0e10cSrcweir pCurTagData->push_back(sStatusValue); 140cdf0e10cSrcweir } // endif (sStatusValue.Length () == 0) 141cdf0e10cSrcweir } 142cdf0e10cSrcweir else if ( ComponentDescriptionImpl::CheckEndOfDescription(pBufferPosition) ) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir pBufferPosition += ComponentDescriptionImpl::DescriptionEndTagSize(); 145cdf0e10cSrcweir pCurCD = 0; 146cdf0e10cSrcweir } 147cdf0e10cSrcweir else 148cdf0e10cSrcweir { 149cdf0e10cSrcweir eStatus = inconsistent_file; 150cdf0e10cSrcweir return FALSE; 151cdf0e10cSrcweir } // endif (pCurTagData != 0) elseif() else 152cdf0e10cSrcweir } 153cdf0e10cSrcweir else if ( pCurTagData->MatchesEndTag(pBufferPosition) ) 154cdf0e10cSrcweir { 155cdf0e10cSrcweir // Finish token: 156cdf0e10cSrcweir pBufferPosition += pCurTagData->EndTagLength(); 157cdf0e10cSrcweir bWithinElement = FALSE; 158cdf0e10cSrcweir 159cdf0e10cSrcweir // Remove leading and trailing spaces: 160cdf0e10cSrcweir while ( isspace(*pTokenStart) ) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir pTokenStart++; 163cdf0e10cSrcweir nTokenLength--; 164cdf0e10cSrcweir } 165cdf0e10cSrcweir while ( nTokenLength > 0 166cdf0e10cSrcweir && isspace(pTokenStart[nTokenLength-1]) ) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir nTokenLength--; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir // Add token to tag values list. 171cdf0e10cSrcweir pCurTagData->push_back(ByteString(pTokenStart,nTokenLength)); 172cdf0e10cSrcweir } 173cdf0e10cSrcweir else 174cdf0e10cSrcweir { 175cdf0e10cSrcweir nTokenLength++; 176cdf0e10cSrcweir ++pBufferPosition; 177cdf0e10cSrcweir } // endif (!bWithinElement) else if () else 178cdf0e10cSrcweir break; 179cdf0e10cSrcweir default: 180cdf0e10cSrcweir if (bWithinElement) 181cdf0e10cSrcweir { 182cdf0e10cSrcweir ++nTokenLength; 183cdf0e10cSrcweir } 184cdf0e10cSrcweir ++pBufferPosition; 185cdf0e10cSrcweir } // end switch 186cdf0e10cSrcweir } // end for 187cdf0e10cSrcweir 188cdf0e10cSrcweir if (bWithinElement) 189cdf0e10cSrcweir { 190cdf0e10cSrcweir eStatus = inconsistent_file; 191cdf0e10cSrcweir return FALSE; 192cdf0e10cSrcweir } 193cdf0e10cSrcweir } // end for 194cdf0e10cSrcweir 195cdf0e10cSrcweir return TRUE; 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir INT32 199cdf0e10cSrcweir CompDescrsFromAnXmlFile::NrOfDescriptions() const 200cdf0e10cSrcweir { 201cdf0e10cSrcweir return dpDescriptions->size(); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir const ComponentDescription & 205cdf0e10cSrcweir CompDescrsFromAnXmlFile::operator[](INT32 i_nIndex) const 206cdf0e10cSrcweir { 207cdf0e10cSrcweir static const ComponentDescriptionImpl aNullDescr_; 208cdf0e10cSrcweir return 0 <= i_nIndex && i_nIndex < dpDescriptions->size() 209cdf0e10cSrcweir ? *(*dpDescriptions)[i_nIndex] 210cdf0e10cSrcweir : aNullDescr_; 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213cdf0e10cSrcweir void 214cdf0e10cSrcweir CompDescrsFromAnXmlFile::Empty() 215cdf0e10cSrcweir { 216cdf0e10cSrcweir for ( std::vector< ComponentDescriptionImpl* >::iterator aIter = dpDescriptions->begin(); 217cdf0e10cSrcweir aIter != dpDescriptions->end(); 218cdf0e10cSrcweir ++aIter ) 219cdf0e10cSrcweir { 220cdf0e10cSrcweir delete *aIter; 221cdf0e10cSrcweir } 222cdf0e10cSrcweir dpDescriptions->erase( dpDescriptions->begin(), 223cdf0e10cSrcweir dpDescriptions->end() ); 224cdf0e10cSrcweir } 225cdf0e10cSrcweir 226cdf0e10cSrcweir 227cdf0e10cSrcweir 228