%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% aebxmp.sty package,                      2006-09-22  %%
%% Copyright (C) 2006  D. P. Story                      %%
%%   dpstory@acrotex.net                                %%
%%                                                      %%
%% This program can redistributed and/or modified under %%
%% the terms of the LaTeX Project Public License        %%
%% Distributed from CTAN archives in directory          %%
%% macros/latex/base/lppl.txt; either version 1 of the  %%
%% License, or (at your option) any later version.      %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ProvidesPackage{aebxmp}[2007/03/13 v0.2 Populate advanced metadata (dps)]

% Changes
% v0.2 (2007/03/13) Added support for unicode escape sequences in the
%      \copyrightNotice and \copyrightInfoURL fields.
% v0.1 Original distribution

\ProcessOptions
\@ifpackageloaded{insdljs}{\let\execjs=y}
    {\RequirePackage[execJS]{insdljs}}

\newcommand{\copyrightStatus}[1]{\def\webcopyrightStatus{#1}}
\let\webcopyrightStatus\@empty
\newcommand{\copyrightNotice}[1]{\def\webcopyrightNotice{#1}}
\let\webcopyrightNotice\@empty
\newcommand{\copyrightInfoURL}[1]{\def\webcopyrightInfoURL{#1}}
\let\webcopyrightInfoURL\@empty
\newcommand{\authortitle}[1]{\def\webauthortitle{#1}}
\let\webauthortitle\@empty
\newcommand{\descriptionwriter}[1]{\def\webdescriptionwriter{#1}}
\let\webdescriptionwriter\@empty

\def\insertMarked{\ifx\webcopyrightStatus\@empty
    \else<xapRights:Marked>\webcopyrightStatus</xapRights:Marked>\fi}
\def\insertWebStatement{\ifx\webcopyrightInfoURL\@empty
    \else<xapRights:WebStatement>{"\webcopyrightInfoURL"}</xapRights:WebStatement>\fi}

\begin{execJS}{execXMP}
    var meta = this.metadata;
    var aebXMPData = new XML(meta);
    var aebx = new Namespace("adobe:ns:meta/");
    var aebrdf = new Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    var aebpdf = new Namespace("http://ns.adobe.com/pdf/1.3/");
    var aebxap = new Namespace("http://ns.adobe.com/xap/1.0/");
    var aebdc = new Namespace("http://purl.org/dc/elements/1.1/");
    var aebxapRights = new Namespace("http://ns.adobe.com/xap/1.0/rights/");
    var aebphotoshop = new Namespace("http://ns.adobe.com/photoshop/1.0/")
    var p = aebXMPData.aebrdf::RDF.aebrdf::Description;
/*
    We test whether this element has a value already, if no, we assign it a value, otherwise
    we assign it another value.
*/
    if (p.aebdc::rights.aebrdf::Alt.aebrdf::li.toString() == "") {
        p[0] +=  <rdf:Description rdf:about=""
            xmlns:dc="http://purl.org/dc/elements/1.1/"
            xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
            <dc:rights>
            <rdf:Alt>
                <rdf:li xml:lang="x-default">{"\webcopyrightNotice"}</rdf:li>
            </rdf:Alt>
            </dc:rights>
        </rdf:Description>
    } else
        p.aebdc::rights.aebrdf::Alt.aebrdf::li = "\webcopyrightNotice";
/*
    We save xapRights:Marked and xap:WebStatement, delete old values,
    then replace them with the new values.
*/
    delete p.@aebxapRights::Marked;
    delete p.@aebxapRights::WebStatement;

    p[0] +=  <rdf:Description rdf:about=""
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:xapRights="http://ns.adobe.com/xap/1.0/rights/">
        \insertMarked
        \insertWebStatement
    </rdf:Description>

//  Convert aebXMPData into a string
    aebNewXMPStr=aebXMPData.toXMLString();
// and assign it to the document metadata
    this.metadata = aebNewXMPStr;
\end{execJS}
\endinput
