~ubuntu-branches/ubuntu/natty/steam/natty

« back to all changes in this revision

Viewing changes to server/libraries/Slotter.pmod/Composer.pike

  • Committer: Bazaar Package Importer
  • Author(s): Alain Schroeder
  • Date: 2005-05-14 16:33:35 UTC
  • Revision ID: james.westby@ubuntu.com-20050514163335-5v7lbxibmlww15dx
Tags: upstream-1.6.3
ImportĀ upstreamĀ versionĀ 1.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2004  Thomas Bopp, Thorsten Hampel, Ludger Merkens
 
2
 *
 
3
 *  This program is free software; you can redistribute it and/or modify
 
4
 *  it under the terms of the GNU General Public License as published by
 
5
 *  the Free Software Foundation; either version 2 of the License, or
 
6
 *  (at your option) any later version.
 
7
 *
 
8
 *  This program is distributed in the hope that it will be useful,
 
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *  GNU General Public License for more details.
 
12
 *
 
13
 *  You should have received a copy of the GNU General Public License
 
14
 *  along with this program; if not, write to the Free Software
 
15
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
16
 * 
 
17
 * $Id: Composer.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $
 
18
 */
 
19
 
 
20
constant cvs_version="$Id: Composer.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $";
 
21
 
 
22
#include <database.h>
 
23
 
 
24
Slotter.Slot oRootSlot;
 
25
array(string) Stylesheets=({});
 
26
array(string) JavaScripts=({});
 
27
array(string) Meta=({});
 
28
 
 
29
//mapping(string:Slotter.Slot) allSlots;
 
30
 
 
31
int iSID;
 
32
 
 
33
Slotter.Slot get_root_slot()
 
34
{
 
35
    if (!objectp(oRootSlot))
 
36
        oRootSlot = Slotter.Slot();
 
37
    return oRootSlot;
 
38
}
 
39
 
 
40
/**
 
41
 *  traverses the slots and inserts tree and calculates
 
42
 *  the html pieces "generated" by the visited inserts
 
43
 *  as a sideeffect each insert is asked for stylesheets it may need,
 
44
 *  to collect information for the header generation
 
45
 *
 
46
 *  @param array subparts - an array as returned from an insert generator
 
47
 *  @param string sFunction - the generator to call
 
48
 *                             "generate" - the standard html generator
 
49
 *                             "preview"  - a debugging generator
 
50
 *
 
51
 *  @result an array of strings, which can be flattened with
 
52
 *  @see flatten_tree
 
53
 *  @see compose_header
 
54
 *
 
55
 *  @author Ludger Merkens
 
56
 */
 
57
array build_tree(array subparts, string sFunction)
 
58
{
 
59
    Slotter.Insert currInsert;
 
60
    Slotter.Slot   currSlot;
 
61
 
 
62
    for(int i;i<sizeof(subparts); i++)
 
63
    {
 
64
        if (objectp( currSlot = subparts[i]))
 
65
        {
 
66
            //            string name= currSlot->get_path_slot_name();
 
67
            //            allSlots[name] = currSlot;
 
68
            currInsert = currSlot->get_insert();
 
69
            if (currInsert)
 
70
            {
 
71
                if (!currInsert[sFunction])
 
72
                    throw( ({sprintf("Missing ("+sFunction+") in %O\n",
 
73
                                     currInsert), backtrace()}));
 
74
                array temp = currInsert[sFunction]();
 
75
                if (!temp)
 
76
                    throw(({ sprintf("No Result from "+
 
77
                                    sFunction+" in %s\n",
 
78
                                    master()->describe_program(
 
79
                                        object_program(currInsert))) }));
 
80
                if (!temp || search(temp,0)!=-1)
 
81
                    throw(({sprintf("Empty Subparts returned %O\n",currInsert),
 
82
                          backtrace()}));
 
83
                subparts[i] = build_tree(temp, sFunction);
 
84
                Stylesheets |= currInsert->need_style_sheets();
 
85
                JavaScripts |= currInsert->need_java_scripts();
 
86
                Meta |= currInsert->need_meta();
 
87
            }
 
88
            else
 
89
                subparts[i]= "<td>empty</td>";
 
90
        }
 
91
    }
 
92
    return subparts;
 
93
}
 
94
 
 
95
/**
 
96
 * take a tree of strings as resulted from build_tree and glue them
 
97
 * together to a flat string
 
98
 * @param array(mixed) tree - the string tree to flatten
 
99
 * @result string
 
100
 *
 
101
 * @author Ludger Merkens
 
102
 * 
 
103
 */
 
104
string flatten_tree(array(mixed) tree)
 
105
{
 
106
    string out="";
 
107
    foreach(tree, mixed leave)
 
108
    {
 
109
        if (arrayp(leave))
 
110
            out += flatten_tree(leave);
 
111
        else
 
112
            out += leave;
 
113
    }
 
114
    return out;
 
115
}
 
116
 
 
117
/**
 
118
 * compose the collected header information durin "build_tree" to a
 
119
 * header
 
120
 *
 
121
 * @author Ludger Merkens
 
122
 */
 
