| 1 |
package xmltree |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/xml" |
| 5 |
"io" |
| 6 |
|
| 7 |
"golang.org/x/net/html/charset" |
| 8 |
|
| 9 |
"github.com/ChrisTrenkamp/goxpath/tree" |
| 10 |
"github.com/ChrisTrenkamp/goxpath/tree/xmltree/xmlbuilder" |
| 11 |
"github.com/ChrisTrenkamp/goxpath/tree/xmltree/xmlele" |
| 12 |
) |
| 13 |
|
| 14 |
//ParseOptions is a set of methods and function pointers that alter |
| 15 |
//the way the XML decoder works and the Node types that are created. |
| 16 |
//Options that are not set will default to what is set in internal/defoverride.go |
| 17 |
type ParseOptions struct { |
| 18 |
Strict bool |
| 19 |
XMLRoot func() xmlbuilder.XMLBuilder |
| 20 |
} |
| 21 |
|
| 22 |
//DirectiveParser is an optional interface extended from XMLBuilder that handles |
| 23 |
//XML directives. |
| 24 |
type DirectiveParser interface { |
| 25 |
xmlbuilder.XMLBuilder |
| 26 |
Directive(xml.Directive, *xml.Decoder) |
| 27 |
} |
| 28 |
|
| 29 |
//ParseSettings is a function for setting the ParseOptions you want when |
| 30 |
//parsing an XML tree. |
| 31 |
type ParseSettings func(s *ParseOptions) |
| 32 |
|
| 33 |
//MustParseXML is like ParseXML, but panics instead of returning an error. |
| 34 |
func MustParseXML(r io.Reader, op ...ParseSettings) tree.Node { |
| 35 |
ret, err := ParseXML(r, op...) |
| 36 |
|
| 37 |
if err != nil { |
| 38 |
panic(err) |
| 39 |
} |
| 40 |
|
| 41 |
return ret |
| 42 |
} |
| 43 |
|
| 44 |
//ParseXML creates an XMLTree structure from an io.Reader. |
| 45 |
func ParseXML(r io.Reader, op ...ParseSettings) (tree.Node, error) { |
| 46 |
ov := ParseOptions{ |
| 47 |
Strict: true, |
| 48 |
XMLRoot: xmlele.Root, |
| 49 |
} |
| 50 |
for _, i := range op { |
| 51 |
i(&ov) |
| 52 |
} |
| 53 |
|
| 54 |
dec := xml.NewDecoder(r) |
| 55 |
dec.CharsetReader = charset.NewReaderLabel |
| 56 |
dec.Strict = ov.Strict |
| 57 |
|
| 58 |
ordrPos := 1 |
| 59 |
xmlTree := ov.XMLRoot() |
| 60 |
|
| 61 |
t, err := dec.Token() |
| 62 |
|
| 63 |
if err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
|
| 67 |
if head, ok := t.(xml.ProcInst); ok && head.Target == "xml" { |
| 68 |
t, err = dec.Token() |
| 69 |
} |
| 70 |
|
| 71 |
opts := xmlbuilder.BuilderOpts{ |
| 72 |
Dec: dec, |
| 73 |
} |
| 74 |
|
| 75 |
for err == nil { |
| 76 |
switch xt := t.(type) { |
| 77 |
case xml.StartElement: |
| 78 |
setEle(&opts, xmlTree, xt, &ordrPos) |
| 79 |
xmlTree = xmlTree.CreateNode(&opts) |
| 80 |
case xml.CharData: |
| 81 |
setNode(&opts, xmlTree, xt, tree.NtChd, &ordrPos) |
| 82 |
xmlTree = xmlTree.CreateNode(&opts) |
| 83 |
case xml.Comment: |
| 84 |
setNode(&opts, xmlTree, xt, tree.NtComm, &ordrPos) |
| 85 |
xmlTree = xmlTree.CreateNode(&opts) |
| 86 |
case xml.ProcInst: |
| 87 |
setNode(&opts, xmlTree, xt, tree.NtPi, &ordrPos) |
| 88 |
xmlTree = xmlTree.CreateNode(&opts) |
| 89 |
case xml.EndElement: |
| 90 |
xmlTree = xmlTree.EndElem() |
| 91 |
case xml.Directive: |
| 92 |
if dp, ok := xmlTree.(DirectiveParser); ok { |
| 93 |
dp.Directive(xt.Copy(), dec) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
t, err = dec.Token() |
| 98 |
} |
| 99 |
|
| 100 |
if err == io.EOF { |
| 101 |
err = nil |
| 102 |
} |
| 103 |
|
| 104 |
return xmlTree, err |
| 105 |
} |
| 106 |
|
| 107 |
func setEle(opts *xmlbuilder.BuilderOpts, xmlTree xmlbuilder.XMLBuilder, ele xml.StartElement, ordrPos *int) { |
| 108 |
opts.NodePos = *ordrPos |
| 109 |
opts.Tok = ele |
| 110 |
opts.Attrs = opts.Attrs[0:0:cap(opts.Attrs)] |
| 111 |
opts.NS = make(map[xml.Name]string) |
| 112 |
opts.NodeType = tree.NtElem |
| 113 |
|
| 114 |
for i := range ele.Attr { |
| 115 |
attr := ele.Attr[i].Name |
| 116 |
val := ele.Attr[i].Value |
| 117 |
|
| 118 |
if (attr.Local == "xmlns" && attr.Space == "") || attr.Space == "xmlns" { |
| 119 |
opts.NS[attr] = val |
| 120 |
} else { |
| 121 |
opts.Attrs = append(opts.Attrs, &ele.Attr[i]) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if nstree, ok := xmlTree.(tree.NSElem); ok { |
| 126 |
ns := make(map[xml.Name]string) |
| 127 |
|
| 128 |
for _, i := range tree.BuildNS(nstree) { |
| 129 |
ns[i.Name] = i.Value |
| 130 |
} |
| 131 |
|
| 132 |
for k, v := range opts.NS { |
| 133 |
ns[k] = v |
| 134 |
} |
| 135 |
|
| 136 |
if ns[xml.Name{Local: "xmlns"}] == "" { |
| 137 |
delete(ns, xml.Name{Local: "xmlns"}) |
| 138 |
} |
| 139 |
|
| 140 |
for k, v := range ns { |
| 141 |
opts.NS[k] = v |
| 142 |
} |
| 143 |
|
| 144 |
if xmlTree.GetNodeType() == tree.NtRoot { |
| 145 |
opts.NS[xml.Name{Space: "xmlns", Local: "xml"}] = tree.XMLSpace |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
opts.AttrStartPos = len(opts.NS) + len(opts.Attrs) + *ordrPos |
| 150 |
*ordrPos = opts.AttrStartPos + 1 |
| 151 |
} |
| 152 |
|
| 153 |
func setNode(opts *xmlbuilder.BuilderOpts, xmlTree xmlbuilder.XMLBuilder, tok xml.Token, nt tree.NodeType, ordrPos *int) { |
| 154 |
opts.Tok = xml.CopyToken(tok) |
| 155 |
opts.NodeType = nt |
| 156 |
opts.NodePos = *ordrPos |
| 157 |
*ordrPos++ |
| 158 |
} |