Index: collective.imstransport/trunk/collective/imstransport/utilities/imsinterchange.py
===================================================================
--- collective.imstransport/trunk/collective/imstransport/utilities/imsinterchange.py	(revision 199)
+++ collective.imstransport/trunk/collective/imstransport/utilities/imsinterchange.py	(revision 202)
@@ -51,88 +51,9 @@
         return result                    
 
-class IMSInterchangeReader(object):
-
-    implements(IIMSManifestReader)
-    name = _(u'IMS Interchange Reader')
-
-    def determineType(self, context, hashref, objDict, filename):
-        """ Determine the type of the incoming object """
-
-        if objDict[hashref].has_key('type') and objDict[hashref]['type']:
-            return objDict[hashref]['type']
-        elif objDict[hashref].has_key('Format') and objDict[hashref]['Format'] in ['text/html', 'text/htm' 'text/plain' 'text/x-rst', 'text/structured']:
-            return 'Document'
-        elif objDict[hashref].has_key('Format') and re.match('^image', objDict[hashref]['Format']):
-            return 'Image'
-        else:
-            return self.determineFileMimetype(filename, context)
-            
-    def createIdFromFile(self, file):
-        """ Get Id from file path """
-        return file.split('/')[-1]
-
-    def createPathFromFile(self, file):
-        """ Get folder path from file path """
-        return '/'.join(file.split('/')[:-1])
-
-
-    def determineFileMimetype(self, name, context):
-        """ Determine the file mimetype """
-        portal = context.portal_url.getPortalObject()
-        registry = portal.mimetypes_registry
-        mimetype = registry.lookupExtension(name)
-
-        if mimetype and mimetype.major() == 'text' and mimetype.minor() == 'html':
-            return 'Document'
-        elif mimetype and mimetype.major() == 'image':
-            return 'Image'
-        else:
-            return 'File'
-
-    def parseFile(self, context, file, objDict, hashref, id, path):
-        """ parse a file object and add data to it """
-        objDict[hashref]['file'] = file
-        objDict[hashref]['id'] = id
-        objDict[hashref]['path'] = path
-        objDict[hashref]['type'] = self.determineType(context, hashref, objDict, file)
-
-
-    def parseResourceMetadata(self, resourcereader, resourceid, context):
-        """ Read the resource metadata """
-
-        resourcereader.readGeneral()
-        resourcereader.readLifecycle()
-        resourcereader.readMetaMetadata()
-        resourcereader.readTechnical()
-        resourcereader.readRights()
-        customnode = resourcereader.getCustomData('', '')
-        if customnode:
-            metadict = resourcereader.readCustomMetadata(customnode)
-            resourcereader.appendCustomData(metadict)
-        return resourcereader.processResourceMetadata()
-        
-    def readCustomData(self, context, prefix, ns, location):
-        """ 
-        Hook for reading custom metadata for additional metadata requirements. 
-        Should return a dictionary of values representing attribute name and value.
-        """
-
-        return {}
-
-    def getObjectDictionary(self):
-        """ Return the object dictionary """
-        return self.objdict
-
-
-    def getPackageName(self):
-
-        return self.name
-
-
-class IMSInterchangeWriter(object):
+
+class IMSWriter(object):
+    """ Base class for IMS writer objects. """
 
     implements(IIMSManifestWriter)
-    name = _(u'IMS Interchange Writer')  
-
 
     def _writeObjectData(self, obj, path):
@@ -223,2 +144,178 @@
         return
     
