comparison stanza.js @ 108:5cb4733c5189

many api changes
author Sonny Piers <sonny@fastmail.net>
date Fri, 13 Jul 2012 15:26:18 +0200
parents 704ce44c1a22
children
comparison
equal deleted inserted replaced
107:704ce44c1a22 108:5cb4733c5189
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 (function() {
20
21 if (typeof define !== 'undefined') {
22 define(function() {
23 return {
24 'stanza': Stanza,
25 'presence': Presence,
26 'iq': IQ,
27 'message': Message,
28 'doc': doc,
29 };
30 });
31 }
32 else {
33 Lightstirng.Stanza = Stanza;
34 Lightstirng.Presence = Presence;
35 Lightstirng.IQ = IQ;
36 Lightstirng.Message = Message;
37 Lightstirng.doc = doc;
38 }
39
40 /**
41 * @private
42 */
43 var doc = document.implementation.createDocument(null, 'dummy', null);
44 /**
45 * @private
46 */
47 var parser = new DOMParser();
48 /**
49 * @private
50 */
51 var serializer = new XMLSerializer();
52 /**
53 * @function Transforms a XML string to a DOM object.
54 * @param {String} aString XML string.
55 * @return {Object} Domified XML.
56 */
57 var parse = function(aString) {
58 var el = parser.parseFromString(aString, 'text/xml').documentElement;
59 if (el.tagName === 'parsererror')
60 ;//do something
61 return el;
62 };
63 /**
64 * @function Transforms a DOM object to a XML string.
65 * @param {Object} aString DOM object.
66 * @return {String} Stringified DOM.
67 */
68 var serialize = function(aElement) {
69 var string = null;
70 try {
71 string = serializer.serializeToString(aElement);
72 }
73 catch (e) {
74 //TODO: error
75 }
76 finally {
77 return string;
78 };
79 };
80
19 81
20 /** 82 /**
21 * @constructor Creates a new Stanza object. 83 * @constructor Creates a new Stanza object.
22 * @param {String|Object} [aStanza] The XML or DOM content of the stanza 84 * @param {String|Object} [aStanza] The XML or DOM content of the stanza
23 * @memberOf Lightstring 85 */
24 */ 86 var Stanza = function(aStanza) {
25 Lightstring.Stanza = function(aStanza) { 87 this.createEl(aStanza);
26 if (typeof aStanza === 'string') 88 };
27 this.el = Lightstring.parse(aStanza); 89 /**
90 * @constructor Creates a new Message stanza object.
91 * @param {String|Object} [aStanza] The XML or DOM content of the stanza
92 */
93 var Message = function(aStanza) {
94 if ((typeof aStanza === 'object') && (!(aStanza instanceof Element)))
95 aStanza.name = 'message';
96 this.createEl(aStanza);
97 };
98 Message.prototype = Stanza.prototype;
99 /**
100 * @constructor Creates a new IQ stanza object.
101 * @param {String|Object} [aStanza] The XML or DOM content of the stanza
102 */
103 var IQ = function(aStanza) {
104 if ((typeof aStanza === 'object') && (!(aStanza instanceof Element)))
105 aStanza.name = 'iq';
106 this.createEl(aStanza);
107 };
108 IQ.prototype = Stanza.prototype;
109 /**
110 * @constructor Creates a new Presence stanza object.
111 * @param {String|Object} [aStanza] The XML or DOM content of the stanza
112 */
113 var Presence = function(aStanza) {
114 if ((typeof aStanza === 'object') && (!(aStanza instanceof Element)))
115 aStanza.name = 'presence';
116 this.createEl(aStanza);
117 };
118 Presence.prototype = Stanza.prototype;
119 Stanza.prototype.createEl = function(aStanza) {
120 if (typeof aStanza === 'string') {
121 this.el = parse(aStanza);
122 }
28 else if (aStanza instanceof Element) 123 else if (aStanza instanceof Element)
29 this.el = aStanza; 124 this.el = aStanza;
125 else if (typeof aStanza === 'object') {
126 var el = doc.createElement(aStanza.name);
127 this.el = el;
128 delete aStanza.name;
129 for (var i in aStanza) {
130 this[i] = aStanza[i];
131 }
132 }
30 else 133 else
31 this.el = null;//TODO error 134 this.el = null;//TODO error
32 }; 135 };
33 Lightstring.Stanza.prototype.toString = function() { 136 Stanza.prototype.toString = function() {
34 return Lightstring.serialize(this.el); 137 return serialize(this.el);
35 }; 138 };
36 139 Stanza.prototype.reply = function(aProps) {
37 Object.defineProperty(Lightstring.Stanza.prototype, "from", { 140 var props = aProps || {};
141
142 props.name = this.name;
143 var reply = new Stanza(props);
144
145 if (this.from)
146 reply.to = this.from;
147
148
149 if (reply.name !== 'iq')
150 return reply;
151
152 if (this.id)
153 reply.id = this.id;
154
155 reply.type = 'result';
156
157 return reply;
158 };
159
160 //from attribute
161 Object.defineProperty(Stanza.prototype, "from", {
38 get : function(){ 162 get : function(){
39 return this.el.getAttribute('from'); 163 return this.el.getAttribute('from');
40 }, 164 },
165 set : function(aString) {
166 this.el.setAttribute('from', aString);
167 },
168 enumerable : true,
169 configurable : true
170 });
171 //stanza tag name
172 Object.defineProperty(Stanza.prototype, "name", {
173 get : function(){
174 return this.el.localName;
175 },
176 //FIXME
41 // set : function(newValue){ bValue = newValue; }, 177 // set : function(newValue){ bValue = newValue; },
42 enumerable : true, 178 enumerable : true,
43 configurable : true 179 configurable : true
44 }); 180 });
45 Object.defineProperty(Lightstring.Stanza.prototype, "name", { 181 //id attribute
46 get : function(){ 182 Object.defineProperty(Stanza.prototype, "id", {
47 return this.el.localName;
48 },
49 // set : function(newValue){ bValue = newValue; },
50 enumerable : true,
51 configurable : true
52 });
53 Object.defineProperty(Lightstring.Stanza.prototype, "id", {
54 get : function(){ 183 get : function(){
55 return this.el.getAttribute('id'); 184 return this.el.getAttribute('id');
56 }, 185 },
57 // set : function(newValue){ bValue = newValue; }, 186 set : function(aString) {
58 enumerable : true, 187 this.el.setAttribute('id', aString);
59 configurable : true 188 },
60 }); 189 enumerable : true,
61 Object.defineProperty(Lightstring.Stanza.prototype, "to", { 190 configurable : true
191 });
192 //to attribute
193 Object.defineProperty(Stanza.prototype, "to", {
62 get : function(){ 194 get : function(){
63 return this.el.getAttribute('to'); 195 return this.el.getAttribute('to');
64 }, 196 },
65 // set : function(newValue){ bValue = newValue; }, 197 set : function(aString) {
66 enumerable : true, 198 this.el.setAttribute('to', aString);
67 configurable : true 199 },
68 }); 200 enumerable : true,
201 configurable : true
202 });
203 //type attribute
204 Object.defineProperty(Stanza.prototype, "type", {
205 get : function(){
206 return this.el.getAttribute('type');
207 },
208 set : function(aString) {
209 this.el.setAttribute('type', aString);
210 },
211 enumerable : true,
212 configurable : true
213 });
214 //body
215 Object.defineProperty(Stanza.prototype, "body", {
216 get : function(){
217 var bodyEl = this.el.querySelector('body').textContent;
218 if (!bodyEl)
219 return null;
220 else
221 return bodyEl.textContent;
222 },
223 set : function(aString) {
224 var bodyEl = this.el.querySelector('body');
225 if (!bodyEl) {
226 bodyEl = doc.createElement('body');
227 bodyEl = this.el.appendChild(bodyEl);
228 }
229 bodyEl.textContent = aString;
230 },
231 enumerable : true,
232 configurable : true
233 });
234 //subject
235 Object.defineProperty(Stanza.prototype, "subject", {
236 get : function(){
237 var subjectEl = this.el.querySelector('subject').textContent;
238 if (!subjectEl)
239 return null;
240 else
241 return subjectEl.textContent;
242 },
243 set : function(aString) {
244 var subjectEl = this.el.querySelector('subject');
245 if (!subjectEl) {
246 subjectEl = doc.createElement('subject');
247 subjectEl = this.el.appendChild(subjectEl);
248 }
249 subjectEl.textContent = aString;
250 },
251 enumerable : true,
252 configurable : true
253 });
254 Stanza.prototype.replyWithSubscribed = function(aProps) {
255 var reply = this.reply(aProps);
256 reply.type = 'subscribed';
257
258 return reply;
259 };
260
261 })();