123
string compose_header() {
 
124
 
 
125
    /*    werror(sprintf("composing header with Meta:%O, Stylesheets:%O and Javascripts:%O\n",
 
126
          Meta, Stylesheets, JavaScripts));*/
 
127
    return "<head>"+
 
128
        "<meta http-equiv=\"Content-Type\" content=\"text/html; "+
 
129
        "charset=UTF-8\">\n"+
 
130
        (sizeof(Meta) ? Meta * "\n" : "")+
 
131
        (sizeof(Stylesheets) ?
 
132
         "  <link rel=\"stylesheet\" href=\""+
 
133
         (Stylesheets*"\">\n  <link rel=\"stylesheet\" href=\"")+
 
134
         "\">\n" : "")+
 
135
        (sizeof(JavaScripts) ?
 
136
         "  <SCRIPT LANGUAGE=\"JavaScript\" SRC=\"" +
 
137
         (JavaScripts*"\" TYPE=\"text/javascript\"></SCRIPT>\n <SCRIPT LANGUAGE=\"JavaScript\" SRC=\"")+
 
138
         "\" TYPE=\"text/javascript\"></SCRIPT>\n" : "")+
 
139
        "</head>\n";
 
140
}
 
141
 
 
142
/**
 
143
 *  run a "generate" composing run
 
144
 *  @author Ludger Merkens
 
145
 */ 
 
146
string compose()
 
147
{
 
148
    Stylesheets = ({});
 
149
    JavaScripts = ({});
 
150
    //    allSlots = ([]);
 
151
    
 
152
    array t = build_tree(({oRootSlot}), "generate");
 
153
    return
 
154
        "<html>"+
 
155
        compose_header() +
 
156
        "<body>"+
 
157
        flatten_tree(t)+
 
158
        "</body>"+
 
159
        "</html>";
 
160
}
 
161
 
 
162
 
 
163
/**
 
164
 * run the "preview" composing run
 
165
 * @author Ludger Merkens
 
166
 */ 
 
167
string compose_preview()
 
168
{
 
169
    Stylesheets = ({});
 
170
    //    allSlots = ([]);
 
171
    array t = build_tree(({oRootSlot}), "preview");
 
172
    return flatten_tree(t);
 
173
}
 
174
 
 
175
/**
 
176
 * during the generation process a mapping of inserts is kept,
 
177
 * thus the results of this function depend on and refer to the
 
178
 * last run of such a generation process.
 
179
 *
 
180
 * @param   string       - pathname, the Slot to search
 
181
 * @returns Slotter.Slot - the designate Slot
 
182
 *
 
183
 * @author Ludger Merkens
 
184
 */
 
185
// Slotter.Slot get_slot_by_name(string pathname)
 
186
// {
 
187
//     return allSlots[pathname];
 
188
// }
 
189
 
 
190
 
 
191
/**
 
192
 * untility function called from e.g. the sTeamHTMLdisplay insert to
 
193
 * read the content of a sTeam Document. Due to this, an insert may be
 
194
 * lightweight and dosn't need to be a sTeam object.
 
195
 *
 
196
 * @param object o - a proxy
 
197
 * @author Ludger Merkens
 
198
 */
 
199
string read_content(object o)
 
200
{
 
201
    return string_to_utf8(o->get_content());
 
202
}
 
203
 
 
204
/**
 
205
 * another utility function, allowig access to get_inventory
 
206
 * @param o - container to access
 
207
 * @param cl - class to filter by.
 
208
 */
 
209
array(object) read_inventory(object o, void|int cl)
 
210
{
 
211
    if (cl)
 
212
        return o->get_inventory_by_class(cl);
 
213
    else
 
214
        return o->get_inventory();
 
215
}
 
216
 
 
217
/**
 
218
 * retreive a steam insert by oid or pathname
 
219
 * @param string insert - a pathname passed to _FILEPATH
 
220
 *        int    insert - an OID
 
221
 * @returns the insert (not the proxy)
 
222
 * @author Ludger Merkens
 
223
 */
 
224
Slotter.Insert get_steam_insert(string|int insert)
 
225
{
 
226
    object oInsert;
 
227
    if (stringp(insert))
 
228
        oInsert = _FILEPATH->path_to_object(insert);
 
229
    else
 
230
        oInsert = find_object(insert);
 
231
 
 
232
    if (objectp(oInsert))
 
233
        return oInsert->get_object();
 
234
    return 0;
 
235
}
 
236
 
 
237
object call_factory(object factory, mapping createInfo)
 
238
{
 
239
    return factory->execute(createInfo);
 
240
}
 
241
 
 
242
mixed delegate_set_attribute(object target, mixed key, mixed value)
 
243
{
 
244
    //    werror(sprintf("delegating: %O, %O, %O\n", target, key, value));
 
245
    return target->set_attribute(key, value);
 
246
}
 
247
 
 
248
 
 
249
mixed delegate_sanction_object(object target, object obj, mixed data)
 
250
{
 
251
    return target->sanction_object(obj, data);
 
252
}
 
253
 
 
254
mixed delegate_set_acquire_attribute(object target, object obj1, mixed v,
 
255
                                     object obj2)
 
256
{
 
257
    return target->set_acquire_attribute(obj1, v, obj2);
 
258
}
 
259
 
 
260
mixed delegate_group_add_member(object group, object member)
 
261
{
 
262
    return group->add_member(member);
 
263
}
 
264
 
 
265
mixed delegate_group_remove_member(object group, object member)
 
266
{
 
267
    return group->remove_member(member);
 
268
}
 
269
 
 
270
string callName()
 
271
{
 
272
    string path = _FILEPATH->object_to_path(this_object());
 
273
 
 
274
    string server = _Server->get_config("web_server");
 
275
    string port = _Server->get_config("web_port_https");
 
276
    
 
277
    if (!path)
 
278
        return _Server->get_server_url_administration()+
 
279
            "/scripts/execute.pike?script="+this_object()->get_object_id();
 
280
    else
 
281
        return _Server->get_server_url_administration()+
 
282
            combine_path(path, this_object()->get_identifier());
 
283
}
 
284
 
 
285
 
 
286