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 152 by Douglas Thrift, 2004-05-29T20:00:41-07:00 vs.
Revision 303 by douglas, 2004-12-12T17:58:03-08:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines