Back to index

plone3  3.1.7
exportimport.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ZCTextIndex export / import support.
00014 
00015 $Id: exportimport.py 71199 2006-11-20 11:03:29Z yuppie $
00016 """
00017 
00018 from BTrees.IOBTree import IOBTree
00019 from BTrees.Length import Length
00020 from BTrees.OIBTree import OIBTree
00021 from zope.component import adapts
00022 
00023 from Products.GenericSetup.interfaces import ISetupEnviron
00024 from Products.GenericSetup.utils import NodeAdapterBase
00025 
00026 from Products.ZCTextIndex.interfaces import IZCLexicon
00027 from Products.ZCTextIndex.interfaces import IZCTextIndex
00028 from Products.ZCTextIndex.PipelineFactory import element_factory
00029 
00030 
00031 class ZCLexiconNodeAdapter(NodeAdapterBase):
00032 
00033     """Node im- and exporter for ZCTextIndex Lexicon.
00034     """
00035 
00036     adapts(IZCLexicon, ISetupEnviron)
00037 
00038     def _exportNode(self):
00039         """Export the object as a DOM node.
00040         """
00041         node = self._getObjectNode('object')
00042         for element in self.context._pipeline:
00043             group, name = self._getKeys(element)
00044             child = self._doc.createElement('element')
00045             child.setAttribute('group', group)
00046             child.setAttribute('name', name)
00047             node.appendChild(child)
00048         return node
00049 
00050     def _importNode(self, node):
00051         """Import the object from the DOM node.
00052         """
00053         pipeline = []
00054         for child in node.childNodes:
00055             if child.nodeName == 'element':
00056                 element = element_factory.instantiate(
00057                       child.getAttribute('group').encode('utf-8'),
00058                       child.getAttribute('name').encode('utf-8'))
00059                 pipeline.append(element)
00060         self.context._pipeline = tuple(pipeline)
00061         #clear lexicon
00062         self.context._wids = OIBTree()
00063         self.context._words = IOBTree()
00064         self.context.length = Length()
00065 
00066     node = property(_exportNode, _importNode)
00067 
00068     def _getKeys(self, element):
00069         for group in element_factory.getFactoryGroups():
00070             for name, factory in element_factory._groups[group].items():
00071                 if factory == element.__class__:
00072                     return group, name
00073 
00074 
00075 class ZCTextIndexNodeAdapter(NodeAdapterBase):
00076 
00077     """Node im- and exporter for ZCTextIndex.
00078     """
00079 
00080     adapts(IZCTextIndex, ISetupEnviron)
00081 
00082     def _exportNode(self):
00083         """Export the object as a DOM node.
00084         """
00085         node = self._getObjectNode('index')
00086 
00087         for value in self.context.getIndexSourceNames():
00088             child = self._doc.createElement('indexed_attr')
00089             child.setAttribute('value', value)
00090             node.appendChild(child)
00091 
00092         child = self._doc.createElement('extra')
00093         child.setAttribute('name', 'index_type')
00094         child.setAttribute('value', self.context.getIndexType())
00095         node.appendChild(child)
00096 
00097         child = self._doc.createElement('extra')
00098         child.setAttribute('name', 'lexicon_id')
00099         child.setAttribute('value', self.context.lexicon_id)
00100         node.appendChild(child)
00101 
00102         return node
00103 
00104     def _importNode(self, node):
00105         """Import the object from the DOM node.
00106         """
00107         indexed_attrs = []
00108         for child in node.childNodes:
00109             if child.nodeName == 'indexed_attr':
00110                 indexed_attrs.append(
00111                                   child.getAttribute('value').encode('utf-8'))
00112         self.context._indexed_attrs = indexed_attrs
00113         self.context.clear()
00114 
00115     node = property(_exportNode, _importNode)