<s> """<STR_LIT>""" <EOL> from compat import * <EOL> import os <EOL> import re <EOL> if os . name == "<STR_LIT>" : <EOL> from javax . xml . parsers import * <EOL> import java <EOL> builder = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) <EOL> def parseDocument ( s ) : <EOL> stream = java . io . ByteArrayInputStream ( java . lang . String ( s ) . getBytes ( ) ) <EOL> return builder . parse ( stream ) <EOL> else : <EOL> from xml . dom import minidom <EOL> from xml . sax import saxutils <EOL> def parseDocument ( s ) : <EOL> return minidom . parseString ( s ) <EOL> def parseAndStripWhitespace ( s ) : <EOL> try : <EOL> element = parseDocument ( s ) . documentElement <EOL> except BaseException , e : <EOL> raise SyntaxError ( str ( e ) ) <EOL> stripWhitespace ( element ) <EOL> return element <EOL> def stripWhitespace ( element , tab = <NUM_LIT:0> ) : <EOL> element . normalize ( ) <EOL> lastSpacer = "<STR_LIT:\n>" + ( "<STR_LIT:\t>" * tab ) <EOL> spacer = lastSpacer + "<STR_LIT:\t>" <EOL> if element . childNodes . length == <NUM_LIT:0> : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> if element . childNodes . length == <NUM_LIT:1> : <EOL> if element . firstChild . nodeType == element . firstChild . TEXT_NODE : <EOL> if element . firstChild . data == lastSpacer : <EOL> element . removeChild ( element . firstChild ) <EOL> return <EOL> elif element . firstChild . nodeType == element . firstChild . ELEMENT_NODE : <EOL> raise SyntaxError ( "<STR_LIT>" % element . tagName ) <EOL> else : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> child = element . firstChild <EOL> while child : <EOL> if child . nodeType == child . ELEMENT_NODE : <EOL> stripWhitespace ( child , tab + <NUM_LIT:1> ) <EOL> child = child . nextSibling <EOL> elif child . nodeType == child . TEXT_NODE : <EOL> if child == element . lastChild : <EOL> if child . data != lastSpacer : <EOL> raise SyntaxError ( "<STR_LIT>" % element . tagName ) <EOL> elif child . data != spacer : <EOL> raise SyntaxError ( "<STR_LIT>" % element . tagName ) <EOL> next = child . nextSibling <EOL> element . removeChild ( child ) <EOL> child = next <EOL> else : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> def checkName ( element , name ) : <EOL> if element . nodeType != element . ELEMENT_NODE : <EOL> raise SyntaxError ( "<STR_LIT>" % name ) <EOL> if name == None : <EOL> return <EOL> if element . tagName != name : <EOL> raise SyntaxError ( "<STR_LIT>" % ( name , element . tagName ) ) <EOL> def getChild ( element , index , name = None ) : <EOL> if element . nodeType != element . ELEMENT_NODE : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> child = element . childNodes . item ( index ) <EOL> if child == None : <EOL> raise SyntaxError ( "<STR_LIT>" % name ) <EOL> checkName ( child , name ) <EOL> return child <EOL> def getChildIter ( element , index ) : <EOL> class ChildIter : <EOL> def __init__ ( self , element , index ) : <EOL> self . element = element <EOL> self . index = index <EOL> def next ( self ) : <EOL> if self . index < len ( self . element . childNodes ) : <EOL> retVal = self . element . childNodes . item ( self . index ) <EOL> self . index += <NUM_LIT:1> <EOL> else : <EOL> retVal = None <EOL> return retVal <EOL> def checkEnd ( self ) : <EOL> if self . index != len ( self . element . childNodes ) : <EOL> raise SyntaxError ( "<STR_LIT>" % self . element . tagName ) <EOL> return ChildIter ( element , index ) <EOL> def getChildOrNone ( element , index ) : <EOL> if element . nodeType != element . ELEMENT_NODE : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> child = element . childNodes . item ( index ) <EOL> return child <EOL> def getLastChild ( element , index , name = None ) : <EOL> if element . nodeType != element . ELEMENT_NODE : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> child = element . childNodes . item ( index ) <EOL> if child == None : <EOL> raise SyntaxError ( "<STR_LIT>" % name ) <EOL> if child != element . lastChild : <EOL> raise SyntaxError ( "<STR_LIT>" % element . tagName ) <EOL> checkName ( child , name ) <EOL> return child <EOL> nsRegEx = "<STR_LIT>" <EOL> cryptoIDRegEx = "<STR_LIT>" <EOL> urlRegEx = "<STR_LIT>" <EOL> sha1Base64RegEx = "<STR_LIT>" <EOL> base64RegEx = "<STR_LIT>" <EOL> certsListRegEx = "<STR_LIT>" <EOL> keyRegEx = "<STR_LIT>" <EOL> keysListRegEx = "<STR_LIT>" <EOL> dateTimeRegEx = "<STR_LIT>" <EOL> shortStringRegEx = "<STR_LIT>" <EOL> exprRegEx = "<STR_LIT>" <EOL> notAfterDeltaRegEx = "<STR_LIT>" <EOL> booleanRegEx = "<STR_LIT>" <EOL> def getReqAttribute ( element , attrName , regEx = "<STR_LIT>" ) : <EOL> if element . nodeType != element . ELEMENT_NODE : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> value = element . getAttribute ( attrName ) <EOL> if not value : <EOL> raise SyntaxError ( "<STR_LIT>" + attrName ) <EOL> if not re . match ( regEx , value ) : <EOL> raise SyntaxError ( "<STR_LIT>" % ( attrName , value ) ) <EOL> element . removeAttribute ( attrName ) <EOL> return str ( value ) <EOL> def getAttribute ( element , attrName , regEx = "<STR_LIT>" ) : <EOL> if element . nodeType != element . ELEMENT_NODE : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> value = element . getAttribute ( attrName ) <EOL> if value : <EOL> if not re . match ( regEx , value ) : <EOL> raise SyntaxError ( "<STR_LIT>" % ( attrName , value ) ) <EOL> element . removeAttribute ( attrName ) <EOL> return str ( value ) <EOL> def checkNoMoreAttributes ( element ) : <EOL> if element . nodeType != element . ELEMENT_NODE : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> if element . attributes . length != <NUM_LIT:0> : <EOL> raise SyntaxError ( "<STR_LIT>" % element . tagName ) <EOL> def getText ( element , regEx = "<STR_LIT>" ) : <EOL> textNode = element . firstChild <EOL> if textNode == None : <EOL> raise SyntaxError ( "<STR_LIT>" % element . tagName ) <EOL> if textNode . nodeType != textNode . TEXT_NODE : <EOL> raise SyntaxError ( "<STR_LIT>" % element . tagName ) <EOL> if not re . match ( regEx , textNode . data ) : <EOL> raise SyntaxError ( "<STR_LIT>" % ( element . tagName , textNode . data ) ) <EOL> return str ( textNode . data ) <EOL> def indent ( s , steps , ch = "<STR_LIT:\t>" ) : <EOL> tabs = ch * steps <EOL> if s [ - <NUM_LIT:1> ] != "<STR_LIT:\n>" : <EOL> s = tabs + s . replace ( "<STR_LIT:\n>" , "<STR_LIT:\n>" + tabs ) <EOL> else : <EOL> s = tabs + s . replace ( "<STR_LIT:\n>" , "<STR_LIT:\n>" + tabs ) <EOL> s = s [ : - len ( tabs ) ] <EOL> return s <EOL> def escape ( s ) : <EOL> return saxutils . escape ( s ) </s>
