Example #1
0
 def contextDiscover(self, cntxElement):
     id = cntxElement.getAttribute("id")
     self.modelXbrl.contexts[id] = modelContext = ModelObject.createContext(
         self, cntxElement)
     for container in (("segment", modelContext.segDimValues,
                        modelContext.segNonDimValues),
                       ("scenario", modelContext.scenDimValues,
                        modelContext.scenNonDimValues)):
         containerName, containerDimValues, containerNonDimValues = container
         for containerElement in XmlUtil.descendants(
                 cntxElement, XbrlConst.xbrli, containerName):
             for sElt in containerElement.childNodes:
                 if sElt.nodeType == 1:
                     if sElt.namespaceURI == XbrlConst.xbrldi and sElt.localName in (
                             "explicitMember", "typedMember"):
                         XmlValidate.validate(self.modelXbrl, sElt)
                         modelDimValue = ModelObject.createDimensionValue(
                             self, sElt)
                         dimension = modelDimValue.dimension
                         if dimension and dimension not in containerDimValues:
                             containerDimValues[dimension] = modelDimValue
                         else:
                             modelContext.errorDimValues.append(
                                 modelDimValue)
                         modelContext.qnameDims[
                             modelDimValue.
                             dimensionQname] = modelDimValue  # both seg and scen
                     else:
                         containerNonDimValues.append(sElt)
Example #2
0
 def schemaDiscoverChildElements(self, parentElement):
     for element in parentElement.childNodes:
         if element.nodeType == 1: #element
             ln = element.localName
             ns = element.namespaceURI
             modelObject = None
             if ns == XbrlConst.link:
                 if ln == "roleType":
                     modelObject = ModelObject.create(self, element)
                     self.modelXbrl.roleTypes[modelObject.roleURI].append(modelObject)
                 elif ln == "arcroleType":
                     modelObject = ModelObject.create(self, element)
                     self.modelXbrl.arcroleTypes[modelObject.arcroleURI].append(modelObject)
                 elif ln == "linkbaseRef":
                     self.schemaLinkbaseRefDiscover(element)
                 elif ln == "linkbase":
                     self.linkbaseDiscover(element)
             elif ns == XbrlConst.xsd:
                 if ln in xsdModelObjects:
                     modelObject = ModelObject.create(self, element)
                 elif ln == "import" or ln == "include":
                     self.importDiscover(element)
             # save document objects indexed by id
             if modelObject is not None and element.hasAttribute("id"):
                 self.idObjects[element.getAttribute("id")] = modelObject
             # recurse to children
             self.schemaDiscoverChildElements(element)
Example #3
0
 def schemaDiscoverChildElements(self, parentElement):
     for element in parentElement.childNodes:
         if element.nodeType == 1:  #element
             ln = element.localName
             ns = element.namespaceURI
             modelObject = None
             if ns == XbrlConst.link:
                 if ln == "roleType":
                     modelObject = ModelObject.create(self, element)
                     self.modelXbrl.roleTypes[modelObject.roleURI].append(
                         modelObject)
                 elif ln == "arcroleType":
                     modelObject = ModelObject.create(self, element)
                     self.modelXbrl.arcroleTypes[
                         modelObject.arcroleURI].append(modelObject)
                 elif ln == "linkbaseRef":
                     self.schemaLinkbaseRefDiscover(element)
                 elif ln == "linkbase":
                     self.linkbaseDiscover(element)
             elif ns == XbrlConst.xsd:
                 if ln in xsdModelObjects:
                     modelObject = ModelObject.create(self, element)
                 elif ln == "import" or ln == "include":
                     self.importDiscover(element)
             # save document objects indexed by id
             if modelObject is not None and element.hasAttribute("id"):
                 self.idObjects[element.getAttribute("id")] = modelObject
             # recurse to children
             self.schemaDiscoverChildElements(element)
