1 from neco.design.box import Box
3 from xml.dom import minidom
12 if isinstance(s, str):
13 rv = s.decode("latin1")
14 elif not isinstance(s, unicode):
18 return rv.replace(u'\x00',u'�')
21 return s.replace(u'�',u'\x00').encode("utf8")
24 if isinstance(value, str):
26 if isinstance(value, bool):
28 if isinstance(value, int):
30 if isinstance(value, float):
33 def to_type(type, value):
37 return value == "True"
43 class XMLParser(object):
44 def to_xml(self, box):
45 doc = minidom.Document()
47 root = doc.createElement("boxes")
51 self._traverse_boxes(doc, traversed, box)
54 for guid in sorted(traversed.keys()):
55 bnode = traversed[guid]
56 root.appendChild(bnode)
59 xml = doc.toprettyxml(indent=" ", encoding="UTF-8")
61 print >>sys.stderr, "Oops: generating XML from %s" % (data,)
66 def _traverse_boxes(self, doc, traversed, box):
67 bnode = doc.createElement("box")
68 bnode.setAttribute("guid", xmlencode(box.guid))
69 bnode.setAttribute("label", xmlencode(box.label))
70 bnode.setAttribute("x", xmlencode(box.x))
71 bnode.setAttribute("y", xmlencode(box.y))
72 bnode.setAttribute("width", xmlencode(box.width))
73 bnode.setAttribute("height", xmlencode(box.height))
75 traversed[box.guid] = bnode
77 anode = doc.createElement("attributes")
78 bnode.appendChild(anode)
79 for name in sorted(box.attributes):
80 value = getattr(box.a, name)
81 aanode = doc.createElement("attribute")
82 anode.appendChild(aanode)
83 aanode.setAttribute("name", xmlencode(name))
84 aanode.setAttribute("value", xmlencode(value))
85 aanode.setAttribute("type", from_type(value))
87 tnode = doc.createElement("tags")
88 bnode.appendChild(tnode)
89 for tag in sorted(box.tags):
90 ttnode = doc.createElement("tag")
91 tnode.appendChild(ttnode)
92 ttnode.setAttribute("name", xmlencode(tag))
94 cnode = doc.createElement("connections")
95 bnode.appendChild(cnode)
96 for b in sorted(box.connections):
97 ccnode = doc.createElement("connection")
98 cnode.appendChild(ccnode)
99 ccnode.setAttribute("guid", xmlencode(b.guid))
100 if b.guid not in traversed:
101 self._traverse_boxes(doc, traversed, b)
103 def from_xml(self, xml):
104 doc = minidom.parseString(xml)
105 bnode_list = doc.getElementsByTagName("box")
110 for bnode in bnode_list:
111 if bnode.nodeType == doc.ELEMENT_NODE:
112 guid = int(bnode.getAttribute("guid"))
113 label = xmldecode(bnode.getAttribute("label"))
114 x = float(bnode.getAttribute("x"))
115 y = float(bnode.getAttribute("y"))
116 height = float(bnode.getAttribute("height"))
117 width = float(bnode.getAttribute("width"))
118 box = Box(label=label, guid=guid)
121 anode_list = bnode.getElementsByTagName("attribute")
122 for anode in anode_list:
123 name = xmldecode(anode.getAttribute("name"))
124 value = xmldecode(anode.getAttribute("value"))
125 type = xmldecode(anode.getAttribute("type"))
126 value = to_type(type, value)
127 setattr(box.a, name, value)
129 tnode_list = bnode.getElementsByTagName("tag")
130 for tnode in tnode_list:
131 value = xmldecode(tnode.getAttribute("name"))
134 connections[box] = set()
135 cnode_list = bnode.getElementsByTagName("connection")
136 for cnode in cnode_list:
137 guid = int(cnode.getAttribute("guid"))
138 connections[box].add(guid)
140 for box, conns in connections.iteritems():