]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/util/xmlutils.py
Tweaks for demos on MacOSX
[wxWidgets.git] / wxPython / samples / ide / activegrid / util / xmlutils.py
1 #----------------------------------------------------------------------------
2 # Name: xmlutils.py
3 # Purpose: XML and Marshaller Utilities
4 #
5 # Author: Jeff Norton
6 #
7 # Created: 6/2/05
8 # CVS-ID: $Id$
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
12
13 from activegrid.util.lang import *
14 import os
15 import time
16 import urllib
17 import logging
18 from activegrid.util.lang import *
19 import activegrid.util.objutils as objutils
20 import activegrid.util.xmlmarshaller as xmlmarshaller
21 import activegrid.util.aglogging as aglogging
22
23 xmlLogger = logging.getLogger("activegrid.util.xml")
24
25 def load(fileName, knownTypes=None, knownNamespaces=None):
26 loadedObject = None
27 fileObject = file(fileName)
28 timeStart = time.time()
29 try:
30 xml = fileObject.read()
31 loadedObject = unmarshal(xml, knownTypes=knownTypes, knownNamespaces=knownNamespaces, xmlSource=fileName)
32 loadedObject.fileName = os.path.abspath(fileName)
33 if hasattr(loadedObject, 'initialize'):
34 loadedObject.initialize()
35 finally:
36 fileObject.close()
37 timeDone = time.time()
38 aglogging.info(xmlLogger, ('Load statistics for file %s: elapsed time = %f secs' % (fileName, timeDone-timeStart)))
39 return loadedObject
40
41 def loadURI(uri, knownTypes=None, knownNamespaces=None, xmlSource=None):
42 loadedObject = None
43 xml = urllib.urlopen(uri).read()
44 loadedObject = unmarshal(xml, knownTypes=knownTypes, knownNamespaces=knownNamespaces, xmlSource=xmlSource)
45 loadedObject.fileName = uri
46 if hasattr(loadedObject, 'initialize'):
47 loadedObject.initialize()
48 return loadedObject
49
50 def unmarshal(xml, knownTypes=None, knownNamespaces=None, xmlSource=None):
51 if (knownTypes == None):
52 knownTypes, knownNamespaces = getAgKnownTypes()
53 return xmlmarshaller.unmarshal(xml, knownTypes=knownTypes, knownNamespaces=knownNamespaces, xmlSource=xmlSource)
54
55 def save(fileName, objectToSave, prettyPrint=True, marshalType=True, knownTypes=None, knownNamespaces=None, encoding='utf-8'):
56 if hasattr(objectToSave, '_xmlReadOnly') and objectToSave._xmlReadOnly == True:
57 raise xmlmarshaller.MarshallerException('Error marshalling object to file "%s": object is marked "readOnly" and cannot be written' % (fileName))
58 timeStart = time.time()
59 xml = marshal(objectToSave, prettyPrint=prettyPrint, marshalType=marshalType, knownTypes=knownTypes, knownNamespaces=knownNamespaces, encoding=encoding)
60 fileObject = file(fileName, 'w')
61 try:
62 fileObject.write(xml)
63 fileObject.flush()
64 except Exception, errorData:
65 fileObject.close()
66 raise xmlmarshaller.MarshallerException('Error marshalling object to file "%s": %s' % (fileName, str(errorData)))
67 fileObject.close()
68 timeDone = time.time()
69 aglogging.info(xmlLogger, ('Save statistics for file %s: elapsed time = %f secs' % (fileName, timeDone-timeStart)))
70
71 def marshal(objectToSave, prettyPrint=True, marshalType=True, knownTypes=None, knownNamespaces=None, encoding='utf-8'):
72 if (knownTypes == None):
73 knownTypes, knownNamespaces = getAgKnownTypes()
74 return xmlmarshaller.marshal(objectToSave, prettyPrint=prettyPrint, marshalType=marshalType, knownTypes=knownTypes, knownNamespaces=knownNamespaces, encoding=encoding)
75
76 def addNSAttribute(xmlDoc, shortNamespace, longNamespace):
77 if not hasattr(xmlDoc, "__xmlnamespaces__"):
78 xmlDoc.__xmlnamespaces__ = {shortNamespace:longNamespace}
79 elif shortNamespace not in xmlDoc.__xmlnamespaces__:
80 if (hasattr(xmlDoc.__class__, "__xmlnamespaces__")
81 and (xmlDoc.__xmlnamespaces__ is xmlDoc.__class__.__xmlnamespaces__)):
82 xmlDoc.__xmlnamespaces__ = dict(xmlDoc.__xmlnamespaces__)
83 xmlDoc.__xmlnamespaces__[shortNamespace] = longNamespace
84
85 def genShortNS(xmlDoc, longNamespace=None):
86 if not hasattr(xmlDoc, "__xmlnamespaces__"):
87 return "ns1"
88 elif longNamespace != None and longNamespace in xmlDoc.__xmlnamespaces__.items():
89 for key, value in xmlDoc.__xmlnamespaces__.iteritems():
90 if value == longNamespace:
91 return key
92 i = 1
93 while ("ns%d" % i) in xmlDoc.__xmlnamespaces__:
94 i += 1
95 return ("ns%d" % i)
96
97 def genTargetNS(fileName, applicationName=None, type=None):
98 if (applicationName != None):
99 if (type != None):
100 tns = "urn:%s:%s:%s" % (applicationName, type, fileName)
101 else:
102 tns = "urn:%s:%s" % (applicationName, fileName)
103 else:
104 tns = "urn:%s" % fileName
105 return tns
106
107 def splitType(typeName):
108 index = typeName.rfind(':')
109 if index != -1:
110 ns = typeName[:index]
111 complexTypeName = typeName[index+1:]
112 else:
113 ns = None
114 complexTypeName = typeName
115 return (ns, complexTypeName)
116
117 def cloneObject(objectToClone, knownTypes=None, marshalType=True, knownNamespaces=None, encoding='utf-8'):
118 if (knownTypes == None):
119 knownTypes, knownNamespaces = getAgKnownTypes()
120 xml = xmlmarshaller.marshal(objectToClone, prettyPrint=True, marshalType=marshalType, knownTypes=knownTypes, knownNamespaces=knownNamespaces, encoding=encoding)
121 clonedObject = xmlmarshaller.unmarshal(xml, knownTypes=knownTypes, knownNamespaces=knownNamespaces)
122 if hasattr(objectToClone, 'fileName'):
123 clonedObject.fileName = objectToClone.fileName
124 if hasattr(objectToClone, "_parentDoc"):
125 clonedObject._parentDoc = objectToClone._parentDoc
126 try:
127 clonedObject.initialize()
128 except AttributeError:
129 pass
130 return clonedObject
131
132 def getAgVersion(fileName):
133 fileObject = file(fileName)
134 try:
135 xml = fileObject.read()
136 finally:
137 fileObject.close()
138 i = xml.find(' ag:version=')
139 if i >= 0:
140 i += 12
141 else:
142 i2 = xml.find('<ag:')
143 if i2 >= 0:
144 i = xml.find(' version=', i2)
145 if i > 0:
146 i += 9
147 elif xml.find('<project version="10"') >= 0:
148 return "10"
149 else:
150 return None
151 version = None
152 if xml[i:i+1] == '"':
153 j = xml.find('"', i+1)
154 if (j > i+1):
155 version = xml[i+1:j]
156 return version
157
158 def escape(data):
159 """Escape ', ", &, <, and > in a string of data.
160
161 Basically, everything that saxutils.escape does (and this calls that, at
162 least for now), but with " added as well.
163
164 XXX TODO make this faster; saxutils.escape() is really slow
165 """
166
167 import xml.sax.saxutils as saxutils
168
169 data=saxutils.escape(data)
170 data=data.replace("\"", "&quot;")
171
172 # IE doesn't support &apos;
173 # data=data.replace("\'", "&apos;")
174 data=data.replace("\'", "&#039;")
175
176 return data
177
178 def unescape(data):
179 """Unescape ', ", &, <, and > in a string of data.
180
181 Basically, everything that saxutils.unescape does (and this calls that, at
182 least for now), but with " added as well.
183
184 XXX TODO make this faster; saxutils.unescape() is really slow
185 """
186
187 import xml.sax.saxutils as saxutils
188
189 data=data.replace("&quot;", "\"")
190 data=data.replace("&apos;", "\'")
191 return saxutils.unescape(data)
192
193
194 AG_NS_URL = "http://www.activegrid.com/ag.xsd"
195 BPEL_NS_URL = "http://schemas.xmlsoap.org/ws/2003/03/business-process"
196 HTTP_WSDL_NS_URL = "http://schemas.xmlsoap.org/wsdl/http/"
197 MIME_WSDL_NS_URL = "http://schemas.xmlsoap.org/wsdl/mime/"
198 SOAP_NS_URL = "http://schemas.xmlsoap.org/wsdl/soap/"
199 SOAP12_NS_URL = "http://schemas.xmlsoap.org/wsdl/soap12/"
200 WSDL_NS_URL = "http://schemas.xmlsoap.org/wsdl/"
201 XFORMS_NS_URL = "http://www.w3c.org/xform.xsd"
202 XMLSCHEMA_NS_URL = "http://www.w3.org/2001/XMLSchema"
203 XSI_NS_URL = "http://www.w3.org/2001/XMLSchema-instance"
204 XACML_NS_URL = "urn:oasis:names:tc:xacml:2.0:policy:schema:os"
205
206 KNOWN_NAMESPACES = { AG_NS_URL : "ag",
207 BPEL_NS_URL : "bpws",
208 HTTP_WSDL_NS_URL : "http",
209 MIME_WSDL_NS_URL : "mime",
210 SOAP_NS_URL : "soap",
211 SOAP12_NS_URL : "soap12",
212 WSDL_NS_URL : "wsdl",
213 XFORMS_NS_URL : "xforms",
214 XMLSCHEMA_NS_URL : "xs",
215 XACML_NS_URL : "xacml",
216 }
217
218 global agXsdToClassName
219 agXsdToClassName = None
220 def getAgXsdToClassName():
221 global agXsdToClassName
222 if (agXsdToClassName == None):
223 agXsdToClassName = {
224 "ag:append" : "activegrid.model.processmodel.AppendOperation",
225 "ag:attribute" : "activegrid.model.identitymodel.Attribute",
226 "ag:body" : "activegrid.model.processmodel.Body",
227 "ag:category_substitutions" : "activegrid.server.layoutrenderer.CategorySubstitutions",
228 "ag:command" : "activegrid.model.wsdl.Command",
229 "ag:css" : "activegrid.server.layoutrenderer.CSS",
230 "ag:cssRule" : "activegrid.model.processmodel.CssRule",
231 "ag:databaseService" : "activegrid.server.deployment.DatabaseService",
232 "ag:datasource" : "activegrid.data.dataservice.DataSource",
233 "ag:dataObjectList" : "activegrid.data.datalang.DataObjectList",
234 "ag:debug" : "activegrid.model.processmodel.DebugOperation",
235 "ag:deployment" : "activegrid.server.deployment.Deployment",
236 "ag:generator" : "activegrid.server.layoutrenderer.SerializableGenerator",
237 "ag:head" : "activegrid.server.layoutrenderer.Head",
238 "ag:hr" : "activegrid.model.processmodel.HorizontalRow",
239 "ag:identity" : "activegrid.model.identitymodel.Identity",
240 "ag:identityref" : "activegrid.server.deployment.IdentityRef",
241 "ag:image" : "activegrid.model.processmodel.Image",
242 "ag:label" : "activegrid.model.processmodel.Label",
243 "ag:layout" : "activegrid.server.layoutrenderer.Layout",
244 "ag:layouts" : "activegrid.server.layoutrenderer.Layouts",
245 "ag:ldapsource" : "activegrid.model.identitymodel.LDAPSource",
246 "ag:localService" : "activegrid.server.deployment.LocalService",
247 "ag:parameter" : "activegrid.server.layoutrenderer.Parameter",
248 "ag:parameters" : "activegrid.server.layoutrenderer.Parameters",
249 "ag:processref" : "activegrid.server.deployment.ProcessRef",
250 "ag:query" : "activegrid.model.processmodel.Query",
251 "ag:soapService" : "activegrid.server.deployment.SoapService",
252 "ag:requiredFile" : "activegrid.server.layoutrenderer.RequiredFile",
253 "ag:resource" : "activegrid.model.identitymodel.IDResource",
254 "ag:restService" : "activegrid.server.deployment.RestService",
255 "ag:rewrite" : "activegrid.model.wsdl.Rewrite",
256 "ag:role" : "activegrid.model.identitymodel.IDRole",
257 "ag:roledefn" : "activegrid.model.identitymodel.RoleDefn",
258 "ag:rssService" : "activegrid.server.deployment.RssService",
259 "ag:rule" : "activegrid.model.identitymodel.IDRule",
260 "ag:schemaOptions" : "activegrid.model.schema.SchemaOptions",
261 "ag:schemaref" : "activegrid.server.deployment.SchemaRef",
262 "ag:serviceCache" : "activegrid.server.deployment.ServiceCache",
263 "ag:serviceExtension": "activegrid.model.wsdl.ServiceExtension",
264 "ag:serviceExtensions": "activegrid.model.wsdl.ServiceExtensions",
265 "ag:serviceParameter": "activegrid.server.deployment.ServiceParameter",
266 "ag:serviceref" : "activegrid.server.deployment.ServiceRef",
267 "ag:set" : "activegrid.model.processmodel.SetOperation",
268 "ag:skinref" : "activegrid.server.deployment.SkinRef",
269 "ag:skin" : "activegrid.server.layoutrenderer.Skin",
270 "ag:skin_element_ref": "activegrid.server.layoutrenderer.SkinElementRef",
271 "ag:skin_element" : "activegrid.server.layoutrenderer.SkinElement",
272 "ag:skins" : "activegrid.server.layoutrenderer.Skins",
273 "ag:substitution" : "activegrid.server.layoutrenderer.Substitution",
274 "ag:text" : "activegrid.model.processmodel.Text",
275 "ag:title" : "activegrid.model.processmodel.Title",
276 "ag:usertemplate" : "activegrid.model.identitymodel.UserTemplate",
277 "ag:xformref" : "activegrid.server.deployment.XFormRef",
278 "bpws:case" : "activegrid.model.processmodel.BPELCase",
279 "bpws:catch" : "activegrid.model.processmodel.BPELCatch",
280 "bpws:faultHandlers" : "activegrid.model.processmodel.BPELFaultHandlers",
281 "bpws:flow" : "activegrid.model.processmodel.BPELFlow",
282 "bpws:invoke" : "activegrid.model.processmodel.BPELInvoke",
283 "bpws:onMessage" : "activegrid.model.processmodel.BPELOnMessage",
284 "bpws:otherwise" : "activegrid.model.processmodel.BPELOtherwise",
285 "bpws:pick" : "activegrid.model.processmodel.BPELPick",
286 "bpws:process" : "activegrid.model.processmodel.BPELProcess",
287 "bpws:receive" : "activegrid.model.processmodel.BPELReceive",
288 "bpws:reply" : "activegrid.model.processmodel.BPELReply",
289 "bpws:scope" : "activegrid.model.processmodel.BPELScope",
290 "bpws:sequence" : "activegrid.model.processmodel.BPELSequence",
291 "bpws:switch" : "activegrid.model.processmodel.BPELSwitch",
292 "bpws:terminate" : "activegrid.model.processmodel.BPELTerminate",
293 "bpws:variable" : "activegrid.model.processmodel.BPELVariable",
294 "bpws:variables" : "activegrid.model.processmodel.BPELVariables",
295 "bpws:while" : "activegrid.model.processmodel.BPELWhile",
296 "http:address" : "activegrid.model.wsdl.HttpAddress",
297 "http:binding" : "activegrid.model.wsdl.HttpBinding",
298 "http:operation" : "activegrid.model.wsdl.HttpOperation",
299 "http:urlEncoded" : "activegrid.model.wsdl.HttpUrlEncoded",
300 "mime:content" : "activegrid.model.wsdl.MimeContent",
301 "mime:mimeXml" : "activegrid.model.wsdl.MimeMimeXml",
302 "soap:address" : "activegrid.model.wsdl.SoapAddress",
303 "soap:binding" : "activegrid.model.wsdl.SoapBinding",
304 "soap:body" : "activegrid.model.wsdl.SoapBody",
305 "soap:fault" : "activegrid.model.wsdl.SoapFault",
306 "soap:header" : "activegrid.model.wsdl.SoapHeader",
307 "soap:operation" : "activegrid.model.wsdl.SoapOperation",
308 "soap12:address" : "activegrid.model.wsdl.Soap12Address",
309 "soap12:binding" : "activegrid.model.wsdl.Soap12Binding",
310 "soap12:body" : "activegrid.model.wsdl.Soap12Body",
311 "soap12:fault" : "activegrid.model.wsdl.Soap12Fault",
312 "soap12:header" : "activegrid.model.wsdl.Soap12Header",
313 "soap12:operation" : "activegrid.model.wsdl.Soap12Operation",
314 "wsdl:binding" : "activegrid.model.wsdl.WsdlBinding",
315 "wsdl:definitions" : "activegrid.model.wsdl.WsdlDocument",
316 "wsdl:documentation" : "activegrid.model.wsdl.WsdlDocumentation",
317 "wsdl:fault" : "activegrid.model.wsdl.WsdlFault",
318 "wsdl:import" : "activegrid.model.wsdl.WsdlImport",
319 "wsdl:input" : "activegrid.model.wsdl.WsdlInput",
320 "wsdl:message" : "activegrid.model.wsdl.WsdlMessage",
321 "wsdl:operation" : "activegrid.model.wsdl.WsdlOperation",
322 "wsdl:output" : "activegrid.model.wsdl.WsdlOutput",
323 "wsdl:part" : "activegrid.model.wsdl.WsdlPart",
324 "wsdl:port" : "activegrid.model.wsdl.WsdlPort",
325 "wsdl:portType" : "activegrid.model.wsdl.WsdlPortType",
326 "wsdl:service" : "activegrid.model.wsdl.WsdlService",
327 "wsdl:types" : "activegrid.model.wsdl.WsdlTypes",
328 "xacml:Action" : "activegrid.model.identitymodel.XACMLAction",
329 "xacml:ActionAttributeDesignator" : "activegrid.model.identitymodel.XACMLActionAttributeDesignator",
330 "xacml:ActionMatch" : "activegrid.model.identitymodel.XACMLActionMatch",
331 "xacml:Actions" : "activegrid.model.identitymodel.XACMLActions",
332 "xacml:AttributeValue" : "activegrid.model.identitymodel.XACMLAttributeValue",
333 "xacml:Policy" : "activegrid.model.identitymodel.XACMLPolicy",
334 "xacml:Resource" : "activegrid.model.identitymodel.XACMLResource",
335 "xacml:ResourceAttributeDesignator" : "activegrid.model.identitymodel.XACMLResourceAttributeDesignator",
336 "xacml:ResourceMatch" : "activegrid.model.identitymodel.XACMLResourceMatch",
337 "xacml:Resources" : "activegrid.model.identitymodel.XACMLResources",
338 "xacml:Rule" : "activegrid.model.identitymodel.XACMLRule",
339 "xacml:Target" : "activegrid.model.identitymodel.XACMLTarget",
340 "xforms:copy" : "activegrid.model.processmodel.XFormsCopy",
341 "xforms:group" : "activegrid.model.processmodel.XFormsGroup",
342 "xforms:include" : "activegrid.model.processmodel.XFormsInclude",
343 "xforms:input" : "activegrid.model.processmodel.XFormsInput",
344 "xforms:item" : "activegrid.model.processmodel.XFormsItem",
345 "xforms:itemset" : "activegrid.model.processmodel.XFormsItemset",
346 "xforms:label" : "activegrid.model.processmodel.XFormsLabel",
347 "xforms:model" : "activegrid.model.processmodel.XFormsModel",
348 "xforms:output" : "activegrid.model.processmodel.XFormsOutput",
349 "xforms:secret" : "activegrid.model.processmodel.XFormsSecret",
350 "xforms:select1" : "activegrid.model.processmodel.XFormsSelect1",
351 "xforms:submission" : "activegrid.model.processmodel.XFormsSubmission",
352 "xforms:submit" : "activegrid.model.processmodel.XFormsSubmit",
353 "xforms:value" : "activegrid.model.processmodel.XFormsValue",
354 "xforms:xform" : "activegrid.model.processmodel.View",
355 "xforms:xforms" : "activegrid.model.processmodel.XFormsRoot",
356 "xs:all" : "activegrid.model.schema.XsdSequence",
357 "xs:any" : "activegrid.model.schema.XsdAny",
358 "xs:attribute" : "activegrid.model.schema.XsdAttribute",
359 "xs:complexContent" : "activegrid.model.schema.XsdComplexContent",
360 "xs:complexType" : "activegrid.model.schema.XsdComplexType",
361 "xs:element" : "activegrid.model.schema.XsdElement",
362 "xs:enumeration" : "activegrid.model.schema.XsdEnumeration",
363 "xs:extension" : "activegrid.model.schema.XsdExtension",
364 "xs:field" : "activegrid.model.schema.XsdKeyField",
365 "xs:import" : "activegrid.model.schema.XsdInclude",
366 "xs:include" : "activegrid.model.schema.XsdInclude",
367 "xs:key" : "activegrid.model.schema.XsdKey",
368 "xs:keyref" : "activegrid.model.schema.XsdKeyRef",
369 "xs:length" : "activegrid.model.schema.XsdLength",
370 "xs:list" : "activegrid.model.schema.XsdList",
371 "xs:maxLength" : "activegrid.model.schema.XsdMaxLength",
372 "xs:restriction" : "activegrid.model.schema.XsdRestriction",
373 "xs:schema" : "activegrid.model.schema.Schema",
374 "xs:selector" : "activegrid.model.schema.XsdKeySelector",
375 "xs:sequence" : "activegrid.model.schema.XsdSequence",
376 "xs:simpleContent" : "activegrid.model.schema.XsdSimpleContent",
377 "xs:simpleType" : "activegrid.model.schema.XsdSimpleType",
378 "xs:totalDigits" : "activegrid.model.schema.XsdTotalDigits",
379 }
380 return agXsdToClassName
381
382 global agKnownTypes
383 agKnownTypes = None
384 def getAgKnownTypes():
385 global agKnownTypes
386 if agKnownTypes == None:
387 try:
388 tmpAgKnownTypes = {}
389 import activegrid.model.processmodel
390 import activegrid.model.schema
391 import activegrid.server.deployment
392 import activegrid.model.wsdl
393 ifDefPy()
394 import activegrid.data.dataservice
395 endIfDef()
396 for keyName, className in getAgXsdToClassName().iteritems():
397 classType = objutils.classForName(className)
398 if (classType == None):
399 raise Exception("Cannot get class type for %s" % className)
400 else:
401 tmpAgKnownTypes[keyName] = classType
402 if len(tmpAgKnownTypes) > 0:
403 agKnownTypes = tmpAgKnownTypes
404 except ImportError:
405 agKnownTypes = {}
406 if len(agKnownTypes) == 0: # standalone IDE and XmlMarshaller don't contain known AG types
407 noKnownNamespaces = {}
408 return agKnownTypes, noKnownNamespaces
409 return agKnownTypes, KNOWN_NAMESPACES