xml.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package xml for config provider.
  15. //
  16. // depend on github.com/beego/x2j.
  17. //
  18. // go install github.com/beego/x2j.
  19. //
  20. // Usage:
  21. // import(
  22. // _ "github.com/astaxie/beego/config/xml"
  23. // "github.com/astaxie/beego/config"
  24. // )
  25. //
  26. // cnf, err := config.NewConfig("xml", "config.xml")
  27. //
  28. //More docs http://beego.me/docs/module/config.md
  29. package xml
  30. import (
  31. "encoding/xml"
  32. "errors"
  33. "fmt"
  34. "io/ioutil"
  35. "os"
  36. "strconv"
  37. "strings"
  38. "sync"
  39. "github.com/astaxie/beego/config"
  40. "github.com/beego/x2j"
  41. )
  42. // Config is a xml config parser and implements Config interface.
  43. // xml configurations should be included in <config></config> tag.
  44. // only support key/value pair as <key>value</key> as each item.
  45. type Config struct{}
  46. // Parse returns a ConfigContainer with parsed xml config map.
  47. func (xc *Config) Parse(filename string) (config.Configer, error) {
  48. context, err := ioutil.ReadFile(filename)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return xc.ParseData(context)
  53. }
  54. // ParseData xml data
  55. func (xc *Config) ParseData(data []byte) (config.Configer, error) {
  56. x := &ConfigContainer{data: make(map[string]interface{})}
  57. d, err := x2j.DocToMap(string(data))
  58. if err != nil {
  59. return nil, err
  60. }
  61. x.data = config.ExpandValueEnvForMap(d["config"].(map[string]interface{}))
  62. return x, nil
  63. }
  64. // ConfigContainer A Config represents the xml configuration.
  65. type ConfigContainer struct {
  66. data map[string]interface{}
  67. sync.Mutex
  68. }
  69. // Bool returns the boolean value for a given key.
  70. func (c *ConfigContainer) Bool(key string) (bool, error) {
  71. if v := c.data[key]; v != nil {
  72. return config.ParseBool(v)
  73. }
  74. return false, fmt.Errorf("not exist key: %q", key)
  75. }
  76. // DefaultBool return the bool value if has no error
  77. // otherwise return the defaultval
  78. func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
  79. v, err := c.Bool(key)
  80. if err != nil {
  81. return defaultval
  82. }
  83. return v
  84. }
  85. // Int returns the integer value for a given key.
  86. func (c *ConfigContainer) Int(key string) (int, error) {
  87. return strconv.Atoi(c.data[key].(string))
  88. }
  89. // DefaultInt returns the integer value for a given key.
  90. // if err != nil return defaltval
  91. func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
  92. v, err := c.Int(key)
  93. if err != nil {
  94. return defaultval
  95. }
  96. return v
  97. }
  98. // Int64 returns the int64 value for a given key.
  99. func (c *ConfigContainer) Int64(key string) (int64, error) {
  100. return strconv.ParseInt(c.data[key].(string), 10, 64)
  101. }
  102. // DefaultInt64 returns the int64 value for a given key.
  103. // if err != nil return defaltval
  104. func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
  105. v, err := c.Int64(key)
  106. if err != nil {
  107. return defaultval
  108. }
  109. return v
  110. }
  111. // Float returns the float value for a given key.
  112. func (c *ConfigContainer) Float(key string) (float64, error) {
  113. return strconv.ParseFloat(c.data[key].(string), 64)
  114. }
  115. // DefaultFloat returns the float64 value for a given key.
  116. // if err != nil return defaltval
  117. func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
  118. v, err := c.Float(key)
  119. if err != nil {
  120. return defaultval
  121. }
  122. return v
  123. }
  124. // String returns the string value for a given key.
  125. func (c *ConfigContainer) String(key string) string {
  126. if v, ok := c.data[key].(string); ok {
  127. return v
  128. }
  129. return ""
  130. }
  131. // DefaultString returns the string value for a given key.
  132. // if err != nil return defaltval
  133. func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
  134. v := c.String(key)
  135. if v == "" {
  136. return defaultval
  137. }
  138. return v
  139. }
  140. // Strings returns the []string value for a given key.
  141. func (c *ConfigContainer) Strings(key string) []string {
  142. v := c.String(key)
  143. if v == "" {
  144. return nil
  145. }
  146. return strings.Split(v, ";")
  147. }
  148. // DefaultStrings returns the []string value for a given key.
  149. // if err != nil return defaltval
  150. func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
  151. v := c.Strings(key)
  152. if v == nil {
  153. return defaultval
  154. }
  155. return v
  156. }
  157. // GetSection returns map for the given section
  158. func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
  159. if v, ok := c.data[section].(map[string]interface{}); ok {
  160. mapstr := make(map[string]string)
  161. for k, val := range v {
  162. mapstr[k] = config.ToString(val)
  163. }
  164. return mapstr, nil
  165. }
  166. return nil, fmt.Errorf("section '%s' not found", section)
  167. }
  168. // SaveConfigFile save the config into file
  169. func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
  170. // Write configuration file by filename.
  171. f, err := os.Create(filename)
  172. if err != nil {
  173. return err
  174. }
  175. defer f.Close()
  176. b, err := xml.MarshalIndent(c.data, " ", " ")
  177. if err != nil {
  178. return err
  179. }
  180. _, err = f.Write(b)
  181. return err
  182. }
  183. // Set writes a new value for key.
  184. func (c *ConfigContainer) Set(key, val string) error {
  185. c.Lock()
  186. defer c.Unlock()
  187. c.data[key] = val
  188. return nil
  189. }
  190. // DIY returns the raw value by a given key.
  191. func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
  192. if v, ok := c.data[key]; ok {
  193. return v, nil
  194. }
  195. return nil, errors.New("not exist key")
  196. }
  197. func init() {
  198. config.Register("xml", &Config{})
  199. }