Example #4
0
    def inlineXbrlDiscover(self, htmlElement):
        self.schemaLinkbaseRefsDiscover(htmlElement)
        for inlineElement in htmlElement.getElementsByTagNameNS(
                XbrlConst.ixbrl, "resources"):
            self.instanceContentsDiscover(inlineElement)

        tuplesByElement = {}
        tuplesByTupleID = {}
        for inlineElement in htmlElement.getElementsByTagNameNS(
                XbrlConst.ixbrl, "tuple"):
            modelInlineFact = ModelObject.createInlineFact(self, inlineElement)
            modelInlineFact.unorderedTupleFacts = []
            if modelInlineFact.tupleID:
                tuplesByTupleID[modelInlineFact.tupleID] = modelInlineFact
            tuplesByElement[inlineElement] = modelInlineFact
        # hook up tuples to their container
        for tupleFact in tuplesByElement.values():
            self.inlineXbrlLocateFactInTuple(tupleFact, tuplesByTupleID,
                                             tuplesByElement)

        for ln in ("nonNumeric", "nonFraction", "fraction"):
            for inlineElement in htmlElement.getElementsByTagNameNS(
                    XbrlConst.ixbrl, ln):
                modelInlineFact = ModelObject.createInlineFact(
                    self, inlineElement)
                self.inlineXbrlLocateFactInTuple(modelInlineFact,
                                                 tuplesByTupleID,
                                                 tuplesByElement)
        # order tuple facts
        for tupleFact in tuplesByElement.values():
            tupleFact.modelTupleFacts = [
                self.modelXbrl.modelObject(objectIndex)
                for order, objectIndex in sorted(tupleFact.unorderedTupleFacts)
            ]
Example #5
0
    def inlineXbrlDiscover(self, htmlElement):
        self.schemaLinkbaseRefsDiscover(htmlElement)
        for inlineElement in htmlElement.getElementsByTagNameNS(XbrlConst.ixbrl, "resources"):
            self.instanceContentsDiscover(inlineElement)
            
        tuplesByElement = {}
        tuplesByTupleID = {}
        for inlineElement in htmlElement.getElementsByTagNameNS(XbrlConst.ixbrl, "tuple"):
            modelInlineFact = ModelObject.createInlineFact(self, inlineElement)
            modelInlineFact.unorderedTupleFacts = []
            if modelInlineFact.tupleID:
                tuplesByTupleID[modelInlineFact.tupleID] = modelInlineFact
            tuplesByElement[inlineElement] = modelInlineFact
        # hook up tuples to their container
        for tupleFact in tuplesByElement.values():
            self.inlineXbrlLocateFactInTuple(tupleFact, tuplesByTupleID, tuplesByElement)

        for ln in ("nonNumeric", "nonFraction", "fraction"):
            for inlineElement in htmlElement.getElementsByTagNameNS(XbrlConst.ixbrl, ln):
                modelInlineFact = ModelObject.createInlineFact(self, inlineElement)
                self.inlineXbrlLocateFactInTuple(modelInlineFact, tuplesByTupleID, tuplesByElement)
        # order tuple facts
        for tupleFact in tuplesByElement.values():
            tupleFact.modelTupleFacts = [
                 self.modelXbrl.modelObject(objectIndex) 
                 for order,objectIndex in sorted(tupleFact.unorderedTupleFacts)]
Example #6
0
 def testcaseDiscover(self, testcaseElement):
     if XmlUtil.xmlnsprefix(testcaseElement, XbrlConst.cfcn):
         self.type = Type.REGISTRYTESTCASE
     self.testcaseVariations = [ModelObject.createTestcaseVariation(self, variationElement)
                                for variationElement in testcaseElement.getElementsByTagName("variation")]
     if len(self.testcaseVariations) == 0:
         # may be a inline test case
         for i in range(len(testcaseElement.attributes)):
             if testcaseElement.attributes.item(i).value == XbrlConst.ixbrl:
                 modelVariation = ModelObject.createTestcaseVariation(self, testcaseElement)
                 self.testcaseVariations.append(modelVariation)
                 break
