1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from StringIO import StringIO
9 from .core import Section
10 from .parser import SSTParser
13 class Document(object):
14 # Do I use meta_context?
15 def __init__(self, edoc, meta_context=None):
18 root_elem = edoc.getroot()
19 if meta_context is not None:
20 root_elem.meta_context = meta_context
22 if not isinstance(root_elem, Section):
23 if root_elem.tag != SSTNS('section'):
24 raise ValidationError("Invalid root element. Found '%s', should be '%s'" % (
25 root_elem.tag, SSTNS('section')))
27 raise ValidationError("Invalid class of root element. "
28 "Use librarian.parser.SSTParser.")
31 def from_string(cls, xml, *args, **kwargs):
32 return cls.from_file(StringIO(xml), *args, **kwargs)
35 def from_file(cls, xmlfile, *args, **kwargs):
36 # first, prepare for parsing
37 if isinstance(xmlfile, basestring):
38 file = open(xmlfile, 'rb')
46 if not isinstance(data, unicode):
47 data = data.decode('utf-8')
49 data = data.replace(u'\ufeff', '')
52 tree = etree.parse(StringIO(data.encode('utf-8')), parser)
54 return cls(tree, *args, **kwargs)
58 """ Document's metadata is root's metadata. """
59 return self.edoc.getroot().meta