示例#1
0
def get_xml(filename, xpath):
    element_tree = get_file_element_tree(filename)
    xpath_engine = XPathFactory.newInstance().newXPath()
    node_list = xpath_engine.evaluate(xpath, element_tree, XPathConstants.NODESET)
    nodes = [node_list.item(i) for i in range(node_list.getLength())]

    return nodes
示例#2
0
    def __init__(self, cmo, localServers):

        # default page for 11.1.1.x
        self.url = 'http://docs.oracle.com/cd/E28280_01/core.1111/e12036/target_appendix_soa.htm'

        # retrieve values passed by properties file
        self.LOCALSERVERS = localServers

        # map item types to table in doc and actual retrieval method
        self.PARSEMAP = {
            # item type : ( table title, method to retrieve actual system items )
            'Application': ('SOA Application Targets', cmo.getAppDeployments),
            'Library': ('Oracle SOA Enterprise Deployment Targeting Library', cmo.getLibraries),
            'StartupClass': ('Oracle SOA Enterprise Deployment Startup Class Targets', cmo.getStartupClasses),
            'ShutdownClass': ('SOA EDG targeting shutdown class', cmo.getShutdownClasses),
            'JMS Resource': ('JMS System Resource Targets', cmo.getJMSSystemResources),
            'WLDF Resource': ('WLDF System Resource Targets', cmo.getWLDFSystemResources),
        }

        # where we'll store the mappings
        self.mappings = {}

        # init utility XPath object
        xpFactory = XPathFactory.newInstance()
        self.xp = xpFactory.newXPath()
        # namespace resolution is compulsory!!
        self.xp.setNamespaceContext(XhtmlNamespaceResolver())
def svgToRois(xmlPath):
	doc = loadXMLFromFile(xmlPath)
	xpath = XPathFactory.newInstance().newXPath();
	expression = "//path[@structure_id=\"8\"]/@d"
	print("svg:xpath")
	print(xpath.evaluate(expression, doc))
	return("")
def pathToRoi(path):
	doc = loadXMLFromString(path)
	xpath = XPathFactory.newInstance().newXPath();
	expression = "//path[@structure_id=\"8\"]/@d"
	print("xpath")
	print(xpath.evaluate(expression, doc))
	nodes = xpath.evaluate(expression, doc, XPathConstants.NODESET)
	nodeString = xpath.evaluate(expression, doc, XPathConstants.STRING)
	print("nodeString:")
	print(nodeString)
	#NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
	print(nodes.length)
	for i in range(0, nodes.length):
		print(nodes.item(i).getNodeName())
	#print(nodes.item(1).getFirstChild().getNodeValue()) 

	return("")
示例#5
0
    def findXpathNodes(self, xmlAsText, xpathExpr):
        xmlText = InputSource(ByteArrayInputStream(lang.String(xmlAsText).getBytes()))
        docBuilderFactory = DocumentBuilderFactory.newInstance()
        docBuilderFactory.setValidating(0)
        docBuilderFactory.setNamespaceAware(0)
        docBuilder = docBuilderFactory.newDocumentBuilder()
        doc = docBuilder.parse(xmlText)
        xpathFactory = XPathFactory.newInstance()
        xPath = xpathFactory.newXPath()
        expr = xPath.compile(xpathExpr);

        nodeList = expr.evaluate(doc, XPathConstants.NODESET)
        nodeCount = nodeList.getLength()
        count = 0
        xpathNodes = []
        while count < nodeCount:
            xpathNodes.append(nodeList.item(count))
            count = count + 1
        return xpathNodes
    def findXpathNodes(self, xmlAsText, xpathExpr):
        xmlText = InputSource(
            ByteArrayInputStream(lang.String(xmlAsText).getBytes()))
        docBuilderFactory = DocumentBuilderFactory.newInstance()
        docBuilderFactory.setValidating(0)
        docBuilderFactory.setNamespaceAware(0)
        docBuilder = docBuilderFactory.newDocumentBuilder()
        doc = docBuilder.parse(xmlText)
        xpathFactory = XPathFactory.newInstance()
        xPath = xpathFactory.newXPath()
        expr = xPath.compile(xpathExpr)

        nodeList = expr.evaluate(doc, XPathConstants.NODESET)
        nodeCount = nodeList.getLength()
        count = 0
        xpathNodes = []
        while count < nodeCount:
            xpathNodes.append(nodeList.item(count))
            count = count + 1
        return xpathNodes