Example #7
0
 def testcaseDiscover(self, testcaseElement):
     if XmlUtil.xmlnsprefix(testcaseElement, XbrlConst.cfcn):
         self.type = Type.REGISTRYTESTCASE
     self.testcaseVariations = [
         ModelObject.createTestcaseVariation(self, variationElement)
         for variationElement in testcaseElement.getElementsByTagName(
             "variation")
     ]
     if len(self.testcaseVariations) == 0:
         # may be a inline test case
         for i in range(len(testcaseElement.attributes)):
             if testcaseElement.attributes.item(i).value == XbrlConst.ixbrl:
                 modelVariation = ModelObject.createTestcaseVariation(
                     self, testcaseElement)
                 self.testcaseVariations.append(modelVariation)
                 break
Example #8
0
 def contextDiscover(self, cntxElement):
     id = cntxElement.getAttribute("id")
     self.modelXbrl.contexts[id] = modelContext = ModelObject.createContext(self,cntxElement)
     for container in (("segment", modelContext.segDimValues, modelContext.segNonDimValues),
                       ("scenario", modelContext.scenDimValues, modelContext.scenNonDimValues)):
         containerName, containerDimValues, containerNonDimValues = container
         for containerElement in XmlUtil.descendants(cntxElement, XbrlConst.xbrli, containerName):
             for sElt in containerElement.childNodes:
                 if sElt.nodeType == 1:
                     if sElt.namespaceURI == XbrlConst.xbrldi and sElt.localName in ("explicitMember","typedMember"):
                         XmlValidate.validate(self.modelXbrl, sElt)
                         modelDimValue = ModelObject.createDimensionValue(self,sElt)
                         dimension = modelDimValue.dimension
                         if dimension and dimension not in containerDimValues:
                             containerDimValues[dimension] = modelDimValue
                         else:
                             modelContext.errorDimValues.append(modelDimValue)
                         modelContext.qnameDims[modelDimValue.dimensionQname] = modelDimValue # both seg and scen
                     else:
                         containerNonDimValues.append(sElt)
Example #9
0
 def factDiscover(self, factElement, modelFacts):
     modelFact = ModelObject.createFact(self, factElement)
     modelFacts.append( modelFact )
     self.modelXbrl.factsInInstance.append( modelFact )
     id = modelFact.id
     if id is not None:
         self.idObjects[id] = modelFact
     for tupleElement in factElement.childNodes:
         if tupleElement.nodeType == 1: #element
             self.factDiscover(tupleElement, modelFact.modelTupleFacts)
             
     return modelFact
Example #10
0
    def factDiscover(self, factElement, modelFacts):
        modelFact = ModelObject.createFact(self, factElement)
        modelFacts.append(modelFact)
        self.modelXbrl.factsInInstance.append(modelFact)
        id = modelFact.id
        if id is not None:
            self.idObjects[id] = modelFact
        for tupleElement in factElement.childNodes:
            if tupleElement.nodeType == 1:  #element
                self.factDiscover(tupleElement, modelFact.modelTupleFacts)

        return modelFact
Example #11
0
def ineffectiveArcs(baseSetModelLinks, arcrole, arcqname=None):
    relationships = defaultdict(list)
    for modelLink in baseSetModelLinks:
        arcs = []
        for linkChild in modelLink.element.childNodes:
            if (
                linkChild.nodeType == 1
                and linkChild.getAttributeNS(XbrlConst.xlink, "type") == "arc"
                and arcrole == linkChild.getAttributeNS(XbrlConst.xlink, "arcrole")
                and (arcqname is None or arcqname == linkChild)
            ):
                arcs.append(linkChild)

        # build network
        for arcElement in arcs:
            arcrole = arcElement.getAttributeNS(XbrlConst.xlink, "arcrole")
            fromLabel = arcElement.getAttributeNS(XbrlConst.xlink, "from")
            toLabel = arcElement.getAttributeNS(XbrlConst.xlink, "to")
            for fromResource in modelLink.labeledResources[fromLabel]:
                for toResource in modelLink.labeledResources[toLabel]:
                    modelRel = ModelObject.createRelationship(
                        modelLink.modelDocument, arcElement, fromResource.dereference(), toResource.dereference()
                    )
                    relationships[modelRel.equivalenceKey].append(modelRel)
    # determine ineffective relationships
    ineffectives = []
    for equivalenceKey, relationship in relationships.items():
        # sort by priority, prohibited
        equivalentRels = []
        i = 0
        for modelRel in relationship:
            equivalentRels.append((modelRel.priority, modelRel.prohibitedUseSortKey, i))
            i += 1
        equivalentRels.sort()
        priorRel = None
        for rel in equivalentRels:
            if rel[1] == 2:  # this rel is prohibited
                if priorRel is None:
                    ineffectives.append(relationship[rel[2]])  # this rel ineffective
                elif priorRel[1] == 2:  # prior rel is prohibited
                    ineffectives.append(priorRel[2])
            else:
                if priorRel is not None and priorRel[1] != 2:
                    ineffectives.append(relationship[priorRel[2]])  # prior ineffective
            priorRel = rel
    return ineffectives
Example #12
0
def ineffectiveArcs(baseSetModelLinks, arcrole, arcqname=None):
    relationships = defaultdict(list)
    for modelLink in baseSetModelLinks:
        arcs = []
        for linkChild in modelLink.element.childNodes:
            if linkChild.nodeType == 1 and \
               linkChild.getAttributeNS(XbrlConst.xlink, "type") == "arc" and \
               arcrole == linkChild.getAttributeNS(XbrlConst.xlink, "arcrole") and \
               (arcqname is None or arcqname == linkChild):
                arcs.append(linkChild)
                    
        # build network
        for arcElement in arcs:
            arcrole = arcElement.getAttributeNS(XbrlConst.xlink, "arcrole")
            fromLabel = arcElement.getAttributeNS(XbrlConst.xlink, "from")
            toLabel = arcElement.getAttributeNS(XbrlConst.xlink, "to")
            for fromResource in modelLink.labeledResources[fromLabel]:
                for toResource in modelLink.labeledResources[toLabel]:
                    modelRel = ModelObject.createRelationship(modelLink.modelDocument, arcElement, fromResource.dereference(), toResource.dereference())
                    relationships[modelRel.equivalenceKey].append(modelRel)
    # determine ineffective relationships
    ineffectives = []
    for equivalenceKey, relationship in relationships.items():
        #sort by priority, prohibited
        equivalentRels = []
        i = 0
        for modelRel in relationship:
            equivalentRels.append((modelRel.priority,modelRel.prohibitedUseSortKey,i))
            i += 1
        equivalentRels.sort()
        priorRel = None
        for rel in equivalentRels:
            if rel[1] == 2: # this rel is prohibited
                if priorRel is None:
                    ineffectives.append(relationship[rel[2]]) # this rel ineffective
                elif priorRel[1] == 2: # prior rel is prohibited
                    ineffectives.append(priorRel[2])
            else:
                if priorRel is not None and \
                   priorRel[1] != 2:
                    ineffectives.append(relationship[priorRel[2]]) # prior ineffective
            priorRel = rel
    return ineffectives
