1 /** @file oils_dom_element.js
2 * -----------------------------------------------------------------------------
3 * This file holds all of the core OILS DOM elements.
4 * -----------------------------------------------------------------------------
6 * -----------------------------------------------------------------------------
7 * Make sure you load md5.js into your script/html/etc. before loading this
9 * -----------------------------------------------------------------------------
11 * -----------------------------------------------------------------------------
12 * 1. Use these if you are running via xpcshell, but not if you are running
13 * directly from mozilla.
14 * 2. BTW. XMLSerializer doesn't like being run outside of mozilla, so
17 * var DOMParser = new Components.Constructor(
18 * "@mozilla.org/xmlextras/domparser;1", "nsIDOMParser" );
20 * var XMLSerializer = new Components.Constructor(
21 * "@mozilla.org/xmlextras/xmlserializer;1", "nsIDOMSerializer" );
22 * -----------------------------------------------------------------------------
28 * -----------------------------------------------------------------------------
29 * DOM - Top level generic class
30 * -----------------------------------------------------------------------------
39 /** Returns a string representation of the XML doc. If full_bool
40 * is true, it will return the entire doc and not just the relevant
41 * portions (i.e. our object's element).
43 DOM.prototype.toString = function(full_bool) {
45 return new XMLSerializer().serializeToString(this.doc);
47 return new XMLSerializer().serializeToString(this.element);
51 /** Initializes a document and sets this.doc, this.root and this.element */
52 DOM.prototype.init = function( elementName ) {
54 this.doc = new DOMParser().parseFromString(
55 "<?xml version='1.0' encoding='UTF-8'?><oils:root xmlns:oils='http://open-ils.org/namespaces/oils_v1'></oils:root>", "text/xml" );
56 this.root = this.doc.documentElement;
57 this.element = this.doc.createElement( "oils:" + elementName );
61 /** Good for creating objects from raw xml. This builds a new doc and inserts
62 * the node provided. So you can build a dummy object, then call replaceNode
63 * with the new xml to build an object from XML on the fly.
65 DOM.prototype._replaceNode = function( name, new_element ) {
67 var node = this.doc.importNode( new_element, true );
69 if( node.nodeName != "oils:" + name ) {
70 throw new oils_ex_dom( "Invalid Tag to " + name + "::replaceNode()" );
73 this.doc = new DOMParser().parseFromString(
74 "<?xml version='1.0' encoding='UTF-8'?><oils:root xmlns:oils='http://open-ils.org/namespaces/oils_v1'></oils:root>", "text/xml" );
75 this.root = this.doc.documentElement;
77 this.root.appendChild( node );
78 this.element = this.root.firstChild;
84 // -----------------------------------------------------------------------------
86 // -----------------------------------------------------------------------------
88 domainObjectAttr.prototype = new DOM();
89 domainObjectAttr.prototype.constructor = domainObjectAttr;
90 domainObjectAttr.baseClass = DOM.prototype.constructor;
92 /** A domainObjectAttr is a single XML node with 'name' and 'value' attributes */
93 function domainObjectAttr( name, value ) {
95 var node = this.init( "domainObjectAttr" );
96 if( ! name && value ) { return; }
97 node.setAttribute( "name", name );
98 node.setAttribute( "value", value );
99 this.root.appendChild( node );
103 /** Returns the attribute name */
104 domainObjectAttr.prototype.getName = function() {
105 return this.element.getAttribute("name");
108 /** Returns the attribut value. */
109 domainObjectAttr.prototype. getValue = function() {
110 return this.element.getAttribute("value");
113 /** Sets the attribute value. */
114 domainObjectAttr.prototype. setValue = function( value ) {
115 return this.element.setAttribute("value", value );
118 /** Pass in the this.element component of a domainObjectAttr and this will
119 * replace the old element with the new one
121 domainObjectAttr.prototype.replaceNode = function( domainObjectAttr_element ) {
122 return this._replaceNode( "domainObjectAttr", domainObjectAttr_element );
125 // -----------------------------------------------------------------------------
127 // -----------------------------------------------------------------------------
130 userAuth.prototype = new DOM();
131 userAuth.prototype.constructor = userAuth;
132 userAuth.baseClass = DOM.prototype.constructor;
134 function userAuth( username, secret ) {
136 var node = this.init( "userAuth" );
137 if( !( username && secret) ) { return; }
138 node.setAttribute( "username", username );
140 //There is no way to specify the hash seed with the
141 //md5 utility provided
142 var hash = hex_md5( secret );
143 node.setAttribute( "secret", hash );
144 node.setAttribute( "hashseed", "" );
146 this.root.appendChild( node );
149 userAuth.prototype.getUsername = function() {
150 return this.element.getAttribute( "username" );
153 userAuth.prototype.getSecret = function() {
154 return this.element.getAttribute( "secret" );
157 userAuth.prototype.getHashseed = function() {
158 return this.element.getAttribute( "hashseed" );
161 userAuth.prototype.replaceNode = function( userAuth_element ) {
162 return this._replaceNode( "userAuth", userAuth_element );
166 // -----------------------------------------------------------------------------
168 // -----------------------------------------------------------------------------
170 domainObject.prototype = new DOM();
171 domainObject.baseClass = DOM.prototype.constructor;
172 domainObject.prototype.constructor = domainObject;
174 /** Holds the XML for a DomainObject (see oils_domain_object.js or the DomainObject class) */
175 function domainObject( name ) {
176 this._init_domainObject( name );
179 /** Initializes the domainObject XML node */
180 domainObject.prototype._init_domainObject = function( name ) {
182 var node = this.init( "domainObject" );
184 if( ! name ) { return; }
185 node.setAttribute( "name", name );
186 this.root.appendChild( node );
190 /** Pass in any DOM Element or DomainObject and this method will as the
191 * new object as the next child of the root (domainObject) node
193 domainObject.prototype.add = function( new_DOM ) {
195 this.element.appendChild(
196 new_DOM.element.cloneNode( true ) );
199 /** Removes the original domainObject XML node and replaces it with the
200 * one provided. This is useful for building new domainObject's from
201 * raw xml. Just create a new one, then call replaceNode with the new XML.
203 domainObject.prototype.replaceNode = function( domainObject_element ) {
204 return this._replaceNode( "domainObject", domainObject_element );
209 // -----------------------------------------------------------------------------
211 // -----------------------------------------------------------------------------
214 param.prototype = new DOM();
215 param.baseClass = DOM.prototype.constructor;
216 param.prototype.constructor = param;
218 function param( value ) {
219 var node = this.init( "param" );
220 node.appendChild( this.doc.createTextNode( value ) );
221 this.root.appendChild( node );
224 param.prototype.getValue = function() {
225 return this.element.textContent;
228 param.prototype.replaceNode = function( param_element ) {
229 return this._replaceNode( "param", param_element );
233 // -----------------------------------------------------------------------------
234 // domainObjectCollection
235 // -----------------------------------------------------------------------------
237 domainObjectCollection.prototype = new DOM();
238 domainObjectCollection.prototype.constructor =
239 domainObjectCollection;
240 domainObjectCollection.baseClass = DOM.prototype.constructor;
242 function domainObjectCollection( name ) {
243 var node = this.init( "domainObjectCollection" );
244 this.root.appendChild( node );
245 if(name) { this._initCollection( name ); }
248 domainObjectCollection.prototype._initCollection = function( name ) {
250 this.element.setAttribute( "name", name );
254 domainObjectCollection.prototype.add = function( new_domainObject ) {
255 this.element.appendChild(
256 new_domainObject.element.cloneNode( true ) );
259 domainObjectCollection.prototype.replaceNode = function( new_node ) {
260 return this._replaceNode( "domainObjectCollection", new_node );