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