1 // The Antville Project 2 // http://code.google.com/p/antville 3 // 4 // Copyright 2007-2011 by Tobi Schäfer. 5 // 6 // Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer, 7 // Matthias & Michael Platzer, Christoph Lincke. 8 // 9 // Licensed under the Apache License, Version 2.0 (the ``License''); 10 // you may not use this file except in compliance with the License. 11 // You may obtain a copy of the License at 12 // 13 // http://www.apache.org/licenses/LICENSE-2.0 14 // 15 // Unless required by applicable law or agreed to in writing, software 16 // distributed under the License is distributed on an ``AS IS'' BASIS, 17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 // See the License for the specific language governing permissions and 19 // limitations under the License. 20 // 21 // $Revision$ 22 // $LastChangedBy$ 23 // $LastChangedDate$ 24 // $URL$ 25 26 /** 27 * @fileOverview Defines metadata extensions of Helma�s built-in 28 * HopObject prototype. 29 */ 30 31 /** 32 * 33 // * @param {String} name 34 */ 35 HopObject.prototype.handleMetadata = function(name) { 36 this.__defineGetter__(name, function() { 37 return this.getMetadata(name); 38 }); 39 this.__defineSetter__(name, function(value) { 40 return this.setMetadata(name, value); 41 }); 42 this[name + "_macro"] = function(param) { 43 var value; 44 if (value = this[name]) { 45 res.write(value); 46 } 47 return; 48 }; 49 return; 50 } 51 52 /** 53 * 54 * @param {String} name 55 * @returns {Object} 56 */ 57 HopObject.prototype.getMetadata = function(name) { 58 if (!this.metadata) { 59 throw Error("No metadata collection defined for prototype " + this.constructor.name); 60 } else { 61 this.metadata.prefetchChildren(); 62 } 63 64 var self = this; 65 66 if (!name) { 67 var result = {}; 68 this.metadata.forEach(function() { 69 var name = this.name; 70 if (name) { 71 result[name] = self.getMetadata(name); 72 } 73 }); 74 return result; 75 } 76 77 var meta = this.metadata.get(name); 78 if (!meta) { 79 return null; 80 } 81 82 return meta.getValue(); 83 } 84 85 /** 86 * 87 * @param {String} name 88 * @param {Object} value 89 */ 90 HopObject.prototype.setMetadata = function(name, value) { 91 if (!this.metadata) { 92 throw Error("No metadata collection defined for prototype " + this.constructor.name); 93 } 94 95 if (!name) { 96 throw Error("Insufficient arguments"); 97 } 98 99 if (typeof name === "object") { 100 for (var i in name) { 101 this.setMetadata(i, name[i]); 102 } 103 return; 104 } 105 106 var metadata = this.metadata.get(name); 107 108 if (metadata) { 109 metadata.setValue(value); 110 } else { 111 metadata = new Metadata(this, name, value); 112 if (metadata.value !== null) { 113 // metadata.persist() is not enough or there will be redundant records! 114 this.metadata.add(metadata); 115 } 116 } 117 return; 118 } 119 120 /** 121 * 122 * @param {String} name 123 */ 124 HopObject.prototype.deleteMetadata = function(name) { 125 if (!this.metadata) { 126 throw Error("No metadata collection defined for prototype " + this.constructor.name); 127 } 128 129 var self = this; 130 131 if (arguments.length === 0) { 132 return HopObject.remove.call(this.metadata); 133 } 134 135 Array.prototype.forEach.call(arguments, function(name) { 136 var metadata = self.metadata.get(name); 137 metadata && metadata.remove(); 138 return; 139 }); 140 return; 141 } 142 143 /** 144 * 145 * @param {Object} param 146 * @param {String} name 147 */ 148 HopObject.prototype.metadata_macro = function(param, name) { 149 var value = this.getMetadata(name); 150 value && res.write(value); 151 return; 152 } 153