1/* 2 testhandler.js - SAX handler for example and tests of minisax.js 3 Copyright (C) 2004-2005 Guido Wesdorp 4 email johnny@debris.demon.nl 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 20 $Id: testhandler.js,v 1.1.1.1 2011/11/09 06:36:04 sungmin Exp $ 21 22*/ 23 24function TestHandler() { 25 /* generates XML from XML 26 27 test code (and an example, although there's a bit much 28 cruft to get the result nice-looking) for the SAX parser 29 */ 30 31 this.startDocument = function() { 32 this.xml = ''; 33 this.namespaces = {}; 34 this.lastns = 0; 35 this.firstelement = ''; 36 }; 37 38 this.startElement = function(namespace, nodename, attributes) { 39 var xml = '<'; 40 if (namespace != '') { 41 var nsname; 42 if (namespace in this.namespaces) { 43 nsname = this.namespaces[namespace]; 44 } else { 45 nsname = this._createNamespaceName(); 46 this.namespaces[namespace] = nsname; 47 }; 48 if (nsname != 'ns0') { 49 xml += nsname + ':'; 50 }; 51 }; 52 xml += nodename; 53 for (anamespace in attributes) { 54 var attrdict = attributes[anamespace]; 55 var nsname = null; 56 if (anamespace != "") { 57 if (anamespace in this.namespaces) { 58 nsname = this.namespaces[anamespace]; 59 } else { 60 nsname = this._createNamespaceName(); 61 this.namespaces[anamespace] = nsname; 62 }; 63 }; 64 for (var aname in attrdict) { 65 xml += ' '; 66 if (nsname) { 67 xml += nsname + ':'; 68 }; 69 xml += aname + '="' + string.entitize(attrdict[aname]) + '"'; 70 }; 71 }; 72 if (this.firstelement == '') { 73 this.firstelement = xml; 74 } else { 75 this.xml += xml + '>'; 76 }; 77 }; 78 79 this.endElement = function(namespace, nodename) { 80 this.xml += '</'; 81 if (namespace) { 82 this.xml += this.namespaces[namespace] + ':'; 83 }; 84 this.xml += nodename + '>'; 85 }; 86 87 this.characters = function(content) { 88 this.xml += string.entitize(content); 89 }; 90 91 this.endDocument = function() { 92 var xml = this.firstelement; 93 for (namespace in this.namespaces) { 94 xml += ' xmlns'; 95 if (this.namespaces[namespace] != 'ns0') { 96 xml += ':' + this.namespaces[namespace]; 97 }; 98 xml += '="' + namespace + '"'; 99 }; 100 xml += '>' + this.xml; 101 this.xml = xml; 102 }; 103 104 this._createNamespaceName = function() { 105 var name = 'ns' + this.lastns; 106 this.lastns++; 107 return name; 108 }; 109}; 110 111TestHandler.prototype = new SAXHandler; 112 113 114