Example #13
0
    def __init__(self, modelXbrl, arcrole, linkrole=None, linkqname=None, arcqname=None, includeProhibits=False):
        self.isChanged = False
        self.modelXbrl = modelXbrl
        self.arcrole = arcrole
        self.linkrole = linkrole
        self.linkqname = linkqname
        self.arcqname = arcqname

        baseSetKey = (arcrole, linkrole, linkqname, arcqname)
        relationshipSetKey = (arcrole, linkrole, linkqname, arcqname, includeProhibits)

        # base sets does not care about the #includeProhibits
        if baseSetKey in self.modelXbrl.baseSets:
            modelLinks = self.modelXbrl.baseSets[baseSetKey]
        else:
            modelLinks = []

        # gather arcs
        relationships = {}
        isDimensionRel = self.arcrole == "XBRL-dimensions"  # all dimensional relationship arcroles
        isFormulaRel = self.arcrole == "XBRL-formulae"  # all formula relationship arcroles
        isEuRenderingRel = self.arcrole == "EU-rendering"
        isFootnoteRel = self.arcrole == "XBRL-footnotes"  # all footnote relationship arcroles

        for modelLink in modelLinks:
            arcs = []
            linkEltQname = modelLink.qname
            for linkChild in modelLink.element.childNodes:
                if (
                    linkChild.nodeType == 1
                    and linkChild.getAttributeNS(XbrlConst.xlink, "type") == "arc"
                    and linkChild.hasAttributeNS(XbrlConst.xlink, "arcrole")
                ):
                    linkChildArcrole = linkChild.getAttributeNS(XbrlConst.xlink, "arcrole")
                    linkChildQname = linkChild
                    if isFootnoteRel:
                        arcs.append(linkChild)
                    elif isDimensionRel:
                        if XbrlConst.isDimensionArcrole(linkChildArcrole):
                            arcs.append(linkChild)
                    elif isFormulaRel:
                        if XbrlConst.isFormulaArcrole(linkChildArcrole):
                            arcs.append(linkChild)
                    elif isEuRenderingRel:
                        if XbrlConst.isEuRenderingArcrole(linkChildArcrole):
                            arcs.append(linkChild)
                    elif (
                        arcrole == linkChildArcrole
                        and (arcqname is None or arcqname == linkChildQname)
                        and (linkqname is None or linkqname == linkEltQname)
                    ):
                        arcs.append(linkChild)

            # build network
            for arcElement in arcs:
                arcrole = arcElement.getAttributeNS(XbrlConst.xlink, "arcrole")
                fromLabel = arcElement.getAttributeNS(XbrlConst.xlink, "from")
                toLabel = arcElement.getAttributeNS(XbrlConst.xlink, "to")
                for fromResource in modelLink.labeledResources[fromLabel]:
                    for toResource in modelLink.labeledResources[toLabel]:
                        modelRel = ModelObject.createRelationship(
                            modelLink.modelDocument, arcElement, fromResource.dereference(), toResource.dereference()
                        )
                        modelRelEquivalenceKey = (
                            modelRel.equivalenceKey
                        )  # this is a complex tuple to compute, get once for below
                        if modelRelEquivalenceKey not in relationships or modelRel.priorityOver(
                            relationships[modelRelEquivalenceKey]
                        ):
                            relationships[modelRelEquivalenceKey] = modelRel

        # reduce effective arcs and order relationships...
        self.modelRelationships = []
        self.modelRelationshipsFrom = None
        self.modelRelationshipsTo = None
        self.modelConceptRoots = None
        self.modellinkRoleUris = None
        orderRels = defaultdict(list)
        for modelRel in relationships.values():
            if includeProhibits or not modelRel.isProhibited:
                orderRels[modelRel.order].append(modelRel)
        for order in sorted(orderRels.keys()):
            for modelRel in orderRels[order]:
                self.modelRelationships.append(modelRel)
        modelXbrl.relationshipSets[relationshipSetKey] = self
Example #14
0
 def unitDiscover(self, unitElement):
     id = unitElement.getAttribute("id")
     self.modelXbrl.units[id] = ModelObject.createUnit(self,unitElement)
