aboutsummaryrefslogtreecommitdiff
blob: d1200b6ce3babcf3bc33b60fa43d6b66099aa280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: UTF-8 -*-
# Copyright 2004-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

from xml.dom import NotFoundErr

class DomRewriter:
    """
    The old DOM rewriter is still around for index based stuff. It can
    be used for all the complex stuff but portage needed features should
    be in StreamRewriterBase subclasses as they are much faster.
    """
    def __init__(self, modifyElems = None, attributes = None , values=None, index=None):
        self.modifyElems = modifyElems
        self.attributes = attributes
        self.values = values
        self.index = index


    def delete_elements(self, document, **kwargs):
        if not self.modifyElems:
            return

        tomodify = []
        for tag in self.modifyElems:
            matches = document.getElementsByTagName(tag)
            if matches:
                if self.index == None:
                    for match in matches:
                        tomodify.append(match)
                else:
                    tomodify.append(matches[self.index])

        for elem in tomodify:
            for i,attr in enumerate(self.attributes):
                if self.values:
                    elem.setAttribute(attr, self.values[i])
                else:
                    try:
                        elem.removeAttribute(attr)
                    except DomRewriter.NotFoundErr:
                        continue


    def add_gentoo_classpath(self,document,**kwargs):
        if 'classpath' not in kwargs or not kwargs['classpath']:
            return

        cp = document.createElement("classpath")
        cp.setAttribute("path", kwargs['classpath'])
        last_parent = None

        # Add our classpath element to every node already containing a classpath element.
        for match in document.getElementsByTagName("classpath"):
            if last_parent != match.parentNode:
                match.parentNode.appendChild(cp.cloneNode(True))
                last_parent = match.parentNode

        # Add our classpath element to every javac node we missed earlier.
        for match in document.getElementsByTagName("javac") + document.getElementsByTagName("xjavac"):
            if not match.getElementsByTagName("classpath"):
                match.appendChild(cp.cloneNode(True))


    def process(self,in_stream,callback=None,*args,**kwargs):
        from xml.dom import minidom
        self.document = minidom.parseString(in_stream);

        if callback:
            callback(self.document,*args,**kwargs)


    def write(self,stream):
        stream.write(self.document.toxml('utf-8').decode('utf-8'))

# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap: