コード例 #1
0
def SerializeRDF(context,
                 resultset,
                 type='rdfxml',
                 nsMapString=None,
                 fixUp=None,
                 fixUpPredicate=None):
    '''Returns a nodeset containing a RDF serialization of the
  RxPathDOM nodes contained in resultset parameter.
  
  nsMapString is a namespace dictionary encoded as a string in the form of "prefix^uri^prefix^uri..."
  
  '''
    stmts = []
    uri2prefixMap = None
    if nsMapString:
        import itertools
        nslist = StringValue(nsMapString).split('^')
        uri2prefixMap = dict(
            itertools.izip(itertools.islice(nslist, 1, None, 2),
                           itertools.islice(nslist, 0, None, 2)))
    if resultset:
        if uri2prefixMap is None:
            uri2prefixMap = resultset[0].rootNode.nsRevMap
        if resultset[0].nodeName == '#document':
            resultset = resultset[0].childNodes

        for n in resultset:
            nl = [n]
            if RxPath.isResource(context, nl):
                preds = n.childNodes
            elif RxPath.isPredicate(context, nl):
                preds = nl
            else:
                preds = []  #error?

            for p in preds:
                stmts.extend(p.getModelStatements())
                if (RxPath.isResource(context, p.childNodes)
                        and p.firstChild.isCompound()):
                    #object is a list so add all the list items too
                    stmts.extend(p.firstChild.getModelStatements())

    return RxPath.serializeRDF(stmts, type, uri2prefixMap, fixUp,
                               fixUpPredicate)
コード例 #2
0
ファイル: rxml.py プロジェクト: onecommons/rhizome
def getRXAsZMLFromNode(resourceNodes,
                       nsMap=None,
                       includeRoot=False,
                       INDENT='    ',
                       NL='\n',
                       INITINDENT='',
                       rescomment='',
                       fixUp=None,
                       fixUpPredicate=None):
    '''given a nodeset of RxPathDom nodes, return RxML serialization in ZML markup format'''
    def getResourceNameFromURI(resNode):
        namespaceURI = resNode.getAttributeNS(RDF_MS_BASE, 'about')
        assert namespaceURI
        prefixURI, rest = RxPath.splitUri(namespaceURI)
        #print >>sys.stderr, 'spl %s %s %s' % (namespaceURI, prefixURI, rest)
        #print revNsMap
        if not rest:
            printResourceElem = True
        elif revNsMap.has_key(prefixURI):
            printResourceElem = False
        #elif resNode.ownerDocument.nsRevMap.has_key(prefixURI):
        #    prefix = resNode.ownerDocument.nsRevMap[prefixURI]
        #    nsMap[prefix] = prefixURI
        #    revNsMap[prefixURI] = prefix
        #    printResourceElem = False
        else:
            printResourceElem = True

        if not printResourceElem:
            prefix = revNsMap[prefixURI]
            if prefix:
                retVal = prefix + ':' + rest
            else:
                retVal = rest
            if fixUp:
                retVal = fixUp % utils.kw2dict(
                    uri=namespaceURI,
                    encodeduri=urllib.quote(namespaceURI),
                    res=retVal)
        else:
            if fixUp:
                namespaceURI = fixUp % utils.kw2dict(
                    uri=namespaceURI,
                    encodeduri=urllib.quote(namespaceURI),
                    res=namespaceURI)
            #retVal = rxPrefix + 'resource id="' + namespaceURI + '"'
            retVal = '{' + namespaceURI + '}'
        return retVal

    def outputPredicate(predNode, indent):
        if revNsMap.has_key(predNode.namespaceURI):
            prefix = revNsMap[predNode.namespaceURI]
        else:
            prefix = predNode.prefix
            nsMap[prefix] = predNode.namespaceURI
            revNsMap[predNode.namespaceURI] = prefix

        if predNode.namespaceURI == RDF_MS_BASE and predNode.localName == 'type':
            predicateString = rxPrefix + 'a'  #use rx:a instead rdf:type
        elif prefix:
            predicateString = prefix + ':' + predNode.localName
        else:
            predicateString = predNode.localName

        if fixUpPredicate:
            predURI = RxPath.getURIFromElementName(predNode)
            eu = urllib.quote(predURI)
            predicateString = fixUpPredicate % utils.kw2dict(
                uri=predURI, encodeduri=eu, predicate=predicateString)

        line = indent + predicateString

        id = predNode.getAttributeNS(RDF_MS_BASE, 'ID')
        if id:
            line += ' ' + rxPrefix + RX_STMTID_ATTRIB + '="' + id + '"'

        assert len(predNode.childNodes) == 1
        if predNode.childNodes[0].nodeType == predNode.TEXT_NODE:
            lang = predNode.getAttributeNS(XML_NAMESPACE, 'lang')
            datatype = predNode.getAttributeNS(RDF_MS_BASE, 'datatype')
            if lang or datatype:
                line += ': '
                line += NL
                indent += INDENT
                line += indent + rxPrefix + RX_LITERALELEM
                if lang:
                    line += ' xml:lang="' + lang + '"'
                if datatype:
                    #note we don't bother to check if its xml literal and parse and output as zml
                    line += ' rdf:datatype="' + datatype + '"'
            line += ': '
            line += doQuote(predNode.childNodes[0].nodeValue) + NL
        else:
            object = predNode.childNodes[0]
            isList = object.isCompound()
            if isList:
                line += ' ' + rxPrefix + 'list="' + object.getAttributeNS(
                    RDF_MS_BASE, 'about') + '"'
                isList = isList[len(RDF_MS_BASE):]
                if isList != 'List':
                    assert isList in ['Alt', 'Seq',
                                      'Bag'], 'isList should not be ' + isList
                    line += ' ' + rxPrefix + 'listType="rdf:' + isList + '"'

            line += ': '
            line += NL
            indent += INDENT

            if isList:  #is the object a list resource?
                for li in [
                        p.childNodes[0] for p in object.childNodes
                        if RxPath.getURIFromElementName(p) in
                    [RDF_MS_BASE + 'first', RDF_SCHEMA_BASE + 'member']
                ]:

                    if li.nodeType == li.TEXT_NODE:
                        lang = li.parentNode.getAttributeNS(
                            XML_NAMESPACE, 'lang')
                        datatype = li.parentNode.getAttributeNS(
                            RDF_MS_BASE, 'datatype')
                        if lang:
                            attr = ' xml:lang="' + lang + '"'
                        elif datatype:
                            #note we don't bother to check if its xml literal and parse and output as zml
                            attr = ' rdf:datatype="' + datatype + '"'
                        else:
                            attr = ''
                        line += indent + rxPrefix + RX_LITERALELEM + attr + ':' + doQuote(
                            li.nodeValue) + NL
                    elif li.nodeType == li.ELEMENT_NODE:
                        line += indent + getResourceNameFromURI(li) + NL
            else:
                line += indent + getResourceNameFromURI(object) + NL

        return line

    if fixUp:  #if fixUp we assume we're outputing xml/html not zml
        doQuote = lambda s: '`' + utils.htmlQuote(s)
    else:
        doQuote = quoteString
    if nsMap is None:
        nsMap = {'bnode': BNODE_BASE, RX_META_DEFAULT: RX_NS}
    revNsMap = dict([(x[1], x[0]) for x in nsMap.items()
                     if x[0] and ':' not in x[0]
                     and x[0] not in [RX_META_DEFAULT, RX_BASE_DEFAULT]])
    if nsMap.has_key(RX_META_DEFAULT):
        revNsMap[nsMap[RX_META_DEFAULT]] = ''

    rxPrefix = revNsMap.get(RX_NS, 'rx')
    if rxPrefix: rxPrefix += ':'

    indent = INITINDENT
    line = prefixes = root = ''

    if includeRoot:
        indent += INDENT
        root += '#?zml0.7 markup' + NL
        root += INITINDENT + rxPrefix + 'rx:' + NL
    elif not fixUp:  #if fixUp we assume we're outputing xml/html not zml
        root += '#?zml0.7 markup' + NL

    if not isinstance(resourceNodes, (list, tuple)):
        resourceNodes = [resourceNodes]

    for resourceNode in resourceNodes:
        if RxPath.isPredicate(None, [resourceNode]):
            predicateNodes = [resourceNode]
            resourceNode = resourceNode.parentNode
        else:
            predicateNodes = resourceNode.childNodes

        line += indent + getResourceNameFromURI(resourceNode) + ':'
        if rescomment:
            line += ' #' + rescomment
        line += NL
        for p in predicateNodes:
            line += outputPredicate(p, indent + INDENT)
        line += NL

    if nsMap:
        prefixes = indent + rxPrefix + 'prefixes:' + NL
        for prefix, ns in nsMap.items():
            prefixes += indent + INDENT + prefix + ': `' + ns + NL
        prefixes += NL

    return root + prefixes + line