Example #15
0
 def linkbaseDiscover(self, linkbaseElement, inInstance=False):
     for lbElement in linkbaseElement.childNodes:
         if lbElement.nodeType == 1: #element
             lbLn = lbElement.localName
             lbNs = lbElement.namespaceURI
             if lbNs == XbrlConst.link:
                 if lbLn == "roleRef" or lbLn == "arcroleRef":
                     href = self.discoverHref(lbElement)
                     if href is None:
                         self.modelXbrl.error(
                                 "Linkbase in {0} {1} href attribute missing or malformed".format(
                                   os.path.basename(self.uri),
                                   lbLn),
                                 "err", "xbrl:hrefMissing")
                     else:
                         self.hrefObjects.append(href)
                     continue
             if lbElement.getAttributeNS(XbrlConst.xlink, "type") == "extended":
                 self.schemalocateElementNamespace(lbElement)
                 arcrolesFound = set()
                 dimensionArcFound = False
                 formulaArcFound = False
                 euRenderingArcFound = False
                 linkQn = qname(lbElement)
                 linkrole = lbElement.getAttributeNS(XbrlConst.xlink, "role")
                 modelLink = ModelObject.createLink(self, lbElement)
                 if inInstance:
                     #index footnote links even if no arc children
                     baseSetKeys = (("XBRL-footnotes",None,None,None), 
                                    ("XBRL-footnotes",linkrole,None,None))
                     for baseSetKey in baseSetKeys:
                         self.modelXbrl.baseSets[baseSetKey].append(modelLink)
                 for linkElement in lbElement.childNodes:
                     if linkElement.nodeType == 1: #element
                         self.schemalocateElementNamespace(linkElement)
                         xlinkType = linkElement.getAttributeNS(XbrlConst.xlink, "type")
                         modelResource = None
                         if xlinkType == "locator":
                             nonDTS = linkElement.namespaceURI != XbrlConst.link or linkElement.localName != "loc"
                             # only link:loc elements are discovered or processed
                             href = self.discoverHref(linkElement, nonDTS=nonDTS)
                             if href is None:
                                 self.modelXbrl.error(
                                         "Linkbase in {0} {1} href attribute missing or malformed".format(
                                           os.path.basename(self.uri),
                                           lbLn),
                                         "err", "xbrl:hrefMissing")
                             else:
                                 modelResource = ModelObject.createLocator(self, linkElement, href)
                         elif xlinkType == "arc":
                             arcQn = qname(linkElement)
                             arcrole = linkElement.getAttributeNS(XbrlConst.xlink, "arcrole")
                             if arcrole not in arcrolesFound:
                                 if linkrole == "":
                                     linkrole = XbrlConst.defaultLinkRole
                                 #index by both arcrole and linkrole#arcrole and dimensionsions if applicable
                                 baseSetKeys = [(arcrole, linkrole, linkQn, arcQn)]
                                 baseSetKeys.append((arcrole, linkrole, None, None))
                                 baseSetKeys.append((arcrole, None, None, None))
                                 if XbrlConst.isDimensionArcrole(arcrole) and not dimensionArcFound:
                                     baseSetKeys.append(("XBRL-dimensions", None, None, None)) 
                                     baseSetKeys.append(("XBRL-dimensions", linkrole, None, None))
                                     dimensionArcFound = True
                                 if XbrlConst.isFormulaArcrole(arcrole) and not formulaArcFound:
                                     baseSetKeys.append(("XBRL-formulae", None, None, None)) 
                                     baseSetKeys.append(("XBRL-formulae", linkrole, None, None))
                                     formulaArcFound = True
                                 if XbrlConst.isEuRenderingArcrole(arcrole) and not euRenderingArcFound:
                                     baseSetKeys.append(("EU-rendering", None, None, None)) 
                                     baseSetKeys.append(("EU-rendering", linkrole, None, None)) 
                                     euRenderingArcFound = True
                                     self.modelXbrl.hasEuRendering = True
                                 for baseSetKey in baseSetKeys:
                                     self.modelXbrl.baseSets[baseSetKey].append(modelLink)
                                 arcrolesFound.add(arcrole)
                         elif xlinkType == "resource": 
                             # create resource and make accessible by id for document
                             modelResource = ModelObject.createResource(self, linkElement)
                         if modelResource is not None:
                             if linkElement.hasAttribute("id"):
                                 self.idObjects[linkElement.getAttribute("id")] = modelResource
                             modelLink.labeledResources[linkElement.getAttributeNS(XbrlConst.xlink, "label")] \
                                 .append(modelResource)
                         else:
                             XmlUtil.markIdAttributes(linkElement)
