source: TI02-CSML/trunk/csml/csmllibs/csmlxlink.py @ 3286

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/csmllibs/csmlxlink.py@3286
Revision 3286, 2.4 KB checked in by domlowe, 13 years ago (diff)

overhaul of scanning command args

Line 
1
2'''csmlxlink - contains functions used for processin xlinks in csml documents.
3created 23 01 2007 by Dominic Lowe, CCLRC, BADC
4'''
5
6import sys
7import csml
8
9def createXlinks(dataset):
10    '''returns a CSMLStorageDescriptor with any fileLists reduced to xlinks to other fileLists where possible.
11    e.g if 3 features share the same fileList, the fileList will be explicit in the first feature, and referenced only by xlink from the second 2 features.
12    This function compares the fileLists of all AggregatedArrays  in the CSMLStorageDescriptor and looks for matching fileLists'''
13    ds=dataset
14    if not hasattr(ds, 'storageDescriptor'):
15        return ds
16    else:
17        #xlinkDictionary = {axislabel_axisspanned :(values, gmlid), .... }
18        #process:
19        #go through each ordinate in each domain for each feature.
20        #if ordinate (exact match with labels, span + value) is not in dictionary add it
21        #and create a new gml id.
22        #else if it is in the dictionary, get the gml id and replace the ordinate values with an xlink.
23        xlinkDictionary = {}
24        for descriptor in ds.storageDescriptor.descriptors:
25            if type(descriptor)==csml.parser.AggregatedArray:
26                for comp in descriptor.components:
27                    fID=comp.fileList.fileNames.CONTENT
28                    if fID in xlinkDictionary:
29                        #replace content with xlink
30                        gmlid=xlinkDictionary[fID][1]
31                        setattr(comp, 'fileListXLINK', csml.parser.csString())
32                        comp.fileListXLINK.href='#%s'%gmlid
33                        del comp.fileList
34                    else:
35                        #add to dictionary & set gml:id attribute
36                        newid=csml.csmllibs.csmlextra.getRandomID()
37                        xlinkDictionary[fID]=(fID, newid)
38                        comp.fileList.id=newid
39
40    return ds
41   
42   
43class XlinkResolver(object):
44    def __init__(self, ds):
45            ''' iterate the ds and store list of objects'''
46            items=ds.iterate()
47            self.itemdict={}
48            for item in items:
49                if hasattr(item, 'id'):
50                    self.itemdict[item.id]=item
51           
52           
53    def resolveXlink(self, xlinkid):
54        try:
55            return self.itemdict[xlinkid]
56        except KeyError:
57            print 'keyerror'
58            return None
59     
Note: See TracBrowser for help on using the repository browser.