📄 attributecodec.py
字号:
## Copyright (c) 2001-2006, John Mettraux, OpenWFE.org# All rights reserved.# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met:# # . Redistributions of source code must retain the above copyright notice, this# list of conditions and the following disclaimer. # # . Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution.# # . Neither the name of the "OpenWFE" nor the names of its contributors may be# used to endorse or promote products derived from this software without# specific prior written permission.# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE.## $Id: attributeCodec.py 2275 2005-12-26 23:02:56Z jmettraux $#""" codec for Attributes of workitems (and headers) $Id: attributeCodec.py 2275 2005-12-26 23:02:56Z jmettraux $"""from openwfe.utils import isListfrom openwfe.workitem import MapAttributefrom openwfe.workitem import ListAttributefrom openwfe.workitem import StringMapAttributefrom openwfe.workitem import StringAttributefrom openwfe.workitem import IntegerAttributefrom openwfe.workitem import LongAttributefrom openwfe.workitem import DoubleAttributefrom openwfe.workitem import BooleanAttributefrom openwfe.workitem import XmlAttributefrom openwfe.workitem import Base64Attributefrom openwfe.xmlutils import extractTextfrom openwfe.xmlutils import firstChildfrom openwfe.xmlutils import childrenfrom openwfe.xmlutils import getChildrenByTagName#from openwfe.xmlutils import toMinidomStringfrom openwfe.xmlutils import getEncodingENCODING = getEncoding()## some 'constants'ATTRIBUTES = 'attributes'STRING_MAP = 'smap'MAP = 'map'LIST = 'list'STRING = 'string'INTEGER = 'integer'LONG = 'long'DOUBLE = 'double'BOOLEAN = 'boolean'XML = 'raw-xml'BASE64 = 'base64'ENTRY = 'entry'KEY = 'key'VALUE = 'value'## DECODINGdef parseAttribute (elt): #print 'elt.NodeName is >%s<' % elt.nodeName if isList(elt): if len(elt) < 1: return None return parseAttribute(elt[0]) if elt.nodeName == STRING_MAP: return parseStringMap(elt) if elt.nodeName == MAP: return parseMap(elt) if elt.nodeName == LIST: return parseList(elt) if elt.nodeName == STRING: return StringAttribute(extractText(elt)) if elt.nodeName == INTEGER: return IntegerAttribute(int(extractText(elt))) if elt.nodeName == LONG: return IntegerAttribute(long(extractText(elt))) if elt.nodeName == DOUBLE: return DoubleAttribute(float(extractText(elt))) if elt.nodeName == BOOLEAN: return BooleanAttribute(extractText(elt) == 'true') if elt.nodeName == XML: return XmlAttribute(firstChild(elt)) if elt.nodeName == BASE64: return Base64Attribute(extractText(elt)) if elt.nodeName == ATTRIBUTES: return parseAttribute(getChildrenByTagName(elt, STRING_MAP)) return Nonedef parseStringMap (elt): return _parseMap(StringMapAttribute(), elt)def parseMap (elt): return _parseMap(MapAttribute(), elt)def _parseMap (ma, elt): for e in getChildrenByTagName(elt, ENTRY): #key = parseAttribute(firstChild(getChildrenByTagName(e, KEY)[0])) #value = parseAttribute(firstChild(getChildrenByTagName(e, VALUE)[0])) cs = children(e) key = cs[0] value = cs[1] #print 'key.nodeName is "%s"' % key.nodeName if key.nodeName == KEY: key = firstChild(key) value = firstChild(value) key = parseAttribute(key) value = parseAttribute(value) ma[key] = value return madef parseList (elt): li = ListAttribute() for e in children(elt): li.append(parseAttribute(e)) return li## ENCODINGdef encodeAttribute (doc, attribute): #print ' ******* encodingAttribute %s' % attribute if isinstance(attribute, StringMapAttribute): return encodeMapAttribute(doc, attribute) if isinstance(attribute, MapAttribute): return encodeMapAttribute(doc, attribute) if isinstance(attribute, ListAttribute): return encodeListAttribute(doc, attribute) tag = None if isinstance(attribute, StringAttribute): tag = STRING elif isinstance(attribute, IntegerAttribute): tag = INTEGER elif isinstance(attribute, LongAttribute): tag = LONG elif isinstance(attribute, DoubleAttribute): tag = DOUBLE elif isinstance(attribute, BooleanAttribute): tag = BOOLEAN elif isinstance(attribute, XmlAttribute): tag = XML elif isinstance(attribute, Base64Attribute): tag = BASE64 else: tag = 'unkown-attribute-type' #print ' ********* tag is %s' % tag #print ' ********* value is "%s"' % str(attribute.value) e = doc.createElement(tag) if attribute: if isinstance(attribute, BooleanAttribute): if attribute.value and not attribute.value == 'false': value = 'true' else: value = 'false' ee = doc.createTextNode(value) elif isinstance(attribute, XmlAttribute): #cdata = attribute.value\ # .toprettyxml(indent=' ', newl='\n', encoding=ENCODING) #cdata = attribute.value\ # .toprettyxml(indent=' ', newl='\n') #ee = doc.createCDATASection(cdata) ee = attribute.value else: value = attribute.value if isinstance(value, basestring): value = attribute.value else: value = unicode(attribute.value) #print 'value type is %s' % type(value) #print 'is value a string ? %s' % isinstance(value, basestring) #print 'value is >%s<' % value.encode('latin-1') ee = doc.createTextNode(value) e.appendChild(ee) return edef encodeMapAttribute (doc, attribute): tag = MAP if isinstance(attribute, StringMapAttribute): tag = STRING_MAP eMap = doc.createElement(tag) for k in attribute.keys(): eKey = doc.createElement(KEY) eKey.appendChild(encodeAttribute(doc, k)) eValue = doc.createElement(VALUE) eValue.appendChild(encodeAttribute(doc, attribute[k])) eEntry = doc.createElement(ENTRY) eEntry.appendChild(eKey) eEntry.appendChild(eValue) eMap.appendChild(eEntry) return eMapdef encodeListAttribute (doc, attribute): eList = doc.createElement(LIST) for a in attribute: eList.appendChild(encodeAttribute(doc, a)) return eList
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -