ソースを参照

added p5WFS_GetFeatureFromXmlString, updated js Promise

Piotr Labudda 7 年 前
コミット
66c083f723
1 ファイル変更19 行追加27 行削除
  1. 19 27
      SE/static/p5WFS/GetFeature.js

+ 19 - 27
SE/static/p5WFS/GetFeature.js

@@ -179,7 +179,7 @@ function p5WFS_GetFeature(typeName, query) { // @returns Promise
 	}
 	var method = (postData) ? 'POST' : 'GET'
 
-	return httpFetch(link, Object.assign({
+	var wfsPromise = httpFetch(link, Object.assign({
 		method: method,
 		credentials: 'include',
 	}, ('POST' === method)
@@ -189,45 +189,36 @@ function p5WFS_GetFeature(typeName, query) { // @returns Promise
 		: {}
 	)).then(function (response) {
 		return response.text()
-	}).then(function (responseText) {
-		return (new DOMParser()).parseFromString(responseText, "text/xml");
-	}).then(function (xmlResponse) {
-		if (!xmlResponse.children.length) throw "Missing response xml root element"
-		return xmlResponse.children[0]
-	}).then(function (xmlRoot) {
-		if ('html' === xmlRoot.localName) {
-			console.log(xmlRoot) // html / body / parseerror / div
-			throw "Parse xml error"
-		}
-		if ('FeatureCollection' !== xmlRoot.localName) {
-			if ('ServiceExceptionReport' === xmlRoot.tagName) {
-				if (xmlRoot.children[0] && xmlRoot.children[0].textContent) throw xmlRoot.children[0].textContent
-				throw "WFS API Exception"
-			}
-			throw "Missing FeatureCollection as root element in wfs response"
-		}
-		var features = []
-		for (var i = 0; i < xmlRoot.children.length; i++) {
-			features.push(p5WFS_ParseFeatureMember(xmlRoot.children[i]))
-		}
-		return features.filter(function (item) { return !!item; })
-	})
+	});
+	return _p5WFS_GetFeaturePromise(wfsPromise);
 }
 
 function p5WFS_GetFeatureFromXml(url) {
-	return httpFetch(url, {
+	var wfsPromise = httpFetch(url, {
 		method: 'GET',
 		credentials: 'include',
 	}).then(function (response) {
 		return response.text()
-	}).then(function (responseText) {
+	});
+	return _p5WFS_GetFeaturePromise(wfsPromise);
+}
+
+function p5WFS_GetFeatureFromXmlString(xmlString) {
+	var wfsPromise = new Promise(function (resolve, reject) {
+		resolve(xmlString);
+	});
+	return _p5WFS_GetFeaturePromise(wfsPromise);
+}
+
+function _p5WFS_GetFeaturePromise(wfsPromise) {
+	return wfsPromise.then(function (responseText) {
 		return (new DOMParser()).parseFromString(responseText, "text/xml");
 	}).then(function (xmlResponse) {
 		if (!xmlResponse.children.length) throw "Missing response xml root element"
 		return xmlResponse.children[0]
 	}).then(function (xmlRoot) {
 		if ('html' === xmlRoot.localName) {
-			console.log(xmlRoot) // html / body / parseerror / div
+			// console.log(xmlRoot) // html / body / parseerror / div
 			throw "Parse xml error"
 		}
 		if ('FeatureCollection' !== xmlRoot.localName) {
@@ -250,4 +241,5 @@ module.exports = {
 	p5WFS_ParseFeatureFieldRecurse: p5WFS_ParseFeatureFieldRecurse,
 	p5WFS_GetFeature: p5WFS_GetFeature,
 	p5WFS_GetFeatureFromXml: p5WFS_GetFeatureFromXml,
+	p5WFS_GetFeatureFromXmlString: p5WFS_GetFeatureFromXmlString,
 }