xmldep.tcl

Go to the documentation of this file.
00001 /*  xmldep.tcl --*/
00002 /* */
00003 /*  Find the dependencies in an XML document.*/
00004 /*  Supports external entities and XSL include/import.*/
00005 /* */
00006 /*  TODO:*/
00007 /*  XInclude*/
00008 /* */
00009 /*  Copyright (c) 2001-2003 Zveno Pty Ltd*/
00010 /*  http://www.zveno.com/*/
00011 /* */
00012 /*  See the file "LICENSE" in this distribution for information on usage and*/
00013 /*  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00014 /* */
00015 /*  $Id: xmldep.tcl,v 1.3 2003/12/09 04:43:15 balls Exp $*/
00016 
00017 package require xml
00018 
00019 package provide xml::dep 1.0
00020 
00021 namespace xml::dep {
00022     namespace export depend
00023 
00024     variable extEntities
00025     array  extEntities =  {}
00026 
00027     variable XSLTNS http://www.w3.org/1999/XSL/Transform
00028 }
00029 
00030 /*  xml::dep::depend --*/
00031 /* */
00032 /*  Find the resources which an XML document*/
00033 /*  depends on.  The document is parsed*/
00034 /*  sequentially, rather than using DOM, for efficiency.*/
00035 /* */
00036 /*  TODO:*/
00037 /*  Asynchronous parsing.*/
00038 /* */
00039 /*  Arguments:*/
00040 /*  xml XML document entity*/
00041 /*  args    configuration options*/
00042 /* */
00043 /*  Results:*/
00044 /*  Returns list of resource (system) identifiers*/
00045 
00046 ret  xml::dep::depend (type xml , type args) {
00047     variable resources
00048     variable entities
00049 
00050     set resources {}
00051     catch {unset entities}
00052     array set entities {}
00053 
00054     set p [xml::parser \
00055         -elementstartcommand [namespace code ElStart]   \
00056         -doctypecommand [namespace code DocTypeDecl]    \
00057         -entitydeclcommand [namespace code EntityDecl]  \
00058         -entityreferencecommand [namespace code EntityReference]    \
00059         -validate 1 \
00060         ]
00061     if {[llength $args]} {
00062     eval [list $p] configure $args
00063     }
00064     $p parse $xml
00065 
00066     return $resources
00067 }
00068 
00069 /*  xml::dep::ElStart --*/
00070 /* */
00071 /*  Process start element*/
00072 /* */
00073 /*  Arguments:*/
00074 /*  name    tag name*/
00075 /*  atlist  attribute list*/
00076 /*  args    options*/
00077 /* */
00078 /*  Results:*/
00079 /*  May add to resources list*/
00080 
00081 ret  xml::dep::ElStart (type name , type atlist , type args) {
00082     variable XSLTNS
00083     variable resources
00084 
00085     array set opts {
00086     -namespace {}
00087     }
00088     array set opts $args
00089 
00090     switch -- $opts(-namespace) \
00091         $XSLTNS {
00092     switch $name {
00093         import -
00094         include {
00095         array set attr {
00096             href {}
00097         }
00098         array set attr $atlist
00099 
00100         if {[string length $attr(href)]} {
00101             if {[lsearch $resources $attr(href)] < 0} {
00102             lappend resources $attr(href)
00103             }
00104         }
00105 
00106         }
00107     }
00108     }
00109 }
00110 
00111 /*  xml::dep::DocTypeDecl --*/
00112 /* */
00113 /*  Process Document Type Declaration*/
00114 /* */
00115 /*  Arguments:*/
00116 /*  name    Document element*/
00117 /*  pubid   Public identifier*/
00118 /*  sysid   System identifier*/
00119 /*  dtd Internal DTD Subset*/
00120 /* */
00121 /*  Results:*/
00122 /*  Resource added to list*/
00123 
00124 ret  xml::dep::DocTypeDecl (type name , type pubid , type sysid , type dtd) {
00125     variable resources
00126 
00127     puts stderr [list DocTypeDecl $name $pubid $sysid dtd]
00128 
00129     if {[string length $sysid] && \
00130         [lsearch $resources $sysid] < 0} {
00131     lappend resources $sysid
00132     }
00133 
00134     return {}
00135 }
00136 
00137 /*  xml::dep::EntityDecl --*/
00138 /* */
00139 /*  Process entity declaration, looking for external entity*/
00140 /* */
00141 /*  Arguments:*/
00142 /*  name    entity name*/
00143 /*  sysid   system identifier*/
00144 /*  pubid   public identifier or repl. text*/
00145 /* */
00146 /*  Results:*/
00147 /*  Store external entity info for later reference*/
00148 
00149 ret  xml::dep::EntityDecl (type name , type sysid , type pubid) {
00150     variable extEntities
00151 
00152     puts stderr [list EntityDecl $name $sysid $pubid]
00153 
00154     set extEntities($name) $sysid
00155 }
00156 
00157 /*  xml::dep::EntityReference --*/
00158 /* */
00159 /*  Process entity reference*/
00160 /* */
00161 /*  Arguments:*/
00162 /*  name    entity name*/
00163 /* */
00164 /*  Results:*/
00165 /*  May add to resources list*/
00166 
00167 ret  xml::dep::EntityReference name (
00168     type variable , type extEntities
00169     , type variable , type resources
00170 
00171     , type puts , type stderr [, type list , type EntityReference $, type name]
00172 
00173     , type if , optional [info =exists extEntities($name)] =&& \
00174     =[lsearch $resources =$extEntities($name)] < =0 , optional 
00175     lappend =resources $extEntities($name)
00176     
00177 
00178 )
00179 
00180 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1