Commit 14488ca6 authored by Paul Warren's avatar Paul Warren
Browse files

Add AddPrefixes method on Encoder.

parent 131a43fd
......@@ -8,6 +8,7 @@ import (
"bufio"
"bytes"
"encoding"
"errors"
"fmt"
"io"
"reflect"
......@@ -317,6 +318,36 @@ type printer struct {
xmlDeclaration bool
}
// Addes the prefix -> namespace mappings in the supplied map.
// Required prefixes will be generated automatically, but supplying your own
// mapping allows you to:
//
// * Provide preferred prefixes for namespaces
// * Declare prefixes higher in the document tree, reducing repeated declarations.
// * Namespace declarations needed for element or attribute values (such as
// QNames or XPath expressions), although Unmarshallers for such types should
// use GetPrefix in order to avoid possible prefix clashes.
//
// Returns an error if a prefix already exists for the supplied namespace, or
// if the a prefix is already declared. These are not technically XML errors,
// and this restriction may be removed in the future.
//
// Note that all elements currently use the default namespace, so this mapping
// will not result in elements using these prefixes.
//
func (e *Encoder) AddPrefixes(m map[string]string) error {
for prefix, ns := range m {
if _, prefixExists := e.p.attrNS[prefix]; prefixExists {
return errors.New(fmt.Sprintf("Prefix '%s' is already declared", prefix))
}
if _, namespaceExists := e.p.attrPrefix[ns]; namespaceExists {
return errors.New(fmt.Sprintf("Namespace '%s' is already declared", ns))
}
e.p.addPrefix(prefix, ns)
}
return nil
}
// Returns a prefix for the specified namespace, defining a new one if
// necessary. Any new prefixes will be serialised on the next start element
// token to be encoded.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment