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
  
// This module contains utility functions for XML creation and 
// some other useful stuff common to all the modules. 
 
static constant DOC_COMMENT= "//!"; 
 
static int isDigit(int c) { return '0' <= c && c <= '9'; } 
 
static int isDocComment(string s) { 
  return (strlen(s) >= 3 && s[0..2] == DOC_COMMENT); 
} 
 
//FIXME: should it return 0 for keywords ?? 
static int isIdent(string s) { 
  if (!s || s == "") 
    return 0; 
  int first = s[0]; 
  switch(s[0]) { 
    case 'a'..'z': 
    case 'A'..'Z': 
    case '_': 
    case '`': 
      break; 
    default: 
      if (s[0] < 128) 
        return 0; 
  } 
  return 1; 
} 
 
static string xmlquote(string s) { 
  return replace(s, ({ "<", ">", "&" }), ({ "&lt;", "&gt;", "&amp;" })); 
} 
 
static string attributequote(string s) { 
  return replace(s, ({ "<", ">", "\"", "'", "&" }), 
                 ({ "&lt;", "&gt;", "&#34;", "&#39;", "&amp;" })); 
} 
 
static string writeattributes(mapping(string:string) attrs) { 
  string s = ""; 
  foreach (indices(attrs || ([]) ), string attr) { 
    s += sprintf(" %s='%s'", attr, attributequote(attrs[attr])); 
  } 
  return s; 
} 
 
static string opentag(string t, mapping(string:string)|void attributes) { 
  return "<" + t + writeattributes(attributes) + ">"; 
} 
 
static string closetag(string t) { return "</" + t + ">"; } 
 
static string xmltag(string t, string|mapping(string:string)|void arg1, 
                     string|void arg2) 
{ 
  mapping attributes = mappingp(arg1) ? arg1 : 0; 
  string content = stringp(arg1) ? arg1 : stringp(arg2) ? arg2 : 0; 
  if (content && content != "") 
    return opentag(t, attributes) + content + closetag(t); 
  return "<" + t + "/>"; 
}