Class: Yast::XMLClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) main



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File '../../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

- (void) xmlCreateDoc(doc, docSettings)

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



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

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

- (Object) XMLError

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



181
182
183
# File '../../src/modules/XML.rb', line 181

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

- (Object) XMLToYCPFile(xmlFile)

Read XML file into YCP

Parameters:

  • xmlFile (String)

    XML file name to read

Returns:

  • Map with YCP data



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File '../../src/modules/XML.rb', line 139

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

- (Object) XMLToYCPString(xmlString)

Read XML string into YCP

Parameters:

  • XML

    string to read

Returns:

  • Map with YCP data



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File '../../src/modules/XML.rb', line 162

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

- (Boolean) YCPToXMLFile(docType, contents, outputPath)

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



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

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

- (Object) YCPToXMLString(docType, contents)

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



122
123
124
125
126
127
128
129
130
131
132
133
134
# File '../../src/modules/XML.rb', line 122

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)
  if Ops.is_string?(ret)
    return Convert.to_string(ret)
  else
    return ""
  end
end