1 /** 2 * @fileOverview 3 * Copyright (c) 2013 Regione Autonoma della Sardegna 4 * Published under the GPL license.<br> 5 * See <a href="https://sardegnageoportale.it/webgis/license.txt">https://sardegnageoportale.it/webgis/license.txt</a> 6 * for the full text of the license. 7 * @version 0.1 8 */ 9 10 /* 11 * This program is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation, either version 3 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program. If not, see <http://www.gnu.org/licenses/>. 23 */ 24 25 /** 26 * @namespace framework.data 27 */ 28 Ext.namespace("framework.data"); 29 30 31 /** 32 * XSL reader for address search service of Regione Sardegna 33 * @class XSL reader for address search service of Regione Sardegna 34 * @name_ GeocoderOpenLS_XLSReader 35 * 36 * @extends Ext.data.XmlReader - See <a href="http://dev.sencha.com/deploy/ext-3.3.1/docs?class=Ext.data.XmlReader">Ext.data.XmlReader</a> 37 * @constructor 38 * @param {object} meta object configuration 39 * @param {Ext.data.Record} recordType object definition 40 */ 41 framework.data.GeocoderOpenLS_XLSReader = function(meta, recordType) { 42 meta = meta || {}; 43 44 45 Ext.applyIf(meta, { 46 idProperty: meta.idProperty || meta.idPath || meta.id, 47 successProperty: meta.successProperty || meta.success 48 }); 49 50 framework.data.GeocoderOpenLS_XLSReader.superclass.constructor.call(this, meta, recordType || meta.fields); 51 }; 52 53 54 55 Ext.extend(framework.data.GeocoderOpenLS_XLSReader, Ext.data.XmlReader, 56 /** 57 * @lends framework.data.GeocoderOpenLS_XLSReader.prototype 58 */ 59 { 60 61 /** This method is only used by a DataProxy which has retrieved data from a remote server. 62 * Create responseXML if the responseText is a XML format 63 * @public 64 * @param {Object} response The XHR object which contains the parsed XML document. 65 * The response is expected to contain a method called 'responseXML' that returns an XML document object. 66 * @returns {Object} records A data block which is used by an {@link Ext.data.Store} as 67 * a cache of Ext.data.Records. 68 */ 69 read : function(response){ 70 if(!response.responseXML){ 71 var text = response.responseText; 72 try { // code for IE 73 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 74 xmlDoc.async = "false"; 75 xmlDoc.loadXML(text); 76 response.responseXML = xmlDoc; 77 }catch(error) { // code for Mozilla, Firefox, Opera, etc. 78 try { 79 var parser = new DOMParser(); 80 var xmlDoc = parser.parseFromString(text,"text/xml"); 81 response.responseXML = xmlDoc; 82 }catch(error) { 83 alert(error.message); 84 return; 85 } 86 } 87 } 88 var doc = response.responseXML; 89 if(!doc) { 90 throw {message: "XmlReader.read: XML Document not available"}; 91 } 92 return this.readRecords(doc); 93 }, 94 95 /** get a content node by a xml tag name 96 * @public 97 * @param {string}format xml reader and writer 98 * @param {string} text html text 99 * @param {string} node xml node 100 * @param {string} tagname tag from where extract data 101 * @param {string} sep separator character 102 * @returns {object} html text 103 */ 104 addOptXlsText: function(format, text, node, tagname, sep) { 105 var elms = format.getElementsByTagNameNS(node, "http://www.opengis.net/xls", tagname); 106 if (elms) { 107 Ext.each(elms, function(elm, index) { 108 var str = format.getChildValue(elm); 109 /* if (str) { 110 text = text + sep + str; 111 }*/ 112 if (tagname === "Place") { 113 var attvaluetype = elm.getAttribute('type'); 114 if (attvaluetype === 'Municipality') 115 text = text + sep + '<b>' + str + '</b>'; 116 else if (attvaluetype === 'CountrySecondarySubdivision') 117 text = text + sep + '(' + str + ')'; 118 } 119 else if (tagname === "Street") { 120 var attvalueon = elm.getAttribute('officialName'); 121 text = text + sep + attvalueon; 122 } 123 }); 124 } 125 126 return text; 127 }, 128 129 /** fill up a object with query result 130 * @public 131 * @param {string} doc xml document 132 * @returns {object} array of record plus search informations 133 */ 134 readRecords: function(doc) { 135 136 this.xmlData = doc; 137 138 var root = doc.documentElement || doc; 139 140 var records = this.extractData(root); 141 142 return { 143 success: true, 144 records: records, 145 totalRecords: records.length 146 }; 147 }, 148 149 /** create array of record extracting data from a xml 150 * @public create records from xml content 151 * @param {string} root xml document 152 * @returns {array} record 153 */ 154 extractData: function(root) { 155 var opts = { 156 /** 157 * Property: namespaces 158 * {Object} Mapping of namespace aliases to namespace URIs. 159 */ 160 namespaces: { 161 gml: "http://www.opengis.net/gml", 162 xls: "http://www.opengis.net/xls" 163 } 164 }; 165 166 var records = []; 167 var format = new OpenLayers.Format.XML(opts); 168 var addresses = format.getElementsByTagNameNS(root, "http://www.opengis.net/xls", 'GeocodedAddress'); 169 170 // Create record for each address 171 var recordType = Ext.data.Record.create([ 172 {name: "lon", type: "number"}, 173 {name: "lat", type: "number"}, 174 "text", 175 "xml", 176 "type" 177 ]); 178 var reader = this; 179 180 Ext.each(addresses, function(address, index) { 181 var pos = format.getElementsByTagNameNS(address, "http://www.opengis.net/gml", 'pos'); 182 var xy = ''; 183 if (pos && pos[0]) { 184 xy = format.getChildValue(pos[0]); 185 } 186 187 var xyArr = xy.split(' '); 188 189 var text = ''; 190 var xml = null; 191 var type = 'municipality'; 192 193 text = reader.addOptXlsText(format, text, address, 'Street', ''); 194 if (text) { 195 type = 'addr'; 196 xml = format.getElementsByTagNameNS(address, "http://www.opengis.net/xls", 'Address'); 197 xml = xml[0].outerHTML; 198 } 199 text = reader.addOptXlsText(format, text, address, 'Place', ' '); 200 201 202 var values = { 203 lon: parseFloat(xyArr[0]), 204 lat: parseFloat(xyArr[1]), 205 text: text, 206 xml: xml, 207 type: type 208 }; 209 var record = new recordType(values, index); 210 records.push(record); 211 }); 212 return records; 213 } 214 }); 215 216