]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/javascript/opensrf_dom_element.js
Initial revision
[Evergreen.git] / OpenSRF / src / javascript / opensrf_dom_element.js
1 /** @file oils_dom_element.js
2   * -----------------------------------------------------------------------------
3   * This file holds all of the core OILS DOM elements.
4   *  -----------------------------------------------------------------------------
5
6   * -----------------------------------------------------------------------------
7   * Make sure you load md5.js into  your script/html/etc. before loading this 
8   * file
9   * -----------------------------------------------------------------------------
10   * 
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
15   *             toString() fails.
16   * 
17   * var DOMParser = new Components.Constructor(
18   *             "@mozilla.org/xmlextras/domparser;1", "nsIDOMParser" );
19   * 
20   *     var XMLSerializer = new Components.Constructor(
21   *             "@mozilla.org/xmlextras/xmlserializer;1", "nsIDOMSerializer" );
22   *  -----------------------------------------------------------------------------
23   * 
24   * 
25   */
26
27 /**
28   *  -----------------------------------------------------------------------------
29   *  DOM - Top level generic class
30   *  -----------------------------------------------------------------------------
31   */
32
33 function DOM() { 
34         this.doc = null; 
35         this.root = null;
36         this.element = null;
37 }
38
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).
42   */
43 DOM.prototype.toString = function(full_bool) {
44         if( full_bool ) {
45                 return new XMLSerializer().serializeToString(this.doc);
46         } else {
47                 return new XMLSerializer().serializeToString(this.element);
48         }
49 }
50
51 /** Initializes a document and sets this.doc, this.root and this.element */
52 DOM.prototype.init = function( elementName ) {
53         
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 );
58         return this.element;
59 }
60
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.
64   */
65 DOM.prototype._replaceNode = function( name, new_element ) {
66
67         var node = this.doc.importNode( new_element, true );
68
69         if( node.nodeName != "oils:" + name ) {
70                 throw new oils_ex_dom( "Invalid Tag to " + name + "::replaceNode()" );
71         }
72
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;
76
77         this.root.appendChild( node );
78         this.element = this.root.firstChild;
79         return this;
80 }
81
82
83
84 // -----------------------------------------------------------------------------
85 // domainObjectAttr
86 // -----------------------------------------------------------------------------
87
88 domainObjectAttr.prototype      = new DOM();
89 domainObjectAttr.prototype.constructor = domainObjectAttr;
90 domainObjectAttr.baseClass      = DOM.prototype.constructor;
91
92 /** A domainObjectAttr is a single XML node with 'name' and 'value' attributes */
93 function domainObjectAttr( name, value ) {
94
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 );
100 }
101
102
103 /** Returns the attribute name */
104 domainObjectAttr.prototype.getName = function() {
105         return this.element.getAttribute("name"); 
106 }
107
108 /** Returns the attribut value. */
109 domainObjectAttr.prototype. getValue = function() {
110         return this.element.getAttribute("value"); 
111 }
112
113 /** Sets the attribute value. */
114 domainObjectAttr.prototype. setValue = function( value ) {
115         return this.element.setAttribute("value", value ); 
116 }
117
118 /** Pass in the this.element component of a domainObjectAttr and this will
119   * replace the old element with the new one 
120   */
121 domainObjectAttr.prototype.replaceNode = function( domainObjectAttr_element ) {
122         return this._replaceNode( "domainObjectAttr", domainObjectAttr_element );
123 }
124
125 // -----------------------------------------------------------------------------
126 // userAuth
127 // -----------------------------------------------------------------------------
128
129
130 userAuth.prototype = new DOM();
131 userAuth.prototype.constructor = userAuth;
132 userAuth.baseClass = DOM.prototype.constructor;
133
134 function userAuth( username, secret ) {
135
136         var node = this.init( "userAuth" );
137         if( !( username && secret) ) { return; }
138         node.setAttribute( "username", username );
139
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", "" ); 
145
146         this.root.appendChild( node );
147 }
148
149 userAuth.prototype.getUsername = function() {
150         return this.element.getAttribute( "username" );
151 }
152
153 userAuth.prototype.getSecret = function() {
154         return this.element.getAttribute( "secret" );
155 }
156
157 userAuth.prototype.getHashseed = function() {
158         return this.element.getAttribute( "hashseed" );
159 }
160
161 userAuth.prototype.replaceNode = function( userAuth_element ) {
162         return this._replaceNode( "userAuth", userAuth_element );
163 }
164
165
166 // -----------------------------------------------------------------------------
167 // domainObject
168 // -----------------------------------------------------------------------------
169
170 domainObject.prototype = new DOM(); 
171 domainObject.baseClass = DOM.prototype.constructor;
172 domainObject.prototype.constructor = domainObject;
173
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 ); 
177 }
178
179 /** Initializes the domainObject XML node */
180 domainObject.prototype._init_domainObject = function( name ) {
181
182         var node = this.init( "domainObject" );
183
184         if( ! name ) { return; }
185         node.setAttribute( "name", name );
186         this.root.appendChild( node ); 
187
188 }
189
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 
192   */
193 domainObject.prototype.add = function( new_DOM ) {
194
195         this.element.appendChild( 
196                         new_DOM.element.cloneNode( true ) );
197 }
198
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.
202   */
203 domainObject.prototype.replaceNode = function( domainObject_element ) {
204         return this._replaceNode( "domainObject", domainObject_element );
205 }
206
207
208
209 // -----------------------------------------------------------------------------
210 // Param 
211 // -----------------------------------------------------------------------------
212
213
214 param.prototype = new DOM();
215 param.baseClass = DOM.prototype.constructor;
216 param.prototype.constructor = param;
217
218 function param( value ) {
219         var node = this.init( "param" );
220         node.appendChild( this.doc.createTextNode( value ) );
221         this.root.appendChild( node );
222 }
223
224 param.prototype.getValue = function() {
225         return this.element.textContent;
226 }
227
228 param.prototype.replaceNode = function( param_element ) {
229         return this._replaceNode( "param", param_element );
230 }
231
232
233 // -----------------------------------------------------------------------------
234 // domainObjectCollection
235 // -----------------------------------------------------------------------------
236
237 domainObjectCollection.prototype = new DOM();
238 domainObjectCollection.prototype.constructor = 
239         domainObjectCollection;
240 domainObjectCollection.baseClass = DOM.prototype.constructor;
241
242 function domainObjectCollection( name ) {
243         var node = this.init( "domainObjectCollection" );       
244         this.root.appendChild( node );
245         if(name) { this._initCollection( name ); }
246 }
247
248 domainObjectCollection.prototype._initCollection = function( name ) {
249         if( name ) {
250                 this.element.setAttribute( "name", name );
251         }
252 }
253
254 domainObjectCollection.prototype.add = function( new_domainObject ) {
255         this.element.appendChild( 
256                         new_domainObject.element.cloneNode( true ) );
257 }
258
259 domainObjectCollection.prototype.replaceNode = function( new_node ) {
260         return this._replaceNode( "domainObjectCollection", new_node );
261 }
262
263