Files
openlayers/src/ol/format/XML.js
Frederic Junod 484f45fc1b Move readFromDocument implementation to the base class
All the child classes have the same code; move to the base class
2020-04-07 07:26:38 +02:00

55 lines
1.1 KiB
JavaScript

/**
* @module ol/format/XML
*/
import {isDocument, parse} from '../xml.js';
/**
* @classdesc
* Generic format for reading non-feature XML data
*
* @abstract
*/
class XML {
/**
* Read the source document.
*
* @param {Document|Element|string} source The XML source.
* @return {Object} An object representing the source.
* @api
*/
read(source) {
if (!source) {
return null;
} else if (typeof source === 'string') {
const doc = parse(source);
return this.readFromDocument(doc);
} else if (isDocument(source)) {
return this.readFromDocument(/** @type {Document} */ (source));
} else {
return this.readFromNode(/** @type {Element} */ (source));
}
}
/**
* @param {Document} doc Document.
* @return {Object} Object
*/
readFromDocument(doc) {
for (let n = doc.firstChild; n; n = n.nextSibling) {
if (n.nodeType == Node.ELEMENT_NODE) {
return this.readFromNode(/** @type {Element} */ (n));
}
}
return null;
}
/**
* @abstract
* @param {Element} node Node.
* @return {Object} Object
*/
readFromNode(node) {}
}
export default XML;