summaryrefslogtreecommitdiffstats
path: root/sandbox/sebastien/cpp/apr-2/modules/js/htdocs/atomutil.js
blob: 6b998dceda68dec6d524516366b65ece7fe8b17c (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
/*
 * 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.    
 */

/**
 * ATOM data conversion functions.
 */
var atom = new Object();

/**
 * Convert a list of elements to a list of values representing an ATOM entry.
 */
atom.entryElementsToValues = function(e) {
    var lt = filter(selector(mklist(element, "'title")), e);
    var t = isNil(lt)? '' : elementValue(car(lt));
    var li = filter(selector(mklist(element, "'id")), e);
    var i = isNil(li)? '' : elementValue(car(li));
    var lc = filter(selector(mklist(element, "'content")), e);
    return mklist(t, i, elementValue(car(lc)));
};

/**
 * Convert a list of elements to a list of values representing ATOM entries.
 */
atom.entriesElementsToValues = function(e) {
    if (isNil(e))
        return e;
    return cons(atom.entryElementsToValues(car(e)), atom.entriesElementsToValues(cdr(e)));
};

/**
 * Convert a list of strings to a list of values representing an ATOM entry.
 */
atom.readATOMEntry = function(l) {
    var e = readXML(l);
    if (isNil(e))
        return mklist();
    return atom.entryElementsToValues(car(e));
};

/**
 * Convert a list of values representy an ATOM entry to a value.
 */
atom.entryValue = function(e) {
    var v = elementsToValues(mklist(caddr(e)));
    return cons(car(e), (cadr(e), cdr(car(v))));
};

/**
 * Return true if a list of strings represents an ATOM feed.
 */
atom.isATOMFeed = function(l) {
    if (!isXML(l))
        return false;
    return car(l).match('<feed') != null && car(l).match('="http://www.w3.org/2005/Atom"') != null;
};

/**
 * Convert a DOM document to a list of values representing an ATOM feed.
 */
atom.readATOMFeedDocument = function(doc) {
    var f = readXMLDocument(doc);
    if (isNil(f))
        return mklist();
    var t = filter(selector(mklist(element, "'title")), car(f));
    var i = filter(selector(mklist(element, "'id")), car(f));
    var e = filter(selector(mklist(element, "'entry")), car(f));
    if (isNil(e))
        return mklist(elementValue(car(t)), elementValue(car(i)));
    return cons(elementValue(car(t)), cons(elementValue(car(i)), atom.entriesElementsToValues(e)));
};

/**
 * Convert a list of strings to a list of values representing an ATOM feed.
 */
atom.readATOMFeed = function(l) {
    return atom.readAtomFeedDocument(parseXML(l));
};

/**
 * Convert an ATOM feed containing elements to an ATOM feed containing values.
 */
atom.feedValues = function(e) {
    function feedValuesLoop(e) {
        if (isNil(e))
            return e;
        return cons(entryValue(car(e)), feedValuesLoop(cdr(e)));
    }

    return cons(car(e), cons(cadr(e), feedValuesLoop(cddr(e))));
};

/**
 * Convert a list of values representy an ATOM entry to a list of elements.
 */
atom.entryElement = function(l) {
    return mklist(element, "'entry", mklist(attribute, "'xmlns", "http://www.w3.org/2005/Atom"),
            mklist(element, "'title", mklist(attribute, "'type", "text"), car(l)),
            mklist(element, "'id", cadr(l)),
            mklist(element, "'content", mklist(attribute, "'type", (isList(caddr(l))? "application/xml" : "text")), caddr(l)),
            mklist(element, "'link", mklist(attribute, "'href", cadr(l))));
};

/**
 * Convert a list of values representing ATOM entries to a list of elements.
 */
atom.entriesElements = function(l) {
    if (isNil(l))
        return l;
    return cons(atom.entryElement(car(l)), atom.entriesElements(cdr(l)));
};

/**
 * Convert a list of values representing an ATOM entry to an ATOM entry.
 */
atom.writeATOMEntry = function(l) {
    return writeXML(mklist(atom.entryElement(l)), true);
};

/**
 * Convert a list of values representing an ATOM feed to an ATOM feed.
 */
atom.writeATOMFeed = function(l) {
    var f = mklist(element, "'feed", mklist(attribute, "'xmlns", "http://www.w3.org/2005/Atom"),
            mklist(element, "'title", mklist(attribute, "'type", "text"), car(l)),
            mklist(element, "'id", cadr(l)));
    if (isNil(cddr(l)))
        return writeXML(mklist(f), true);
    var fe = append(f, atom.entriesElements(cddr(l)));
    return writeXML(mklist(fe), true);
};

/**
 * Convert an ATOM entry containing a value to an ATOM entry containing an item element.
 */
atom.entryValuesToElements = function(v) {
    if (isList(caddr(v)))
        return cons(car(v), cons(cadr(v), valuesToElements(mklist(cons("'item", caddr(v))))));
    return cons(car(v), cons(cadr(v), valuesToElements(mklist(mklist("'item", caddr(v))))));
};

/**
 * Convert an ATOM feed containing values to an ATOM feed containing elements.
 */
atom.feedValuesToElements = function(v) {
    function feedValuesToElementsLoop(v) {
        if (isNil(v))
            return v;
        return cons(atom.entryValuesToElements(car(v)), feedValuesToElementsLoop(cdr(v)));
    }

    return cons(car(v), cons(cadr(v), feedValuesToElementsLoop(cddr(v))));
};