summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/modules/js/htdocs/xmlutil.js
blob: c8878bf2afb062e2ae1bab9bca6fa938f17b461a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * 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) == '<?xml';
}

/**
 * Parse a list of strings representing an XML document.
 */
var xmlParser = new DOMParser();
function parseXML(l) {
    var s = writeStrings(l);
    return xmlParser.parseFromString(s, "text/xml");
}

/**
 * Read a list of values from an XML document.
 */
function readXMLDocument(doc) {
    var root = childElements(doc);
    if (isNull(root))
        return nil;
    return mklist(readElement(car(root), childAttributes));
}

/**
 * Read a list of values from an XHTML element.
 */
function readXHTMLElement(xhtml) {
    return mklist(readElement(xhtml, childAttributes));
}

/**
 * Read a list of values from a list of strings representing an XML document.
 */
function readXML(l) {
    return readXMLDocument(parseXML(l));
}

/**
 * Return a list of strings representing an XML document.
 */
var xmlSerializer = new XMLSerializer();
function writeXMLDocument(doc) {
    return mklist(xmlSerializer.serializeToString(doc));
}

/**
 * Write a list of XML element and attribute tokens.
 */
function expandElementValues(n, l) {
    if (isNull(l))
        return l;
    return cons(cons(element, cons(n, car(l))), expandElementValues(n, cdr(l)));
}

function writeList(l, node, doc) {
    if (isNull(l))
        return node;

    var token = car(l);
    if (isTaggedList(token, attribute)) {
        if (isMSIE()) {
            var aname = attributeName(token).substring(1);
            if (aname != 'xmlns')
                node.setAttribute(aname, '' + attributeValue(token));
        } else
            node.setAttribute(attributeName(token).substring(1), '' + attributeValue(token));

    } else if (isTaggedList(token, element)) {

        function mkelem(tok, doc) {
            function xmlns(l) {
                if (isNull(l))
                    return null;
                var t = car(l);
                if (isTaggedList(t, attribute)) {
                    if (attributeName(t).substring(1) == 'xmlns')
                        return attributeValue(t);
                }
                return xmlns(cdr(l));
            }

            var ns = xmlns(elementChildren(tok));
            if (isMSIE())
                return doc.createElementNS(ns != null? ns : node.namespaceURI, elementName(tok).substring(1));
            if (ns == null)
                return doc.createElement(elementName(tok).substring(1));
            return doc.createElementNS(ns, elementName(tok).substring(1));
        }

        if (elementHasValue(token)) {
            var v = elementValue(token);
            if (isList(v)) {
                var e = expandElementValues(elementName(token), v);
                writeList(e, node, doc);
            } else {
                var child = mkelem(token, doc);
                writeList(elementChildren(token), child, doc);
                node.appendChild(child);
            }
        } else {
            var child = mkelem(token, doc);
            writeList(elementChildren(token), child, doc);
            node.appendChild(child);
        }
    } else
        node.appendChild(doc.createTextNode('' + token));

    writeList(cdr(l), node, doc);
    return node;
}

/**
 * Make a new XML document.
 */
function mkXMLDocument() { 
    return document.implementation.createDocument('', '', null); 
}

/**
 * Convert a list of values to a list of strings representing an XML document.
 */
function writeXML(l, xmlTag) {
    var doc = mkXMLDocument();
    writeList(l, doc, doc);
    if (!xmlTag)
        return writeXMLDocument(doc);
    return mklist('<?xml version="1.0" encoding="UTF-8"?>\n' + car(writeXMLDocument(doc)) + '\n');
}