File: encoding.R

package info (click to toggle)
r-cran-xml 3.98-1.5-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 9,464 kB
  • ctags: 636
  • sloc: xml: 79,579; ansic: 6,518; asm: 644; sh: 16; makefile: 1
file content (30 lines) | stat: -rw-r--r-- 1,008 bytes parent folder | download | duplicates (7)
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
CE_NATIVE = 0L
CE_UTF8 = 1L
CE_LATIN1 = 2L

  # Map an encoding or document's encoding to the corresponding R internal enum value
setGeneric("getEncodingREnum", 
              function(doc, ...)
	        standardGeneric("getEncodingREnum"))

setMethod("getEncodingREnum", "XMLInternalDocument",
           function(doc, ...) 
              getEncodingREnum( getEncoding(doc) ))

setMethod("getEncodingREnum", "XMLInternalElementNode", # was XMLInternalElement, but no such class?
           function(doc, ...) 
              getEncodingREnum( as(doc, "XMLInternalDocument") ))

setMethod("getEncodingREnum", "character",
           function(doc, ...) {
             if(length(doc) == 0 || is.na(doc))
               return(CE_NATIVE)
             
             str = tolower(doc)
             if(any(str == c("utf8", "utf-8")))
                 CE_UTF8
             else if(any(str == c("latin1", "iso-8859-1")))
                 CE_LATIN1
             else
                 CE_NATIVE # or NA?
           })