ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/SiteMapper/SiteMapper.cpp
Revision: 156
Committed: 2004-06-10T01:24:32-07:00 (21 years ago) by Douglas Thrift
File size: 4585 byte(s)
Log Message:
Added output.

File Contents

# User Rev Content
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 Douglas Thrift 152 bool debug(false);
13 Douglas Thrift 128
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 152 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 156 if (!siteIndex.empty() && !siteMap.empty())
40 Douglas Thrift 132 {
41 Douglas Thrift 128 SiteMapper mapper(siteIndex, siteMap);
42     }
43     else
44     {
45 Douglas Thrift 133 cout << "Usage: " << program << " -index=index -map=map [-D]\n";
46 Douglas Thrift 128 }
47 Douglas Thrift 129
48 Douglas Thrift 126 return 0;
49     }
50    
51     SiteMapper::SiteMapper(const string& siteIndex, const string& siteMap)
52     {
53 Douglas Thrift 133 oldMap(siteMap);
54 Douglas Thrift 142 newIndex(siteIndex);
55 Douglas Thrift 134 newMap(siteMap);
56 Douglas Thrift 133 }
57    
58     void SiteMapper::oldMap(const string& siteMap)
59     {
60 Douglas Thrift 154 ext::Handle<xml::Document> document(xml::Parse(siteMap));
61     ext::Handle<xml::Node> list(*document/"page"/"section"/"list");
62 Douglas Thrift 133
63 Douglas Thrift 154 comment = *document/"comment()";
64 Douglas Thrift 133
65 Douglas Thrift 137 oldMap(pages, list);
66 Douglas Thrift 133 }
67    
68 Douglas Thrift 154 void SiteMapper::oldMap(vector<Page>& pages, xml::Node* list)
69 Douglas Thrift 135 {
70 Douglas Thrift 154 xml::NodeSet nodes(*list/"item");
71 Douglas Thrift 135
72 Douglas Thrift 154 for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End();
73     ++node)
74 Douglas Thrift 135 {
75 Douglas Thrift 154 string url(**node/"link"/"@address"), title(**node/"link");
76     Page page(url, title);
77     ext::Handle<xml::Node> list(**node/"list");
78 Douglas Thrift 141
79 Douglas Thrift 154 if (!list.IsEmpty()) oldMap(page.getChildren(), list);
80 Douglas Thrift 135
81     pages.push_back(page);
82     }
83     }
84    
85 Douglas Thrift 142 void SiteMapper::newIndex(const string& siteIndex)
86 Douglas Thrift 133 {
87 Douglas Thrift 154 ext::Handle<xml::Document> document(xml::Parse(siteIndex));
88     xml::NodeSet nodes(*document/"index"/"page");
89 Douglas Thrift 138
90 Douglas Thrift 154 for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End();
91     ++node)
92 Douglas Thrift 138 {
93 Douglas Thrift 154 string address(**node/"address");
94     string port(**node/"port");
95 Douglas Thrift 139
96 Douglas Thrift 154 if (!port.empty())
97 Douglas Thrift 139 {
98 Douglas Thrift 154 address += ':' + port;
99 Douglas Thrift 139 }
100    
101 Douglas Thrift 154 string path(**node/"path"), title(**node/"title");
102     Page page(address, path, title);
103 Douglas Thrift 142 Matcher matcher;
104 Douglas Thrift 140
105 Douglas Thrift 142 if (page == matcher(string("^Douglas\\sThrift's\\sWebsite\\s\\|\\sDou")
106     + "glas\\sThrift's\\sBlog:\\s(.+)$"))
107 Douglas Thrift 140 {
108 Douglas Thrift 156 if (Matcher("^\\w+\\s\\d{4}\\sArchives$") == matcher[1])
109 Douglas Thrift 142 {
110     page.setTitle(matcher[1]);
111    
112     if (newIndex(pages, page)) continue;
113     }
114     else continue;
115     }
116     else if (page == matcher("^Douglas\\sThrift's.+Website\\s\\|\\s(.+)$"))
117     {
118 Douglas Thrift 140 page.setTitle(matcher[1]);
119    
120 Douglas Thrift 142 if (newIndex(pages, page)) continue;
121 Douglas Thrift 140 }
122 Douglas Thrift 142 else continue;
123    
124 Douglas Thrift 143 multimap<string, Page> items;
125    
126     newPages.insert(pair<string, multimap<string, Page>
127     >(page.getAddress(), items)).first->second.insert(pair<string,
128     Page>(page.getChildOf(), page));
129 Douglas Thrift 138 }
130 Douglas Thrift 126 }
131 Douglas Thrift 133
132 Douglas Thrift 142 bool SiteMapper::newIndex(vector<Page>& pages, Page& page)
133     {
134 Douglas Thrift 153 for (unsigned index(0); index < pages.size(); ++index)
135 Douglas Thrift 142 {
136     if (pages[index] == page.getAddress())
137     {
138     Matcher matcher;
139    
140     if (pages[index] == page)
141     {
142     page.setChildren(pages[index].getChildren());
143    
144     pages[index] = page;
145    
146 Douglas Thrift 156 cout << "Updated: " << page.getUrl() << '\n';
147    
148 Douglas Thrift 142 return true;
149     }
150     else if (matcher('^' + pages[index].getPath()) == page)
151     {
152     page.setChildOf(matcher[0]);
153    
154     if (matcher('^' + pages[index].getTitle() + "\\s\\|\\s(.+)$")
155     == page)
156     {
157     page.setTitle(matcher[1]);
158     }
159    
160     return newIndex(pages[index].getChildren(), page);
161     }
162     }
163     }
164    
165     return false;
166     }
167    
168 Douglas Thrift 133 void SiteMapper::newMap(const string& siteMap)
169     {
170 Douglas Thrift 144 ofstream fout(siteMap.c_str());
171    
172     fout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
173     << "<?xml-stylesheet type=\"text/xsl\" href=\"stylesheets/sitemap.xsl"
174     << "\"?>\n"
175     << "<!DOCTYPE page SYSTEM \"stylesheets/page.dtd\">\n"
176 Douglas Thrift 154 << "<!--" << comment << "-->\n"
177 Douglas Thrift 144 << "<page>\n"
178     << "\t<title>Sitemap</title>\n"
179     << "\t<section>\n"
180     << "\t\t<list>\n";
181    
182 Douglas Thrift 153 for (unsigned index(0); index < pages.size(); ++index)
183 Douglas Thrift 140 {
184 Douglas Thrift 143 if (newPages.find(pages[index].getAddress()) != newPages.end())
185     {
186     newMap(pages[index].getChildren(), pages[index].getPath(),
187     newPages.find(pages[index].getAddress())->second);
188     }
189    
190 Douglas Thrift 144 fout << pages[index](3) << '\n';
191 Douglas Thrift 140 }
192 Douglas Thrift 144
193     fout << "\t\t</list>\n"
194     << "\t</section>\n"
195     << "</page>\n";
196    
197     fout.close();
198 Douglas Thrift 133 }
199 Douglas Thrift 143
200     void SiteMapper::newMap(vector<Page>& pages, const string& childOf,
201     multimap<string, Page>& newPages)
202     {
203 Douglas Thrift 153 for (unsigned index(0); index < pages.size(); ++index)
204 Douglas Thrift 143 {
205     newMap(pages[index].getChildren(), pages[index].getPath(), newPages);
206     }
207    
208 Douglas Thrift 153 for (multimap<string, Page>::iterator itor(newPages.lower_bound(childOf));
209 Douglas Thrift 143 itor != newPages.upper_bound(childOf); itor++)
210     {
211 Douglas Thrift 156 cout << "Added: " << itor->second.getUrl() << '\n';
212    
213 Douglas Thrift 143 pages.push_back(itor->second);
214     }
215    
216     newPages.erase(childOf);
217     }

Properties

Name Value
svn:eol-style native
svn:keywords Id