Atlas  0.7.0
Networking protocol for the Worldforge system.
gen_xml.py
1 #generate XML from objects
2 
3 #Copyright 2000 by Aloril
4 #Copyright 2002 by AIR-IX SUUNNITTELU/Ahiplan Oy
5 
6 #This library is free software; you can redistribute it and/or
7 #modify it under the terms of the GNU Lesser General Public
8 #License as published by the Free Software Foundation; either
9 #version 2.1 of the License, or (at your option) any later version.
10 
11 #This library is distributed in the hope that it will be useful,
12 #but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 #Lesser General Public License for more details.
15 
16 #You should have received a copy of the GNU Lesser General Public
17 #License along with this library; if not, write to the Free Software
18 #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 
21 from atlas.typemap import *
22 from . import encoder
23 
25  begin_string = "<atlas>\n"
26  middle_string = "\n"
27  end_string = "\n</atlas>\n"
28  empty_end_string = "<atlas/>\n"
29 
30  def encode1stream(self, object):
31  return gen_xml(object, "\t")
32 
33  def encode1(self, object):
34  return gen_xml(object)
35 
36 
37 
38 def get_encoder(stream_flag=None):
39  return Encoder(stream_flag)
40 
41 
42 def gen_xml(obj, indent=""):
43  gen = GenerateXML(indent)
44  return gen.encode(obj)
45 
47  def __init__(self, base_indent=""):
48  self.base_indent = base_indent
49  def encode(self, obj):
50  i = self.base_indent
51  if hasattr(obj, "keys"):
52  return "%s<map>\n%s\n%s</map>" % (i, self.obj2xml(obj, indent="\t"+i), i)
53  else:
54  return "%s<list>\n%s\n%s</list>" % (i, self.list2xml(obj, indent="\t"+i), i)
55 
56 
57  def to_string(self, value):
58  if isinstance(value, str):
59  value = value.replace("&","&amp;")
60  value = value.replace("<","&lt;")
61  value = value.replace(">","&gt;")
62  return value
63  elif isinstance(value, int):
64  value = str(value)
65  if value[-1]=="L": value = value[:-1]
66  return value
67  return str(value)
68 
69  def encode_attribute(self, indent, name, str_type, str_value):
70  return '%s<%s name="%s">%s</%s>' % \
71  (indent, str_type, name, str_value, str_type)
72 
73  def obj2xml(self, obj, indent):
74  """this encodes mappings"""
75  str_list = []
76  add_line = str_list.append
77  for name, value in list(obj.items()):
78  if value is not None:
79  if name == "parent":
80  str_type = "string"
81  if isinstance(value, str):
82  str_value = self.to_string(value)
83  else:
84  str_value = self.to_string(value.id)
85  else:
86  str_type = get_atlas_type(value)
87  add_nl = 0
88  if str_type=="map":
89  str_value = self.obj2xml(value, indent+"\t")
90  if str_value:
91  add_nl = 1
92  elif str_type=="list":
93  str_value = self.list2xml(value, indent+"\t")
94  if str_value.find("\t")>=0:
95  add_nl = 1
96  else:
97  #int/float/string
98  str_value = self.to_string(value)
99  if add_nl:
100  str_value = "\n%s\n%s" % (str_value, indent)
101  add_line(self.encode_attribute(indent, name, str_type, str_value))
102  return "\n".join(str_list)
103 
104  def list2xml(self, lst, indent):
105  str_list = []
106  add_item = str_list.append
107  complex_in_list = 0
108  for item in lst:
109  str_type = get_atlas_type(item)
110  if str_type=="map":
111  complex_in_list = 1
112  add_item("<map>\n%s\n%s</map>" % \
113  (self.obj2xml(item, indent+"\t"), indent))
114  elif str_type=="list":
115  complex_in_list = 1
116  add_item("<list>%s</list>" % \
117  self.list2xml(item, indent+"\t"))
118  else:
119  #int/float/string
120  add_item("<%s>%s</%s>" % (str_type, self.to_string(item), str_type))
121  if complex_in_list:
122  str_list = list(map(lambda s,i=indent:i+s, str_list))
123  return "\n".join(str_list)
124  else:
125  return "".join(str_list)
def to_string(self, value)
Definition: gen_xml.py:57
def obj2xml(self, obj, indent)
Definition: gen_xml.py:73
def encode_attribute(self, indent, name, str_type, str_value)
Definition: gen_xml.py:69
def list2xml(self, lst, indent)
Definition: gen_xml.py:104