File: DTDRef.R

package info (click to toggle)
r-cran-xml 3.99-0.18-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,688 kB
  • sloc: ansic: 6,656; xml: 2,890; asm: 486; sh: 12; makefile: 2
file content (78 lines) | stat: -rw-r--r-- 2,462 bytes parent folder | download | duplicates (11)
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
# These are classes and facilities for referring to a DTD for the
# DOCTYPE field of an XML document


# The 4 elements are + or -//creator//name of what is being referenced//language (decribed by ISO639)
# See XML Elements of Style by Simon St. Laurent.

validatePublicIdentifier =
function(object)
{
    els = strsplit(object, "//")[[1]]
    if(length(els) != 4)
      return("a PUBLIC identifier must have 4 parts, separated by //")
    if(! (els[1] %in%  c("+", "-")))
      return("first element of PUBLIC identifier must be + or -")

    TRUE
}  

setClass("DTDPublicIdentifier",
         contains = "character",
         validity = validatePublicIdentifier)

 # name is the node name for the top-level node.
 # system and public identify the DTD.
setClass("Doctype", representation(name = "character",
                                   system = "character",
                                   public = "character"),
                    validity = function(object) {
                        if(length(nchar(object@system)) > 0 && length(object@public) > 0)
                           return("only one of system and public can be specified")

                        if(length(object@public) > 0 && length(object@public) != 2)
                          return("the public part of the Doctype must have exactly 2 elements.")

                        if(length(object@public) > 0) {
                           tmp = validatePublicIdentifier(object@public[1])
                           if(!is.logical(tmp))
                             return(tmp)
                         }
                        
                        TRUE
                      })

Doctype =
function(system = character(), public = character(), name = "")
{
  if(length(public) == 1 && length(system) > 0) {
      public = c(public, system)
      system = character()
  }
  new("Doctype", name = name, system = system, public = public)
}

ddQuote =
function(x)
{
  if(length(x) == 0)
    return(character())

  paste('"', x, '"', sep = "")
}  

setAs("Doctype", "character",
       function(from) {

         extra = character()
         if(sum(nchar(from@public), nchar(from@system))) {
            if(length(from@system))
              extra = c(extra, "SYSTEM", ddQuote(from@system))
            if(length(from@public))
              extra = c(extra, "PUBLIC", ddQuote(from@public))
          }
         paste("<!DOCTYPE", from@name, paste(extra, collapse = " "), ">")
       })