+
+    
+
+
+class IMSInterchangeReader(object):
+
+    implements(IIMSManifestReader)
+    name = _(u'IMS Interchange Reader')
+
+    def determineType(self, context, hashref, objDict, filename):
+        """ Determine the type of the incoming object """
+
+        if objDict[hashref].has_key('type') and objDict[hashref]['type']:
+            return objDict[hashref]['type']
+        elif objDict[hashref].has_key('Format') and objDict[hashref]['Format'] in ['text/html', 'text/htm' 'text/plain' 'text/x-rst', 'text/structured']:
+            return 'Document'
+        elif objDict[hashref].has_key('Format') and re.match('^image', objDict[hashref]['Format']):
+            return 'Image'
+        else:
+            return self.determineFileMimetype(filename, context)
+            
+    def createIdFromFile(self, file):
+        """ Get Id from file path """
+        return file.split('/')[-1]
+
+    def createPathFromFile(self, file):
+        """ Get folder path from file path """
+        return '/'.join(file.split('/')[:-1])
+
+
+    def determineFileMimetype(self, name, context):
+        """ Determine the file mimetype """
+        portal = context.portal_url.getPortalObject()
+        registry = portal.mimetypes_registry
+        mimetype = registry.lookupExtension(name)
+
+        if mimetype and mimetype.major() == 'text' and mimetype.minor() == 'html':
+            return 'Document'
+        elif mimetype and mimetype.major() == 'image':
+            return 'Image'
+        else:
+            return 'File'
+
+    def parseFile(self, context, file, objDict, hashref, id, path):
+        """ parse a file object and add data to it """
+        objDict[hashref]['file'] = file
+        objDict[hashref]['id'] = id
+        objDict[hashref]['path'] = path
+        objDict[hashref]['type'] = self.determineType(context, hashref, objDict, file)
+
+
+    def parseResourceMetadata(self, resourcereader, resourceid, context):
+        """ Read the resource metadata """
+
+        resourcereader.readGeneral()
+        resourcereader.readLifecycle()
+        resourcereader.readMetaMetadata()
+        resourcereader.readTechnical()
+        resourcereader.readRights()
+        customnode = resourcereader.getCustomData('', '')
+        if customnode:
+            metadict = resourcereader.readCustomMetadata(customnode)
+            resourcereader.appendCustomData(metadict)
+        return resourcereader.processResourceMetadata()
+        
+    def readCustomData(self, context, prefix, ns, location):
+        """ 
+        Hook for reading custom metadata for additional metadata requirements. 
+        Should return a dictionary of values representing attribute name and value.
+        """
+
+        return {}
+
+    def getObjectDictionary(self):
+        """ Return the object dictionary """
+        return self.objdict
+
+
+    def getPackageName(self):
+
+        return self.name
+
+
+class IMSInterchangeWriter(object):
+
+    implements(IIMSManifestWriter)
+    name = _(u'IMS Interchange Writer')  
+
+
+    def _writeObjectData(self, obj, path):
+        """ Write file data to the destination object. """
+        if type(obj) == type(''):
+            data = obj        
+        else:
+            format = ''
+            if hasattr(obj.aq_explicit, 'Format'):
+                format = obj.Format()
+            if obj.Type() in ['File', 'Image'] and hasattr(obj.aq_explicit, 'data'):
+                    data = obj.data
+            elif 'text/html' == format and hasattr(obj.aq_explicit, 'getText'):
+                    data = obj.getText()
+            elif format in ['text/plain', 'text/x-rst', 'text/structured'] and hasattr(obj.aq_explicit, 'getRawText'):
+                    data = obj.getRawText()
+            else:
+                data = ''
+
+        if self.destination:
+            self.destination.writeFile(path, data)
+
+    def _getAllObjects(self, context):
+        """ Get all sub objects. """
+        objects = [obj.getObject() for obj in context.portal_catalog.searchResults(path={'query':('/'.join(context.getPhysicalPath())+'/'),'depth':-1})]
+        return [object for object in objects if not object.isPrincipiaFolderish or getattr(object.aq_explicit, 'getText', None)]
+
+    def _getChildrenObjects(self, parent, objects):
+        """ Get objects in current folder """
+        parentpath = '/'.join(parent.getPhysicalPath())
+        children = []
+        for object in objects:
+            rpath = '/'.join(object.getPhysicalPath()).replace(parentpath, '')
+
+            if len(rpath.split('/')) == 1:
+                children.append(object)
+                       
+        return children
+
+    def _getObjectPath(self, obj, context):
+        """ Get the path of an object. """
+
+        root_path = context.aq_explicit.virtual_url_path()
+        obj_path = obj.aq_explicit.virtual_url_path()
+
+        if obj_path.find(root_path) != 0:
+            return ''
+
+        # Remove the path of the folder object
+        path = obj_path.replace(root_path, '')
+        if path and path[0] == '/':
+            path = path[1:]
+
+        if not path:
+            return ''
+
+        if hasattr(obj.aq_explicit, 'Format'):
+            if 'text/html' == obj.Format() and obj.isPrincipiaFolderish:
+                path += '.html'
+
+        return path
+
+    def _createPathId(self, path, pre='RES'):
+        """ Create a unique id given a path """
+        return pre + str(md5.md5(path).hexdigest())
+
+    def _getCopyrightString(self, copyright, rights_holder, rights_holder_email):
+        cp = ''
+        if copyright:
+            cp += copyright
+        if rights_holder:
+            if cp:
+                cp += ', '
+            cp += rights_holder
+        if rights_holder_email:
+            if cp:
+                cp += ', '
+            cp += rights_holder_email
+        return cp
+ 
+    def getObjSize(self, object):
+        """ Retrieves the correct size of the object"""
+        return '%d' %object.get_size()
+
+    def writeCustomMetadata(self, object):
+        """ Write the custom metdata for the package. Overridden at lower levels. """
+
+        return
+    
