| 1 |
// =========================================================================== |
| 2 |
// |
| 3 |
// PUBLIC DOMAIN NOTICE |
| 4 |
// National Center for Biotechnology Information (NCBI) |
| 5 |
// |
| 6 |
// This software/database is a "United States Government Work" under the |
| 7 |
// terms of the United States Copyright Act. It was written as part of |
| 8 |
// the author's official duties as a United States Government employee and |
| 9 |
// thus cannot be copyrighted. This software/database is freely available |
| 10 |
// to the public for use. The National Library of Medicine and the U.S. |
| 11 |
// Government do not place any restriction on its use or reproduction. |
| 12 |
// We would, however, appreciate having the NCBI and the author cited in |
| 13 |
// any work or product based on this material. |
| 14 |
// |
| 15 |
// Although all reasonable efforts have been taken to ensure the accuracy |
| 16 |
// and reliability of the software and data, the NLM and the U.S. |
| 17 |
// Government do not and cannot warrant the performance or results that |
| 18 |
// may be obtained by using this software or data. The NLM and the U.S. |
| 19 |
// Government disclaim all warranties, express or implied, including |
| 20 |
// warranties of performance, merchantability or fitness for any particular |
| 21 |
// purpose. |
| 22 |
// |
| 23 |
// =========================================================================== |
| 24 |
// |
| 25 |
// File Name: toml.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"bufio" |
| 35 |
"bytes" |
| 36 |
"github.com/komkom/toml" |
| 37 |
"io" |
| 38 |
"os" |
| 39 |
"runtime" |
| 40 |
"strings" |
| 41 |
) |
| 42 |
|
| 43 |
// TOMLConverter parses TOML files through JSON into XML object stream |
| 44 |
func TOMLConverter(inp io.Reader) <-chan string { |
| 45 |
|
| 46 |
if inp == nil { |
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
out := make(chan string, chanDepth) |
| 51 |
if out == nil { |
| 52 |
DisplayError("Unable to create TOML converter channel") |
| 53 |
os.Exit(1) |
| 54 |
} |
| 55 |
|
| 56 |
convertTOML := func(inp io.Reader, out chan<- string) { |
| 57 |
|
| 58 |
// close channel when all records have been sent |
| 59 |
defer close(out) |
| 60 |
|
| 61 |
var buffer strings.Builder |
| 62 |
|
| 63 |
scanr := bufio.NewScanner(inp) |
| 64 |
|
| 65 |
for scanr.Scan() { |
| 66 |
|
| 67 |
line := scanr.Text() |
| 68 |
buffer.WriteString(line + "\n") |
| 69 |
} |
| 70 |
|
| 71 |
txt := buffer.String() |
| 72 |
|
| 73 |
buffer.Reset() |
| 74 |
|
| 75 |
if txt == "" { |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
rdr := toml.New(bytes.NewBufferString(txt)) |
| 80 |
if rdr == nil { |
| 81 |
DisplayError("Unable to create TOML reader") |
| 82 |
os.Exit(1) |
| 83 |
} |
| 84 |
|
| 85 |
jcnv := JSONConverter(rdr, "", "ConfigFile", "element") |
| 86 |
|
| 87 |
if jcnv == nil { |
| 88 |
DisplayError("Unable to create TOML to JSON to XML converter") |
| 89 |
os.Exit(1) |
| 90 |
} |
| 91 |
|
| 92 |
for str := range jcnv { |
| 93 |
if str != "" { |
| 94 |
out <- str |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
runtime.Gosched() |
| 99 |
} |
| 100 |
|
| 101 |
go convertTOML(inp, out) |
| 102 |
|
| 103 |
return out |
| 104 |
} |
| 105 |
|
| 106 |
// TOMLtoXML converts TOML to an XML string |
| 107 |
func TOMLtoXML(tml string) string { |
| 108 |
|
| 109 |
return StringToXML(tml, TOMLConverter) |
| 110 |
} |