ae4ffa0c553321976e1f2d51864a1af03a195105
[librarian.git] / librarian / parser.py
1 # -*- coding: utf-8 -*-
2 from librarian import ValidationError, NoDublinCore,  ParseError
3 from librarian import RDFNS
4 from librarian import dcparser
5
6 from xml.parsers.expat import ExpatError
7 from lxml import etree
8 from lxml.etree import XMLSyntaxError, XSLTApplyError
9
10 import re
11 from StringIO import StringIO
12
13 class WLDocument(object):
14     LINE_SWAP_EXPR = re.compile(r'/\s', re.MULTILINE | re.UNICODE);
15
16     def __init__(self, edoc, parse_dublincore=True):
17         self.edoc = edoc
18
19         root_elem = edoc.getroot()
20        
21         dc_path = './/' + RDFNS('RDF')
22         
23         if root_elem.tag != 'utwor':
24             raise ValidationError("Invalid root element. Found '%s', should be 'utwor'" % root_elem.tag)
25
26         if parse_dublincore:
27             self.rdf_elem = root_elem.find(dc_path)
28
29             if self.rdf_elem is None:
30                 raise NoDublinCore('Document has no DublinCore - which is required.')
31             
32             self.book_info = dcparser.BookInfo.from_element(self.rdf_elem)
33         else:
34             self.book_info = None
35     
36     @classmethod
37     def from_string(cls, xml, swap_endlines=False, parse_dublincore=True):
38         return cls.from_file(StringIO(xml), swap_endlines, parse_dublincore=parse_dublincore)
39
40     @classmethod
41     def from_file(cls, xmlfile, swap_endlines=False, parse_dublincore=True):
42
43         # first, prepare for parsing
44         if isinstance(xmlfile, basestring):
45             file = open(xmlfile, 'rb')
46             try:
47                 data = file.read()
48             finally:
49                 file.close()
50         else:
51             data = xmlfile.read()
52
53         if not isinstance(data, unicode):
54             data = data.decode('utf-8')
55
56         if swap_endlines:
57             data = cls.LINE_SWAP_EXPR.sub(u'<br />\n', data)
58     
59         try:
60             parser = etree.XMLParser(remove_blank_text=False)
61             return cls(etree.parse(StringIO(data), parser), parse_dublincore=parse_dublincore)
62         except (ExpatError, XMLSyntaxError, XSLTApplyError), e:
63             raise ParseError(e)                  
64
65     def chunk(self, path):
66         # convert the path to XPath        
67         expr = self.path_to_xpath(path)
68         elems = self.edoc.xpath(expr)
69
70         if len(elems) == 0:
71             return None
72         else:        
73             return elems[0]
74
75     def path_to_xpath(self, path):
76         parts = []
77
78         for part in path.split('/'):
79             match = re.match(r'([^\[]+)\[(\d+)\]', part)
80             if not match:
81                 parts.append(part)
82             else:
83                 tag, n = match.groups()
84                 parts.append("*[%d][name() = '%s']" % (int(n)+1, tag) )
85
86         if parts[0] == '.':
87             parts[0] = ''
88
89         return '/'.join(parts)
90
91     def transform(self, stylesheet, **options):
92         return self.edoc.xslt(stylesheet, **options)
93
94     def update_dc(self):
95         if self.book_info:
96             parent = self.rdf_elem.getparent()
97             parent.replace( self.rdf_elem, self.book_info.to_etree(parent) )
98
99     def serialize(self):
100         self.update_dc()
101         return etree.tostring(self.edoc, encoding=unicode, pretty_print=True)
102
103     def merge_chunks(self, chunk_dict):
104         unmerged = []
105
106         for key, data in chunk_dict.iteritems():
107             try:
108                 xpath = self.path_to_xpath(key)
109                 node = self.edoc.xpath(xpath)[0]
110                 repl = etree.fromstring(u"<%s>%s</%s>" %(node.tag, data, node.tag) )
111                 node.getparent().replace(node, repl);
112             except Exception, e:
113                 unmerged.append( repr( (key, xpath, e) ) )
114
115         return unmerged