1 |
// Site Mapper |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "SiteMapper.hpp" |
8 |
#include "Matcher.hpp" |
9 |
#include "Page.hpp" |
10 |
|
11 |
string program; |
12 |
bool debug = false; |
13 |
|
14 |
int main(int argc, char* argv[]) |
15 |
{ |
16 |
program = argv[0]; |
17 |
|
18 |
string siteIndex, siteMap; |
19 |
|
20 |
for (int index = 1; index < argc; index++) |
21 |
{ |
22 |
string arg(argv[index]); |
23 |
Matcher matcher; |
24 |
|
25 |
if (arg == matcher("^-index=(.*)$")) |
26 |
{ |
27 |
siteIndex = matcher[1]; |
28 |
} |
29 |
else if (arg == matcher("^-map=(.*)$")) |
30 |
{ |
31 |
siteMap = matcher[1]; |
32 |
} |
33 |
else if (arg == "-D") |
34 |
{ |
35 |
if (!debug) debug = true; |
36 |
} |
37 |
} |
38 |
|
39 |
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"; |
52 |
} |
53 |
|
54 |
return 0; |
55 |
} |
56 |
|
57 |
SiteMapper::SiteMapper(const string& siteIndex, const string& siteMap) |
58 |
{ |
59 |
oldMap(siteMap); |
60 |
index(siteIndex); |
61 |
newMap(siteMap); |
62 |
} |
63 |
|
64 |
void SiteMapper::oldMap(const string& siteMap) |
65 |
{ |
66 |
support.setParserLiaison(&liaison); |
67 |
|
68 |
XalanDOMString file(siteMap.c_str()); |
69 |
LocalFileInputSource source(file.c_str()); |
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 |
this->list = evaluator.createXPath(XalanDOMString("list").c_str()); |
84 |
|
85 |
oldMap(pages, list); |
86 |
|
87 |
evaluator.destroyXPath(item); |
88 |
evaluator.destroyXPath(address); |
89 |
evaluator.destroyXPath(link); |
90 |
evaluator.destroyXPath(this->list); |
91 |
} |
92 |
|
93 |
void SiteMapper::oldMap(vector<Page>& pages, XalanNode* list) |
94 |
{ |
95 |
NodeRefList nodes = evaluator.selectNodeList(support, list, *item); |
96 |
|
97 |
for (int index = 0; index < nodes.getLength(); index++) |
98 |
{ |
99 |
XalanNode* node = nodes.item(index); |
100 |
ostringstream url, title; |
101 |
|
102 |
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)); |
108 |
|
109 |
if (list != 0) oldMap(page.getChildren(), list); |
110 |
|
111 |
pages.push_back(page); |
112 |
} |
113 |
} |
114 |
|
115 |
void SiteMapper::index(const string& siteIndex) |
116 |
{ |
117 |
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 |
path = evaluator.createXPath(XalanDOMString("path").c_str()); |
126 |
port = evaluator.createXPath(XalanDOMString("port").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++) |
133 |
{ |
134 |
XalanNode* node = nodes.item(index); |
135 |
} |
136 |
|
137 |
evaluator.destroyXPath(address); |
138 |
evaluator.destroyXPath(path); |
139 |
evaluator.destroyXPath(port); |
140 |
evaluator.destroyXPath(title); |
141 |
} |
142 |
|
143 |
void SiteMapper::newMap(const string& siteMap) |
144 |
{ |
145 |
// |
146 |
|
147 |
for (int index = 0; index < pages.size(); index++) |
148 |
{ |
149 |
cout << pages[index] << '\n'; |
150 |
} |
151 |
} |