Ticket #43386: xmlcatalog-1.0.tcl

File xmlcatalog-1.0.tcl, 11.7 KB (added by lockhart (Thomas Lockhart), 10 years ago)

New portgroup to support common operations across docbook sgml and xml.

Line 
1# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
2# $Id$
3#
4# Copyright (c) 2013 The MacPorts Project
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met:
10#
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16# 3. Neither the name of The MacPorts Project nor the names of its
17#    contributors may be used to endorse or promote products derived from
18#    this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31#
32#
33# This PortGroup manages global XML catalogs for docbook and other packages
34# with xml and sgml catalogs.
35# Set name and version as for a normal standalone port,
36# then set catalog parameters as described in more detail below.
37#
38# Usage:
39#
40# PortGroup xmlcatalog 1.0
41# xml.catalog childcatalog
42# xml.addtocatalog rootcatalog newcatalog
43# sgml.catalog childcatalog
44# sgml.addtocatalog rootcatalog newcatalog
45#
46# where xml.catalog and sgml.catalog will add a new catalog
47# to the root catalog and xml.addtocatalog and sgml.addtocatalog
48# will add a new catalog to the catalog specified as the first argument.
49# Catalogs will be created if they do not exist.
50
51default categories xmlcatalog
52
53depends_run port:xmlcatmgr
54
55global xml.rootdir
56set xml.rootdir ${prefix}/etc
57global xml.confdir
58set xml.confdir ${prefix}/etc/xml
59global sgml.confdir
60set sgml.confdir ${prefix}/etc/sgml
61
62options xml.catalog
63option_proc xml.catalog xml._set_catalog
64options xml.addtocatalog
65option_proc xml.addtocatalog xml._set_catalog_entry
66global xml.rootcatalog xml.catalog_list
67set xml.rootcatalog ${xml.confdir}/catalog
68set xml.catalog_list {}
69
70options xml.entity
71option_proc xml.entity xml._set_entity
72global xml.entity_list
73set xml.entity_list {}
74
75options xml.rewrite
76option_proc xml.rewrite xml._set_rewrite
77global xml.rewrite_list
78set xml.rewrite_list {}
79
80options sgml.catalog
81option_proc sgml.catalog sgml._set_catalog
82options sgml.addtocatalog
83option_proc sgml.addtocatalog sgml._set_catalog_entry
84global sgml.rootcatalog sgml.catalog_list
85set sgml.rootcatalog ${sgml.confdir}/catalog
86set sgml.catalog_list {}
87
88depends_lib-append port:xmlcatmgr
89
90proc xml._set_catalog {option action args} {
91    global xml.catalog_list
92    global xml.rootcatalog
93    if {$action != "set"} return
94    foreach catalog [option ${option}] {
95        ui_debug "add catalog ${catalog} to ${xml.rootcatalog}"
96        lappend xml.catalog_list [subst {${xml.rootcatalog} ${catalog}}]
97    }
98}
99
100proc xml._set_catalog_entry {option action args} {
101    global xml.catalog_list
102    global xml.rootcatalog
103    if {$action != "set"} return
104    set clist [option ${option}]
105    if {[llength ${clist}] >= 2} {
106        set c [lindex $clist 0]
107        foreach catalog [lrange ${clist} 1 end] {
108            ui_debug "add catalog ${catalog} to ${c}"
109            lappend xml.catalog_list [subst {${c} ${catalog}}]
110        }
111    }
112}
113
114proc xml._set_entity {option action args} {
115    global xml.entity_list
116    if {$action != "set"} return
117    set entity [option ${option}]
118    ui_debug "add entity ${entity}"
119    lappend xml.entity_list ${entity}
120}
121
122proc xml._set_rewrite {option action args} {
123    global xml.rewrite_list
124    if {$action != "set"} return
125    set rewrite [option ${option}]
126    ui_debug "add rewrite ${rewrite}"
127    lappend xml.rewrite_list ${rewrite}
128}
129
130proc xml._install_catalogs {} {
131    global xml.catalog_list xml.entity_list xml.rewrite_list
132    global xml.rootcatalog
133    foreach catalog ${xml.catalog_list} {
134        set c [join [lindex ${catalog} 0]]
135        set v [join [lindex ${catalog} 1]]
136        if {![file exists ${c}]} {
137            ui_debug "create catalog ${c}"
138            system "xmlcatmgr -c ${c} create"
139        }
140        # Add the nextCatalog entry to the catalog if it does not exist
141        # xmlcatmgr returns one if the existing entry is not found
142        if {[catch {exec xmlcatmgr -c ${c} lookup ${v}}]} {
143            ui_debug "add catalog ${v} to ${c}"
144            system "xmlcatmgr -c ${c} add nextCatalog '${v}'"
145        } else {
146            ui_debug "catalog ${v} found in ${c}"
147        }
148    }
149    # Each entity is stored as a list
150    foreach entity ${xml.entity_list} {
151        set o [join [lindex ${entity} 0]]
152        set v [join [lindex ${entity} 1]]
153        if {![file exists ${xml.rootcatalog}]} {
154            ui_debug "create catalog ${xml.rootcatalog}"
155            system "xmlcatmgr -c ${xml.rootcatalog} create"
156        }
157        # Add the entity to the catalog if it does not exist
158        # xmlcatmgr returns one if the existing entry is not found
159        ui_debug "check for entity ${entity} (${o} ${v})"
160        if {[catch {exec xmlcatmgr -c ${xml.rootcatalog} lookup ${o}}]} {
161            ui_debug "add entity public ${o}"
162            system "xmlcatmgr -c ${xml.rootcatalog} add public '${o}' '${v}'"
163        } else {
164            ui_debug "entity ${o} found; not added"
165        }
166    }
167    foreach rewrite ${xml.rewrite_list} {
168        set t [join [lindex ${rewrite} 0]]
169        set o [join [lindex ${rewrite} 1]]
170        set v [join [lindex ${rewrite} 2]]
171        if {![file exists ${xml.rootcatalog}]} {
172            ui_debug "create catalog ${xml.rootcatalog}"
173            system "xmlcatmgr -c ${xml.rootcatalog} create"
174        }
175        ui_debug "add rewrite ${rewrite} (${t} ${o} ${v})"
176        # Add the rewrite entry to the catalog if it does not exist
177        # xmlcatmgr returns one if the existing entry is not found
178        if {[catch {exec xmlcatmgr -c ${xml.rootcatalog} lookup ${o}}]} {
179            system "xmlcatmgr -c ${xml.rootcatalog} add rewrite${t} ${o} ${v}"
180        }
181    }
182}
183
184proc xml._uninstall_catalogs {} {
185    global xml.catalog_list xml.entity_list xml.rewrite_list
186    global xml.rootcatalog
187    foreach catalog ${xml.catalog_list} {
188        set c [join [lindex ${catalog} 0]]
189        set v [join [lindex ${catalog} 1]]
190        # Remove the CATALOG entry from the catalog
191        # xmlcatmgr returns zero if the existing entry is found
192        if {![catch {exec xmlcatmgr -c ${c} lookup ${v}}]} {
193            ui_debug "remove catalog ${v} from ${c}"
194            system "xmlcatmgr -c ${c} remove nextCatalog ${v}"
195        } else {
196            ui_debug "catalog ${v} not found in ${c}"
197        }
198    }
199    foreach entity ${xml.entity_list} {
200        set o [join [lindex ${entity} 0]]
201        set v [join [lindex ${entity} 1]]
202        # Remove the public entry from the catalog
203        # xmlcatmgr returns zero if the existing entry is found
204        if {![catch {exec xmlcatmgr -c ${xml.rootcatalog} lookup ${o}}]} {
205            ui_debug "remove entity ${entity}"
206            system "xmlcatmgr -c ${xml.rootcatalog} remove public '${o}'"
207        } else {
208            ui_debug "entity ${entity} not found for removal"
209        }
210    }
211    foreach rewrite ${xml.rewrite_list} {
212        set t [join [lindex ${rewrite} 0]]
213        set o [join [lindex ${rewrite} 1]]
214        set v [join [lindex ${rewrite} 2]]
215        # Remove the rewrite entry from the catalog
216        # xmlcatmgr returns zero if the existing entry is found
217        if {![catch {exec xmlcatmgr -c ${xml.rootcatalog} lookup ${o}}]} {
218            ui_debug "remove rewrite ${rewrite}"
219            system "xmlcatmgr -c ${xml.rootcatalog} remove rewrite${t} '${o}'"
220        } else {
221            ui_debug "rewrite ${rewrite} not found for removal"
222        }
223    }
224}
225
226proc sgml._set_catalog {option action args} {
227    global sgml.catalog_list
228    global sgml.rootcatalog
229    if {$action != "set"} return
230    foreach catalog [option ${option}] {
231        ui_debug "add catalog ${catalog} to ${sgml.rootcatalog}"
232        lappend sgml.catalog_list [subst {${sgml.rootcatalog} ${catalog}}]
233    }
234}
235
236proc sgml._set_catalog_entry {option action args} {
237    global sgml.catalog_list
238    global sgml.rootcatalog
239    if {$action != "set"} return
240    set clist [option ${option}]
241    if {[llength ${clist}] >= 2} {
242        set c [lindex $clist 0]
243        foreach catalog [lrange ${clist} 1 end] {
244            ui_debug "add catalog ${catalog} to ${c}"
245            lappend sgml.catalog_list [subst {${c} ${catalog}}]
246        }
247    }
248}
249
250proc sgml._install_catalogs {} {
251    global sgml.catalog_list
252    global sgml.rootcatalog
253    foreach catalog ${sgml.catalog_list} {
254        set c [join [lindex ${catalog} 0]]
255        set v [join [lindex ${catalog} 1]]
256        if {![file exists ${c}]} {
257            ui_debug "create catalog ${c}"
258            system "xmlcatmgr -s -c ${c} create"
259        }
260        # Add the nextCatalog entry to the catalog if it does not exist
261        # xmlcatmgr returns one if the existing entry is not found
262        if {[catch {exec xmlcatmgr -s -c ${c} lookup ${v}}]} {
263            ui_debug "add catalog ${v} to ${c}"
264            system "xmlcatmgr -s -c ${c} add CATALOG '${v}'"
265        } else {
266            ui_debug "catalog ${v} found in ${c}"
267        }
268    }
269}
270
271proc sgml._uninstall_catalogs {} {
272    global sgml.catalog_list
273    global sgml.rootcatalog
274    foreach catalog ${sgml.catalog_list} {
275        set c [join [lindex ${catalog} 0]]
276        set v [join [lindex ${catalog} 1]]
277        # Remove the CATALOG entry from the catalog
278        # xmlcatmgr returns zero if the existing entry is found
279        if {![catch {exec xmlcatmgr -s -c ${c} lookup ${v}}]} {
280            ui_debug "remove catalog ${v} from ${c}"
281            system "xmlcatmgr -s -c ${c} remove CATALOG ${v}"
282        } else {
283            ui_debug "catalog ${v} not found in ${c}"
284        }
285    }
286}
287
288post-activate {
289    global xml.catalog_list xml.entity_list xml.rewrite_list
290    global xml.rootcatalog
291    global xml.rootdir
292    global xml.confdir
293    global sgml.confdir
294    global sgml.rootcatalog
295    # XML catalog
296    # Make the directory if it doesn't exist
297    if {![file exists ${xml.confdir}]} {
298        xinstall -m 755 -d ${xml.confdir}
299    }
300
301    # Create the XML catalog file if it doesn't exist
302    if {![file exists ${xml.rootcatalog}]} {
303        system "xmlcatmgr -c ${xml.rootcatalog} create"
304    }
305
306    # SGML catalog
307    # Make the directory if it doesn't exist
308    if {![file exists ${sgml.confdir}]} {
309        xinstall -m 755 -d ${sgml.confdir}
310    }
311
312    # Create the SGML catalog file if it doesn't exist
313    if {![file exists ${sgml.rootcatalog}]} {
314        system "xmlcatmgr -s -c ${sgml.rootcatalog} create"
315    }
316
317    xml._install_catalogs
318    sgml._install_catalogs
319}
320
321pre-deactivate {
322    sgml._uninstall_catalogs
323    xml._uninstall_catalogs
324    # Should not remove the catalogs because if we update this package
325    # other dependent packages will not be reinstalled to replace entries
326    # they may have inserted in the catalog(s).
327    # exec xmlcatmgr -s -c ${sgml.rootcatalog} destroy
328    # exec xmlcatmgr -c ${xml.rootcatalog} destroy
329}