ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/SiteMapper/SiteMapper.cpp
(Generate patch)

Comparing SiteMapper/SiteMapper.cpp (file contents):
Revision 142 by Douglas Thrift, 2004-03-31T20:53:28-08:00 vs.
Revision 156 by Douglas Thrift, 2004-06-10T01:24:32-07:00

# Line 9 | Line 9
9   #include "Page.hpp"
10  
11   string program;
12 < bool debug = false;
12 > bool debug(false);
13  
14   int main(int argc, char* argv[])
15   {
# Line 17 | Line 17 | int main(int argc, char* argv[])
17  
18          string siteIndex, siteMap;
19  
20 <        for (int index = 1; index < argc; index++)
20 >        for (int index(1); index < argc; index++)
21          {
22                  string arg(argv[index]);
23                  Matcher matcher;
# Line 36 | Line 36 | int main(int argc, char* argv[])
36                  }
37          }
38  
39 <        if (siteIndex != "" && siteMap != "")
39 >        if (!siteIndex.empty() && !siteMap.empty())
40          {
41                XMLPlatformUtils::Initialize();
42                XPathEvaluator::initialize();
43
41                  SiteMapper mapper(siteIndex, siteMap);
45
46                XPathEvaluator::terminate();
47                XMLPlatformUtils::Terminate();
42          }
43          else
44          {
# Line 63 | Line 57 | SiteMapper::SiteMapper(const string& sit
57  
58   void SiteMapper::oldMap(const string& siteMap)
59   {
60 <        support.setParserLiaison(&liaison);
61 <
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;
60 >        ext::Handle<xml::Document> document(xml::Parse(siteMap));
61 >        ext::Handle<xml::Node> list(*document/"page"/"section"/"list");
62  
63 <        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());
63 >        comment = *document/"comment()";
64  
65          oldMap(pages, list);
86
87        evaluator.destroyXPath(item);
88        evaluator.destroyXPath(address);
89        evaluator.destroyXPath(link);
90        evaluator.destroyXPath(list_);
66   }
67  
68 < void SiteMapper::oldMap(vector<Page>& pages, XalanNode* list_)
68 > void SiteMapper::oldMap(vector<Page>& pages, xml::Node* list)
69   {
70 <        NodeRefList nodes = evaluator.selectNodeList(support, list_, *item);
70 >        xml::NodeSet nodes(*list/"item");
71  
72 <        for (int index = 0; index < nodes.getLength(); index++)
72 >        for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End();
73 >                ++node)
74          {
75 <                XalanNode* node = nodes.item(index);
76 <                ostringstream url, title;
77 <
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_));
75 >                string url(**node/"link"/"@address"), title(**node/"link");
76 >                Page page(url, title);
77 >                ext::Handle<xml::Node> list(**node/"list");
78  
79 <                if (list != 0) oldMap(page.getChildren(), list);
79 >                if (!list.IsEmpty()) oldMap(page.getChildren(), list);
80  
81                  pages.push_back(page);
82          }
# Line 114 | Line 84 | void SiteMapper::oldMap(vector<Page>& pa
84  
85   void SiteMapper::newIndex(const string& siteIndex)
86   {
87 <        XalanDOMString file(siteIndex.c_str());
88 <        LocalFileInputSource source(file.c_str());
87 >        ext::Handle<xml::Document> document(xml::Parse(siteIndex));
88 >        xml::NodeSet nodes(*document/"index"/"page");
89  
90 <        XalanDocument* document = liaison.parseXMLStream(source);
91 <
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++)
90 >        for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End();
91 >                ++node)
92          {
93 <                XalanNode* node = nodes.item(index);
94 <                ostringstream address;
136 <
137 <                address << evaluator.evaluate(support, node, *(this->address))->str();
93 >                string address(**node/"address");
94 >                string port(**node/"port");
95  
96 <                double port = evaluator.evaluate(support, node, *(this->port))->num();
140 <
141 <                if (port >= 0 && port <= 65535)
96 >                if (!port.empty())
97                  {
98 <                        address << ':' << int(port);
98 >                        address += ':' + port;
99                  }
100  
101 <                ostringstream path, title;
102 <
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());
101 >                string path(**node/"path"), title(**node/"title");
102 >                Page page(address, path, title);
103                  Matcher matcher;
104  
105                  if (page == matcher(string("^Douglas\\sThrift's\\sWebsite\\s\\|\\sDou")
106                          + "glas\\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  
# Line 170 | Line 121 | void SiteMapper::newIndex(const string&
121                  }
122                  else continue;
123  
124 <                cerr << page.getTitle() << ' ' << page.getChildOf() << '\n';
174 < //              newPages.insert(page);
175 <        }
124 >                multimap<string, Page> items;
125  
126 <        evaluator.destroyXPath(address);
127 <        evaluator.destroyXPath(port);
128 <        evaluator.destroyXPath(path);
129 <        evaluator.destroyXPath(title);
126 >                newPages.insert(pair<string, multimap<string, Page>
127 >                        >(page.getAddress(), items)).first->second.insert(pair<string,
128 >                        Page>(page.getChildOf(), page));
129 >        }
130   }
131  
132   bool SiteMapper::newIndex(vector<Page>& pages, Page& page)
133   {
134 <        for (unsigned index = 0; index < pages.size(); index++)
134 >        for (unsigned index(0); index < pages.size(); ++index)
135          {
136                  if (pages[index] == page.getAddress())
137                  {
# Line 194 | Line 143 | bool SiteMapper::newIndex(vector<Page>&
143  
144                                  pages[index] = page;
145  
146 +                                cout << "Updated: " << page.getUrl() << '\n';
147 +
148                                  return true;
149                          }
150                          else if (matcher('^' + pages[index].getPath()) == page)
# Line 211 | Line 162 | bool SiteMapper::newIndex(vector<Page>&
162                  }
163          }
164  
214        cerr << "Here!\n";
215
165          return false;
166   }
167  
168   void SiteMapper::newMap(const string& siteMap)
169   {
170 <        //
170 >        ofstream fout(siteMap.c_str());
171  
172 <        for (unsigned index = 0; index < pages.size(); index++)
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 >                << "<!--" << comment << "-->\n"
177 >                << "<page>\n"
178 >                << "\t<title>Sitemap</title>\n"
179 >                << "\t<section>\n"
180 >                << "\t\t<list>\n";
181 >
182 >        for (unsigned index(0); index < pages.size(); ++index)
183          {
184 <                cout << pages[index] << '\n';
184 >                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 >                fout << pages[index](3) << '\n';
191          }
192 +
193 +        fout << "\t\t</list>\n"
194 +                << "\t</section>\n"
195 +                << "</page>\n";
196 +
197 +        fout.close();
198 + }
199 +
200 + void SiteMapper::newMap(vector<Page>& pages, const string& childOf,
201 +        multimap<string, Page>& newPages)
202 + {
203 +        for (unsigned index(0); index < pages.size(); ++index)
204 +        {
205 +                newMap(pages[index].getChildren(), pages[index].getPath(), newPages);
206 +        }
207 +
208 +        for (multimap<string, Page>::iterator itor(newPages.lower_bound(childOf));
209 +                itor != newPages.upper_bound(childOf); itor++)
210 +        {
211 +                cout << "Added: " << itor->second.getUrl() << '\n';
212 +
213 +                pages.push_back(itor->second);
214 +        }
215 +
216 +        newPages.erase(childOf);
217   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines