File: xincludes.R

package info (click to toggle)
r-cran-xml 3.99-0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,688 kB
  • sloc: ansic: 6,659; xml: 2,890; asm: 486; sh: 12; makefile: 2
file content (151 lines) | stat: -rw-r--r-- 3,221 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
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
148
149
150
151

setGeneric("findXIncludeStartNodes", 
function(doc, ...)  
{
  standardGeneric("findXIncludeStartNodes")
})

setMethod("findXIncludeStartNodes", "character", 
function(doc, ...)  
{
  findXIncludeStartNodes(xmlParse(doc), ...)
})

setMethod("findXIncludeStartNodes", "XMLInternalDocument", 
function(doc, ...)  
{
  findXIncludeStartNodes(xmlRoot(doc), ...)
})

setMethod("findXIncludeStartNodes", "XMLInternalElementNode", 
function(doc, ...)  
{
  nodes = .Call("R_findXIncludeStartNodes", xmlRoot(doc), PACKAGE = "XML")
  names(nodes) = sapply(nodes, xmlGetAttr, "href", NA)
  nodes
})



findXInclude =
function(x, asNode = FALSE, recursive = FALSE)
{
  while(!is.null(x)) {
    tmp = getSiblingXIncludeStart(x, TRUE)
    if(!is.null(tmp))
      return(fixFindXInclude(tmp, asNode, recursive))

     sib = x
     if(is(sib, "XMLXIncludeStartNode"))
        return(fixFindXInclude(sib, asNode, recursive)) # if(asNode) sib else xmlAttrs(sib))

     x = xmlParent(x)
  }

  fixFindXInclude(x, asNode, recursive)
}

bad.findXInclude = 
 # This version just looks in the left sibling, not all siblings to the left.
function(x, asNode = FALSE, recursive = FALSE)
{
  ans = NULL
  while(!is.null(x)) {
     prev = getSiblingXIncludeStart(x, FALSE)
     if(inherits(prev, "XMLXIncludeStartNode")) {
        ans = prev
        break
     }

     x = xmlParent(x)
  }

  fixFindXInclude(ans, asNode, recursive)
}

fixFindXInclude = 
function(ans, asNode = FALSE, recursive = FALSE)
{
  if(is.null(ans))
    return(NULL)

  if(recursive) {
    tmp = getXIncludePath(ans)
    if(FALSE && grepl(sprintf("^(%s|http:|ftp:)", .Platform$file.sep), tmp))
      tmp
    else
      sprintf("%s%s%s",
               paste(dirname(unique(tmp)), collapse = .Platform$file.sep),
               .Platform$file.sep,
               xmlAttrs(ans))
  } else
    if(asNode) ans else xmlAttrs(ans)["href"]
}

getXIncludePath =
function(node)
{
  x = xmlParent(node)
  ans = character()
  while(!is.null(x)) {
    ans = c(ans, findXInclude(x))
    prev = x
    x = xmlParent(x)
  }
  c(docName(prev), ans)
}

getSiblingXIncludeStart =
function(x, asNode = FALSE)
{
     sib = x
     while(!is.null(sib)) {
       if(inherits(sib, "XMLXIncludeEndNode"))
         return(NULL)
       
       if(inherits(sib, "XMLXIncludeStartNode"))
         return(if(asNode) sib else xmlAttrs(sib))
       
       sib <- getSibling(sib, FALSE)
     }

     NULL
}


getNodePosition =
function(x) {
   if(is.list(x))
     return(sapply(x, getNodePosition))
   
    tmp = getNodeLocation(x)
    sprintf("%s:%d", tmp$file[1], tmp$line)
}


getNodeLocation =
function(node, recursive = TRUE, fileOnly = FALSE)
{
   if(is.list(node))
     return(lapply(node, getNodeLocation, recursive, fileOnly))
            
   fil = findXInclude(node, recursive = recursive)
   if(is.null(fil))
     fil = docName(node)

   if(fileOnly)
      fil[1]
   else
      list(file = fil, line = getLineNumber(node))
}


getLineNumber =
function(node, ...)
{
  if(!is(node, "XMLInternalNode"))
      stop("This must be an C-level/native/internal XML node, i.e. of class 'XMLInternalNode'. Got ", paste(class(node), collapse = ", "))

  .Call("R_getLineNumber", node, PACKAGE = "XML")
}