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
  
//! Things that belong elsewhere but can't lie there for various silly 
//! reasons. Everything here is considered internal and not part of 
//! the RXML.pmod API. 
//! 
//! E.g. one reason is to avoid circular references in the parser 
//! objects when the callbacks are defined in them. 
//! 
//! Created 2000-01-21 by Martin Stjernholm 
//! 
//! $Id: utils.pmod,v 1.22 2001/05/19 05:35:15 mast Exp $ 
 
constant short_format_length = 40; 
 
final string format_short (mixed val) 
// This one belongs somewhere else.. 
{ 
  string res = ""; 
 
  void format_val (mixed val) 
  { 
    if (arrayp (val) || multisetp (val)) { 
      string end; 
      if (multisetp (val)) res += "(<", end = ">)", val = indices (val); 
      else res += "({", end = "})"; 
      if (sizeof (res) >= short_format_length) throw (0); 
      for (int i = 0; i < sizeof (val);) { 
        res += format_val (val[i]); 
        if (++i < sizeof (val) - 1) res += ", "; 
        if (sizeof (res) >= short_format_length) throw (0); 
      } 
      res += end; 
    } 
    else if (mappingp (val)) { 
      res += "(["; 
      if (sizeof (res) >= short_format_length) throw (0); 
      array ind = sort (indices (val)); 
      for (int i = 0; i < sizeof (ind);) { 
        res += format_val (ind[i]) + ": "; 
        if (sizeof (res) >= short_format_length) throw (0); 
        res += format_val (val[ind[i]]); 
        if (++i < sizeof (ind) - 1) res += ", "; 
        if (sizeof (res) >= short_format_length) throw (0); 
      } 
      res += "])"; 
    } 
    else { 
      if (stringp (val) && sizeof (val) > short_format_length - sizeof (res)) { 
        sscanf (val, "%[ \t\n\r]", string lead); 
        if (sizeof (lead) > sizeof ("/.../") && sizeof (lead) < sizeof (val)) 
          val = val[sizeof (lead)..], res += "/.../"; 
        if (sizeof (val) > short_format_length - sizeof (res)) { 
          res += sprintf ("%O", val[..short_format_length - sizeof (res) - 1]); 
          throw (0); 
        } 
      } 
      res += sprintf ("%O", val); 
    } 
  }; 
 
  mixed err = catch { 
    format_val (val); 
    return res; 
  }; 
  if (err) throw (err); 
  return res + "/.../"; 
} 
 
final array return_zero (mixed... ignored) {return 0;} 
final array return_empty_array (mixed... ignored) {return ({});} 
 
final int(1..1)|string|array free_text_error (object/*(RMXL.PXml)*/ p, string str) 
{ 
  sscanf (str, "%[ \t\n\r]", string ws); 
  if (str != ws) 
    RXML.parse_error ("Free text %s is not allowed in context of type %s.\n", 
                      format_short (String.trim_all_whites (str)), p->type->name); 
  return ({}); 
} 
 
final int(1..1)|string|array unknown_tag_error (object/*(RMXL.PXml)*/ p, string str) 
{ 
  RXML.parse_error ("Unknown tag %s is not allowed in context of type %s.\n", 
                    format_short (p->tag_name()), p->type->name); 
  return ({}); 
} 
 
final int(1..1)|string|array unknown_pi_tag_error (object/*(RMXL.PXml)*/ p, string str) 
{ 
  sscanf (str, "%[^ \t\n\r]", str); 
  RXML.parse_error ( 
    "Unknown processing instruction %s not allowed in context of type %s.\n", 
    format_short ("<" + p->tag_name() + str), p->type->name); 
  return ({}); 
} 
 
final int(1..1)|string|array invalid_cdata_error (object/*(RXML.PXml)*/ p, string str) 
{ 
  RXML.parse_error ("CDATA text %O is not allowed in context of type %s.\n", 
                    format_short (str), p->type->name); 
  return ({}); 
} 
 
