5 |
|
// $Id$ |
6 |
|
|
7 |
|
#include "SiteMapper.hpp" |
8 |
< |
#include "Matcher.hpp" |
9 |
< |
#include "Page.hpp" |
8 |
> |
#include "Matcher/Matcher.hpp" |
9 |
|
|
10 |
< |
string program; |
11 |
< |
bool debug = false; |
10 |
> |
#include <menes-api/exename.hpp> |
11 |
> |
#include <menes-app/application.hpp> |
12 |
|
|
13 |
< |
int main(int argc, char* argv[]) |
14 |
< |
{ |
16 |
< |
program = argv[0]; |
17 |
< |
|
18 |
< |
string siteIndex, siteMap; |
13 |
> |
ext::String program; |
14 |
> |
bool debug(false); |
15 |
|
|
16 |
< |
for (int index = 1; index < argc; index++) |
16 |
> |
struct SiteMapperCommand : public app::Application |
17 |
> |
{ |
18 |
> |
virtual int Run(const app::ArgumentList& args) |
19 |
|
{ |
20 |
< |
string arg(argv[index]); |
21 |
< |
Matcher matcher; |
20 |
> |
program = api::GetExecutablePath().GetName(); |
21 |
> |
|
22 |
> |
ext::String siteIndex, siteMap; |
23 |
|
|
24 |
< |
if (arg == matcher("^-index=(.*)$")) |
24 |
> |
_foreach (app::ArgumentList, arg, args) |
25 |
|
{ |
26 |
< |
siteIndex = matcher[1]; |
26 |
> |
Matcher matcher; |
27 |
> |
|
28 |
> |
if (*arg == matcher("^-index=(.*)$")) |
29 |
> |
{ |
30 |
> |
siteIndex = matcher[1]; |
31 |
> |
} |
32 |
> |
else if (*arg == matcher("^-map=(.*)$")) |
33 |
> |
{ |
34 |
> |
siteMap = matcher[1]; |
35 |
> |
} |
36 |
> |
else if (*arg == "-D") |
37 |
> |
{ |
38 |
> |
if (!debug) debug = true; |
39 |
> |
} |
40 |
|
} |
41 |
< |
else if (arg == matcher("^-map=(.*)$")) |
41 |
> |
|
42 |
> |
if (!siteIndex.IsEmpty() && !siteMap.IsEmpty()) |
43 |
|
{ |
44 |
< |
siteMap = matcher[1]; |
44 |
> |
SiteMapper mapper(siteIndex, siteMap); |
45 |
|
} |
46 |
< |
else if (arg == "-D") |
46 |
> |
else |
47 |
|
{ |
48 |
< |
if (!debug) debug = true; |
48 |
> |
api::Cout << "Usage: " << program << " -index=index -map=map [-D]\n"; |
49 |
|
} |
37 |
– |
} |
50 |
|
|
51 |
< |
if (siteIndex != "" && siteMap != "") |
40 |
< |
{ |
41 |
< |
XMLPlatformUtils::Initialize(); |
42 |
< |
XPathEvaluator::initialize(); |
43 |
< |
|
44 |
< |
SiteMapper mapper(siteIndex, siteMap); |
45 |
< |
|
46 |
< |
XPathEvaluator::terminate(); |
47 |
< |
XMLPlatformUtils::Terminate(); |
48 |
< |
} |
49 |
< |
else |
50 |
< |
{ |
51 |
< |
cout << "Usage: " << program << " -index=index -map=map [-D]\n"; |
51 |
> |
return 0; |
52 |
|
} |
53 |
+ |
} mapper; |
54 |
|
|
55 |
< |
return 0; |
55 |
< |
} |
56 |
< |
|
57 |
< |
SiteMapper::SiteMapper(const string& siteIndex, const string& siteMap) |
55 |
> |
SiteMapper::SiteMapper(const ext::String& siteIndex, const ext::String& siteMap) |
56 |
|
{ |
57 |
|
oldMap(siteMap); |
58 |
|
newIndex(siteIndex); |
59 |
|
newMap(siteMap); |
60 |
|
} |
61 |
|
|
62 |
< |
void SiteMapper::oldMap(const string& siteMap) |
62 |
> |
void SiteMapper::oldMap(const ext::String& siteMap) |
63 |
|
{ |
64 |
< |
support.setParserLiaison(&liaison); |
65 |
< |
|
68 |
< |
XalanDOMString file(siteMap.c_str()); |
69 |
< |
LocalFileInputSource source(file.c_str()); |
64 |
> |
ext::Handle<xml::Document> document(xml::Parse(siteMap)); |
65 |
> |
ext::Handle<xml::Node> list(*document/"page"/"section"/"list"); |
66 |
|
|
67 |
< |
XalanDocument* document = liaison.parseXMLStream(source); |
67 |
> |
comment = *document/"comment()"; |
68 |
|
|
69 |
< |
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()); |
69 |
> |
if (debug) api::Cerr << "comment = " << comment << ios::NewLine; |
70 |
|
|
71 |
|
oldMap(pages, list); |
86 |
– |
|
87 |
– |
evaluator.destroyXPath(item); |
88 |
– |
evaluator.destroyXPath(address); |
89 |
– |
evaluator.destroyXPath(link); |
90 |
– |
evaluator.destroyXPath(list_); |
72 |
|
} |
73 |
|
|
74 |
< |
void SiteMapper::oldMap(vector<Page>& pages, XalanNode* list_) |
74 |
> |
void SiteMapper::oldMap(ext::Vector<Page>& pages, xml::Node* list) |
75 |
|
{ |
76 |
< |
NodeRefList nodes = evaluator.selectNodeList(support, list_, *item); |
76 |
> |
xml::NodeSet nodes(*list/"item"); |
77 |
|
|
78 |
< |
for (int index = 0; index < nodes.getLength(); index++) |
78 |
> |
_foreach (xml::NodeSet, node, nodes) |
79 |
|
{ |
80 |
< |
XalanNode* node = nodes.item(index); |
81 |
< |
ostringstream url, title; |
80 |
> |
ext::String url(**node/"link"/"@address"), title(**node/"link"); |
81 |
> |
Page page(url, title); |
82 |
> |
ext::Handle<xml::Node> list(**node/"list"); |
83 |
|
|
84 |
< |
url << evaluator.evaluate(support, node, *address)->str(); |
103 |
< |
title << evaluator.evaluate(support, node, *link)->str(); |
84 |
> |
if (!list.IsEmpty()) oldMap(page.getChildren(), list); |
85 |
|
|
86 |
< |
Page page(url.str(), title.str()); |
106 |
< |
XalanNode* list = evaluator.selectSingleNode(support, node, |
107 |
< |
*(this->list_)); |
108 |
< |
|
109 |
< |
if (list != 0) oldMap(page.getChildren(), list); |
110 |
< |
|
111 |
< |
pages.push_back(page); |
86 |
> |
pages.InsertLast(page); |
87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
< |
void SiteMapper::newIndex(const string& siteIndex) |
90 |
> |
void SiteMapper::newIndex(const ext::String& siteIndex) |
91 |
|
{ |
92 |
< |
XalanDOMString file(siteIndex.c_str()); |
93 |
< |
LocalFileInputSource source(file.c_str()); |
119 |
< |
|
120 |
< |
XalanDocument* document = liaison.parseXMLStream(source); |
92 |
> |
ext::Handle<xml::Document> document(xml::Parse(siteIndex)); |
93 |
> |
xml::NodeSet nodes(*document/"index"/"page"); |
94 |
|
|
95 |
< |
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++) |
95 |
> |
_foreach (xml::NodeSet, node, nodes) |
96 |
|
{ |
97 |
< |
XalanNode* node = nodes.item(index); |
98 |
< |
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 |
< |
ostringstream path, title; |
97 |
> |
ios::String address(**node/"address"); |
98 |
> |
ext::String port(**node/"port"); |
99 |
|
|
100 |
< |
path << evaluator.evaluate(support, node, *(this->path))->str(); |
149 |
< |
title << evaluator.evaluate(support, node, *(this->title))->str(); |
100 |
> |
if (!port.IsEmpty()) address << ":" << port; |
101 |
|
|
102 |
< |
Page page(address.str(), path.str(), title.str()); |
103 |
< |
Matcher matcher; |
102 |
> |
ext::String path(**node/"path"), title(**node/"title"); |
103 |
> |
Page page(address, path, title); |
104 |
> |
Matcher matcher(PCRE_UNGREEDY | PCRE_DOTALL); |
105 |
|
|
106 |
< |
if (page == matcher(string("^Douglas\\sThrift's\\sWebsite\\s\\|\\sDou") |
155 |
< |
+ "glas\\sThrift's\\sBlog:\\s(.+)$")) |
106 |
> |
if (page == matcher("^Douglas\\sThrift's\\sWebsite\\s\\|\\sDouglas\\sThrift's\\sBlog:\\s(.+)$")) |
107 |
|
{ |
108 |
< |
if (Matcher("^\\w+\\s\\d\\d\\d\\d\\sArchives$") == matcher[1]) |
108 |
> |
if (Matcher("^\\w+\\s\\d{4}\\sArchives$") == matcher[1]) |
109 |
|
{ |
110 |
|
page.setTitle(matcher[1]); |
111 |
|
|
121 |
|
} |
122 |
|
else continue; |
123 |
|
|
124 |
< |
cerr << page.getTitle() << ' ' << page.getChildOf() << '\n'; |
174 |
< |
// newPages.insert(page); |
175 |
< |
} |
124 |
> |
std::multimap<std::string, Page> items; |
125 |
|
|
126 |
< |
evaluator.destroyXPath(address); |
127 |
< |
evaluator.destroyXPath(port); |
179 |
< |
evaluator.destroyXPath(path); |
180 |
< |
evaluator.destroyXPath(title); |
126 |
> |
newPages.insert(std::pair<std::string, std::multimap<std::string, Page> >(page.getAddress(), items)).first->second.insert(std::pair<std::string, Page>(page.getChildOf(), page)); |
127 |
> |
} |
128 |
|
} |
129 |
|
|
130 |
< |
bool SiteMapper::newIndex(vector<Page>& pages, Page& page) |
130 |
> |
bool SiteMapper::newIndex(ext::Vector<Page>& pages, Page& page) |
131 |
|
{ |
132 |
< |
for (unsigned index = 0; index < pages.size(); index++) |
132 |
> |
_mforeach (ext::Vector<Page>, page_, pages) |
133 |
|
{ |
134 |
< |
if (pages[index] == page.getAddress()) |
134 |
> |
if (*page_ == page.getAddress()) |
135 |
|
{ |
136 |
|
Matcher matcher; |
137 |
|
|
138 |
< |
if (pages[index] == page) |
138 |
> |
if (*page_ == page) |
139 |
|
{ |
140 |
< |
page.setChildren(pages[index].getChildren()); |
140 |
> |
page.setChildren(page_->getChildren()); |
141 |
|
|
142 |
< |
pages[index] = page; |
142 |
> |
*page_ = page; |
143 |
> |
|
144 |
> |
api::Cout << "Updated: " << page.getUrl() << ios::NewLine; |
145 |
|
|
146 |
|
return true; |
147 |
|
} |
148 |
< |
else if (matcher('^' + pages[index].getPath()) == page) |
148 |
> |
else if (matcher("^" + page_->getPath()) == page) |
149 |
|
{ |
150 |
|
page.setChildOf(matcher[0]); |
151 |
|
|
152 |
< |
if (matcher('^' + pages[index].getTitle() + "\\s\\|\\s(.+)$") |
204 |
< |
== page) |
205 |
< |
{ |
206 |
< |
page.setTitle(matcher[1]); |
207 |
< |
} |
152 |
> |
if (matcher("^" + page_->getTitle() + "\\s\\|\\s(.+)$") == page) page.setTitle(matcher[1]); |
153 |
|
|
154 |
< |
return newIndex(pages[index].getChildren(), page); |
154 |
> |
return newIndex(page_->getChildren(), page); |
155 |
|
} |
156 |
|
} |
157 |
|
} |
158 |
|
|
214 |
– |
cerr << "Here!\n"; |
215 |
– |
|
159 |
|
return false; |
160 |
|
} |
161 |
|
|
162 |
< |
void SiteMapper::newMap(const string& siteMap) |
162 |
> |
void SiteMapper::newMap(const ext::String& siteMap) |
163 |
> |
{ |
164 |
> |
api::FileWriter file(siteMap); |
165 |
> |
ios::FormatWriter fout(file); |
166 |
> |
xml::TextWriter xml(file); |
167 |
> |
|
168 |
> |
fout << ios::NewLine << "<?xml-stylesheet type=\"text/xsl\" href=\"stylesheets/sitemap.xsl\"?>" << ios::NewLine << "<!DOCTYPE page SYSTEM \"stylesheets/page.dtd\">"; |
169 |
> |
|
170 |
> |
xml.OutputComment(comment); |
171 |
> |
|
172 |
> |
xml::ScopeElement page(xml, "page"); |
173 |
> |
|
174 |
> |
xml.OpenElement("title"); |
175 |
> |
xml.OutputText("Sitemap"); |
176 |
> |
xml.CloseElement(); |
177 |
> |
|
178 |
> |
xml::ScopeElement section(xml, "section"), list(xml, "list"); |
179 |
> |
|
180 |
> |
_mforeach (ext::Vector<Page>, page, pages) |
181 |
> |
{ |
182 |
> |
if (newPages.find(page->getAddress()) != newPages.end()) newMap(page->getChildren(), page->getPath(), newPages.find(page->getAddress())->second); |
183 |
> |
|
184 |
> |
xml << *page; |
185 |
> |
} |
186 |
> |
} |
187 |
> |
|
188 |
> |
void SiteMapper::newMap(ext::Vector<Page>& pages, const ext::String& childOf, std::multimap<std::string, Page>& newPages) |
189 |
|
{ |
190 |
< |
// |
190 |
> |
_mforeach (ext::Vector<Page>, page, pages) newMap(page->getChildren(), page->getPath(), newPages); |
191 |
|
|
192 |
< |
for (unsigned index = 0; index < pages.size(); index++) |
192 |
> |
typedef std::multimap<std::string, Page> MultiMap; |
193 |
> |
|
194 |
> |
_for (MultiMap::const_iterator, itor, newPages.lower_bound(childOf), newPages.upper_bound(childOf)) |
195 |
|
{ |
196 |
< |
cout << pages[index] << '\n'; |
196 |
> |
api::Cout << "Added: " << itor->second.getUrl() << ios::NewLine; |
197 |
> |
|
198 |
> |
pages.InsertLast(itor->second); |
199 |
|
} |
200 |
+ |
|
201 |
+ |
newPages.erase(childOf); |
202 |
|
} |