1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "html_parser.hpp"
28 #define TAG_MAX_LEN 64
30 #define SPACECHR " \t\r\n\f"
35 #include <sys/types.h>
38 namespace mp = metaproxy_1;
40 mp::HTMLParser::HTMLParser()
44 mp::HTMLParser::~HTMLParser()
48 static void parse_str(mp::HTMLParserEvent & event, const char * str);
50 void mp::HTMLParser::parse(mp::HTMLParserEvent & event, const char *str) const
52 parse_str(event, str);
55 static int skipSpace(const char *cp)
58 while (cp[i] && strchr(SPACECHR, cp[i]))
63 static int skipName(const char *cp, char *dst)
67 for (i = 0; cp[i] && !strchr(SPACECHR "/>=", cp[i]); i++)
68 if (j < TAG_MAX_LEN-1)
70 dst[j] = tolower(cp[j]);
77 static int skipAttribute(const char *cp, char *name, const char **value, int *val_len)
79 int i = skipName(cp, name);
83 i += skipSpace(cp + i);
88 i += skipSpace(cp + i);
89 if (cp[i] == '\"' || cp[i] == '\'')
93 while (cp[i] != tr && cp[i])
102 while (cp[i] && !strchr(SPACECHR ">", cp[i]))
109 i += skipSpace(cp + i);
113 static int tagAttrs(mp::HTMLParserEvent & event,
117 char attr_name[TAG_MAX_LEN];
118 const char *attr_value;
120 int i = skipSpace(cp);
121 while (cp[i] && cp[i] != '>' && cp[i] != '/')
123 int nor = skipAttribute(cp+i, attr_name, &attr_value, &val_len);
127 DEBUG(printf ("------ attr %s=%.*s\n", attr_name, val_len, attr_value));
128 event.attribute(tagName, attr_name, attr_value, val_len);
139 static int tagStart(mp::HTMLParserEvent & event,
140 char *tagName, const char *cp, const char which)
142 int i = skipName(cp, tagName);
146 DEBUG(printf("------ tag close %s\n", tagName));
147 event.closeTag(tagName);
150 DEBUG(printf("------ dtd %s\n", tagName));
153 DEBUG(printf("------ pi %s\n", tagName));
156 DEBUG(printf("------ tag open %s\n", tagName));
157 event.openTagStart(tagName);
163 static int tagEnd(mp::HTMLParserEvent & event, const char *tagName, const char *cp)
167 while (cp[i] && cp[i] != '>')
175 event.anyTagEnd(tagName, close_it);
181 static void tagText(mp::HTMLParserEvent & event, const char *text_start, const char *text_end)
183 if (text_end - text_start) //got text to flush
185 DEBUG(printf("------ text %.*s\n",
186 (int) (text_end - text_start), text_start));
187 event.text(text_start, text_end-text_start);
191 static void parse_str(mp::HTMLParserEvent & event, const char *cp)
193 const char *text_start = cp;
194 const char *text_end = cp;
197 if (cp[0] == '<' && cp[1]) //tag?
202 if (!strchr(SPACECHR, cp[1])) //valid tag starts
204 tagText(event, text_start, text_end); //flush any text
205 char tagName[TAG_MAX_LEN];
209 cp += tagStart(event, tagName, cp, which);
211 else if (which == '!' || which == '?') //pi or dtd
214 cp += tagStart(event, tagName, cp, which);
218 cp += tagStart(event, tagName, cp, which);
219 cp += tagAttrs(event, tagName, cp);
221 cp += tagEnd(event, tagName, cp);
231 tagText(event, text_start, text_end); //flush any text
237 * c-file-style: "Stroustrup"
238 * indent-tabs-mode: nil
240 * vim: shiftwidth=4 tabstop=8 expandtab