File: SDMXDataStructure-methods.R

package info (click to toggle)
r-cran-rsdmx 1%3A0.6-5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,024 kB
  • sloc: sh: 14; makefile: 2
file content (147 lines) | stat: -rw-r--r-- 4,757 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#' @name SDMXDataStructure
#' @rdname SDMXDataStructure
#' @aliases SDMXDataStructure,SDMXDataStructure-method
#' 
#' @usage
#' SDMXDataStructure(xmlObj, namespaces)
#' 
#' @param xmlObj object of class "XMLInternalDocument derived from XML package
#' @param namespaces object of class "data.frame" given the list of namespace URIs
#' @return an object of class "SDMXDataStructure"
#' 
#' @seealso \link{readSDMX}
#' @export
#' 
SDMXDataStructure <- function(xmlObj, namespaces){
  
  sdmxVersion <- version.SDMXSchema(xmlDoc(xmlObj), namespaces)
  VERSION.21 <- sdmxVersion == "2.1"
  
  messageNs <- findNamespace(namespaces, "message")
  strNs <- findNamespace(namespaces, "structure")
  
  #attributes
  #=========
  id = xmlGetAttr(xmlObj, "id")
  if(is.null(id)) id <- as.character(NA)
  
  agencyId = xmlGetAttr(xmlObj, "agencyID")
  if(is.null(agencyId)) agencyId <- as.character(NA)
  
  version = xmlGetAttr(xmlObj, "version")
  if(is.null(version)) version <- as.character(NA)
  
  uri = xmlGetAttr(xmlObj, "uri")
  if(is.null(uri)) uri <- as.character(NA)
  
  urn = xmlGetAttr(xmlObj, "urn")
  if(is.null(urn)) urn <- as.character(NA)
  
  isExternalReference = xmlGetAttr(xmlObj, "isExternalReference")
  if(is.null(isExternalReference)){
    isExternalReference <- NA
  }else{
    isExternalReference <- as.logical(isExternalReference)
  }
  
  isFinal = xmlGetAttr(xmlObj, "isFinal")
  if(is.null(isFinal)){
    isFinal <- NA
  }else{
    isFinal <- as.logical(isFinal)
  }
  
  validFrom = xmlGetAttr(xmlObj,"validFrom")
  if(is.null(validFrom)) validFrom <- as.character(NA)
  
  validTo = xmlGetAttr(xmlObj, "validTo")
  if(is.null(validTo)) validTo <- as.character(NA)
  
  #elements
  #========
  #name (multi-languages)
  dsNamesXML <- NULL
  if(VERSION.21){
    comNs <- findNamespace(namespaces, "common")
    dsNamesXML <- getNodeSet(xmlDoc(xmlObj),
                             "//str:DataStructure/com:Name",
                             namespaces = c(str = as.character(strNs),
                                            com = as.character(comNs)))
  }else{
    dsNamesXML <- getNodeSet(xmlDoc(xmlObj),
                             "//str:KeyFamily/str:Name",
                             namespaces = c(str = as.character(strNs)))
  }
  dsNames <- list()
  if(length(dsNamesXML) > 0){
    dsNames <- new.env()
    sapply(dsNamesXML,
           function(x){
             lang <- xmlGetAttr(x,"xml:lang")
             if(is.null(lang)) lang <- xmlGetAttr(x,"lang")
             if(is.null(lang)) lang <- "default"
             dsNames[[lang]] <- xmlValue(x)
           })
    dsNames <- as.list(dsNames)
  }
  
  #description (multi-languages)
  dsDesXML <- NULL
  if(VERSION.21){
    comNs <- findNamespace(namespaces, "common")
    dsDesXML <- getNodeSet(xmlDoc(xmlObj),
                           "//str:DataStructure/com:Description",
                           namespaces = c(str = as.character(strNs),
                                          com = as.character(comNs)))
  }else{
    dsDesXML <- getNodeSet(xmlDoc(xmlObj),
                           "//str:KeyFamily/str:Description",
                           namespaces = c(str = as.character(strNs)))
  }
  
  dsDescriptions <- list()
  if(length(dsDesXML) > 0){
    dsDescriptions <- new.env()
    sapply(dsDesXML,
           function(x){
             lang <- xmlGetAttr(x,"xml:lang")
             if(is.null(lang)) lang <- xmlGetAttr(x,"lang")
             if(is.null(lang)) lang <- "default"
             dsDescriptions[[lang]] <- xmlValue(x)
           })
    dsDescriptions <- as.list(dsDescriptions)
  }
  
  #Components
  compXML <- NULL
  if(VERSION.21){
    compXML <- getNodeSet(xmlDoc(xmlObj),
                          "//str:DataStructureComponents",
                          namespaces = c(str = as.character(strNs)))
  }else{
    compXML <- getNodeSet(xmlDoc(xmlObj),
                          "//str:Components",
                          namespaces = c(str = as.character(strNs)))
  }
  components <- NULL
  if(length(compXML) > 0) components <- SDMXComponents(xmlObj, namespaces)
  
  #instantiate the object
  obj<- new("SDMXDataStructure",    
            #attributes
            id = id,
            agencyID = agencyId,
            version = version,
            uri = uri,
            urn = urn,
            isExternalReference = isExternalReference,
            isFinal = isFinal,
            validFrom = validFrom,
            validTo = validTo,
            
            #elements,
            Name = dsNames,
            Description = dsDescriptions,
            Components = components
  )
}