/* * 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. */ /** * 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) { return filter(function(n) { return n.nodeType == 3 && n.nodeValue.trim().length != 0; }, nodeList(e.childNodes)); } /** * Read a list of XML attributes. */ function readAttributes(a) { if (isNull(a)) return a; var x = car(a); return cons(mklist(attribute, "'" + x.name, x.value), readAttributes(cdr(a))); } /** * Read an XML element. */ function readElement(e, childf) { var l = append(append(mklist(element, "'" + e.nodeName), readAttributes(childf(e))), readElements(childElements(e), childf)); var t = childText(e); if (isNull(t)) return l; var tv = reduce(function(a, n) { return a + n.nodeValue; }, '', t); return append(l, mklist(tv)); } /** * 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' + car(writeXMLDocument(doc)) + '\n'); }