+ xmlXPathObjectPtr xpathObj;
+ xmlChar *part = NULL;
+ xmlChar *conf = NULL;
+ char *result = NULL;
+ int i;
+
+ /* add pdf/a namespaces */
+ xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
+ xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
+ xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
+
+ /* reads pdf/a part */
+ /* first syntax: child node */
+ xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
+ if (xpathObj != NULL) {
+ if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
+ part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+
+ xmlXPathFreeObject (xpathObj);
+ }
+ if (part == NULL) {
+ /* second syntax: attribute */
+ xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
+ if (xpathObj != NULL) {
+ if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
+ part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+
+ xmlXPathFreeObject (xpathObj);
+ }
+ }
+
+ /* reads pdf/a conformance */
+ /* first syntax: child node */
+ xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
+ if (xpathObj != NULL) {
+ if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
+ conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+
+ xmlXPathFreeObject (xpathObj);
+ }
+ if (conf == NULL) {
+ /* second syntax: attribute */
+ xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
+ if (xpathObj != NULL) {
+ if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
+ conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+
+ xmlXPathFreeObject (xpathObj);
+ }
+ }
+
+ if (part != NULL && conf != NULL) {
+ /* makes conf lowercase */
+ for (i = 0; conf[i]; i++)
+ conf[i] = g_ascii_tolower (conf[i]);
+
+ /* return buffer */
+ result = g_strdup_printf ("PDF/A - %s%s", part, conf);
+ }
+
+ /* Cleanup */
+ xmlFree (part);
+ xmlFree (conf);
+
+ return result;
+}
+
+static EvDocumentLicense *
+pdf_document_get_license_from_metadata (xmlDocPtr doc,
+ xmlXPathContextPtr xpathCtx)
+{
+ xmlXPathObjectPtr xpathObj;
+ xmlChar *marked = NULL;
+ const char *language_string;
+ char *aux;
+ gchar **tags;
+ gchar *tag, *tag_aux;
+ int i, j;
+ EvDocumentLicense *license;
+
+ /* register namespaces */
+ xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
+ xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
+ xmlXPathRegisterNs (xpathCtx, BAD_CAST "dc", BAD_CAST "http://purl.org/dc/elements/1.1/");
+ /* XMP Rights Management Schema */
+ xmlXPathRegisterNs (xpathCtx, BAD_CAST "xmpRights", BAD_CAST "http://ns.adobe.com/xap/1.0/rights/");
+ /* Creative Commons Schema */
+ xmlXPathRegisterNs (xpathCtx, BAD_CAST "cc", BAD_CAST "http://creativecommons.org/ns#");
+
+ /* checking if the document has been marked as defined on the XMP Rights
+ * Management Schema */
+ xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_MARKED, xpathCtx);
+ if (xpathObj != NULL) {
+ if (xpathObj->nodesetval != NULL &&
+ xpathObj->nodesetval->nodeNr != 0)
+ marked = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+ xmlXPathFreeObject (xpathObj);
+ }
+
+ /* a) Not marked => No XMP Rights information */
+ if (!marked) {
+ xmlFree (marked);
+ return NULL;
+ }
+
+ license = ev_document_license_new ();
+
+ /* b) Marked False => Public Domain, no copyrighted material and no
+ * license needed */
+ if (g_strrstr ((char *) marked, "False") != NULL) {
+ license->text = g_strdup (_("This work is in the Public Domain"));
+ /* c) Marked True => Copyrighted material */
+ } else {
+ /* Checking usage terms as defined by the XMP Rights Management
+ * Schema. This field is recomended to be checked by Creative
+ * Commons */
+ /* 1) checking for a suitable localized string */
+ language_string = pango_language_to_string (gtk_get_default_language ());
+ tags = g_strsplit (language_string, "-", -1);
+ i = g_strv_length (tags);
+ while (i-- && !license->text) {
+ tag = g_strdup (tags[0]);
+ for (j = 1; j <= i; j++) {
+ tag_aux = g_strdup_printf ("%s-%s", tag, tags[j]);
+ g_free (tag);
+ tag = tag_aux;
+ }
+ aux = g_strdup_printf (LICENSE_TEXT, tag);
+ xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
+ if (xpathObj != NULL) {
+ if (xpathObj->nodesetval != NULL &&
+ xpathObj->nodesetval->nodeNr != 0)
+ license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+ xmlXPathFreeObject (xpathObj);
+ }
+ g_free (tag);
+ g_free (aux);
+ }
+ g_strfreev(tags);
+
+ /* 2) if not, use the default string */
+ if (!license->text) {
+ aux = g_strdup_printf (LICENSE_TEXT, "x-default");
+ xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
+ if (xpathObj != NULL) {
+ if (xpathObj->nodesetval != NULL &&
+ xpathObj->nodesetval->nodeNr != 0)
+ license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+ xmlXPathFreeObject (xpathObj);
+ }
+ g_free (aux);
+ }
+
+ /* Checking the license URI as defined by the Creative Commons
+ * Schema. This field is recomended to be checked by Creative
+ * Commons */
+ xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_URI, xpathCtx);
+ if (xpathObj != NULL) {
+ if (xpathObj->nodesetval != NULL &&
+ xpathObj->nodesetval->nodeNr != 0)
+ license->uri = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+ xmlXPathFreeObject (xpathObj);
+ }
+
+ /* Checking the web statement as defined by the XMP Rights
+ * Management Schema. Checking it out is a sort of above-and-beyond
+ * the basic recommendations by Creative Commons. It can be
+ * considered as a "reinforcement" approach to add certainty. */
+ xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_WEB_STATEMENT, xpathCtx);
+ if (xpathObj != NULL) {
+ if (xpathObj->nodesetval != NULL &&
+ xpathObj->nodesetval->nodeNr != 0)
+ license->web_statement = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+ xmlXPathFreeObject (xpathObj);
+ }
+ }
+ xmlFree (marked);
+
+ if (!license->text && !license->uri && !license->web_statement) {
+ ev_document_license_free (license);
+ return NULL;
+ }
+
+ return license;