1 /* 2 * Copyright 2006 Sony Computer Entertainment Inc. 3 * 4 * Licensed under the MIT Open Source License, for details please see license.txt or the website 5 * http://www.opensource.org/licenses/mit-license.php 6 * 7 */ 8 9 #include <sstream> 10 #include <dae.h> 11 #include <dae/daeIDRef.h> 12 #include <dae/daeDatabase.h> 13 #include <dae/daeErrorHandler.h> 14 15 using namespace std; 16 17 void 18 daeIDRef::initialize() 19 { 20 id = ""; 21 container = NULL; 22 } 23 24 daeIDRef::daeIDRef() 25 { 26 initialize(); 27 } 28 29 daeIDRef::daeIDRef(daeString IDRefString) 30 { 31 initialize(); 32 setID(IDRefString); 33 } 34 35 daeIDRef::daeIDRef(const daeIDRef& copyFrom_) 36 { 37 initialize(); 38 copyFrom(copyFrom_); 39 } 40 41 daeIDRef::daeIDRef(daeElement& container) { 42 initialize(); 43 setContainer(&container); 44 } 45 46 47 void 48 daeIDRef::reset() 49 { 50 setID(""); 51 } 52 53 bool daeIDRef::operator==(const daeIDRef& other) const { 54 return (!strcmp(other.getID(), getID())); 55 } 56 57 daeIDRef &daeIDRef::operator=( const daeIDRef& other) { 58 if (!container) 59 container = other.container; 60 id = other.getID(); 61 return *this; 62 } 63 64 daeString 65 daeIDRef::getID() const 66 { 67 return id.c_str(); 68 } 69 70 void 71 daeIDRef::setID(daeString _IDString) 72 { 73 id = _IDString ? _IDString : ""; 74 } 75 76 daeElement* daeIDRef::getElement() const { 77 if (container) 78 return container->getDAE()->getIDRefResolvers().resolveElement(id, container->getDocument()); 79 return NULL; 80 } 81 82 daeElement* daeIDRef::getContainer() const { 83 return(container); 84 } 85 86 void daeIDRef::setContainer(daeElement* cont) { 87 container = cont; 88 } 89 90 void 91 daeIDRef::print() 92 { 93 fprintf(stderr,"id = %s\n",id.c_str()); 94 fflush(stderr); 95 } 96 97 // These methods are provided for backward compatibility only. 98 void daeIDRef::validate() { } 99 100 void daeIDRef::resolveElement( daeString ) { } 101 102 void daeIDRef::resolveID() { } 103 104 daeIDRef &daeIDRef::get( daeUInt idx ) { 105 (void)idx; 106 return *this; 107 } 108 109 size_t daeIDRef::getCount() const { 110 return 1; 111 } 112 113 daeIDRef& daeIDRef::operator[](size_t index) { 114 (void)index; 115 return *this; 116 } 117 118 void 119 daeIDRef::copyFrom(const daeIDRef& copyFrom) { 120 *this = copyFrom; 121 } 122 123 daeIDRef::ResolveState daeIDRef::getState() const { 124 if (id.empty()) 125 return id_empty; 126 if (getElement()) 127 return id_success; 128 return id_failed_id_not_found; 129 } 130 131 132 daeIDRefResolver::daeIDRefResolver(DAE& dae) : dae(&dae) { } 133 134 daeIDRefResolver::~daeIDRefResolver() { } 135 136 137 daeDefaultIDRefResolver::daeDefaultIDRefResolver(DAE& dae) : daeIDRefResolver(dae) { } 138 139 daeDefaultIDRefResolver::~daeDefaultIDRefResolver() { } 140 141 daeString 142 daeDefaultIDRefResolver::getName() 143 { 144 return "DefaultIDRefResolver"; 145 } 146 147 daeElement* daeDefaultIDRefResolver::resolveElement(const string& id, daeDocument* doc) { 148 return doc ? dae->getDatabase()->idLookup(id, doc) : NULL; 149 } 150 151 152 daeIDRefResolverList::daeIDRefResolverList() { } 153 154 daeIDRefResolverList::~daeIDRefResolverList() { 155 for (size_t i = 0; i < resolvers.getCount(); i++) 156 delete resolvers[i]; 157 } 158 159 void daeIDRefResolverList::addResolver(daeIDRefResolver* resolver) { 160 resolvers.append(resolver); 161 } 162 163 void daeIDRefResolverList::removeResolver(daeIDRefResolver* resolver) { 164 resolvers.remove(resolver); 165 } 166 167 daeElement* daeIDRefResolverList::resolveElement(const string& id, daeDocument* doc) { 168 for(size_t i = 0; i < resolvers.getCount(); i++) 169 if (daeElement* el = resolvers[i]->resolveElement(id, doc)) 170 return el; 171 return NULL; 172 } 173