/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /** * XML handling functions. */ /** * Convert a DOM node list to a regular list. */ function nodeList(n) { var l = new Array(); if (isNull(n)) return l; for (var i = 0; i < n.length; i++) l[i] = n[i]; return l; } /** * Append a list of nodes to a parent node. */ function appendNodes(nodes, p) { if (isNull(nodes)) return p; p.appendChild(car(nodes)); return appendNodes(cdr(nodes), p); }; /** * Return the child attributes of an element. */ function childAttributes(e) { return filter(function(n) { return n.nodeType == 2; }, nodeList(e.attributes)); } /** * Return the child elements of an element. */ function childElements(e) { return filter(function(n) { return n.nodeType == 1; }, nodeList(e.childNodes)); } /** * Return the child text nodes of an element. */ function childText(e) { function trim(s) { return s.replace(/^\s*/, '').replace(/\s*$/, ''); } return filter(function(n) { return n.nodeType == 3 && trim(n.nodeValue) != ''; }, nodeList(e.childNodes)); } /** * Read a list of XML attributes. */ function readAttributes(p, a) { if (isNull(a)) return a; var x = car(a); return cons(mklist(attribute, "'" + x.nodeName, x.nodeValue), readAttributes(p, cdr(a))); } /** * Read an XML element. */ function readElement(e, childf) { var l = append(append(mklist(element, "'" + e.nodeName), readAttributes(e, childf(e))), readElements(childElements(e), childf)); var t = childText(e); if (isNull(t)) return l; return append(l, mklist(car(t).nodeValue)); } /** * Read a list of XML elements. */ function readElements(l, childf) { if (isNull(l)) return l; return cons(readElement(car(l), childf), readElements(cdr(l), childf)); } /** * Return true if a list of strings contains an XML document. */ function isXML(l) { if (isNull(l)) return false; return car(l).substring(0, 5) == '\n' + writeXMLDocument(doc) + '\n'); }