From 995c10cf2bf071dde2c321ed2905a1d932efee5b Mon Sep 17 00:00:00 2001 From: wenzi1 Date: Wed, 6 Jun 2018 14:58:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4gcharset=E4=B8=AD=E7=9A=84get?= =?UTF-8?q?charset=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/encoding/gcharset/gcharset.go | 9 --------- g/encoding/gxml/gxml.go | 16 +++++++++------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/g/encoding/gcharset/gcharset.go b/g/encoding/gcharset/gcharset.go index 4a2490759..b2829e25b 100644 --- a/g/encoding/gcharset/gcharset.go +++ b/g/encoding/gcharset/gcharset.go @@ -18,15 +18,6 @@ import ( ) - -//获取支持的字符集 -func GetCharset(name string) (*mahonia.Charset, error) { - s := mahonia.GetCharset(name) - if s == nil { - return nil, errors.New(fmt.Sprintf("not support charset: %s", name)) - } - return s, nil -} //2个字符集之间的转换 func Convert(dstCharset string, srcCharset string, src string) (dst string, err error) { s := mahonia.GetCharset(srcCharset) diff --git a/g/encoding/gxml/gxml.go b/g/encoding/gxml/gxml.go index 04ef17bb7..b155a1045 100644 --- a/g/encoding/gxml/gxml.go +++ b/g/encoding/gxml/gxml.go @@ -11,8 +11,10 @@ import ( "github.com/clbanning/mxj" "encoding/xml" "io" - "gitee.com/johng/gf/g/encoding/gcharset" "gitee.com/johng/gf/g/util/gregx" + "github.com/axgle/mahonia" + "errors" + "fmt" ) // 将XML内容解析为map变量 @@ -47,9 +49,9 @@ func ToJson(xmlbyte []byte) ([]byte, error) { func Prepare(xmlbyte []byte) error { patten := "<\\?xml\\s+version\\s*=.*?\\s+encoding\\s*=\\s*[\\'|\"](.*?)[\\'|\"]\\s*\\?\\s*>" charsetReader := func(charset string, input io.Reader) (io.Reader, error) { - reader, err := gcharset.GetCharset(charset) - if err != nil { - return nil, err + reader := mahonia.GetCharset(charset) + if reader == nil { + return nil, errors.New(fmt.Sprintf("not support charset:%s", charset)) } return reader.NewDecoder().NewReader(input), nil } @@ -59,9 +61,9 @@ func Prepare(xmlbyte []byte) error { return err } - charset, err := gcharset.GetCharset(matchStr[1]) - if err != nil { - return err + charset := mahonia.GetCharset(matchStr[1]) + if charset == nil { + return errors.New(fmt.Sprintf("not support charset:%s", matchStr[1])) } if charset.Name != "UTF-8" {