The XML document reader provides an integration between the SAX L{Parser} and the document cache.
| 54 | |
| 55 | |
| 56 | class DocumentReader(Reader): |
| 57 | """ |
| 58 | The XML document reader provides an integration |
| 59 | between the SAX L{Parser} and the document cache. |
| 60 | """ |
| 61 | |
| 62 | def open(self, url): |
| 63 | """ |
| 64 | Open an XML document at the specified I{url}. |
| 65 | First, the document attempted to be retrieved from |
| 66 | the I{object cache}. If not found, it is downloaded and |
| 67 | parsed using the SAX parser. The result is added to the |
| 68 | cache for the next open(). |
| 69 | @param url: A document url. |
| 70 | @type url: str. |
| 71 | @return: The specified XML document. |
| 72 | @rtype: I{Document} |
| 73 | """ |
| 74 | cache = self.cache() |
| 75 | id = self.mangle(url, 'document') |
| 76 | d = cache.get(id) |
| 77 | if d is None: |
| 78 | d = self.download(url) |
| 79 | cache.put(id, d) |
| 80 | self.plugins.document.parsed(url=url, document=d.root()) |
| 81 | return d |
| 82 | |
| 83 | def download(self, url): |
| 84 | """ |
| 85 | Download the docuemnt. |
| 86 | @param url: A document url. |
| 87 | @type url: str. |
| 88 | @return: A file pointer to the docuemnt. |
| 89 | @rtype: file-like |
| 90 | """ |
| 91 | store = DocumentStore() |
| 92 | fp = store.open(url) |
| 93 | if fp is None: |
| 94 | fp = self.options.transport.open(Request(url)) |
| 95 | content = fp.read() |
| 96 | fp.close() |
| 97 | ctx = self.plugins.document.loaded(url=url, document=content) |
| 98 | content = ctx.document |
| 99 | sax = Parser() |
| 100 | return sax.parse(string=content) |
| 101 | |
| 102 | def cache(self): |
| 103 | """ |
| 104 | Get the cache. |
| 105 | @return: The I{options} when I{cachingpolicy} = B{0}. |
| 106 | @rtype: L{Cache} |
| 107 | """ |
| 108 | if self.options.cachingpolicy == 0: |
| 109 | return self.options.cache |
| 110 | else: |
| 111 | return NoCache() |
| 112 | |
| 113 |