LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package xml | 5 package xml |
6 | 6 |
| 7 const ( |
| 8 XML_NS = "http://www.w3.org/XML/1998/namespace" |
| 9 XMLNS_NS = "http://www.w3.org/2000/xmlns/" |
| 10 ) |
| 11 |
7 // Track namespace context. This includes the namespace of the current | 12 // Track namespace context. This includes the namespace of the current |
8 // element, and also the mapping between namespace prefixes and | 13 // element, and also the mapping between namespace prefixes and |
9 // namespace URIs. The marshaler needs ns -> pfx, and the unmarshaler | 14 // namespace URIs. The marshaler needs ns -> pfx, and the unmarshaler |
10 // needs the opposite. Each element's associated context inherits from | 15 // needs the opposite. Each element's associated context inherits from |
11 // its parent's context. | 16 // its parent's context. |
12 type Context struct { | 17 type context struct { |
13 » // Xmlns is the namespace (URI) of the current element. | 18 » // xmlns is the namespace (URI) of the current element. |
14 » Xmlns string | 19 » xmlns string |
15 » // Map is the mapping between prefix and namespace of the | 20 » // pfxmap is the mapping between prefix and namespace of the |
16 // current element. Ancestor elements have their own | 21 // current element. Ancestor elements have their own |
17 // associated maps whose entries are not visible here. If this | 22 // associated maps whose entries are not visible here. If this |
18 » // object is associated with an Encoder, its Map is namespace | 23 » // object is associated with an Encoder, its pfxmap is |
19 » // -> prefix; if a Decoder, then prefix -> namespace. Use this | 24 » // namespace -> prefix; if a Decoder, then prefix -> |
20 » // object to add new mappings, and the Get method to read the | 25 » // namespace. Use this object to add new mappings, and the Get |
21 » // current mapping. | 26 » // method to read the current mapping. |
22 » Map map[string]string | 27 » pfxmap map[string]string |
23 » parent *Context | 28 » parent *context |
24 } | 29 } |
25 | 30 |
26 // Get reads the mapping for this element, including the mappings for | 31 // Get reads the mapping for this element, including the mappings for |
27 // all its parent elements. The second return value will be true if | 32 // all its parent elements. The second return value will be true if |
28 // the mapping was found. | 33 // the mapping was found. |
29 func (n *Context) Get(k string) (string, bool) { | 34 func (n *context) Get(k string) (string, bool) { |
30 » if v, ok := n.Map[k] ; ok { | 35 » if v, ok := n.pfxmap[k]; ok { |
31 return v, ok | 36 return v, ok |
32 } | 37 } |
33 | 38 |
34 if n.parent != nil && n.parent != n { | 39 if n.parent != nil && n.parent != n { |
35 return n.parent.Get(k) | 40 return n.parent.Get(k) |
36 } | 41 } |
37 | 42 |
38 return "", false | 43 return "", false |
39 } | 44 } |
40 | 45 |
41 func (n *Context) child() *Context { | 46 func (n *context) child() *context { |
42 » child := &Context{} | 47 » child := &context{} |
43 » child.Map = make(map[string]string) | 48 » child.pfxmap = make(map[string]string) |
44 child.parent = n | 49 child.parent = n |
45 return child | 50 return child |
46 } | 51 } |
47 | 52 |
48 func rootNs2Pfx() *Context { | 53 func rootNs2Pfx() *context { |
49 » n := &Context{} | 54 » n := &context{} |
50 » n.Map = make(map[string]string) | 55 » n.pfxmap = make(map[string]string) |
51 n.parent = n | 56 n.parent = n |
52 » n.Map["http://www.w3.org/XML/1998/namespace"] = "xml" | 57 » n.pfxmap[XML_NS] = "xml" |
53 » n.Map["http://www.w3.org/2000/xmlns/"] = "xmlns" | 58 » n.pfxmap[XMLNS_NS] = "xmlns" |
54 return n | 59 return n |
55 } | 60 } |
56 | |
57 func rootPfx2Ns() *Context { | |
58 n := &Context{} | |
59 n.Map = make(map[string]string) | |
60 n.parent = n | |
61 n.Map["xml"] = "http://www.w3.org/XML/1998/namespace" | |
62 n.Map["xmlns"] = "http://www.w3.org/2000/xmlns/" | |
63 return n | |
64 } | |
LEFT | RIGHT |