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 the Metadata prototype (version 2).
 28  */
 29 
 30 /**
 31  * FIXME: This could be useful when defining prototypes with the definePrototype() method.
 32  * @see http://helma.org/wiki/Defining+HopObject+mappings+programmatically/
 33  */
 34 Metadata.getTypeProperties = function() {
 35    return {
 36       collection: "Metadata",
 37       "local.1": "$id",
 38       "foreign.1": "parent_id",
 39       "local.2": "$prototype",
 40       "foreign.2": "parent_type",
 41       accessName: "name"
 42    }
 43 }
 44 
 45 Metadata.normalize = function(value) {
 46    if (value === null || value === undefined) {
 47       return [null, null];
 48    }
 49 
 50    if (!value.constructor) {
 51       value = String(value);
 52    }
 53 
 54    var Constructor = value.constructor;
 55    switch (Constructor) {
 56       case Boolean:
 57       case String:
 58       case Number:
 59       value = String(value);
 60       break;
 61 
 62       case Date:
 63       value = Number(value);
 64       break;
 65 
 66       default:
 67       value = value.toSource();
 68    }
 69    return [value, Constructor.name];
 70 }
 71 
 72 Metadata.prototype.constructor = function(parent, name, value) {
 73    if (parent && name && value) {
 74       this.parent = parent;
 75       this.name = name;
 76       this.setValue(value);
 77    }
 78    return this;
 79 }
 80 
 81 Metadata.prototype.setValue = function(value) {
 82    [this.value, this.type] = Metadata.normalize(value);
 83    if (this.value === null) {
 84       this.remove();
 85    }
 86    return;
 87 }
 88 
 89 Metadata.prototype.getValue = function() {
 90    var Constructor = global[this.type];
 91    switch (Constructor) {
 92       case null:
 93       case undefined:
 94       return null;
 95 
 96       case Boolean:
 97       return eval(this.value).valueOf();
 98 
 99       case Date:
100       return new Date(Number(this.value));
101 
102       case Number:
103       case String:
104       return (new Constructor(this.value)).valueOf();
105 
106       default:
107       return eval(this.value);
108    }
109 }
110 
111 /**
112  * 
113  */
114 Metadata.prototype.toString = function() {
115    return "Metadata of " + this.parent + " (" + this.name + " = " + this.value + ")";
116 }
117 
118 /**
119  *
120  * @param {String} name
121  * @returns {HopObject}
122  */
123 // FIXME: Is this obsolete?
124 Metadata.prototype.onUnhandledMacro = function(name) {
125    return this.get(name);
126 }
127