示例#7
0
文件: nmap.py 项目: deezeesms/dd-git
 def _parseOpenPortsFromXml(output):
     'str->?'
     result = []
     xpath = XPathFactory.newInstance().newXPath()
     xmlFactory = DocumentBuilderFactory.newInstance()
     xmlFactory.setNamespaceAware(True)
     builder = xmlFactory.newDocumentBuilder()
     document = builder.parse(ByteArrayInputStream(String(output).getBytes()))
     ports = xpath.evaluate(r'/nmaprun/host/ports/port/state[@state="open"]/..|/nmaprun/host/ports/port/state[@state="open|filtered"]/..', document, XPathConstants.NODESET)
     if ports:
         protocolTypesMap = {'tcp': PortType.TCP.getProtocol(), 'udp': PortType.UDP.getProtocol()}
         for portIndex in xrange(ports.getLength()):
             portNode = ports.item(portIndex)
             protocolStr = portNode.getAttribute('protocol')
             protocol = protocolTypesMap.get(protocolStr)
             port = portNode.getAttribute('portid')
             if port and protocol:
                 result.append((protocol, int(port)))
             else:
                 logger.debug("Port [%s] or protocol [%s] values are invalid. Skip..." % (port, protocolStr))
     return result
示例#8
0
def _getXpath():
    r'@types: -> javax.xml.xpath.XPath'
    return XPathFactory.newInstance().newXPath()
示例#9
0
    import javax.xml.XMLConstants as XMLConstants
    import javax.xml.xpath.XPathFactory as XPathFactory
    import javax.xml.xpath.XPathConstants as XPathConstants
    import javax.xml.xpath.XPathExpressionException as XPathExpressionException
    import javax.xml.transform.TransformerFactory as TransformerFactory
    import javax.xml.transform.dom.DOMSource as DOMSource
    import javax.xml.transform.stream.StreamResult as StreamResult

    import org.w3c.dom.Node as Node
    import org.w3c.dom.DOMException as DOMException

    documentFactory = DocumentBuilderFactory.newInstance()
    documentFactory.setNamespaceAware(True)
    documentBuilder = documentFactory.newDocumentBuilder()

    xpathFactory = XPathFactory.newInstance()

    transformerFactory = TransformerFactory.newInstance()
    identityTransformation = transformerFactory.newTransformer()

    class XPathEvalError(Exception): pass

    class _ElementStringResult(unicode):
        @classmethod
        def _create(cls, node):
            if node.getNodeType() == Node.ATTRIBUTE_NODE:
                self = _ElementStringResult(node.getValue())
                self.attrname = node.getName()
                self.is_attribute = True
                self.is_text = False
                self.is_tail = False
示例#10
0
文件: __init__.py 项目: yarang/pyhwp
from java.io import IOException
from java.io import FileInputStream
from java.io import FileOutputStream
from java.io import ByteArrayInputStream
from java.io import ByteArrayOutputStream
from java.io import ByteArrayInputStream
from java.util import Iterator
from java.util import NoSuchElementException

logger = logging.getLogger(__name__)

fac = DocumentBuilderFactory.newInstance()
fac.setNamespaceAware(True)
builder = fac.newDocumentBuilder()
transformfac = TransformerFactory.newInstance()
xpathfac = XPathFactory.newInstance()


class XSLTCoverage(object):
    def __init__(self):
        self.traces = dict()

    def trace(self, systemId, startline, endline):
        files = self.traces
        lines = files.setdefault(systemId, dict())
        lines[startline] = lines.get(startline, 0) + 1
        lines[endline] = lines.get(endline, 0) + 1

    def writeto(self, f):
        trace_root = Element('coverage')
        packages = SubElement(trace_root, 'packages')
示例#11
0
def _getXpath():
    r'@types: -> javax.xml.xpath.XPath'
    return XPathFactory.newInstance().newXPath()
