| 1 | /*************************************************************************** |
| 2 | propertymanager2.cpp - description |
| 3 | ------------------- |
| 4 | begin : january 28th, 2007 |
| 5 | copyright : (C) 2007-2008 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: propertymanager2.cpp 375 2008-10-28 14:47:15Z neoneurone $ |
| 9 | ***************************************************************************/ |
| 10 | |
| 11 | /*************************************************************************** |
| 12 | * * |
| 13 | * This program is free software; you can redistribute it and/or modify * |
| 14 | * it under the terms of the GNU General Public License as published by * |
| 15 | * the Free Software Foundation; either version 2 of the License, or * |
| 16 | * any later version. * |
| 17 | * * |
| 18 | ***************************************************************************/ |
| 19 | |
| 20 | // OpenCity headers |
| 21 | #include "propertymanager2.h" |
| 22 | |
| 23 | // Libraries headers |
| 24 | #include "xpath_processor.h" |
| 25 | #include "xpath_static.h" |
| 26 | |
| 27 | // Local defines |
| 28 | #define OC_METADATA_XML_FILE "config/graphism.xml" |
| 29 | #define OC_METADATA_FILE_NODE "//graphism/file" |
| 30 | |
| 31 | |
| 32 | using namespace TinyXPath; |
| 33 | |
| 34 | |
| 35 | /*=====================================================================*/ |
| 36 | PropertyManager2::PropertyManager2() |
| 37 | { |
| 38 | OPENCITY_DEBUG( "ctor" ); |
| 39 | |
| 40 | TiXmlDocument fileList; // The graphism file list (in XML) |
| 41 | |
| 42 | // Now try to open the config file then read it |
| 43 | OPENCITY_INFO( |
| 44 | "Reading XML metadata file: \"" << ocConfigDirPrefix(OC_METADATA_XML_FILE) << "\"" |
| 45 | ); |
| 46 | |
| 47 | // Load the XML metadata file list |
| 48 | string fn = ocConfigDirPrefix(OC_METADATA_XML_FILE); |
| 49 | if (!fileList.LoadFile(fn) || fileList.Error()) { |
| 50 | OPENCITY_FATAL( fileList.ErrorDesc() ); |
| 51 | abort(); |
| 52 | } |
| 53 | |
| 54 | // Get the root element |
| 55 | TiXmlNode* pRoot = fileList.RootElement(); |
| 56 | if (pRoot == NULL) { |
| 57 | OPENCITY_FATAL( fileList.ErrorDesc() ); |
| 58 | abort(); |
| 59 | } |
| 60 | |
| 61 | // Select all the "//graphism/file" nodes |
| 62 | uint nbFile = 0; |
| 63 | xpath_processor* xpProcessor = new xpath_processor(pRoot, OC_METADATA_FILE_NODE); |
| 64 | assert( xpProcessor != NULL ); |
| 65 | nbFile = xpProcessor->u_compute_xpath_node_set(); |
| 66 | assert( nbFile > 0 ); |
| 67 | OPENCITY_DEBUG( "Number of files expected: " << nbFile ); |
| 68 | |
| 69 | // Allocate memory to store the property array |
| 70 | _uiNumberProperty = nbFile; |
| 71 | _aProperty = new Property* [_uiNumberProperty]; |
| 72 | |
| 73 | // FOR each <file> node DO |
| 74 | uint i = 0; |
| 75 | string strAc = string(".ac"); |
| 76 | string strXml = string(".xml"); |
| 77 | string fileGraphism, fileXml; |
| 78 | string::size_type pos; |
| 79 | TiXmlElement* pElement = NULL; |
| 80 | for (i = 0; i < nbFile; i++) { |
| 81 | pElement = (xpProcessor->XNp_get_xpath_node(i))->ToElement(); |
| 82 | fileGraphism = pElement->GetText(); |
| 83 | fileXml = fileGraphism; |
| 84 | |
| 85 | // Replace ".ac" by ".xml" |
| 86 | pos = fileXml.rfind( strAc ); |
| 87 | if (pos != fileXml.npos) { |
| 88 | fileXml.replace( pos, strXml.size(), strXml ); |
| 89 | } |
| 90 | |
| 91 | // Load the properties at the index i |
| 92 | if (_mapIndex.find(fileGraphism) != _mapIndex.end()) { |
| 93 | OPENCITY_DEBUG( |
| 94 | "XML file \"" << fileGraphism << |
| 95 | "\" already exists at : " << _mapIndex[fileGraphism] |
| 96 | ); |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | _aProperty[i] = Property::LoadProperties( ocDataDirPrefix(fileXml) ); |
| 101 | _mapIndex[fileGraphism] = i; |
| 102 | } |
| 103 | |
| 104 | // Clean up |
| 105 | delete xpProcessor; |
| 106 | xpProcessor = NULL; // Safe |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /*=====================================================================*/ |
| 111 | PropertyManager2::~PropertyManager2() |
| 112 | { |
| 113 | OPENCITY_DEBUG( "dtor" ); |
| 114 | |
| 115 | // Delete the whole property array |
| 116 | delete [] _aProperty; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /*=====================================================================*/ |
| 121 | const uint |
| 122 | PropertyManager2::GetNumberProperty() const |
| 123 | { |
| 124 | return _uiNumberProperty; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /*=====================================================================*/ |
| 129 | const Property* const |
| 130 | PropertyManager2::Get(const string& key) const |
| 131 | { |
| 132 | std::map<string, uint>::const_iterator cit = _mapIndex.find(key); |
| 133 | return cit != _mapIndex.end() ? _aProperty[cit->second] : NULL; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /*=====================================================================*/ |
| 138 | const Property* const |
| 139 | PropertyManager2::Get(const uint index) const |
| 140 | { |
| 141 | return _aProperty[index]; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 |