1 |
Douglas Thrift |
126 |
// Site Mapper |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include "SiteMapper.hpp" |
8 |
|
|
#include "Matcher.hpp" |
9 |
Douglas Thrift |
128 |
#include "Page.hpp" |
10 |
Douglas Thrift |
126 |
|
11 |
Douglas Thrift |
128 |
string program; |
12 |
|
|
bool debug = false; |
13 |
|
|
|
14 |
Douglas Thrift |
126 |
int main(int argc, char* argv[]) |
15 |
|
|
{ |
16 |
Douglas Thrift |
128 |
program = argv[0]; |
17 |
Douglas Thrift |
132 |
|
18 |
Douglas Thrift |
126 |
string siteIndex, siteMap; |
19 |
|
|
|
20 |
Douglas Thrift |
132 |
for (int index = 1; index < argc; index++) |
21 |
Douglas Thrift |
126 |
{ |
22 |
Douglas Thrift |
128 |
string arg(argv[index]); |
23 |
|
|
Matcher matcher; |
24 |
Douglas Thrift |
126 |
|
25 |
Douglas Thrift |
128 |
if (arg == matcher("^-index=(.*)$")) |
26 |
|
|
{ |
27 |
|
|
siteIndex = matcher[1]; |
28 |
|
|
} |
29 |
|
|
else if (arg == matcher("^-map=(.*)$")) |
30 |
|
|
{ |
31 |
|
|
siteMap = matcher[1]; |
32 |
|
|
} |
33 |
Douglas Thrift |
133 |
else if (arg == "-D") |
34 |
|
|
{ |
35 |
|
|
if (!debug) debug = true; |
36 |
|
|
} |
37 |
Douglas Thrift |
126 |
} |
38 |
|
|
|
39 |
Douglas Thrift |
128 |
if (siteIndex != "" && siteMap != "") |
40 |
Douglas Thrift |
132 |
{ |
41 |
Douglas Thrift |
128 |
XMLPlatformUtils::Initialize(); |
42 |
|
|
XPathEvaluator::initialize(); |
43 |
Douglas Thrift |
126 |
|
44 |
Douglas Thrift |
128 |
SiteMapper mapper(siteIndex, siteMap); |
45 |
|
|
|
46 |
|
|
XPathEvaluator::terminate(); |
47 |
|
|
XMLPlatformUtils::Terminate(); |
48 |
|
|
} |
49 |
|
|
else |
50 |
|
|
{ |
51 |
Douglas Thrift |
133 |
cout << "Usage: " << program << " -index=index -map=map [-D]\n"; |
52 |
Douglas Thrift |
128 |
} |
53 |
Douglas Thrift |
129 |
|
54 |
Douglas Thrift |
126 |
return 0; |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
SiteMapper::SiteMapper(const string& siteIndex, const string& siteMap) |
58 |
|
|
{ |
59 |
Douglas Thrift |
133 |
oldMap(siteMap); |
60 |
Douglas Thrift |
134 |
index(siteIndex); |
61 |
|
|
newMap(siteMap); |
62 |
Douglas Thrift |
133 |
} |
63 |
|
|
|
64 |
|
|
void SiteMapper::oldMap(const string& siteMap) |
65 |
|
|
{ |
66 |
|
|
support.setParserLiaison(&liaison); |
67 |
|
|
|
68 |
Douglas Thrift |
134 |
XalanDOMString file(siteMap.c_str()); |
69 |
|
|
LocalFileInputSource source(file.c_str()); |
70 |
Douglas Thrift |
133 |
|
71 |
Douglas Thrift |
134 |
XalanDocument* document = liaison.parseXMLStream(source); |
72 |
Douglas Thrift |
133 |
|
73 |
Douglas Thrift |
134 |
if (document == 0) return; |
74 |
Douglas Thrift |
133 |
|
75 |
Douglas Thrift |
134 |
XalanNode* list = evaluator.selectSingleNode(support, document, |
76 |
|
|
XalanDOMString("/page/section/list").c_str()); |
77 |
Douglas Thrift |
133 |
|
78 |
Douglas Thrift |
134 |
if (list == 0) return; |
79 |
Douglas Thrift |
135 |
|
80 |
Douglas Thrift |
137 |
item = evaluator.createXPath(XalanDOMString("item").c_str()); |
81 |
|
|
address = evaluator.createXPath(XalanDOMString("link/@address").c_str()); |
82 |
|
|
link = evaluator.createXPath(XalanDOMString("link").c_str()); |
83 |
Douglas Thrift |
140 |
list_ = evaluator.createXPath(XalanDOMString("list").c_str()); |
84 |
Douglas Thrift |
137 |
|
85 |
|
|
oldMap(pages, list); |
86 |
Douglas Thrift |
138 |
|
87 |
|
|
evaluator.destroyXPath(item); |
88 |
|
|
evaluator.destroyXPath(address); |
89 |
|
|
evaluator.destroyXPath(link); |
90 |
Douglas Thrift |
140 |
evaluator.destroyXPath(list_); |
91 |
Douglas Thrift |
133 |
} |
92 |
|
|
|
93 |
Douglas Thrift |
141 |
void SiteMapper::oldMap(vector<Page>& pages, XalanNode* list_) |
94 |
Douglas Thrift |
135 |
{ |
95 |
Douglas Thrift |
141 |
NodeRefList nodes = evaluator.selectNodeList(support, list_, *item); |
96 |
Douglas Thrift |
135 |
|
97 |
|
|
for (int index = 0; index < nodes.getLength(); index++) |
98 |
|
|
{ |
99 |
|
|
XalanNode* node = nodes.item(index); |
100 |
Douglas Thrift |
138 |
ostringstream url, title; |
101 |
Douglas Thrift |
141 |
|
102 |
Douglas Thrift |
138 |
url << evaluator.evaluate(support, node, *address)->str(); |
103 |
|
|
title << evaluator.evaluate(support, node, *link)->str(); |
104 |
Douglas Thrift |
135 |
|
105 |
|
|
Page page(url.str(), title.str()); |
106 |
Douglas Thrift |
141 |
XalanNode* list = evaluator.selectSingleNode(support, node, |
107 |
|
|
*(this->list_)); |
108 |
Douglas Thrift |
135 |
|
109 |
Douglas Thrift |
137 |
if (list != 0) oldMap(page.getChildren(), list); |
110 |
Douglas Thrift |
135 |
|
111 |
|
|
pages.push_back(page); |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
|
115 |
Douglas Thrift |
133 |
void SiteMapper::index(const string& siteIndex) |
116 |
|
|
{ |
117 |
Douglas Thrift |
138 |
XalanDOMString file(siteIndex.c_str()); |
118 |
|
|
LocalFileInputSource source(file.c_str()); |
119 |
|
|
|
120 |
|
|
XalanDocument* document = liaison.parseXMLStream(source); |
121 |
|
|
|
122 |
|
|
if (document == 0) return; |
123 |
|
|
|
124 |
|
|
address = evaluator.createXPath(XalanDOMString("address").c_str()); |
125 |
Douglas Thrift |
139 |
port = evaluator.createXPath(XalanDOMString("port").c_str()); |
126 |
Douglas Thrift |
138 |
path = evaluator.createXPath(XalanDOMString("path").c_str()); |
127 |
|
|
title = evaluator.createXPath(XalanDOMString("title").c_str()); |
128 |
Douglas Thrift |
141 |
|
129 |
Douglas Thrift |
138 |
NodeRefList nodes = evaluator.selectNodeList(support, document, |
130 |
|
|
XalanDOMString("/index/page").c_str()); |
131 |
|
|
|
132 |
|
|
for (int index = 0; index < nodes.getLength(); index++) |
133 |
|
|
{ |
134 |
|
|
XalanNode* node = nodes.item(index); |
135 |
Douglas Thrift |
139 |
ostringstream address; |
136 |
|
|
|
137 |
|
|
address << evaluator.evaluate(support, node, *(this->address))->str(); |
138 |
|
|
|
139 |
|
|
double port = evaluator.evaluate(support, node, *(this->port))->num(); |
140 |
|
|
|
141 |
|
|
if (port >= 0 && port <= 65535) |
142 |
|
|
{ |
143 |
|
|
address << ':' << int(port); |
144 |
|
|
} |
145 |
|
|
|
146 |
Douglas Thrift |
140 |
ostringstream path, title; |
147 |
|
|
|
148 |
|
|
path << evaluator.evaluate(support, node, *(this->path))->str(); |
149 |
|
|
title << evaluator.evaluate(support, node, *(this->title))->str(); |
150 |
|
|
|
151 |
|
|
Page page(address.str(), path.str(), title.str()); |
152 |
|
|
Matcher matcher("^Douglas Thrift's.+Website \\| (.+)$"); |
153 |
|
|
|
154 |
|
|
if (page == matcher) |
155 |
|
|
{ |
156 |
|
|
page.setTitle(matcher[1]); |
157 |
|
|
|
158 |
|
|
newPages.insert(page); |
159 |
|
|
} |
160 |
Douglas Thrift |
138 |
} |
161 |
|
|
|
162 |
|
|
evaluator.destroyXPath(address); |
163 |
Douglas Thrift |
139 |
evaluator.destroyXPath(port); |
164 |
Douglas Thrift |
138 |
evaluator.destroyXPath(path); |
165 |
|
|
evaluator.destroyXPath(title); |
166 |
Douglas Thrift |
126 |
} |
167 |
Douglas Thrift |
133 |
|
168 |
|
|
void SiteMapper::newMap(const string& siteMap) |
169 |
|
|
{ |
170 |
|
|
// |
171 |
Douglas Thrift |
136 |
|
172 |
Douglas Thrift |
141 |
for (unsigned index = 0; index < pages.size(); index++) |
173 |
Douglas Thrift |
140 |
{ |
174 |
Douglas Thrift |
141 |
cout << pages[index] << '\n'; |
175 |
Douglas Thrift |
140 |
} |
176 |
Douglas Thrift |
133 |
} |