示例#12
0
class CheckRule:
    """
      a simple checker object for assertion management on response
    """
    KEYWORDS = [
        'matches', 'notmatches', 'contains', 'notcontains', 'equals',
        'notequals', 'exists', 'macro', 'eval', 'maxduration'
    ]

    def __init__(self, rule, deferred=False):

        # all keys must be lower case
        self.ruleDefinition = dict(
            (k.lower(), v) for k, v in dict(rule).iteritems())

        # Only one keyword is authorized
        intersect = set(self.KEYWORDS).intersection(
            set(self.ruleDefinition.keys()))
        if len(intersect) > 1:
            logger.error(
                'We cannot have more than one keywords "%s" in the same rule among "%s"'
                % (intersect, self.KEYWORDS))
            raise MySyntaxError(
                'We cannot have than one keywords "%s" in the same rule among"%s"'
                % (intersect, self.KEYWORDS))
        if len(intersect) == 0:
            logger.error(
                'Invalid rule: missing mandatory comparison keywords. One of "%s"'
                % self.KEYWORDS)
            raise MySyntaxError(
                'Invalid rule: missing mandatory comparison keywords. One of "%s"'
                % self.KEYWORDS)

        # the only one keyword
        self.ruleType = list(intersect)[0]
        # For Async assertion
        self.deferred = 'deferred' in self.ruleDefinition or deferred

        # ignoreCase for Xpath & Regexp
        self.ignoreCase = 'ignorecase' in self.ruleDefinition and str(
            self.ruleDefinition['ignorecase']).lower() in ('y', 'yes', 'true')
        # For "not" expression (notequals, notcontains ...)
        self.positiveCheck = True
        # if there is a ${VAR} template
        self.isPlaceholder = False
        # "equals" rule are special "contains" rule
        self.equalsRule = False

        # For Regexp rules
        self.isCompiledRule = False
        self.compiledRule = None
        self.regexpExpression = None

        # For Xpath rules
        self.hasXpath = False
        self.xpathExpression = None
        self.textQuery = False
        self.isXpathPlaceholder = False
        self.compiledXpathRule = None
        self.isXpathCompiledRule = False

        # duration rule
        if 'maxduration' in self.ruleDefinition:
            try:
                s_duration = self.ruleDefinition['maxduration'].strip()
                self.maxDuration = int(s_duration)
            # this means that we have a string
            # see if we have the
            except ValueError:
                try:
                    if s_duration[-2:] == 'ms':
                        self.maxDuration = int(s_duration[:-2])
                    elif s_duration[-1:] == 's':
                        self.maxDuration = int(s_duration[:-1]) * 1000
                except Exception, e:
                    raise MySyntaxError(
                        'Invalid rule: maxDuration must be expressed in milliseconds (ms) or seconds (s), raised: %s'
                        % str(e))

            return

        #
        # quoted string may be forced in both "equals" and ("regexp","contains") keywords
        # default behavior is:
        #  "equals"             :  literal_usage=True
        #  "regexp","contains"  :  literal_usage=False
        # default behavior is superseded by the usage of the keywords "literal","quote_string","regexp"
        #
        # Change: for **equals** keyword, "quoted" is the default
        #
        # We force the literal usage (quoted string) in the case of equals
        self.equalsRule = self.ruleType.lower().find('equals') >= 0
        self.literal_usage = True if self.equalsRule else False

        if len(
                list(
                    set(self.ruleDefinition)
                    & set(['literal', 'quote_string', 'regex']))) > 1:
            logger.error(
                'Only 1 of [literal, quote_string, regex] is accepted - please review test scenario - assertion %s'
                % self.ruleDefinition)
            raise MySyntaxError(
                'Only 1 of [literal, quote_string, regex] is accepted - please review test scenario - assertion %s'
                % self.ruleDefinition)

        #
        # This is a special case where you have complex literal values to compare (for instance quoted Http response)
        # Default is False. *** Activated if "literal: True" or quote_string: True in the Assertion rule ***
        #
        if 'literal' in self.ruleDefinition:
            self.literal_usage = self.ruleDefinition.get('literal')
        if 'quote_string' in self.ruleDefinition:
            self.literal_usage = self.ruleDefinition.get('quote_string')
        if 'regex' in self.ruleDefinition:
            #
            # for not literal_usage to False if regex is explictly specified in rule def
            # for example:  - { response_key: toto, equals: 't[a-z]+', regex: True }
            #
            self.literal_usage = not self.ruleDefinition.get('regex')

        # -------------
        # macro keyword
        # -------------------------
        self.isMacroRule = False
        if self.ruleType == 'macro':
            #
            self.isMacroRule = True
            self.macroExpression = self.ruleDefinition['macro']
            # We check the macro format
            if not GlobalPattern.dynFieldPattern.matcher(
                    self.macroExpression).find():
                raise SyntaxError(
                    'The macro "%s" format is incorrect, it is not a macro of the form module.function(parameter)'
                    % self.ruleDefinition['macro'])
            logger.trace('"macro" rule to evaluate: %s' %
                         (self.ruleDefinition['macro']))
            return

        # -------------
        # eval keyword
        # -------------------------
        self.isEvalRule = False
        self.stringToEvaluate = ''
        if self.ruleType == 'eval':
            self.isEvalRule = True
            self.stringToEvaluate = self.ruleDefinition['eval']
            logger.trace('"eval" rule to evaluate: %s' %
                         (self.ruleDefinition['eval']))
            return

            # Identify the response key to compare to
        self.responseKey=self.ruleDefinition['response_key'] if 'response_key' in self.ruleDefinition else \
        self.ruleDefinition['from'] if 'from' in self.ruleDefinition else 'responseText'

        # we remove any placeholder in the response key
        m = (GlobalPattern.dynPlaceholderPattern).matcher(self.responseKey)
        if m.find():
            self.responseKey = m.group(1)

        # -------------
        # exists keyword
        # allows to check that a rule.responseKey exists or not
        # -------------------------
        if self.ruleType == 'exists':
            return

        # -----------------
        #  Xpath rule
        # -----------------
        if 'xpath' in self.ruleDefinition:
            self.hasXpath = True
            self.xpathExpression = self.ruleDefinition['xpath']
            self.textQuery = self.xpathExpression.find('/text()') >= 0
            # To avoid NOT_FOUND in scenario checkResponse
            self.xpathExpression = self.xpathExpression.replace('/text()', '')
            self.isXpathPlaceholder = GlobalPattern.dynPlaceholderPattern.matcher(
                self.xpathExpression).find()
            if not self.isXpathPlaceholder:
                self.isXpathCompiledRule = True
                try:
                    xpathFactory = XPathFactory.newInstance()
                    self.compiledXpathRule = xpathFactory.newXPath().compile(
                        self.xpathExpression)
                    logger.trace('Compiled Xpath %s has id: %s' %
                                 (self.xpathExpression,
                                  hex(id(self.compiledXpathRule))))
                except:
                    logger.error('Unable to compile xpath rule %s' %
                                 (self.xpathExpression))
                    raise MySyntaxError('Unable to compile  xpath rule %s' %
                                        (self.xpathExpression))

        # positive or not ?
        self.positiveCheck = not self.ruleType.find('not') >= 0

        # In case of "equals", we may have "space" characters ... so we don't strip
        self.regexpExpression = self.ruleDefinition[
            self.ruleType] if self.equalsRule else self.ruleDefinition[
                self.ruleType].strip()

        self.isPlaceholder = (GlobalPattern.dynPlaceholderPattern).matcher(
            self.ruleDefinition[self.ruleType]).find()

        # ---------------
        # JSON rule
        # ----------------
        self.jsonRule = self.ruleDefinition.get('json', False)
        self.jsonStrict = self.ruleDefinition.get('strict', False)
        if self.jsonRule:
            logger.trace('JSON Rule declared')
            # no compilation
            # force literal usage
            self.literal_usage = True
            return

        # ---------------
        # regexp rule
        # optimization : compile rule if there is no placeholder
        # ----------------
        if not self.isPlaceholder:

            self.isCompiledRule = True
            logger.trace(
                '[CheckRule][No placeholder=>compiling rule][equals=%s][literal=%s][value=%s][positiveCheck=%s]'
                % (self.equalsRule, self.literal_usage, self.regexpExpression,
                   str(self.positiveCheck)))
            tmp_regexpExpression = Pattern.quote(str(
                self.regexpExpression)) if self.literal_usage else str(
                    self.regexpExpression)
            if self.equalsRule:
                tmp_regexpExpression = '^%s$' % (tmp_regexpExpression)
                logger.trace(
                    '[CheckRule][equals=%s][literal=%s][tmp_regexp=%s]' %
                    (self.equalsRule, self.literal_usage,
                     tmp_regexpExpression))
            self.compiledRule = Pattern.compile(
                tmp_regexpExpression, Pattern.CASE_INSENSITIVE
                | Pattern.DOTALL) if self.ignoreCase else Pattern.compile(
                    tmp_regexpExpression, Pattern.DOTALL)
示例#13
0
 def __init__(self):
     self.__xpath = XPathFactory.newInstance().newXPath()
示例#14
0
文件: __init__.py 项目: hanul93/pyhwp
from java.io import FileInputStream
from java.io import FileOutputStream
from java.io import ByteArrayInputStream
from java.io import ByteArrayOutputStream
from java.io import ByteArrayInputStream
from java.util import Iterator
from java.util import NoSuchElementException


logger = logging.getLogger(__name__)

fac = DocumentBuilderFactory.newInstance()
fac.setNamespaceAware(True)
builder = fac.newDocumentBuilder()
transformfac = TransformerFactory.newInstance()
xpathfac = XPathFactory.newInstance()


class XSLTCoverage(object):

    def __init__(self):
        self.traces = dict()

    def trace(self, systemId, startline, endline):
        files = self.traces
        lines = files.setdefault(systemId, dict())
        lines[startline] = lines.get(startline, 0) + 1
        lines[endline] = lines.get(endline, 0) + 1

    def writeto(self, f):
        trace_root = Element('coverage')
示例#15
0
 def __init__(self):
     self.__xpath = XPathFactory.newInstance().newXPath()