<commit_before>// Package soap wraps github.com/VictorLowther/simplexml/dom to provide
// convienent methods for dealing with SOAP messages as a SOAP client.
package soap

import (
	"encoding/xml"
	"errors"
	"github.com/VictorLowther/simplexml/dom"
	"github.com/VictorLowther/simplexml/search"
	"io"
)

const ContentType = "application/soap+xml; charset=utf-8"

var (
	envName    xml.Name = xml.Name{Space: NS_ENVELOPE, Local: "Envelope"}
	headerName xml.Name = xml.Name{Space: NS_ENVELOPE, Local: "Header"}
	bodyName   xml.Name = xml.Name{Space: NS_ENVELOPE, Local: "Body"}
	faultName  xml.Name = xml.Name{Space: NS_ENVELOPE, Local: "Fault"}
)

// Message represents a SOAP message.
type Message struct {
	*dom.Document
	header, body *dom.Element
}

// NewMessage creates the skeleton of a new SOAP message.
func NewMessage() *Message {
	res := &Message{
		Document: dom.CreateDocument(),
		body:     dom.CreateElement(bodyName),
		header:   dom.CreateElement(headerName),
	}
	res.SetRoot(dom.CreateElement(envName))
	res.Root().AddChild(res.header)
	res.Root().AddChild(res.body)
	return res
}

// Headers returns the children of the Header element.
func (m *Message) Headers() []*dom.Element {
	return m.header.Children()
}

// Body returns the children of the Body element.
func (m *Message) Body() []*dom.Element {
	return m.body.Children()
}

func set(loc *dom.Element, elems ...*dom.Element) {
	for _, elem := range elems {
		e := search.First(search.Tag(elem.Name.Local, elem.Name.Space),
			loc.Children())
		if e == nil {
			loc.AddChild(elem)
		} else {
			e.Replace(elem)
		}
	}
}

// SetHeader adds (or updates) any number of elements to the SOAP
// header.
//
// Elements that do not exist will be appended to the rest of the headers,
// and headers that already exist will be replaced in place.
// The SOAP message is returned.
func (m *Message) SetHeader(elems ...*dom.Element) *Message {
	set(m.header, elems...)
	return m
}

// RemoveHeader removes an element from the SOAP Header.
// If the element was not a header, nil is returned, otherwise the element
// is returned.
func (m *Message) RemoveHeader(elem *dom.Element) *dom.Element {
	return m.header.RemoveChild(elem)<commit_msg>Add a couple of helpers for searching in a SOAP document.<commit_after>// Package soap wraps github.com/VictorLowther/simplexml/dom to provide
// convienent methods for dealing with SOAP messages as a SOAP client.
package soap

import (
	"encoding/xml"
	"errors"
	"github.com/VictorLowther/simplexml/dom"
	"github.com/VictorLowther/simplexml/search"
	"io"
)

const ContentType = "application/soap+xml; charset=utf-8"

var (
	envName    xml.Name = xml.Name{Space: NS_ENVELOPE, Local: "Envelope"}
	headerName xml.Name = xml.Name{Space: NS_ENVELOPE, Local: "Header"}
	bodyName   xml.Name = xml.Name{Space: NS_ENVELOPE, Local: "Body"}
	faultName  xml.Name = xml.Name{Space: NS_ENVELOPE, Local: "Fault"}
)

// Message represents a SOAP message.
type Message struct {
	*dom.Document
	header, body *dom.Element
}

// NewMessage creates the skeleton of a new SOAP message.
func NewMessage() *Message {
	res := &Message{
		Document: dom.CreateDocument(),
		body:     dom.CreateElement(bodyName),
		header:   dom.CreateElement(headerName),
	}
	res.SetRoot(dom.CreateElement(envName))
	res.Root().AddChild(res.header)
	res.Root().AddChild(res.body)
	return res
}

// Headers returns the children of the Header element.
func (m *Message) Headers() []*dom.Element {
	return m.header.Children()
}

func (m *Message) AllHeaderElements() []*dom.Element {
	return m.header.Descendants()
}

// Body returns the children of the Body element.
func (m *Message) Body() []*dom.Element {
	return m.body.Children()
}

func (m *Message) AllBodyElements() []*dom.Element {
	return m.body.Descendants()
}

func set(loc *dom.Element, elems ...*dom.Element) {
	for _, elem := range elems {
		e := search.First(search.Tag(elem.Name.Local, elem.Name.Space),
			loc.Children())
		if e == nil {
			loc.AddChild(elem)
		} else {
			e.Replace(elem)
		}
	}
}

// SetHeader adds (or updates) any number of elements to the SOAP
// header.
//
// Elements that do not exist will be appended to the rest of the headers,
// and headers that already exist will be replaced in place.
// The SOAP message is returned.
func (m *Message) SetHeader(elems ...*dom.Element) *Message {
	set(m.header, elems...)
	return m
}

// RemoveHeader removes an element from the SOAP Header.
// If the element was not a header, nil is returned, otherwise the element
// is returned.
func (m *Message) RemoveHeader(elem *dom.Element) *dom.Element {
	return m.header.RemoveChild(elem)
{'fraction_non_alphanumeric': 0.1300302899177845, 'fraction_numerical': 0.00043271311120726956, 'mean_word_length': 3.949678800856531, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1109660', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1519, 'n_words': 469}