8 |
|
#include "Matcher.hpp" |
9 |
|
#include "Page.hpp" |
10 |
|
|
11 |
+ |
#include <menes-api/exename.hpp> |
12 |
+ |
#include <menes-app/application.hpp> |
13 |
+ |
|
14 |
|
string program; |
15 |
< |
bool debug = false; |
15 |
> |
bool debug(false); |
16 |
|
|
17 |
< |
int main(int argc, char* argv[]) |
17 |
> |
struct SiteMapperCommand : public app::Application |
18 |
|
{ |
19 |
< |
program = argv[0]; |
17 |
< |
|
18 |
< |
string siteIndex, siteMap; |
19 |
< |
|
20 |
< |
for (int index = 1; index < argc; index++) |
19 |
> |
virtual int Run(const app::ArgumentList& args) |
20 |
|
{ |
21 |
< |
string arg(argv[index]); |
23 |
< |
Matcher matcher; |
21 |
> |
program = api::GetExecutablePath().GetName(); |
22 |
|
|
23 |
< |
if (arg == matcher("^-index=(.*)$")) |
23 |
> |
string siteIndex, siteMap; |
24 |
> |
|
25 |
> |
for (size_t index(0); index < args.GetSize(); index++) |
26 |
|
{ |
27 |
< |
siteIndex = matcher[1]; |
27 |
> |
string arg(args[index]); |
28 |
> |
Matcher matcher; |
29 |
> |
|
30 |
> |
if (arg == matcher("^-index=(.*)$")) |
31 |
> |
{ |
32 |
> |
siteIndex = matcher[1]; |
33 |
> |
} |
34 |
> |
else if (arg == matcher("^-map=(.*)$")) |
35 |
> |
{ |
36 |
> |
siteMap = matcher[1]; |
37 |
> |
} |
38 |
> |
else if (arg == "-D") |
39 |
> |
{ |
40 |
> |
if (!debug) debug = true; |
41 |
> |
} |
42 |
|
} |
43 |
< |
else if (arg == matcher("^-map=(.*)$")) |
43 |
> |
|
44 |
> |
if (!siteIndex.empty() && !siteMap.empty()) |
45 |
|
{ |
46 |
< |
siteMap = matcher[1]; |
46 |
> |
SiteMapper mapper(siteIndex, siteMap); |
47 |
|
} |
48 |
< |
else if (arg == "-D") |
48 |
> |
else |
49 |
|
{ |
50 |
< |
if (!debug) debug = true; |
50 |
> |
cout << "Usage: " << program << " -index=index -map=map [-D]\n"; |
51 |
|
} |
37 |
– |
} |
52 |
|
|
53 |
< |
if (siteIndex != "" && siteMap != "") |
40 |
< |
{ |
41 |
< |
XMLPlatformUtils::Initialize(); |
42 |
< |
XPathEvaluator::initialize(); |
43 |
< |
|
44 |
< |
SiteMapper mapper(siteIndex, siteMap); |
45 |
< |
|
46 |
< |
XPathEvaluator::terminate(); |
47 |
< |
XMLPlatformUtils::Terminate(); |
53 |
> |
return 0; |
54 |
|
} |
55 |
< |
else |
50 |
< |
{ |
51 |
< |
cout << "Usage: " << program << " -index=index -map=map [-D]\n"; |
52 |
< |
} |
53 |
< |
|
54 |
< |
return 0; |
55 |
< |
} |
55 |
> |
} mapper; |
56 |
|
|
57 |
|
SiteMapper::SiteMapper(const string& siteIndex, const string& siteMap) |
58 |
|
{ |
63 |
|
|
64 |
|
void SiteMapper::oldMap(const string& siteMap) |
65 |
|
{ |
66 |
< |
support.setParserLiaison(&liaison); |
66 |
> |
ext::Handle<xml::Document> document(xml::Parse(siteMap)); |
67 |
> |
ext::Handle<xml::Node> list(*document/"page"/"section"/"list"); |
68 |
|
|
69 |
< |
XalanDOMString file(siteMap.c_str()); |
69 |
< |
LocalFileInputSource source(file.c_str()); |
69 |
> |
comment = ext::String(*document/"comment()"); |
70 |
|
|
71 |
< |
XalanDocument* document = liaison.parseXMLStream(source); |
72 |
< |
|
73 |
< |
if (document == 0) return; |
74 |
< |
|
75 |
< |
XalanNode* list = evaluator.selectSingleNode(support, document, |
76 |
< |
XalanDOMString("/page/section/list").c_str()); |
77 |
< |
|
78 |
< |
if (list == 0) return; |
79 |
< |
|
80 |
< |
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 |
< |
list_ = evaluator.createXPath(XalanDOMString("list").c_str()); |
71 |
> |
if (debug) cerr << "comment = " << comment << '\n'; |
72 |
|
|
73 |
|
oldMap(pages, list); |
86 |
– |
|
87 |
– |
evaluator.destroyXPath(item); |
88 |
– |
evaluator.destroyXPath(address); |
89 |
– |
evaluator.destroyXPath(link); |
90 |
– |
evaluator.destroyXPath(list_); |
74 |
|
} |
75 |
|
|
76 |
< |
void SiteMapper::oldMap(vector<Page>& pages, XalanNode* list_) |
76 |
> |
void SiteMapper::oldMap(vector<Page>& pages, xml::Node* list) |
77 |
|
{ |
78 |
< |
NodeRefList nodes = evaluator.selectNodeList(support, list_, *item); |
78 |
> |
xml::NodeSet nodes(*list/"item"); |
79 |
|
|
80 |
< |
for (int index = 0; index < nodes.getLength(); index++) |
80 |
> |
for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End(); |
81 |
> |
++node) |
82 |
|
{ |
83 |
< |
XalanNode* node = nodes.item(index); |
84 |
< |
ostringstream url, title; |
85 |
< |
|
86 |
< |
url << evaluator.evaluate(support, node, *address)->str(); |
103 |
< |
title << evaluator.evaluate(support, node, *link)->str(); |
104 |
< |
|
105 |
< |
Page page(url.str(), title.str()); |
106 |
< |
XalanNode* list = evaluator.selectSingleNode(support, node, |
107 |
< |
*(this->list_)); |
83 |
> |
string url(ext::String(**node/"link"/"@address")), |
84 |
> |
title(ext::String(**node/"link")); |
85 |
> |
Page page(url, title); |
86 |
> |
ext::Handle<xml::Node> list(**node/"list"); |
87 |
|
|
88 |
< |
if (list != 0) oldMap(page.getChildren(), list); |
88 |
> |
if (!list.IsEmpty()) oldMap(page.getChildren(), list); |
89 |
|
|
90 |
|
pages.push_back(page); |
91 |
|
} |
93 |
|
|
94 |
|
void SiteMapper::newIndex(const string& siteIndex) |
95 |
|
{ |
96 |
< |
XalanDOMString file(siteIndex.c_str()); |
97 |
< |
LocalFileInputSource source(file.c_str()); |
96 |
> |
ext::Handle<xml::Document> document(xml::Parse(siteIndex)); |
97 |
> |
xml::NodeSet nodes(*document/"index"/"page"); |
98 |
|
|
99 |
< |
XalanDocument* document = liaison.parseXMLStream(source); |
100 |
< |
|
122 |
< |
if (document == 0) return; |
123 |
< |
|
124 |
< |
address = evaluator.createXPath(XalanDOMString("address").c_str()); |
125 |
< |
port = evaluator.createXPath(XalanDOMString("port").c_str()); |
126 |
< |
path = evaluator.createXPath(XalanDOMString("path").c_str()); |
127 |
< |
title = evaluator.createXPath(XalanDOMString("title").c_str()); |
128 |
< |
|
129 |
< |
NodeRefList nodes = evaluator.selectNodeList(support, document, |
130 |
< |
XalanDOMString("/index/page").c_str()); |
131 |
< |
|
132 |
< |
for (int index = 0; index < nodes.getLength(); index++) |
99 |
> |
for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End(); |
100 |
> |
++node) |
101 |
|
{ |
102 |
< |
XalanNode* node = nodes.item(index); |
103 |
< |
ostringstream address; |
102 |
> |
string address(ext::String(**node/"address")), |
103 |
> |
port(ext::String(**node/"port")); |
104 |
|
|
105 |
< |
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) |
105 |
> |
if (!port.empty()) |
106 |
|
{ |
107 |
< |
address << ':' << int(port); |
107 |
> |
address += ':' + port; |
108 |
|
} |
109 |
|
|
110 |
< |
ostringstream path, title; |
111 |
< |
|
112 |
< |
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()); |
110 |
> |
string path(ext::String(**node/"path")), |
111 |
> |
title(ext::String(**node/"title")); |
112 |
> |
Page page(address, path, title); |
113 |
|
Matcher matcher; |
114 |
|
|
115 |
|
if (page == matcher(string("^Douglas\\sThrift's\\sWebsite\\s\\|\\sDou") |
116 |
|
+ "glas\\sThrift's\\sBlog:\\s(.+)$")) |
117 |
|
{ |
118 |
< |
if (Matcher("^\\w+\\s\\d\\d\\d\\d\\sArchives$") == matcher[1]) |
118 |
> |
if (Matcher("^\\w+\\s\\d{4}\\sArchives$") == matcher[1]) |
119 |
|
{ |
120 |
|
page.setTitle(matcher[1]); |
121 |
|
|
131 |
|
} |
132 |
|
else continue; |
133 |
|
|
134 |
< |
cerr << page.getTitle() << ' ' << page.getChildOf() << '\n'; |
174 |
< |
// newPages.insert(page); |
175 |
< |
} |
134 |
> |
multimap<string, Page> items; |
135 |
|
|
136 |
< |
evaluator.destroyXPath(address); |
137 |
< |
evaluator.destroyXPath(port); |
138 |
< |
evaluator.destroyXPath(path); |
139 |
< |
evaluator.destroyXPath(title); |
136 |
> |
newPages.insert(pair<string, multimap<string, Page> |
137 |
> |
>(page.getAddress(), items)).first->second.insert(pair<string, |
138 |
> |
Page>(page.getChildOf(), page)); |
139 |
> |
} |
140 |
|
} |
141 |
|
|
142 |
|
bool SiteMapper::newIndex(vector<Page>& pages, Page& page) |
143 |
|
{ |
144 |
< |
for (unsigned index = 0; index < pages.size(); index++) |
144 |
> |
for (unsigned index(0); index < pages.size(); ++index) |
145 |
|
{ |
146 |
|
if (pages[index] == page.getAddress()) |
147 |
|
{ |
153 |
|
|
154 |
|
pages[index] = page; |
155 |
|
|
156 |
+ |
cout << "Updated: " << page.getUrl() << '\n'; |
157 |
+ |
|
158 |
|
return true; |
159 |
|
} |
160 |
|
else if (matcher('^' + pages[index].getPath()) == page) |
172 |
|
} |
173 |
|
} |
174 |
|
|
214 |
– |
cerr << "Here!\n"; |
215 |
– |
|
175 |
|
return false; |
176 |
|
} |
177 |
|
|
178 |
|
void SiteMapper::newMap(const string& siteMap) |
179 |
|
{ |
180 |
< |
// |
180 |
> |
ofstream fout(siteMap.c_str()); |
181 |
> |
|
182 |
> |
fout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
183 |
> |
<< "<?xml-stylesheet type=\"text/xsl\" href=\"stylesheets/sitemap.xsl" |
184 |
> |
<< "\"?>\n" |
185 |
> |
<< "<!DOCTYPE page SYSTEM \"stylesheets/page.dtd\">\n" |
186 |
> |
<< "<!--" << comment << "-->\n" |
187 |
> |
<< "<page>\n" |
188 |
> |
<< "\t<title>Sitemap</title>\n" |
189 |
> |
<< "\t<section>\n" |
190 |
> |
<< "\t\t<list>\n"; |
191 |
> |
|
192 |
> |
for (unsigned index(0); index < pages.size(); ++index) |
193 |
> |
{ |
194 |
> |
if (newPages.find(pages[index].getAddress()) != newPages.end()) |
195 |
> |
{ |
196 |
> |
newMap(pages[index].getChildren(), pages[index].getPath(), |
197 |
> |
newPages.find(pages[index].getAddress())->second); |
198 |
> |
} |
199 |
> |
|
200 |
> |
fout << pages[index](3) << '\n'; |
201 |
> |
} |
202 |
> |
|
203 |
> |
fout << "\t\t</list>\n" |
204 |
> |
<< "\t</section>\n" |
205 |
> |
<< "</page>\n"; |
206 |
> |
|
207 |
> |
fout.close(); |
208 |
> |
} |
209 |
> |
|
210 |
> |
void SiteMapper::newMap(vector<Page>& pages, const string& childOf, |
211 |
> |
multimap<string, Page>& newPages) |
212 |
> |
{ |
213 |
> |
for (unsigned index(0); index < pages.size(); ++index) |
214 |
> |
{ |
215 |
> |
newMap(pages[index].getChildren(), pages[index].getPath(), newPages); |
216 |
> |
} |
217 |
|
|
218 |
< |
for (unsigned index = 0; index < pages.size(); index++) |
218 |
> |
for (multimap<string, Page>::iterator itor(newPages.lower_bound(childOf)); |
219 |
> |
itor != newPages.upper_bound(childOf); itor++) |
220 |
|
{ |
221 |
< |
cout << pages[index] << '\n'; |
221 |
> |
cout << "Added: " << itor->second.getUrl() << '\n'; |
222 |
> |
|
223 |
> |
pages.push_back(itor->second); |
224 |
|
} |
225 |
+ |
|
226 |
+ |
newPages.erase(childOf); |
227 |
|
} |