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