xml_data._pike
Go to the documentation of this file.
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: xml_data.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
18  */
19 #include <attributes.h>
20 #include <classes.h>
21 class xml_data {
22 public:
23 
24 
25 
26 
27 
28 /**
29  * Compose a string for a given scalar 's'.
30  *
31  * @param mixed s - scalar to convert to a string.
32  * @return string representation of 's'.
33  */
34 string
35 compose_scalar(mixed s)
36 {
37  if (intp(s))
38  return "<int>" + (string) s + "</int>";
39  else if (stringp(s))
40  return "<string><![CDATA[" + s +"]]></string>";
41  else if (floatp(s))
42  return "<float>" + s +"</float>";
43  else if (objectp(s)) {
44  if ( functionp(s->get_object_id) )
45  return "<object><id>"+s->get_object_id()+"</id><name>"+
46  "<![CDATA["+s->get_identifier()+"]]></name></object>";
47  if ( functionp(s) )
48  return "<function><name>"+function_name(s)+"</name>"+
49  "<object>"+function_object(s)->get_object_id()+"</object>"+
50  "</function>";
51  }
52  else if ( programp(s) )
53  return "<program>" + master()->describe_program(s) + "</program>";
54 
55 }
56 
57 /**
58  * Bring an array into an xml representation.
59  *
60  * @param array a - the array to compose.
61  * @return xml string representation of 'a'.
62  */
63 string
64 compose_array(array a)
65 {
66  int i,sz;
67  string s_compose;
68 
69  s_compose = "<array>";
70  for (i=0,sz = sizeof(a);i<sz;i++)
71  s_compose += compose(a[i]);
72  s_compose += "</array>\n";
73  return s_compose;
74 }
75 
76 /**
77  * Bring a mapping into an xml representation.
78  *
79  * @param mapping m - the mapping to compose.
80  * @return xml string representation of 'm'.
81  * @see compose_struct
82  */
83 string
84 compose_struct(mapping m)
85 {
86  int i,sz;
87  string s_compose;
88  array ind, val;
89  ind = indices(m);
90  val = values(m);
91 
92  s_compose = "<struct>\n";
93  for (i=0,sz=sizeof(ind);i<sz;i++)
94  {
95  s_compose += "<member>\n";
96  s_compose += "<key>" + compose(ind[i]) + "</key>\n";
97  s_compose += "<value>"+ compose(val[i]) + "</value>\n";
98  s_compose += "</member>\n";
99  }
100  s_compose +="</struct>\n";
101  return s_compose;
102 }
103 
104 /**
105  * Bring any data type of pike into an xml representation.
106  *
107  * @param mixed m - some data
108  * @return xml string representation of 'm'.
109  */
110 string
111 compose(mixed m)
112 {
113  if (stringp(m) || intp(m) || floatp(m) || objectp(m) || programp(m) )
114  return compose_scalar(m);
115  if (mappingp(m))
116  return compose_struct(m);
117  if (arrayp(m))
118  return compose_array(m);
119 }
120 
121 
122 };