MCPcopy Create free account
hub / github.com/crewjam/saml / decodeEtreeElement

Function decodeEtreeElement

etree_xml.go:133–211  ·  view source on GitHub ↗

decodeEtreeElement builds an etree element from start and the tokens up to its end. xml.Decoder replaces prefixes with namespace URIs, so each prefix is recovered from the declarations in scope (scope maps prefix to URI; "" is the default namespace). A namespace declared outside the element, such a

(d *xml.Decoder, start xml.StartElement, scope map[string]string)

Source from the content-addressed store, hash-verified

131// the prefix "ds" for XML Signature, or a generated one otherwise, and a
132// declaration on the element.
133func decodeEtreeElement(d *xml.Decoder, start xml.StartElement, scope map[string]string) (*etree.Element, error) {
134 inScope := make(map[string]string, len(scope)+2)
135 for prefix, uri := range scope {
136 inScope[prefix] = uri
137 }
138 for _, attr := range start.Attr {
139 switch {
140 case attr.Name.Space == "xmlns":
141 inScope[attr.Name.Local] = attr.Value
142 case attr.Name.Space == "" && attr.Name.Local == "xmlns":
143 inScope[""] = attr.Value
144 }
145 }
146
147 var extraDecls []xml.Attr
148 prefixFor := func(uri string, allowDefault bool) string {
149 if uri == "" {
150 return ""
151 }
152 if uri == xmlNamespaceURI {
153 return "xml"
154 }
155 if allowDefault {
156 if defaultURI, ok := inScope[""]; ok && defaultURI == uri {
157 return ""
158 }
159 }
160 for prefix, scopeURI := range inScope {
161 if prefix != "" && scopeURI == uri {
162 return prefix
163 }
164 }
165 prefix := ""
166 if _, taken := inScope["ds"]; uri == dsig.Namespace && !taken {
167 prefix = "ds"
168 }
169 for i := 0; prefix == ""; i++ {
170 if _, taken := inScope[fmt.Sprintf("ns%d", i)]; !taken {
171 prefix = fmt.Sprintf("ns%d", i)
172 }
173 }
174 inScope[prefix] = uri
175 extraDecls = append(extraDecls, namespaceDeclaration(prefix, uri))
176 return prefix
177 }
178
179 el := etree.NewElement(qualifiedName(prefixFor(start.Name.Space, true), start.Name.Local))
180 for _, attr := range start.Attr {
181 prefix := attr.Name.Space
182 if prefix != "xmlns" {
183 prefix = prefixFor(attr.Name.Space, false)
184 }
185 el.CreateAttr(qualifiedName(prefix, attr.Name.Local), attr.Value)
186 }
187 for _, decl := range extraDecls {
188 el.CreateAttr(decl.Name.Local, decl.Value)
189 }
190

Callers 2

UnmarshalXMLMethod · 0.85
UnmarshalXMLMethod · 0.85

Calls 2

namespaceDeclarationFunction · 0.85
qualifiedNameFunction · 0.85

Tested by

no test coverage detected