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 153 by Douglas Thrift, 2004-05-29T20:37:48-07:00 vs.
Revision 249 by Douglas Thrift, 2004-09-11T23:21:11-07: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 + #include <menes-api/exename.hpp>
12 + #include <menes-app/application.hpp>
13 +
14 + #include <cassert>
15 +
16   string program;
17   bool debug(false);
18  
19 < int main(int argc, char* argv[])
19 > struct SiteMapperCommand : public app::Application
20   {
21 <        program = argv[0];
21 <
22 <        string siteIndex, siteMap;
23 <
24 <        for (int index(1); index < argc; index++)
21 >        virtual int Run(const app::ArgumentList& args)
22          {
23 <                string arg(argv[index]);
27 <                Matcher matcher;
23 >                program = api::GetExecutableName();
24  
25 <                if (arg == matcher("^-index=(.*)$"))
25 >                string siteIndex, siteMap;
26 >
27 >                for (size_t index(0); index < args.GetSize(); index++)
28                  {
29 <                        siteIndex = matcher[1];
29 >                        string arg(args[index]);
30 >                        Matcher matcher;
31 >
32 >                        if (arg == matcher("^-index=(.*)$"))
33 >                        {
34 >                                siteIndex = matcher[1];
35 >                        }
36 >                        else if (arg == matcher("^-map=(.*)$"))
37 >                        {
38 >                                siteMap = matcher[1];
39 >                        }
40 >                        else if (arg == "-D")
41 >                        {
42 >                                if (!debug) debug = true;
43 >                        }
44                  }
45 <                else if (arg == matcher("^-map=(.*)$"))
45 >
46 >                if (!siteIndex.empty() && !siteMap.empty())
47                  {
48 <                        siteMap = matcher[1];
48 >                        SiteMapper mapper(siteIndex, siteMap);
49                  }
50 <                else if (arg == "-D")
50 >                else
51                  {
52 <                        if (!debug) debug = true;
52 >                        cout << "Usage: " << program << " -index=index -map=map [-D]\n";
53                  }
41        }
42
43        if (siteIndex != "" && siteMap != "")
44        {
45                XMLPlatformUtils::Initialize();
46                XPathEvaluator::initialize();
54  
55 <                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";
55 >                return 0;
56          }
57 <
58 <        return 0;
59 < }
57 > } mapper;
58  
59   SiteMapper::SiteMapper(const string& siteIndex, const string& siteMap)
60   {
# Line 67 | Line 65 | SiteMapper::SiteMapper(const string& sit
65  
66   void SiteMapper::oldMap(const string& siteMap)
67   {
68 <        support.setParserLiaison(&liaison);
69 <
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;
68 >        ext::Handle<xml::Document> document(xml::Parse(siteMap));
69 >        ext::Handle<xml::Node> list(*document/"page"/"section"/"list");
70  
71 <        XalanNode* list = evaluator.selectSingleNode(support, document,
80 <                XalanDOMString("/page/section/list").c_str());
71 >        comment = ext::String(*document/"comment()");
72  
73 <        if (list == 0) return;
73 >        if (debug) cerr << "comment = " << comment << '\n';
74  
75 <        comment << evaluator.evaluate(support, document,
85 <                XalanDOMString("comment()").c_str())->str();
86 <
87 <        item = evaluator.createXPath(XalanDOMString("item").c_str());
88 <        address = evaluator.createXPath(XalanDOMString("link/@address").c_str());
89 <        link = evaluator.createXPath(XalanDOMString("link").c_str());
90 <        this->list = evaluator.createXPath(XalanDOMString("list").c_str());
75 >        assert(comment == " Cheese! ");
76  
77          oldMap(pages, list);
93
94        evaluator.destroyXPath(item);
95        evaluator.destroyXPath(address);
96        evaluator.destroyXPath(link);
97        evaluator.destroyXPath(this->list);
78   }
79  
80 < void SiteMapper::oldMap(vector<Page>& pages, XalanNode* list)
80 > void SiteMapper::oldMap(vector<Page>& pages, xml::Node* list)
81   {
82 <        NodeRefList nodes = evaluator.selectNodeList(support, list, *item);
82 >        xml::NodeSet nodes(*list/"item");
83  
84 <        for (int index(0); index < nodes.getLength(); ++index)
84 >        for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End();
85 >                ++node)
86          {
87 <                XalanNode* node = nodes.item(index);
88 <                ostringstream url, title;
89 <
90 <                url << evaluator.evaluate(support, node, *address)->str();
110 <                title << evaluator.evaluate(support, node, *link)->str();
87 >                string url(ext::String(**node/"link"/"@address")),
88 >                        title(ext::String(**node/"link"));
89 >                Page page(url, title);
90 >                ext::Handle<xml::Node> list(**node/"list");
91  
92 <                Page page(url.str(), title.str());
113 <                XalanNode* list = evaluator.selectSingleNode(support, node,
114 <                        *(this->list));
115 <
116 <                if (list != 0) oldMap(page.getChildren(), list);
92 >                if (!list.IsEmpty()) oldMap(page.getChildren(), list);
93  
94                  pages.push_back(page);
95          }
# Line 121 | Line 97 | void SiteMapper::oldMap(vector<Page>& pa
97  
98   void SiteMapper::newIndex(const string& siteIndex)
99   {
100 <        XalanDOMString file(siteIndex.c_str());
101 <        LocalFileInputSource source(file.c_str());
126 <
127 <        XalanDocument* document = liaison.parseXMLStream(source);
100 >        ext::Handle<xml::Document> document(xml::Parse(siteIndex));
101 >        xml::NodeSet nodes(*document/"index"/"page");
102  
103 <        if (document == 0) return;
104 <
131 <        address = evaluator.createXPath(XalanDOMString("address").c_str());
132 <        port = evaluator.createXPath(XalanDOMString("port").c_str());
133 <        path = evaluator.createXPath(XalanDOMString("path").c_str());
134 <        title = evaluator.createXPath(XalanDOMString("title").c_str());
135 <
136 <        NodeRefList nodes = evaluator.selectNodeList(support, document,
137 <                XalanDOMString("/index/page").c_str());
138 <
139 <        for (int index(0); index < nodes.getLength(); ++index)
103 >        for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End();
104 >                ++node)
105          {
106 <                XalanNode* node = nodes.item(index);
107 <                ostringstream address;
143 <
144 <                address << evaluator.evaluate(support, node, *(this->address))->str();
145 <
146 <                double port = evaluator.evaluate(support, node, *(this->port))->num();
106 >                string address(ext::String(**node/"address")),
107 >                        port(ext::String(**node/"port"));
108  
109 <                if (port >= 0 && port <= 65535)
109 >                if (!port.empty())
110                  {
111 <                        address << ':' << int(port);
111 >                        address += ':' + port;
112                  }
113  
114 <                ostringstream path, title;
115 <
116 <                path << evaluator.evaluate(support, node, *(this->path))->str();
156 <                title << evaluator.evaluate(support, node, *(this->title))->str();
157 <
158 <                Page page(address.str(), path.str(), title.str());
114 >                string path(ext::String(**node/"path")),
115 >                        title(ext::String(**node/"title"));
116 >                Page page(address, path, title);
117                  Matcher matcher;
118  
119                  if (page == matcher(string("^Douglas\\sThrift's\\sWebsite\\s\\|\\sDou")
120                          + "glas\\sThrift's\\sBlog:\\s(.+)$"))
121                  {
122 <                        if (Matcher("^\\w+\\s\\d\\d\\d\\d\\sArchives$") == matcher[1])
122 >                        if (Matcher("^\\w+\\s\\d{4}\\sArchives$") == matcher[1])
123                          {
124                                  page.setTitle(matcher[1]);
125  
# Line 183 | Line 141 | void SiteMapper::newIndex(const string&
141                          >(page.getAddress(), items)).first->second.insert(pair<string,
142                          Page>(page.getChildOf(), page));
143          }
186
187        evaluator.destroyXPath(address);
188        evaluator.destroyXPath(port);
189        evaluator.destroyXPath(path);
190        evaluator.destroyXPath(title);
144   }
145  
146   bool SiteMapper::newIndex(vector<Page>& pages, Page& page)
# Line 204 | Line 157 | bool SiteMapper::newIndex(vector<Page>&
157  
158                                  pages[index] = page;
159  
160 +                                cout << "Updated: " << page.getUrl() << '\n';
161 +
162                                  return true;
163                          }
164                          else if (matcher('^' + pages[index].getPath()) == page)
# Line 232 | Line 187 | void SiteMapper::newMap(const string& si
187                  << "<?xml-stylesheet type=\"text/xsl\" href=\"stylesheets/sitemap.xsl"
188                  << "\"?>\n"
189                  << "<!DOCTYPE page SYSTEM \"stylesheets/page.dtd\">\n"
190 <                << "<!--" << comment.str() << "-->\n"
190 >                << "<!--" << comment << "-->\n"
191                  << "<page>\n"
192                  << "\t<title>Sitemap</title>\n"
193                  << "\t<section>\n"
# Line 267 | Line 222 | void SiteMapper::newMap(vector<Page>& pa
222          for (multimap<string, Page>::iterator itor(newPages.lower_bound(childOf));
223                  itor != newPages.upper_bound(childOf); itor++)
224          {
225 +                cout << "Added: " << itor->second.getUrl() << '\n';
226 +
227                  pages.push_back(itor->second);
228          }
229  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines