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, createGenerics
=False):
27 fileObject
= file(fileName
)
28 timeStart
= time
.time()
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()
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
)))
43 def loadURI(uri
, knownTypes
=None, knownNamespaces
=None, xmlSource
=None, createGenerics
=False):
45 timeStart
= time
.time()
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()
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
)))
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
)
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')
73 except Exception, errorData
:
75 raise xmlmarshaller
.MarshallerException('Error marshalling object to file "%s": %s' % (fileName
, str(errorData
)))
77 timeDone
= time
.time()
78 aglogging
.info(xmlLogger
, ('Save statistics for file %s: elapsed time = %f secs' % (fileName
, timeDone
-timeStart
)))
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
)
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
94 def genShortNS(xmlDoc
, longNamespace
=None):
95 if not hasattr(xmlDoc
, "__xmlnamespaces__"):
97 elif longNamespace
!= None and longNamespace
in xmlDoc
.__xmlnamespaces
__.items():
98 for key
, value
in xmlDoc
.__xmlnamespaces
__.iteritems():
99 if value
== longNamespace
:
102 while ("ns%d" % i
) in xmlDoc
.__xmlnamespaces
__:
106 def genTargetNS(fileName
, applicationName
=None, type=None):
107 if (applicationName
!= None):
109 tns
= "urn:%s:%s:%s" % (applicationName
, type, fileName
)
111 tns
= "urn:%s:%s" % (applicationName
, fileName
)
113 tns
= "urn:%s" % fileName
116 def splitType(typeName
):
117 index
= typeName
.rfind(':')
119 ns
= typeName
[:index
]
120 complexTypeName
= typeName
[index
+1:]
123 complexTypeName
= typeName
124 return (ns
, complexTypeName
)
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
136 clonedObject
.initialize()
137 except AttributeError:
141 def getAgVersion(fileName
):
142 fileObject
= file(fileName
)
144 xml
= fileObject
.read()
147 i
= xml
.find(' ag:version=')
151 i2
= xml
.find('<ag:')
153 i
= xml
.find(' version=', i2
)
156 elif xml
.find('<project version="10"') >= 0:
161 if xml
[i
:i
+1] == '"':
162 j
= xml
.find('"', i
+1)
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"
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",
195 global agXsdToClassName
196 agXsdToClassName
= None
197 def getAgXsdToClassName():
198 global agXsdToClassName
199 if (agXsdToClassName
== None):
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",
374 return agXsdToClassName
378 def getAgKnownTypes():
380 if agKnownTypes
== None:
383 import activegrid
.model
.processmodel
384 import activegrid
.model
.schema
385 import activegrid
.server
.deployment
386 import activegrid
.model
.wsdl
388 import activegrid
.data
.dataservice
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
)
395 tmpAgKnownTypes
[keyName
] = classType
396 if len(tmpAgKnownTypes
) > 0:
397 agKnownTypes
= tmpAgKnownTypes
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