%D \module
%D   [      file=x-fig-00,
%D        version=2001.03.21,
%D          title=\CONTEXT\ Style File,
%D       subtitle=Figure Base Loading,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for 
%C details. 

\readfile{x-res-04} \donothing \donothing \endinput 

% naast label, ook fig als ref en dan naar fig ref springen 

%D This module implements an interface to a figure database
%D and file. The database is formatted in \XML\ conforming 
%D the following \DTD: 
%D
%D \typefile{x-fig-00.dtd}
%D
%D A figure base coded this way looks like: 
%D 
%D \starttypen
%D <!-- texexec --pdf --use=fig-01 figtest.xml --> 
%D 
%D <figurelibrary language="nl"> 
%D 
%D   <description>
%D     <organization>PRAGMA Advanced Document Engineering</organization>
%D     <project>Manuals</project>
%D     <product>Beginners Manual</product>
%D     <comment>A bunch of figures.</comment>
%D   </description>
%D 
%D   <figure>
%D     <file>koe.pdf</file>
%D     <label>a dutch cow</label>
%D     <copyright>Corel Draw Suite</copyright>
%D     <comment>I bet that you've seen this cow before.</comment>
%D   </figure>
%D 
%D   <figure>
%D     <dummy width="4cm" height="3cm">non existent</dummy>
%D     <label>a european cow</label>
%D     <copyright>Nobody</copyright>
%D     <comment>When will we talk about European cows?</comment>
%D   </figure>
%D 
%D </figurelibrary> 
%D \stoptypen 
%D 
%D You can convert this base into a \PDF\ file using 
%D \TEXEXEC\ and another module in this suite. 
%D 
%D \starttypen 
%D texexec --pdf --use=fig-make yourfile.xml
%D \stoptypen 
%D 
%D You can now select a graphic from this file using the 
%D 
%D \starttypen 
%D \externalfigure[a dutch cow][width=4cm]
%D \stoptypen 
%D
%D This module overloads this command so that a figure is 
%D it first searched in the list of databases. 
%D 
%D \starttypen 
%D \usemodule     [fig-base]
%D \usefigurebases[yourfile]
%D \stoptypen 
%D 
%D The special keyword \type {reset} can be used to reset 
%D this list.

\startcommands      dutch                            english
                    german                           czech
                    italian                          romanian

     usefigurebase: gebruikfiguurbestand             usefigurebase 
                    usefigurebase                    usefigurebase 
                    usefigurebase                    usefigurebase 

\stopcommands 

\unprotect 

\startXMLmapping [-] [figbase]

\defineXMLprocess     [figurelibrary]
\defineXMLignore      [description]
\defineXMLignore      [copyright]
\defineXMLignore      [comment]
\defineXMLignore      [status]
\defineXMLpush        [file]
\defineXMLpush        [label]
\defineXMLpush        [dummy]
\defineXMLenvironment [figure] \figbase@StartFigure \figbase@StopFigure

\stopXMLmapping

\newcounter\figurefilepage

\def\figbase@StartFigure
  {\bgroup}

\def\figbase@StopFigure
  {\doglobal\increment\figurefilepage
   \doif\askedlabel{\XMLpop{label}}
     {\doglobal\settrue\figurefiledone
      \xdef\figurefilename{\XMLpop{file}}\endinput}%
   \egroup}

\def\getfigurefilename#1#2%
  {\startnointerference
     \traceXMLelementsfalse
     \startXMLmapping[-][figbase]
       \resetfigurefilebase
       \XMLerase{file} 
       \XMLerase{dummy}
       \XMLerase{label}
       \xdef\figurefilebase{#1}%
       \doglobal\newcounter\figurefilepage
       \def\askedlabel{#2}
       \processcommacommand[\figurepathlist]\dogetfigurefilename
     \stopXMLmapping
   \stopnointerference}

\globalletempty\figurebasepath

\def\dogetfigurefilename#1%
  {\ifx\figurefilename\empty
     \bgroup
     \xdef\figurefilebasepath{#1}%
     \assignfullfilename{#1}\figurefilebase\to\filename
     \expanded{\processXMLfilegrouped{\filename.xml}}%
     \ifx\figurefilename\empty\else
       \globallet\figurefilebase\figurefilebase
     \fi
     \egroup
   \fi}

\def\resetfigurefilebase
  {\globalletempty\figurefilebase
   \globalletempty\figurefilename
   \globalletempty\figurefilebasepath
   \globalletempty\figurefilepage}

\let\normalcalculateexternalfigure\calculateexternalfigure

\def\calculateexternalfigure[#1][#2][#3][#4][#5][#6]%
  {\resetfigurefilebase
   \ifx\figurebaselist\empty 
     \normalcalculateexternalfigure
       [#1][#2][#3][#4][#5][#6]%
   \else
     \def\docommando##1%
       {\getfigurefilename{##1}{#3}%
        \ifx\figurefilename\empty\else\quitcommalist\fi}%
     \processcommacommand[\figurebaselist]\docommando
     \ifx\figurefilename\empty
       \stripspaces\from#3\to\figurefilename % to be sure 
       \normalcalculateexternalfigure
         [#1][#2][\figurefilename][#4][#5][#6]%
     \else
       \let\figurepathlist\figurefilebasepath
       \normalcalculateexternalfigure
         [#1][#2][\figurefilebase.pdf][\c!pagina=\figurefilepage,#4][#5][#6]%
     \fi
   \fi}

\def\usefigurebase[#1]%
  {\doifelse{#1}\v!reset
     {\let\figurebaselist\empty}
     {\addtocommalist{#1}\figurebaselist}}

\let\figurebaselist\empty

\resetfigurefilebase

\protect \endinput 

\usefigurebase[figtest]

\externalfigure[koetje]             [width=3cm]
\externalfigure[de molen op de dijk][width=3cm]
\externalfigure[de molen op de dijk][width=2cm]
\externalfigure[weet ik veel]       [width=3cm]
\externalfigure[weet ik veel]       [width=2cm]
\externalfigure[weet ik wat]        [width=2cm]
\externalfigure[koe]                [width=2cm]
