Class: Yast::XMLClass

Inherits:
Module
  • Object
show all
Defined in:
library/xml/src/modules/XML.rb

Instance Method Summary collapse

Instance Method Details

#mainObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'library/xml/src/modules/XML.rb', line 34

def main
  # Sections in XML file that should be treated as CDATA when saving
  @cdataSections = []

  # How to call a list entry in the XML output
  @listEntries = {}

  # The system ID, or the DTD URI
  @systemID = ""

  # root element of the XML file
  @rootElement = ""

  # Global Namespace xmlns=...
  @nameSpace = "http://www.suse.com/1.0/yast2ns"

  # Type name space xmlns:config for YCP data (http://www.suse.com/1.0/configns)
  @typeNamespace = "http://www.suse.com/1.0/configns"

  @docs = {}
end

#xmlCreateDoc(doc, docSettings) ⇒ void

This method returns an undefined value.

define a new doc type with custom settings, if not defined, global settings will be used.

Parameters:

  • symbol

    Document type identifier

  • map

    Document type Settings



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
# File 'library/xml/src/modules/XML.rb', line 61

def xmlCreateDoc(doc, docSettings)
  docSettings = deep_copy(docSettings)
  current_settings = {
    "cdataSections" => Ops.get_list(
      docSettings,
      "cdataSections",
      @cdataSections
    ),
    "systemID"      => Ops.get_string(docSettings, "systemID", @systemID),
    "rootElement"   => Ops.get_string(
      docSettings,
      "rootElement",
      @rootElement
    ),
    "listEntries"   => Ops.get_map(docSettings, "listEntries", @listEntries)
  }
  if Ops.get_string(docSettings, "typeNamespace", "") != ""
    Ops.set(
      current_settings,
      "typeNamespace",
      Ops.get_string(docSettings, "typeNamespace", "")
    )
  end

  if Ops.get_string(docSettings, "nameSpace", "") != ""
    Ops.set(
      current_settings,
      "nameSpace",
      Ops.get_string(docSettings, "nameSpace", "")
    )
  end
  Ops.set(@docs, doc, current_settings)
  nil
end

#XMLErrorObject

The error string from the xml parser. It should be used when the agent did not return content. A reset happens before a new XML parsing starts.

Returns:

  • parser error



175
176
177
# File 'library/xml/src/modules/XML.rb', line 175

def XMLError
  Convert.to_string(SCR.Read(path(".xml.error_message")))
end

#XMLToYCPFile(xmlFile) ⇒ Object

Read XML file into YCP

Parameters:

  • xmlFile (String)

    XML file name to read

Returns:

  • Map with YCP data



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'library/xml/src/modules/XML.rb', line 133

def XMLToYCPFile(xmlFile)
  if Ops.greater_than(SCR.Read(path(".target.size"), xmlFile), 0)
    Builtins.y2milestone("Reading %1", xmlFile)
    out = Convert.convert(
      SCR.Read(path(".xml"), xmlFile),
      from: "any",
      to:   "map <string, any>"
    )
    Builtins.y2debug("XML Agent output: %1", out)
    return deep_copy(out)
  else
    Builtins.y2warning(
      "XML file %1 (%2) not found",
      xmlFile,
      SCR.Read(path(".target.size"), xmlFile)
    )
    return {}
  end
end

#XMLToYCPString(xmlString) ⇒ Object

Read XML string into YCP

Parameters:

  • XML

    string to read

Returns:

  • Map with YCP data



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'library/xml/src/modules/XML.rb', line 156

def XMLToYCPString(xmlString)
  if Ops.greater_than(Builtins.size(xmlString), 0)
    out = Convert.convert(
      SCR.Read(path(".xml.string"), xmlString),
      from: "any",
      to:   "map <string, any>"
    )
    Builtins.y2debug("XML Agent output: %1", out)
    return deep_copy(out)
  else
    Builtins.y2warning("can't convert empty XML string")
    return {}
  end
end

#YCPToXMLFile(docType, contents, outputPath) ⇒ Boolean

YCPToXMLFile() Write YCP data into formated XML file

Parameters:

  • symbol

    Document type identifier

  • contents (Hash)

    a map with YCP data

  • outputPath (String)

    the path of the XML file

Returns:

  • (Boolean)

    true on sucess



102
103
104
105
106
107
108
109
110
111
112
113
# File 'library/xml/src/modules/XML.rb', line 102

def YCPToXMLFile(docType, contents, outputPath)
  contents = deep_copy(contents)
  if !Builtins.haskey(@docs, docType)
    Builtins.y2error("doc type %1 undecalred...", docType)
    return false
  end
  docSettings = Ops.get_map(@docs, docType, {})
  Ops.set(docSettings, "fileName", outputPath)
  Builtins.y2debug("Write(.xml, %1, %2)", docSettings, contents)
  ret = Convert.to_boolean(SCR.Execute(path(".xml"), docSettings, contents))
  ret
end

#YCPToXMLString(docType, contents) ⇒ Object

Write YCP data into formated XML string

@param symbol Document type identifier
@param [Hash] contents  a map with YCP data
@return [String] String with XML data


119
120
121
122
123
124
125
126
127
128
# File 'library/xml/src/modules/XML.rb', line 119

def YCPToXMLString(docType, contents)
  contents = deep_copy(contents)
  return nil if !Builtins.haskey(@docs, docType)

  docSettings = Ops.get_map(@docs, docType, {})
  Ops.set(docSettings, "fileName", "dummy")
  ret = SCR.Execute(path(".xml.string"), docSettings, contents)

  Ops.is_string?(ret) ? Convert.to_string(ret) : ""
end