1 #----------------------------------------------------------------------------
3 # Purpose: XML and Marshaller Utilities
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
13 from activegrid
.util
.lang
import *
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
23 xmlLogger
= logging
.getLogger("activegrid.util.xml")
25 def load(fileName
, knownTypes
=None, knownNamespaces
=None):
27 fileObject
= file(fileName
)
28 timeStart
= time
.time()
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()
37 timeDone
= time
.time()
38 aglogging
.info(xmlLogger
, ('Load statistics for file %s: elapsed time = %f secs' % (fileName
, timeDone
-timeStart
)))
41 def loadURI(uri
, knownTypes
=None, knownNamespaces
=None, xmlSource
=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()
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
)
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')
64 except Exception, errorData
:
66 raise xmlmarshaller
.MarshallerException('Error marshalling object to file "%s": %s' % (fileName
, str(errorData
)))
68 timeDone
= time
.time()
69 aglogging
.info(xmlLogger
, ('Save statistics for file %s: elapsed time = %f secs' % (fileName
, timeDone
-timeStart
)))
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
)
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
85 def genShortNS(xmlDoc
, longNamespace
=None):
86 if not hasattr(xmlDoc
, "__xmlnamespaces__"):
88 elif longNamespace
!= None and longNamespace
in xmlDoc
.__xmlnamespaces
__.items():
89 for key
, value
in xmlDoc
.__xmlnamespaces
__.iteritems():
90 if value
== longNamespace
:
93 while ("ns%d" % i
) in xmlDoc
.__xmlnamespaces
__:
97 def genTargetNS(fileName
, applicationName
=None, type=None):
98 if (applicationName
!= None):
100 tns
= "urn:%s:%s:%s" % (applicationName
, type, fileName
)
102 tns
= "urn:%s:%s" % (applicationName
, fileName
)
104 tns
= "urn:%s" % fileName
107 def splitType(typeName
):
108 index
= typeName
.rfind(':')
110 ns
= typeName
[:index
]
111 complexTypeName
= typeName
[index
+1:]
114 complexTypeName
= typeName
115 return (ns
, complexTypeName
)
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
127 clonedObject
.initialize()
128 except AttributeError:
132 def getAgVersion(fileName
):
133 fileObject
= file(fileName
)
135 xml
= fileObject
.read()
138 i
= xml
.find(' ag:version=')
142 i2
= xml
.find('<ag:')
144 i
= xml
.find(' version=', i2
)
147 elif xml
.find('<project version="10"') >= 0:
152 if xml
[i
:i
+1] == '"':
153 j
= xml
.find('"', i
+1)
159 """Escape ', ", &, <, and > in a string of data.
161 Basically, everything that saxutils.escape does (and this calls that, at
162 least for now), but with " added as well.
164 XXX TODO make this faster; saxutils.escape() is really slow
167 import xml
.sax
.saxutils
as saxutils
169 data
=saxutils
.escape(data
)
170 data
=data
.replace("\"", """)
172 # IE doesn't support '
173 # data=data.replace("\'", "'")
174 data
=data
.replace("\'", "'")
179 """Unescape ', ", &, <, and > in a string of data.
181 Basically, everything that saxutils.unescape does (and this calls that, at
182 least for now), but with " added as well.
184 XXX TODO make this faster; saxutils.unescape() is really slow
187 import xml
.sax
.saxutils
as saxutils
189 data
=data
.replace(""", "\"")
190 data
=data
.replace("'", "\'")
191 return saxutils
.unescape(data
)
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"
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",
218 global agXsdToClassName
219 agXsdToClassName
= None
220 def getAgXsdToClassName():
221 global agXsdToClassName
222 if (agXsdToClassName
== None):
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",
380 return agXsdToClassName
384 def getAgKnownTypes():
386 if agKnownTypes
== None:
389 import activegrid
.model
.processmodel
390 import activegrid
.model
.schema
391 import activegrid
.server
.deployment
392 import activegrid
.model
.wsdl
394 import activegrid
.data
.dataservice
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
)
401 tmpAgKnownTypes
[keyName
] = classType
402 if len(tmpAgKnownTypes
) > 0:
403 agKnownTypes
= tmpAgKnownTypes
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