Example #16
0
 def linkbaseDiscover(self, linkbaseElement, inInstance=False):
     for lbElement in linkbaseElement.childNodes:
         if lbElement.nodeType == 1:  #element
             lbLn = lbElement.localName
             lbNs = lbElement.namespaceURI
             if lbNs == XbrlConst.link:
                 if lbLn == "roleRef" or lbLn == "arcroleRef":
                     href = self.discoverHref(lbElement)
                     if href is None:
                         self.modelXbrl.error(
                             "Linkbase in {0} {1} href attribute missing or malformed"
                             .format(os.path.basename(self.uri),
                                     lbLn), "err", "xbrl:hrefMissing")
                     else:
                         self.hrefObjects.append(href)
                     continue
             if lbElement.getAttributeNS(XbrlConst.xlink,
                                         "type") == "extended":
                 self.schemalocateElementNamespace(lbElement)
                 arcrolesFound = set()
                 dimensionArcFound = False
                 formulaArcFound = False
                 euRenderingArcFound = False
                 linkQn = qname(lbElement)
                 linkrole = lbElement.getAttributeNS(
                     XbrlConst.xlink, "role")
                 modelLink = ModelObject.createLink(self, lbElement)
                 if inInstance:
                     #index footnote links even if no arc children
                     baseSetKeys = (("XBRL-footnotes", None, None, None),
                                    ("XBRL-footnotes", linkrole, None,
                                     None))
                     for baseSetKey in baseSetKeys:
                         self.modelXbrl.baseSets[baseSetKey].append(
                             modelLink)
                 for linkElement in lbElement.childNodes:
                     if linkElement.nodeType == 1:  #element
                         self.schemalocateElementNamespace(linkElement)
                         xlinkType = linkElement.getAttributeNS(
                             XbrlConst.xlink, "type")
                         modelResource = None
                         if xlinkType == "locator":
                             nonDTS = linkElement.namespaceURI != XbrlConst.link or linkElement.localName != "loc"
                             # only link:loc elements are discovered or processed
                             href = self.discoverHref(linkElement,
                                                      nonDTS=nonDTS)
                             if href is None:
                                 self.modelXbrl.error(
                                     "Linkbase in {0} {1} href attribute missing or malformed"
                                     .format(os.path.basename(self.uri),
                                             lbLn), "err",
                                     "xbrl:hrefMissing")
                             else:
                                 modelResource = ModelObject.createLocator(
                                     self, linkElement, href)
                         elif xlinkType == "arc":
                             arcQn = qname(linkElement)
                             arcrole = linkElement.getAttributeNS(
                                 XbrlConst.xlink, "arcrole")
                             if arcrole not in arcrolesFound:
                                 if linkrole == "":
                                     linkrole = XbrlConst.defaultLinkRole
                                 #index by both arcrole and linkrole#arcrole and dimensionsions if applicable
                                 baseSetKeys = [(arcrole, linkrole, linkQn,
                                                 arcQn)]
                                 baseSetKeys.append(
                                     (arcrole, linkrole, None, None))
                                 baseSetKeys.append(
                                     (arcrole, None, None, None))
                                 if XbrlConst.isDimensionArcrole(
                                         arcrole) and not dimensionArcFound:
                                     baseSetKeys.append(("XBRL-dimensions",
                                                         None, None, None))
                                     baseSetKeys.append(
                                         ("XBRL-dimensions", linkrole, None,
                                          None))
                                     dimensionArcFound = True
                                 if XbrlConst.isFormulaArcrole(
                                         arcrole) and not formulaArcFound:
                                     baseSetKeys.append(("XBRL-formulae",
                                                         None, None, None))
                                     baseSetKeys.append(
                                         ("XBRL-formulae", linkrole, None,
                                          None))
                                     formulaArcFound = True
                                 if XbrlConst.isEuRenderingArcrole(
                                         arcrole
                                 ) and not euRenderingArcFound:
                                     baseSetKeys.append(
                                         ("EU-rendering", None, None, None))
                                     baseSetKeys.append(
                                         ("EU-rendering", linkrole, None,
                                          None))
                                     euRenderingArcFound = True
                                     self.modelXbrl.hasEuRendering = True
                                 for baseSetKey in baseSetKeys:
                                     self.modelXbrl.baseSets[
                                         baseSetKey].append(modelLink)
                                 arcrolesFound.add(arcrole)
                         elif xlinkType == "resource":
                             # create resource and make accessible by id for document
                             modelResource = ModelObject.createResource(
                                 self, linkElement)
                         if modelResource is not None:
                             if linkElement.hasAttribute("id"):
                                 self.idObjects[linkElement.getAttribute(
                                     "id")] = modelResource
                             modelLink.labeledResources[linkElement.getAttributeNS(XbrlConst.xlink, "label")] \
                                 .append(modelResource)
                         else:
                             XmlUtil.markIdAttributes(linkElement)
