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 140 by Douglas Thrift, 2004-03-25T18:04:04-08:00 vs.
Revision 154 by Douglas Thrift, 2004-06-04T02:40:33-07:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 + #ifdef _WIN32
8 + #pragma warning(disable:4503)
9 + #endif
10 +
11   #include "SiteMapper.hpp"
12   #include "Matcher.hpp"
13   #include "Page.hpp"
14  
15   string program;
16 < bool debug = false;
16 > bool debug(false);
17  
18   int main(int argc, char* argv[])
19   {
# Line 17 | Line 21 | int main(int argc, char* argv[])
21  
22          string siteIndex, siteMap;
23  
24 <        for (int index = 1; index < argc; index++)
24 >        for (int index(1); index < argc; index++)
25          {
26                  string arg(argv[index]);
27                  Matcher matcher;
# Line 38 | Line 42 | int main(int argc, char* argv[])
42  
43          if (siteIndex != "" && siteMap != "")
44          {
41                XMLPlatformUtils::Initialize();
42                XPathEvaluator::initialize();
43
45                  SiteMapper mapper(siteIndex, siteMap);
45
46                XPathEvaluator::terminate();
47                XMLPlatformUtils::Terminate();
46          }
47          else
48          {
# Line 57 | Line 55 | int main(int argc, char* argv[])
55   SiteMapper::SiteMapper(const string& siteIndex, const string& siteMap)
56   {
57          oldMap(siteMap);
58 <        index(siteIndex);
58 >        newIndex(siteIndex);
59          newMap(siteMap);
60   }
61  
62   void SiteMapper::oldMap(const string& siteMap)
63   {
64 <        support.setParserLiaison(&liaison);
65 <
68 <        XalanDOMString file(siteMap.c_str());
69 <        LocalFileInputSource source(file.c_str());
70 <
71 <        XalanDocument* document = liaison.parseXMLStream(source);
64 >        ext::Handle<xml::Document> document(xml::Parse(siteMap));
65 >        ext::Handle<xml::Node> list(*document/"page"/"section"/"list");
66  
67 <        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 <        list_ = evaluator.createXPath(XalanDOMString("list").c_str());
67 >        comment = *document/"comment()";
68  
69          oldMap(pages, list);
86
87        evaluator.destroyXPath(item);
88        evaluator.destroyXPath(address);
89        evaluator.destroyXPath(link);
90        evaluator.destroyXPath(list_);
70   }
71  
72 < void SiteMapper::oldMap(list<Page>& pages, XalanNode* list)
72 > void SiteMapper::oldMap(vector<Page>& pages, xml::Node* list)
73   {
74 <        NodeRefList nodes = evaluator.selectNodeList(support, list, *item);
74 >        xml::NodeSet nodes(*list/"item");
75  
76 <        for (int index = 0; index < nodes.getLength(); index++)
76 >        for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End();
77 >                ++node)
78          {
79 <                XalanNode* node = nodes.item(index);
80 <                ostringstream url, title;
81 <                
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, *list_);
79 >                string url(**node/"link"/"@address"), title(**node/"link");
80 >                Page page(url, title);
81 >                ext::Handle<xml::Node> list(**node/"list");
82  
83 <                if (list != 0) oldMap(page.getChildren(), list);
83 >                if (!list.IsEmpty()) oldMap(page.getChildren(), list);
84  
85                  pages.push_back(page);
86          }
87   }
88  
89 < void SiteMapper::index(const string& siteIndex)
89 > void SiteMapper::newIndex(const string& siteIndex)
90   {
91 <        XalanDOMString file(siteIndex.c_str());
92 <        LocalFileInputSource source(file.c_str());
91 >        ext::Handle<xml::Document> document(xml::Parse(siteIndex));
92 >        xml::NodeSet nodes(*document/"index"/"page");
93  
94 <        XalanDocument* document = liaison.parseXMLStream(source);
95 <
121 <        if (document == 0) return;
122 <
123 <        address = evaluator.createXPath(XalanDOMString("address").c_str());
124 <        port = evaluator.createXPath(XalanDOMString("port").c_str());
125 <        path = evaluator.createXPath(XalanDOMString("path").c_str());
126 <        title = evaluator.createXPath(XalanDOMString("title").c_str());
127 <        
128 <        NodeRefList nodes = evaluator.selectNodeList(support, document,
129 <                XalanDOMString("/index/page").c_str());
130 <
131 <        for (int index = 0; index < nodes.getLength(); index++)
94 >        for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End();
95 >                ++node)
96          {
97 <                XalanNode* node = nodes.item(index);
98 <                ostringstream address;
97 >                string address(**node/"address");
98 >                string port(**node/"port");
99  
100 <                address << evaluator.evaluate(support, node, *(this->address))->str();
100 >                if (!port.empty())
101 >                {
102 >                        address += ':' + port;
103 >                }
104  
105 <                double port = evaluator.evaluate(support, node, *(this->port))->num();
105 >                string path(**node/"path"), title(**node/"title");
106 >                Page page(address, path, title);
107 >                Matcher matcher;
108  
109 <                if (port >= 0 && port <= 65535)
109 >                if (page == matcher(string("^Douglas\\sThrift's\\sWebsite\\s\\|\\sDou")
110 >                        + "glas\\sThrift's\\sBlog:\\s(.+)$"))
111                  {
112 <                        address << ':' << int(port);
112 >                        if (Matcher("^\\w+\\s\\d\\d\\d\\d\\sArchives$") == matcher[1])
113 >                        {
114 >                                page.setTitle(matcher[1]);
115 >
116 >                                if (newIndex(pages, page)) continue;
117 >                        }
118 >                        else continue;
119                  }
120 +                else if (page == matcher("^Douglas\\sThrift's.+Website\\s\\|\\s(.+)$"))
121 +                {
122 +                        page.setTitle(matcher[1]);
123  
124 <                ostringstream path, title;
124 >                        if (newIndex(pages, page)) continue;
125 >                }
126 >                else continue;
127  
128 <                path << evaluator.evaluate(support, node, *(this->path))->str();
148 <                title << evaluator.evaluate(support, node, *(this->title))->str();
128 >                multimap<string, Page> items;
129  
130 <                Page page(address.str(), path.str(), title.str());
131 <                Matcher matcher("^Douglas Thrift's.+Website \\| (.+)$");
130 >                newPages.insert(pair<string, multimap<string, Page>
131 >                        >(page.getAddress(), items)).first->second.insert(pair<string,
132 >                        Page>(page.getChildOf(), page));
133 >        }
134 > }
135  
136 <                if (page == matcher)
136 > bool SiteMapper::newIndex(vector<Page>& pages, Page& page)
137 > {
138 >        for (unsigned index(0); index < pages.size(); ++index)
139 >        {
140 >                if (pages[index] == page.getAddress())
141                  {
142 <                        page.setTitle(matcher[1]);
142 >                        Matcher matcher;
143 >
144 >                        if (pages[index] == page)
145 >                        {
146 >                                page.setChildren(pages[index].getChildren());
147 >
148 >                                pages[index] = page;
149 >
150 >                                return true;
151 >                        }
152 >                        else if (matcher('^' + pages[index].getPath()) == page)
153 >                        {
154 >                                page.setChildOf(matcher[0]);
155 >
156 >                                if (matcher('^' + pages[index].getTitle() + "\\s\\|\\s(.+)$")
157 >                                        == page)
158 >                                {
159 >                                        page.setTitle(matcher[1]);
160 >                                }
161  
162 <                        newPages.insert(page);
162 >                                return newIndex(pages[index].getChildren(), page);
163 >                        }
164                  }
165          }
166  
167 <        evaluator.destroyXPath(address);
162 <        evaluator.destroyXPath(port);
163 <        evaluator.destroyXPath(path);
164 <        evaluator.destroyXPath(title);
167 >        return false;
168   }
169  
170   void SiteMapper::newMap(const string& siteMap)
171   {
172 <        //
172 >        ofstream fout(siteMap.c_str());
173 >
174 >        fout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
175 >                << "<?xml-stylesheet type=\"text/xsl\" href=\"stylesheets/sitemap.xsl"
176 >                << "\"?>\n"
177 >                << "<!DOCTYPE page SYSTEM \"stylesheets/page.dtd\">\n"
178 >                << "<!--" << comment << "-->\n"
179 >                << "<page>\n"
180 >                << "\t<title>Sitemap</title>\n"
181 >                << "\t<section>\n"
182 >                << "\t\t<list>\n";
183 >
184 >        for (unsigned index(0); index < pages.size(); ++index)
185 >        {
186 >                if (newPages.find(pages[index].getAddress()) != newPages.end())
187 >                {
188 >                        newMap(pages[index].getChildren(), pages[index].getPath(),
189 >                                newPages.find(pages[index].getAddress())->second);
190 >                }
191 >
192 >                fout << pages[index](3) << '\n';
193 >        }
194 >
195 >        fout << "\t\t</list>\n"
196 >                << "\t</section>\n"
197 >                << "</page>\n";
198 >
199 >        fout.close();
200 > }
201 >
202 > void SiteMapper::newMap(vector<Page>& pages, const string& childOf,
203 >        multimap<string, Page>& newPages)
204 > {
205 >        for (unsigned index(0); index < pages.size(); ++index)
206 >        {
207 >                newMap(pages[index].getChildren(), pages[index].getPath(), newPages);
208 >        }
209  
210 <        for (list<Page>::iterator itor = pages.begin(); itor != pages.end();
211 <                itor++)
210 >        for (multimap<string, Page>::iterator itor(newPages.lower_bound(childOf));
211 >                itor != newPages.upper_bound(childOf); itor++)
212          {
213 <                cout << *itor << '\n';
213 >                pages.push_back(itor->second);
214          }
215 +
216 +        newPages.erase(childOf);
217   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines