File: xmldep.tcl

package info (click to toggle)
coccinella 0.96.20-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 13,108 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 64
file content (179 lines) | stat: -rw-r--r-- 3,432 bytes parent folder | download | duplicates (4)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# xmldep.tcl --
#
#	Find the dependencies in an XML document.
#	Supports external entities and XSL include/import.
#
# TODO:
#	XInclude
#
# Copyright (c) 2001-2003 Zveno Pty Ltd
# http://www.zveno.com/
#
# See the file "LICENSE" in this distribution for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# $Id: xmldep.tcl,v 1.3 2006-12-19 13:27:09 matben Exp $

package require xml

package provide xml::dep 1.0

namespace eval xml::dep {
    namespace export depend

    variable extEntities
    array set extEntities {}

    variable XSLTNS http://www.w3.org/1999/XSL/Transform
}

# xml::dep::depend --
#
#	Find the resources which an XML document
#	depends on.  The document is parsed
#	sequentially, rather than using DOM, for efficiency.
#
# TODO:
#	Asynchronous parsing.
#
# Arguments:
#	xml	XML document entity
#	args	configuration options
#
# Results:
#	Returns list of resource (system) identifiers

proc xml::dep::depend {xml args} {
    variable resources
    variable entities

    set resources {}
    catch {unset entities}
    array set entities {}

    set p [xml::parser \
	    -elementstartcommand [namespace code ElStart]	\
	    -doctypecommand [namespace code DocTypeDecl]	\
	    -entitydeclcommand [namespace code EntityDecl]	\
	    -entityreferencecommand [namespace code EntityReference]	\
	    -validate 1	\
	    ]
    if {[llength $args]} {
	eval [list $p] configure $args
    }
    $p parse $xml

    return $resources
}

# xml::dep::ElStart --
#
#	Process start element
#
# Arguments:
#	name	tag name
#	atlist	attribute list
#	args	options
#
# Results:
#	May add to resources list

proc xml::dep::ElStart {name atlist args} {
    variable XSLTNS
    variable resources

    array set opts {
	-namespace {}
    }
    array set opts $args

    switch -- $opts(-namespace) \
	    $XSLTNS {
	switch $name {
	    import -
	    include {
		array set attr {
		    href {}
		}
		array set attr $atlist

		if {[string length $attr(href)]} {
		    if {[lsearch $resources $attr(href)] < 0} {
			lappend resources $attr(href)
		    }
		}

	    }
	}
    }
}

# xml::dep::DocTypeDecl --
#
#	Process Document Type Declaration
#
# Arguments:
#	name	Document element
#	pubid	Public identifier
#	sysid	System identifier
#	dtd	Internal DTD Subset
#
# Results:
#	Resource added to list

proc xml::dep::DocTypeDecl {name pubid sysid dtd} {
    variable resources

    puts stderr [list DocTypeDecl $name $pubid $sysid dtd]

    if {[string length $sysid] && \
	    [lsearch $resources $sysid] < 0} {
	lappend resources $sysid
    }

    return {}
}

# xml::dep::EntityDecl --
#
#	Process entity declaration, looking for external entity
#
# Arguments:
#	name	entity name
#	sysid	system identifier
#	pubid	public identifier or repl. text
#
# Results:
#	Store external entity info for later reference

proc xml::dep::EntityDecl {name sysid pubid} {
    variable extEntities

    puts stderr [list EntityDecl $name $sysid $pubid]

    set extEntities($name) $sysid
}

# xml::dep::EntityReference --
#
#	Process entity reference
#
# Arguments:
#	name	entity name
#
# Results:
#	May add to resources list

proc xml::dep::EntityReference name {
    variable extEntities
    variable resources

    puts stderr [list EntityReference $name]

    if {[info exists extEntities($name)] && \
	[lsearch $resources $extEntities($name)] < 0} {
	lappend resources $extEntities($name)
    }

}