final int(1..1)|string|array output_error_cb (object/*(RMXL.PXml)*/ p, string str) 
{ 
  if (p->errmsgs) str = p->errmsgs + str, p->errmsgs = 0; 
  if (p->type->free_text) p->_set_data_callback (0); 
  else p->_set_data_callback (free_text_error); 
  return ({str}); 
} 
 
 
// PXml and PEnt callbacks. 
 
final int(1..1)|string|array p_xml_comment_cb (object/*(RXML.PXml)*/ p, string str) 
// FIXME: This is a kludge until quote tags are handled like other tags. 
{ 
  if (p->type->handle_literals) p->handle_literal(); 
  else if (p->p_code) p->p_code_literal(); 
  string name = p->parse_tag_name (str); 
  if (sizeof (name)) { 
    name = p->tag_name() + name; 
    if (string|array|function tdef = p->tags()[name]) { 
      if (stringp (tdef)) 
        return ({tdef}); 
      else if (arrayp (tdef)) 
        return tdef[0] (p, p->parse_tag_args (str), @tdef[1..]); 
      else 
        return tdef (p, p->parse_tag_args (str)); 
    } 
    else if (p->containers()[name]) 
      RXML.parse_error ("Sorry, can't handle containers beginning with " + 
                        p->tag_name() + ".\n"); 
  } 
  return p->type->free_text ? 0 : ({}); 
} 
 
final int(1..1)|string|array p_xml_cdata_cb (object/*(RXML.PXml)*/ p, string str) 
{ 
  return ({str}); 
} 
 
final int(1..1)|string|array p_xml_entity_cb (object/*(RXML.PXml)*/ p, string str) 
{ 
  RXML.Type type = p->type; 
  string entity = p->tag_name(); 
  if (sizeof (entity)) 
    if (entity[0] == '#') { 
      if (!p->type->entity_syntax) { 
        // Don't decode normal entities if we're outputting xml-like stuff. 
        if (sscanf (entity, 
                    (<"#x", "#X">)[entity[..1]] ? "%*2s%x%*c" : "%*c%d%*c", 
                    int char) == 2) 
          catch (str = (string) ({char})); 
        // Lax error handling: Just let it through if it can't be 
        // converted. Not really good, though. 
      } 
    } 
    else 
      if (entity[0] == ':') str = entity[1..]; 
      else if (has_value (entity, ".")) { 
        if (type->handle_literals) p->handle_literal(); 
        else if (p->p_code) p->p_code_literal(); 
        mixed value = p->handle_var ( 
          p, 
          entity, 
          // No quoting of splice args. FIXME: Add some sort of 
          // safeguard against splicing in things like "nice><evil 
          // stuff='...'"? 
          p->html_context() == "splice_arg" ? RXML.t_string : type); 
        if (value != RXML.nil) { 
          if (type->free_text) return ({value}); 
          p->add_value (value); 
        } 
        return ({}); 
      } 
  if (!type->free_text) 
    RXML.parse_error ("Unknown entity \"&%s;\" not allowed context of type %s.\n", 
                      entity, type->name); 
  return ({str}); 
} 
 
final int(1..1)|string|array p_xml_compat_entity_cb (object/*(RMXL.PXml)*/ p, string str) 
{ 
  RXML.Type type = p->type; 
  string entity = p->tag_name(); 
  if (sizeof (entity) && entity[0] != '#') 
    if (entity[0] == ':') str = entity[1..]; 
    else if (has_value (entity, ".")) { 
      if (type->handle_literals) p->handle_literal(); 
      else if (p->p_code) p->p_code_literal(); 
      mixed value = p->handle_var ( 
        p, 
        entity, 
        // No quoting of splice args. FIXME: Add some sort of 
        // safeguard against splicing in things like "nice><evil 
        // stuff='...'"? 
        p->html_context() == "splice_arg" ? RXML.t_string : type); 
      if (value != RXML.nil) { 
        if (type->free_text) return ({value}); 
        p->add_value (value); 
      } 
      return ({}); 
    } 
  if (!type->free_text) 
    RXML.parse_error ("Unknown entity \"&%s;\" not allowed in context of type %s.\n", 
                      entity, type->name); 
  return ({str}); 
}