'Optimise & Modifiy s3xml etree to and produce s3ocr etree
@return: s3ocr etree'

def __s3OCREtree(self):

    r = self.r
    s3xml_etree = self.resource.export_struct(options=True, references=True, stylesheet=None, as_json=False, as_tree=True)
    ITEXT = 'label'
    HINT = 'comment'
    TYPE = 'type'
    HASOPTIONS = 'has_options'
    LINES = 'lines'
    BOXES = 'boxes'
    REFERENCE = 'reference'
    RESOURCE = 'resource'
    s3xml_root = s3xml_etree.getroot()
    resource_element = s3xml_root.getchildren()[0]
    s3ocr_root = etree.Element('s3ocr')
    settings = current.deployment_settings
    self.exclude_component_list = settings.get_pdf_excluded_fields(('%s_%s' % (r.prefix, r.resource.name)))
    if r.component:
        s3ocr_root.append(resource_element)
    else:
        componentetrees = []
        mres = etree.Element('resource')
        for attr in resource_element.attrib.keys():
            mres.set(attr, resource_element.attrib.get(attr))
        for field_element in resource_element:
            if (field_element.tag == 'field'):
                mres.append(field_element)
            elif (field_element.tag == 'resource'):
                componentetrees.append(field_element)
        serialised_component_etrees = componentetrees
        s3ocr_root.append(mres)
        for res in serialised_component_etrees:
            s3ocr_root.append(res)
    self.generic_ocr_field_type = {'string': 'string', 'text': 'textbox', 'boolean': 'boolean', 'double': 'double', 'date': 'date', 'datetime': 'datetime', 'integer': 'integer', 'list:integer': 'multiselect', 'list:string': 'multiselect', 'list:double': 'multiselect', 'list:text': 'multiselect'}
    FIELD_TYPE_LINES = {'string': 1, 'textbox': 2, 'integer': 1, 'double': 1, 'date': 1, 'datetime': 1}
    FIELD_TYPE_BOXES = {'integer': 8, 'double': 16}
    for resource in s3ocr_root.iterchildren():
        rget = resource.attrib.get
        resourcetablename = rget('name')
        if (not r.component):
            if (rget('name') in self.exclude_component_list):
                s3ocr_root.remove(resource)
                continue
        if ('alias' in resource.attrib):
            alias = resource.attrib['alias']
        elif ('_' in resourcetablename):
            alias = resourcetablename.split('_', 1)[1]
        else:
            alias = resourcetablename
        if ((alias == self.resource.alias) and (resourcetablename == self.resource.tablename)):
            fieldresource = self.resource
        elif (alias in self.resource.components):
            fieldresource = self.resource.components[alias]
        else:
            continue
        for field in resource.iterchildren():
            get = field.attrib.get
            set = field.set
            fieldname = get('name')
            fieldtype = get(TYPE)
            if fieldtype.startswith('reference    '):
                set(RESOURCE, fieldtype.split('reference    ')[1])
                set(REFERENCE, '1')
            else:
                set(REFERENCE, '0')
            ocrfieldtype = self.generic_ocr_field_type.get(fieldtype, None)
            if (ocrfieldtype != None):
                set(TYPE, ocrfieldtype)
                fieldtype = get(TYPE)
            fieldhasoptions = get(HASOPTIONS)
            if (fieldhasoptions == 'False'):
                set(LINES, str(FIELD_TYPE_LINES.get(fieldtype, 1)))
                if (fieldtype in FIELD_TYPE_BOXES.keys()):
                    set(BOXES, str(FIELD_TYPE_BOXES.get(fieldtype)))
            if ((get('readable', 'False') == 'True') and (get('writable', 'False') == 'False')):
                fieldname = get('name')
                try:
                    fielddefault = fieldresource.table[fieldname].default
                except KeyError:
                    fielddefault = 'None'
                set('default', str(fielddefault))
            if (fieldtype not in self.generic_ocr_field_type.values()):
                set(TYPE, 'string')
                set(HASOPTIONS, 'False')
                set(LINES, '2')
                fieldtype = get(TYPE)
            if (fieldtype == 'boolean'):
                set(HASOPTIONS, 'True')
            if ((get('readable', 'False') == 'False') and (get('writable', 'False') == 'False')):
                resource.remove(field)
                continue
            if ((get(HASOPTIONS, 'False') == 'True') and (get(TYPE) != 'boolean')):
                s3ocrselect = field.getchildren()[0]
                for option in s3ocrselect.iterchildren():
                    if ((option.text == '') or (option.text == None)):
                        s3ocrselect.remove(option)
                        continue
    return s3ocr_root
