Class: Yast::XMLClass

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

Instance Method Summary collapse

Instance Method Details

#mainObject


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

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, doc_settings) ⇒ 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


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

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

  if Ops.get_string(doc_settings, "nameSpace", "") != ""
    Ops.set(
      current_settings,
      "nameSpace",
      Ops.get_string(doc_settings, "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


173
174
175
# File 'library/xml/src/modules/XML.rb', line 173

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

#XMLToYCPFile(xml_file) ⇒ Object

Read XML file into YCP

Parameters:

  • xml_file (String)

    XML file name to read

Returns:

  • Map with YCP data


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

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

#XMLToYCPString(xml_string) ⇒ Object

Read XML string into YCP

Parameters:

  • xml_string

    string to read

Returns:

  • Map with YCP data


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

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

#YCPToXMLFile(doc_type, contents, output_path) ⇒ Boolean

YCPToXMLFile() Write YCP data into formated XML file

Parameters:

  • doc_type (Symbol)

    Document type identifier

  • contents (Hash)

    a map with YCP data

  • output_path (String)

    the path of the XML file

Returns:

  • (Boolean)

    true on sucess


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

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

#YCPToXMLString(doc_type, contents) ⇒ String

Write YCP data into formated XML string

Parameters:

  • doc_type (Symbol)

    Document type identifier

  • contents (Hash)

    a map with YCP data

Returns:

  • (String)

    String with XML data


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

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

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

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