summaryrefslogtreecommitdiff
path: root/src/tracker-extract/tracker-extract-html.c
blob: 99292ea87ad422a920af6c718cb8a4fa42eeb448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* Tracker Extract - extracts embedded metadata from files
 * Copyright (C) 2007, Jason Kivlighn (jkivlighn@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

#ifdef HAVE_LIBXML2

#include <string.h>
#include <glib.h>
#include <libxml/HTMLparser.h>

typedef enum {
		READ_TITLE,
	} tag_type;

typedef struct {
	GHashTable *metadata;
	tag_type current;
} HTMLParseInfo;

gboolean
has_attribute( const xmlChar ** atts, const char *attr, const char*val )
{
        if (atts == NULL || attr == NULL || val == NULL)
        return FALSE;

	int i;
	for ( i = 0; atts[i]; i+=2 )
	{
		if ( strcasecmp((char*)atts[i],attr) == 0 ) {
			if ( !val || strcasecmp((char*)atts[i+1],val) == 0 ) {
				return TRUE;
			}
		}
	}
	return FALSE;
}

const xmlChar *
lookup_attribute( const xmlChar **atts, const char *attr )
{
	int i;
	for ( i = 0; atts[i]; i+=2 )
	{
		if ( strcasecmp((char*)atts[i],attr) == 0 ) {
			return atts[i+1];
		}
	}

	return NULL;
}

void
startElement (void * info, const xmlChar * name, const xmlChar ** atts)
{
	/* Look for RDFa triple describing the license */
	if ( strcasecmp((char*)name,"a") == 0 ) {
		/* This tag is a license.  Ignore, however, if it is referring to another document */
		if ( has_attribute(atts,"rel","license") && !has_attribute(atts,"about",NULL) ) {
			const xmlChar *href = lookup_attribute(atts,"href");
			if ( href ) {
				g_hash_table_insert (((HTMLParseInfo *)info)->metadata, g_strdup ("File:License"),
				                     g_strdup( (char*)href ));
			}
		}
	} else if ( strcasecmp((char*)name,"title") == 0 ) {
		((HTMLParseInfo *)info)->current = READ_TITLE;
	} else if ( strcasecmp((char*)name,"meta") == 0 ) {
		if ( has_attribute(atts,"name","Author") ) {
			const xmlChar *author = lookup_attribute(atts,"content");
			if ( author ) {
				g_hash_table_insert (((HTMLParseInfo *)info)->metadata, g_strdup ("Doc:Author"),
				                     g_strdup( (char*)author ));
			}
		}
		if ( has_attribute(atts,"name","DC.Description") ) {
			const xmlChar *desc = lookup_attribute(atts,"content");
			if ( desc ) {
				g_hash_table_insert (((HTMLParseInfo *)info)->metadata, g_strdup ("Doc:Comments"),
				                     g_strdup( (char*)desc ));
			}
		}
	}
}

void
characters(void * info, const xmlChar * ch, int len)
{
	switch(((HTMLParseInfo *)info)->current) {
		case READ_TITLE:
				g_hash_table_insert (((HTMLParseInfo *)info)->metadata, g_strdup ("Doc:Title"),
				                     g_strdup( (char*)ch ));
				break;
		default: break;
	}

	((HTMLParseInfo *)info)->current = -1;
}

void tracker_extract_html (gchar* filename, GHashTable *metadata)
{
	xmlSAXHandler SAXHandlerStruct = {
			NULL, /* internalSubset */
			NULL, /* isStandalone */
			NULL, /* hasInternalSubset */
			NULL, /* hasExternalSubset */
			NULL, /* resolveEntity */
			NULL, /* getEntity */
			NULL, /* entityDecl */
			NULL, /* notationDecl */
			NULL, /* attributeDecl */
			NULL, /* elementDecl */
			NULL, /* unparsedEntityDecl */
			NULL, /* setDocumentLocator */
			NULL, /* startDocument */
			NULL, /* endDocument */
			startElement, /* startElement */
			NULL, /* endElement */
			NULL, /* reference */
			characters, /* characters */
			NULL, /* ignorableWhitespace */
			NULL, /* processingInstruction */
			NULL, /* comment */
			NULL, /* xmlParserWarning */
			NULL, /* xmlParserError */
			NULL, /* xmlParserError */
			NULL, /* getParameterEntity */
			NULL, /* cdataBlock */
			NULL, /* externalSubset */
			1,    /* initialized */
			NULL, /* private */
			NULL, /* startElementNsSAX2Func */
			NULL, /* endElementNsSAX2Func */
			NULL  /* xmlStructuredErrorFunc */
	};

	HTMLParseInfo   info = { metadata, -1 };

	htmlDocPtr doc;
	doc = htmlSAXParseFile(filename, NULL, &SAXHandlerStruct, &info);
	if ( doc ) {
		xmlFreeDoc(doc);
	}
}

#else
#warning "Not building HTML metadata extractor."
#endif