%D \module
%D   [       file=xtag-ext,
%D        version=2001.03.21,
%D          title=\CONTEXT\ XML Support,
%D       subtitle=Extra Macros,
%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.

\beginTEX
  \writestatus{xml}{sorry, xml is only supported in (pdf)etex}
  \expandafter \endinput
\endTEX

\writestatus{loading}{Context XML Macros (extras)}

\unprotect

%D \macros
%D  {startXMLmapping}
%D
%D You can define macros within a namespace, so that they
%D will not conflict (don't confuse this with \XML\
%D namespaces.)
%D
%D \starttypen
%D \startXMLmapping [tag] | [-] [tag] | [+] [tag]
%D   definitions
%D \stopXMLmapping
%D \stoptypen
%D
%D When a \type {[+]} is specified, the mappings will
%D nest.

\let\normal@@XMLelement\@@XMLelement

\def\resetXMLmapping
  {\let\@@XMLelement\normal@@XMLelement
   \let\@@XMLmapping\empty}

\resetXMLmapping

\def\startXMLmapping
  {\dodoubleempty\dostartXMLmapping}

% \def\dostartXMLmapping[#1][#2]% sneller maken
%   {\pushmacro\@@XMLelement
%    \pushmacro\@@XMLmapping
%    \ifsecondargument
%      \doifelse{#1}{-}
%        {\donostartXMLmapping{#2}}
%        {\doifelse{#1}{+}
%           {\dodostartXMLmapping{#2}}
%           {\donostartXMLmapping{#2}}}%
%    \else
%      \donostartXMLmapping{#1}%
%    \fi
%    \unprotect}

\def\dostartXMLmapping[#1#2][#3]%
  {\pushmacro\@@XMLelement
   \pushmacro\@@XMLmapping
   \ifsecondargument
     \if\noexpand#1-%
       \@EA\donostartXMLmapping
     \else\if\noexpand#1+%
       \@EAEAEA\dodostartXMLmapping
     \else
       \@EAEAEA\donostartXMLmapping
     \fi\fi{#3}%
   \else
     \donostartXMLmapping{#1#2}%
   \fi
   \unprotect}

\def\donostartXMLmapping#1%
  {\let\@@XMLprevelement\@@XMLelement
   \edef\@@XMLmapping{#1}%
   \edef\@@XMLelement{\normal@@XMLelement+#1}}

\def\dodostartXMLmapping#1%
  {\let\@@XMLprevelement\@@XMLelement
   \edef\@@XMLmapping{\@@XMLmapping+#1}%
   \edef\@@XMLelement{\@@XMLelement+#1}}

\def\stopXMLmapping
  {\protect
   \popmacro\@@XMLmapping
   \popmacro\@@XMLelement}

%D \macros
%D  {startXMLmappinggroup}
%D
%D Imagine something:
%D
%D \starttypen
%D \defineXMLenvironment [something]
%D   {\startXMLmapping[whatever]}
%D   {\stopXMLmapping}
%D \stoptypen
%D
%D Here the \type {\stopXMLmapping} will never be reached
%D because we are in a mapping. Therefore we need:

\def\startXMLmappinggroup
  {\dodoubleempty\dostartXMLmappinggroup}

\def\dostartXMLmappinggroup[#1][#2]%
  {\bgroup
   \letcscsname\savedXMLmeaning\csname\@@XMLelement:/\currentXMLelement\endcsname
   \dostartXMLmapping[#1][#2]% do ! else wrong !
   \letcsnamecs\csname\@@XMLelement:/\currentXMLelement\endcsname\savedXMLmeaning}

\def\stopXMLmappinggroup
  {\stopXMLmapping
   \egroup}

%D Context Directives:

\def\@@CTXML{@@CTXML}

\def\defineXMLdirective
  {\dodoubleempty\dodefineXMLdirective}

\long\def\dodefineXMLdirective[#1][#2]#3%
  {\defineXMLprocessor[context-#1-directive]{\dohandleXMLdirective{#1}{#3}}%
   \ifsecondargument
     \long\setvalue{\@@CTXML-#1-#2}{#3}%
   \fi}

\def\dohandleXMLdirective#1#2#3%
  {\dodohandleXMLdirective#3 @ @ @\end{#1}{#2}}

%\def\dodohandleXMLdirective#1 #2 #3 #4\end#5#6%
%  {\doifdefinedelse{\@@CTXML-#5-#1}
%     {\getvalue{\@@CTXML-#5-#1}[#2=#3]}
%     {#6[#1][#2=#3]}}

\def\dodohandleXMLdirective#1 #2 #3 #4\end#5#6%
  {\executeifdefined{\@@CTXML-#5-#1}{#6[#1]}[#2=#3]}

% \defineXMLdirective [mathml] \setupMMLappearance % [#1][#2=#3]
% \defineXMLdirective [flowchart] [shapes] \setupFLOWshapes % [#2=#3]
% \defineXMLdirective [flowchart] [lines] \setupFLOWlines % [#2=#3]

\defineXMLprocessor [context-begin-group] {\begingroup\gobbleoneargument}
\defineXMLprocessor [context-end-group]   {\endgroup  \gobbleoneargument}

% \def\XMLnspart#1:#2\empty{#1} % call ...:\empty\empty
% \def\XMLidpart#1:#2#3\empty{\ifx#2\empty#1\else\XMLidpart#2#3\empty\empty\fi}

% trial macros (used in setupx), to be sped up !

\bgroup \catcode`\<=\active

\gdef\saveXMLasdata#1#2% name raw data
  {\dodoglobal\setevalue{\@@XMLsave:#1}{#2}} % \edef!

\gdef\saveXMLdata#1#2% name data-name ; definitely no \edef
  {\dodoglobal\copycsname\@@XMLsave:#1\endcsname\csname\@@XMLdata:#2\endcsname}

\gdef\saveXMLdatainelement#1#2#3% name element data
  {\dodoglobal\setevalue{\@@XMLsave:#1}% todo: one level expansion
     {<#2 \currentXMLarguments>\XMLflush{#3}</#2>}}

\gdef\saveXMLdatastructure#1#2#3#4#5#6% name element args before data after
  {\dodoglobal\setevalue{\@@XMLsave:#1}% todo: one level expansion
     {<#2 #3 \currentXMLarguments>#4\XMLflush{#5}#6</#2>}}

\gdef\gsaveXMLasdata       {\doglobal\saveXMLasdata}
\gdef\gsaveXMLdata         {\doglobal\saveXMLdata}
\gdef\gsaveXMLdatainelement{\doglobal\saveXMLdatainelement}
\gdef\gsaveXMLdatastructure{\doglobal\saveXMLdatastructure}

\gdef\doifelseXMLelement#1%
  {\doifdefinedelse{\@@XMLsave:#1}}

\gdef\doifelseXMLelementcontent#1%
  {\ifcsname\@@XMLsave:#1\endcsname
     \bgroup
     \@EA\convertcommand\csname\@@XMLsave:#1\endcsname\to\ascii
     \setbox\scratchbox\hbox{\ignorespaces\ascii\unskip\unskip\unskip}%
     \ifdim\wd\scratchbox>\zeropoint
       \egroup\@EAEAEA\firstoftwoarguments
     \else
       \egroup\@EAEAEA\secondoftwoarguments
     \fi
   \else
     \@EA\secondoftwoarguments
   \fi}

\gdef\doifelseXMLelementequals#1#2%
  {\ifcsname\@@XMLsave:#1\endcsname
     \bgroup
     \@EA\convertcommand\csname\@@XMLsave:#1\endcsname\to\asciia
     \convertargument#2\to\asciib
     \ifx\asciia\asciib
       \egroup\@EAEAEA\firstoftwoarguments
     \else
       \egroup\@EAEAEA\secondoftwoarguments
     \fi
   \else
     \@EA\secondoftwoarguments
   \fi}

\gdef\convertXMLelement#1\to#2%
  {\ifcsname\@@XMLsave:#1\endcsname
     \@EA\convertcommand\csname\@@XMLsave:#1\endcsname\to#2%
   \else
     \let#2\ascii
   \fi}

\gdef\flushXMLelement#1%
  {\csname
     \@@XMLsave:\ifcsname\@@XMLsave:#1\endcsname#1\else\@@XMLsave\fi
   \endcsname}

\letgvalueempty{\@@XMLsave:\@@XMLsave}

\gdef\showXMLelement#1%
  {\showvalue{\@@XMLsave:#1}}

\gdef\eraseXMLelement#1%
  {\dodoglobal\letbeundefined{\@@XMLsave:#1}}

\gdef\processXMLelement#1%
  {\bgroup
   \enableXMLelements
   \getvalue{\@@XMLsave:#1}%
   \egroup}

\gdef\texXMLelement#1%
  {\begingroup
   \setnormalcatcodes
   \scantokens\@EA\@EA\@EA{\csname\@@XMLsave:#1\endcsname}%
   \endgroup}

\gdef\reduceXMLescapeentities
  {\setXMLentity{amp}{\string&}%
   \setXMLentity{lt}{\string<}%
   \setXMLentity{gt}{\string>}%
   \setXMLentity{quot}{\string'}%
   \setXMLentity{dquot}{\string"}}

\gdef\reduceXMLelement#1\to#2%
  {\ifcsname\@@XMLsave:#1\endcsname
     \bgroup
     \reduceXMLescapetokens
     \reduceXMLescapeentities
     \expanded{\egroup\noexpand\def\noexpand#2{\csname\@@XMLsave:#1\endcsname}}%
   \else
     \let#2\empty
   \fi}

\egroup

\def\potentialXMLentity#1%
  {\doifXMLentityelse{#1}{\getXMLentity{#1}}{#1}}

\def\writtenXMLelement#1#2{<#1>#2</#1>}
\def\writtenXMLstart    #1{<#1>}
\def\writtenXMLend      #1{</#1>}
\def\writtenXMLempty    #1{<#1/>}

\protect \endinput