Example #17
0
    def __init__(self, modelXbrl, arcrole, linkrole=None, linkqname=None, arcqname=None, includeProhibits=False):
        self.isChanged = False
        self.modelXbrl = modelXbrl
        self.arcrole = arcrole
        self.linkrole = linkrole
        self.linkqname = linkqname
        self.arcqname = arcqname

        baseSetKey = (arcrole, linkrole, linkqname, arcqname) 
        relationshipSetKey = (arcrole, linkrole, linkqname, arcqname, includeProhibits) 
            
        # base sets does not care about the #includeProhibits
        if baseSetKey in self.modelXbrl.baseSets:
            modelLinks = self.modelXbrl.baseSets[baseSetKey]
        else:
            modelLinks = []
        
        # gather arcs
        relationships = {}
        isDimensionRel =  self.arcrole == "XBRL-dimensions" # all dimensional relationship arcroles
        isFormulaRel =  self.arcrole == "XBRL-formulae" # all formula relationship arcroles
        isEuRenderingRel = self.arcrole == "EU-rendering"
        isFootnoteRel =  self.arcrole == "XBRL-footnotes" # all footnote relationship arcroles
        
        for modelLink in modelLinks:
            arcs = []
            linkEltQname = modelLink.qname
            for linkChild in modelLink.element.childNodes:
                if linkChild.nodeType == 1 and \
                   linkChild.getAttributeNS(XbrlConst.xlink, "type") == "arc" and \
                   linkChild.hasAttributeNS(XbrlConst.xlink, "arcrole"):
                    linkChildArcrole = linkChild.getAttributeNS(XbrlConst.xlink, "arcrole")
                    linkChildQname = linkChild
                    if isFootnoteRel:
                        arcs.append(linkChild)
                    elif isDimensionRel: 
                        if XbrlConst.isDimensionArcrole(linkChildArcrole):
                            arcs.append(linkChild)
                    elif isFormulaRel:
                        if XbrlConst.isFormulaArcrole(linkChildArcrole):
                            arcs.append(linkChild)
                    elif isEuRenderingRel:
                        if XbrlConst.isEuRenderingArcrole(linkChildArcrole):
                            arcs.append(linkChild)
                    elif arcrole == linkChildArcrole and \
                         (arcqname is None or arcqname == linkChildQname) and \
                         (linkqname is None or linkqname == linkEltQname):
                        arcs.append(linkChild)
                        
            # build network
            for arcElement in arcs:
                arcrole = arcElement.getAttributeNS(XbrlConst.xlink, "arcrole")
                fromLabel = arcElement.getAttributeNS(XbrlConst.xlink, "from")
                toLabel = arcElement.getAttributeNS(XbrlConst.xlink, "to")
                for fromResource in modelLink.labeledResources[fromLabel]:
                    for toResource in modelLink.labeledResources[toLabel]:
                        modelRel = ModelObject.createRelationship(modelLink.modelDocument, arcElement, fromResource.dereference(), toResource.dereference())
                        modelRelEquivalenceKey = modelRel.equivalenceKey    # this is a complex tuple to compute, get once for below
                        if modelRelEquivalenceKey not in relationships or \
                           modelRel.priorityOver(relationships[modelRelEquivalenceKey]):
                            relationships[modelRelEquivalenceKey] = modelRel

        #reduce effective arcs and order relationships...
        self.modelRelationships = []
        self.modelRelationshipsFrom = None
        self.modelRelationshipsTo = None
        self.modelConceptRoots = None
        self.modellinkRoleUris = None
        orderRels = defaultdict(list)
        for modelRel in relationships.values():
            if includeProhibits or not modelRel.isProhibited:
                orderRels[modelRel.order].append(modelRel)
        for order in sorted(orderRels.keys()):
            for modelRel in orderRels[order]:
                self.modelRelationships.append(modelRel)
        modelXbrl.relationshipSets[relationshipSetKey] = self
Example #18
0
 def unitDiscover(self, unitElement):
     id = unitElement.getAttribute("id")
     self.modelXbrl.units[id] = ModelObject.createUnit(self, unitElement)