'Adds <reference> elements to a <resource>
@param element: the <resource> element
@param rmap: the reference map for the corresponding record
@param show_ids: insert the record ID as attribute in references'

def add_references(self, element, rmap, show_ids=False, lazy=None):

    REFERENCE = self.TAG.reference
    ATTRIBUTE = self.ATTRIBUTE
    RESOURCE = ATTRIBUTE.resource
    FIELD = ATTRIBUTE.field
    VALUE = ATTRIBUTE.value
    ID = ATTRIBUTE.id
    RB = ATTRIBUTE.replaced_by
    UID = self.UID
    REPLACEDBY = self.REPLACEDBY
    as_json = json.dumps
    SubElement = etree.SubElement
    for i in xrange(0, len(rmap)):
        r = rmap[i]
        f = r.field
        if (f == REPLACEDBY):
            element.set(RB, r.uid[0])
            continue
        reference = SubElement(element, REFERENCE)
        attr = reference.attrib
        attr[FIELD] = f
        attr[RESOURCE] = r.table
        if show_ids:
            if r.multiple:
                ids = str(as_json(r.id))
            else:
                ids = str(r.id[0])
            attr[ID] = ids
        if r.uid:
            if r.multiple:
                uids = str(as_json(r.uid))
            else:
                uids = str(r.uid[0])
            attr[UID] = uids.decode('utf-8')
            if (r.lazy is not None):
                if (lazy is not None):
                    lazy.append((r.lazy, reference, None, None))
                else:
                    r.lazy.render_node(reference, None, None)
            else:
                reference.text = r.text
        else:
            attr[VALUE] = r.value
        r.element = reference
