aboutsummaryrefslogtreecommitdiff
blob: 92844b93eec1f8d4501165994edaad8bc07265dc (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: UTF-8 -*-

# Copyright 2004-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

from xml.dom import NotFoundErr
#import os
#import sys
#import StringIO
#import xml.sax.saxutils import quoteattr,escape

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.
    """
    from xml.dom import NotFoundErr
    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):
        newcp = kwargs.has_key('classpath') and kwargs['classpath'] or "void"
        newcp = newcp.split(":")
        gcp = document.createElement("path")
        for cp in newcp:
            pe = document.createElement("pathelement")
            pe.setAttribute("path",cp)
            gcp.appendChild(pe)


        # classpath nodes:
        # if no refud:
        #  remove inner elems
        #  add our gentoo classpath node
        # else
        #  rename refid references
        matches = document.getElementsByTagName("classpath")
        handled_refs = set()
        for match in matches:
            if not match.hasAttribute("refid"):
                for node in match.childNodes[:]:
                    match.removeChild(node)
                    node.unlink()

                    match.appendChild(gcp.cloneNode(True))
            else:
                refid = match.getAttribute("refid")
                for ref in document.getElementsByTagName("path"):
                    id = ref.getAttribute("id")
                    if id not in handled_refs and id == refid:
                        for node in ref.childNodes[:]:
                            ref.removeChild(node)
                            node.unlink()

                        for pathnode in (gcp.cloneNode(deep=True)).childNodes:
                            ref.appendChild(pathnode.cloneNode(deep=True))

                        handled_refs.add(id)

        # rewrite javac elements
        matches = document.getElementsByTagName("javac")
        for match in matches:
            classpath = match.getAttribute("classpath")
            if classpath:
                match.removeAttribute("classpath")

            for node in match.childNodes[:]:
                if node.nodeName == "classpath":
                    match.removeChild(node)
                    node.unlink()

            classpath = document.createElement("classpath")
            classpath.appendChild(gcp.cloneNode(True))
            match.appendChild(classpath)


    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):
        from xml.dom.ext import PrettyPrint
        PrettyPrint(self.document,stream)

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