xmlstream: mellium.im/xmlstream Index | Examples | Files

package xmlstream

import "mellium.im/xmlstream"

Package xmlstream provides an API for streaming, transforming, and otherwise manipulating XML data.

BE ADVISED: The API is unstable and subject to change.

Index

Examples

Package Files

copy.go doc.go fmt.go func.go limit.go multi.go pipe.go reader.go skip.go tee.go transformer.go whitespace.go wrap.go

Variables

var ErrClosedPipe = errors.New("xmlstream: read/write on closed pipe")

ErrClosedPipe is the error used for read or write operations on a closed pipe.

func Copy Uses

func Copy(dst TokenWriter, src xml.TokenReader) (n int, err error)

Copy consumes a xml.TokenReader and writes its tokens to a TokenWriter until either io.EOF is reached on src or an error occurs. It returns the number of tokens copied and the first error encountered while copying, if any. If an error is returned by the reader or writer, copy returns it immediately. Since Copy is defined as consuming the stream until the end, io.EOF is not returned.

If src implements the WriterTo interface, the copy is implemented by calling src.WriteTo(dst). Otherwise, if dst implements the ReaderFrom interface, the copy is implemented by calling dst.ReadFrom(src).

func Fmt Uses

func Fmt(d xml.TokenReader, opts ...FmtOption) xml.TokenReader

Fmt returns a transformer that indents the given XML stream. The default indentation style is to remove non-significant whitespace, start elements on a new line and indent two spaces per level.

Code:

tokenizer := xmlstream.Fmt(xml.NewDecoder(strings.NewReader(`
<quote>  <p>
             <!-- Chardata is not indented -->
  How now, my hearts! did you never see the picture
of 'we three'?</p>
</quote>`)), xmlstream.Indent("    "))

buf := new(bytes.Buffer)
e := xml.NewEncoder(buf)
for t, err := tokenizer.Token(); err == nil; t, err = tokenizer.Token() {
    e.EncodeToken(t)
}
e.Flush()
fmt.Println(buf.String())

Output:

<quote>
    <p>
        <!-- Chardata is not indented -->

  How now, my hearts! did you never see the picture
of &#39;we three&#39;?
    </p>
</quote>

func Inner Uses

func Inner(r xml.TokenReader) xml.TokenReader

Inner returns a new TokenReader that returns nil, io.EOF when it consumes the end element matching the most recent start element already consumed.

func InnerReader Uses

func InnerReader(r io.Reader) io.Reader

InnerReader is an io.Reader which attempts to decode an xml.StartElement from the stream on the first call to Read (returning an error if an invalid start token is found) and returns a new reader which only reads the inner XML without parsing it or checking its validity. After the inner XML is read, the end token is parsed and if it does not exist or does not match the original start token an error is returned.

Code:

r := xmlstream.InnerReader(strings.NewReader(`<stream:features>
<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'>
<required/>
</starttls>
</stream:features>`))
io.Copy(os.Stdout, r)

Output:

<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'>
<required/>
</starttls>

func LimitReader Uses

func LimitReader(r xml.TokenReader, n uint) xml.TokenReader

LimitReader returns a xml.TokenReader that reads from r but stops with EOF after n tokens (regardless of the validity of the XML at that point in the stream).

Code:

e := xml.NewEncoder(os.Stdout)
var r xml.TokenReader = xml.NewDecoder(strings.NewReader(`<one>One hen</one><two>Two ducks</two>`))

r = xmlstream.LimitReader(r, 3)

if _, err := xmlstream.Copy(e, r); err != nil {
    log.Fatal("Error in LimitReader example:", err)
}
if err := e.Flush(); err != nil {
    log.Fatal("Error flushing:", err)
}

Output:

<one>One hen</one>

func MultiReader Uses

func MultiReader(readers ...xml.TokenReader) xml.TokenReader

MultiReader returns an xml.TokenReader that's the logical concatenation of the provided input readers. They're read sequentially. Once all inputs have returned io.EOF, Token will return io.EOF. If any of the readers return a non-nil, non-EOF error, Token will return that error.

Code:

e := xml.NewEncoder(os.Stdout)
e.Indent("", "  ")

r1 := xml.NewDecoder(strings.NewReader(`<title>Dover Beach</title>`))
r2 := xml.NewDecoder(strings.NewReader(`<author>Matthew Arnold</author>`))
r3 := xml.NewDecoder(strings.NewReader(`<incipit>The sea is calm to-night.</incipit>`))

r := xmlstream.MultiReader(r1, r2, r3)

if _, err := xmlstream.Copy(e, r); err != nil {
    log.Fatal("Error in MultiReader example:", err)
}
if err := e.Flush(); err != nil {
    log.Fatal("Error flushing:", err)
}

Output:

<title>Dover Beach</title>
<author>Matthew Arnold</author>
<incipit>The sea is calm to-night.</incipit>

func Pipe Uses

func Pipe() (*PipeReader, *PipeWriter)

Pipe creates a synchronous in-memory pipe of tokens. It can be used to connect code expecting an TokenReader with code expecting an xmlstream.TokenWriter.

Reads and Writes on the pipe are matched one to one. That is, each Write to the PipeWriter blocks until it has satisfied a Read from the corresponding PipeReader.

It is safe to call Read and Write in parallel with each other or with Close. Parallel calls to Read and parallel calls to Write are also safe: the individual calls will be gated sequentially.

func Skip Uses

func Skip(r xml.TokenReader) error

Skip reads tokens until it has consumed the end element matching the most recent start element already consumed. It recurs if it encounters a start element, so it can be used to skip nested structures. It returns nil if it finds an end element at the same nesting level as the start element; otherwise it returns an error describing the problem. Skip does not verify that the start and end elements match.

Code:

e := xml.NewEncoder(os.Stdout)

r := xml.NewDecoder(strings.NewReader(`<par>I don't like to look out of the windows even—there are so many of those creeping women, and they creep so fast.</par><par>I wonder if they all come out of that wall paper, as I did?</par>`))

r.Token() // <par>

if err := xmlstream.Skip(r); err != nil && err != io.EOF {
    log.Fatal("Error in skipping par:", err)
}
if _, err := xmlstream.Copy(e, r); err != nil {
    log.Fatal("Error in Skip example:", err)
}
if err := e.Flush(); err != nil {
    log.Fatal("Error flushing:", err)
}

Output:

<par>I wonder if they all come out of that wall paper, as I did?</par>

func TeeReader Uses

func TeeReader(r xml.TokenReader, w TokenWriter) xml.TokenReader

TeeReader returns a Reader that writes to w what it reads from r. All reads from r performed through it are matched with corresponding writes to w. There is no internal buffering - the write must complete before the read completes. Any error encountered while writing is reported as a read error.

func Token Uses

func Token(t xml.Token) xml.TokenReader

Token returns a token reader that always returns the given token and io.EOF.

func Unwrap Uses

func Unwrap(r xml.TokenReader) (xml.TokenReader, xml.Token, error)

Unwrap reads the next token from the provided TokenReader and, if it is a start element, returns a new TokenReader that skips the corresponding end element. If the token is not a start element the original TokenReader is returned.

Code:

var r xml.TokenReader = xml.NewDecoder(strings.NewReader(`<message from="ismene@example.org/dIoK6Wi3"><body>No mind that ever lived stands firm in evil days, but goes astray.</body></message>`))
e := xml.NewEncoder(os.Stdout)

r, tok, err := xmlstream.Unwrap(r)
if err != nil {
    log.Fatal("Error unwraping:", err)
}

fmt.Printf("%s:\n", tok.(xml.StartElement).Name.Local)
if _, err := xmlstream.Copy(e, r); err != nil {
    log.Fatal("Error in unwrap example:", err)
}
if err := e.Flush(); err != nil {
    log.Fatal("Error flushing:", err)
}

Output:

message:
<body>No mind that ever lived stands firm in evil days, but goes astray.</body>

func Wrap Uses

func Wrap(r xml.TokenReader, start xml.StartElement) xml.TokenReader

Wrap wraps a token stream in a start element and its corresponding end element.

Code:

var r xml.TokenReader = xml.NewDecoder(strings.NewReader(`<body>No mind that ever lived stands firm in evil days, but goes astray.</body>`))
e := xml.NewEncoder(os.Stdout)
e.Indent("", "  ")

r = xmlstream.Wrap(r, xml.StartElement{
    Name: xml.Name{Local: "message"},
    Attr: []xml.Attr{
        {Name: xml.Name{Local: "from"}, Value: "ismene@example.org/Fo6Eeb2e"},
    },
})

if _, err := xmlstream.Copy(e, r); err != nil {
    log.Fatal("Error in wrap example:", err)
}
if err := e.Flush(); err != nil {
    log.Fatal("Error flushing:", err)
}

Output:

<message from="ismene@example.org/Fo6Eeb2e">
  <body>No mind that ever lived stands firm in evil days, but goes astray.</body>
</message>

type Flusher Uses

type Flusher interface {
    Flush() error
}

The Flusher interface is implemented by TokenWriters that can flush buffered data to an underlying receiver.

type FmtOption Uses

type FmtOption func(*fmter)

FmtOption is used to configure a formatters behavior.

func Indent Uses

func Indent(s string) FmtOption

Indent is inserted before XML elements zero or more times according to their nesting depth in the stream. The default indentation is " " (two ASCII spaces).

func Prefix Uses

func Prefix(s string) FmtOption

Prefix is inserted at the start of every XML element in the stream.

func Suffix Uses

func Suffix(s string) FmtOption

Suffix is inserted at the start of every XML element in the stream. If no option is specified the default suffix is '\n'.

type Marshaler Uses

type Marshaler interface {
    TokenReader() xml.TokenReader
}

Marshaler is the interface implemented by objects that can marshal themselves into valid XML elements.

type PipeReader Uses

type PipeReader struct {
    // contains filtered or unexported fields
}

A PipeReader is the read half of a token pipe.

func (*PipeReader) Close Uses

func (r *PipeReader) Close() error

Close closes the PipeReader; subsequent reads from the read half of the pipe will return no bytes and EOF.

func (*PipeReader) CloseWithError Uses

func (r *PipeReader) CloseWithError(err error)

CloseWithError closes the PipeReader; subsequent reads from the read half of the pipe will return no tokens and the error err, or EOF if err is nil.

func (*PipeReader) Token Uses

func (r *PipeReader) Token() (t xml.Token, err error)

Token implements the TokenReader interface. It reads a token from the pipe, blocking until a writer arrives or the write end is closed. If the write end is closed with an error, that error is returned as err; otherwise err is io.EOF.

type PipeWriter Uses

type PipeWriter struct {
    // contains filtered or unexported fields
}

A PipeWriter is the write half of a token pipe.

func (*PipeWriter) Close Uses

func (w *PipeWriter) Close() error

Close closes the PipeWriter; subsequent reads from the read half of the pipe will return no bytes and EOF.

func (*PipeWriter) CloseWithError Uses

func (w *PipeWriter) CloseWithError(err error)

CloseWithError closes the PipeWriter; subsequent reads from the read half of the pipe will return no tokens and the error err, or EOF if err is nil.

func (*PipeWriter) EncodeToken Uses

func (w *PipeWriter) EncodeToken(t xml.Token) error

EncodeToken implements the TokenWriter interface. It writes a token to the pipe, blocking until one or more readers have consumed all the data or the read end is closed. If the read end is closed with an error, that err is returned as err; otherwise err is ErrClosedPipe.

func (*PipeWriter) Flush Uses

func (w *PipeWriter) Flush() error

Flush is currently a noop and always returns nil.

type ReaderFrom Uses

type ReaderFrom interface {
    ReadXML(xml.TokenReader) (n int, err error)
}

ReaderFrom reads tokens from r until EOF or error. The return value n is the number of tokens read. Any error except io.EOF encountered during the read is also returned.

The Copy function uses ReaderFrom if available.

type ReaderFunc Uses

type ReaderFunc func() (xml.Token, error)

ReaderFunc type is an adapter to allow the use of ordinary functions as an TokenReader. If f is a function with the appropriate signature, ReaderFunc(f) is an TokenReader that calls f.

Code:

state := 0
start := xml.StartElement{Name: xml.Name{Local: "quote"}}
d := xmlstream.ReaderFunc(func() (xml.Token, error) {
    switch state {
    case 0:
        state++
        return start, nil
    case 1:
        state++
        return xml.CharData("the rain it raineth every day"), nil
    case 2:
        state++
        return start.End(), nil
    default:
        return nil, io.EOF
    }
})

e := xml.NewEncoder(os.Stdout)
if _, err := xmlstream.Copy(e, d); err != nil {
    log.Fatal("Error in func example:", err)
}
if err := e.Flush(); err != nil {
    log.Fatal("Error flushing:", err)
}

Output:

<quote>the rain it raineth every day</quote>

func (ReaderFunc) Token Uses

func (f ReaderFunc) Token() (xml.Token, error)

Token calls f.

type TokenReadCloser Uses

type TokenReadCloser interface {
    xml.TokenReader
    io.Closer
}

TokenReadCloser is the interface that groups the basic Token and Close methods.

type TokenReadWriteCloser Uses

type TokenReadWriteCloser interface {
    xml.TokenReader
    TokenWriter
    io.Closer
}

TokenReadWriteCloser is the interface that groups the basic Token, EncodeToken, Flush, and Close methods.

type TokenReadWriter Uses

type TokenReadWriter interface {
    xml.TokenReader
    TokenWriter
}

TokenReadWriter is the interface that groups the basic Token, EncodeToken, and Flush methods.

type TokenWriteCloser Uses

type TokenWriteCloser interface {
    TokenWriter
    io.Closer
}

TokenWriteCloser is the interface that groups the basic EncodeToken, Flush, and Close methods.

type TokenWriter Uses

type TokenWriter interface {
    EncodeToken(t xml.Token) error
}

TokenWriter is anything that can encode tokens to an XML stream, including an xml.Encoder.

func Discard Uses

func Discard() TokenWriter

Discard returns a TokenWriter on which all calls succeed without doing anything.

func MultiWriter Uses

func MultiWriter(writers ...TokenWriter) TokenWriter

MultiWriter creates a writer that duplicates its writes to all the provided writers, similar to the Unix tee(1) command. If any of the writers return an error, the MultiWriter immediately returns the error and stops writing.

type Transformer Uses

type Transformer func(src xml.TokenReader) xml.TokenReader

A Transformer returns a new TokenReader that returns transformed tokens read from src.

func Inspect Uses

func Inspect(f func(t xml.Token)) Transformer

Inspect performs an operation for each token in the stream without transforming the stream in any way.

func Map Uses

func Map(mapping func(t xml.Token) xml.Token) Transformer

Map returns a Transformer that maps the tokens in the input using the given mapping.

func Remove Uses

func Remove(f func(t xml.Token) bool) Transformer

Remove returns a Transformer that removes tokens for which f matches.

Code:

removequote := xmlstream.Remove(func(t xml.Token) bool {
    switch tok := t.(type) {
    case xml.StartElement:
        return tok.Name.Local == "quote"
    case xml.EndElement:
        return tok.Name.Local == "quote"
    }
    return false
})

tokenizer := removequote(xml.NewDecoder(strings.NewReader(`
<quote>
  <p>Foolery, sir, does walk about the orb, like the sun; it shines everywhere.</p>
</quote>`)))

buf := new(bytes.Buffer)
e := xml.NewEncoder(buf)
for t, err := tokenizer.Token(); err == nil; t, err = tokenizer.Token() {
    e.EncodeToken(t)
}
e.Flush()
fmt.Println(buf.String())

Output:

<p>Foolery, sir, does walk about the orb, like the sun; it shines everywhere.</p>

func RemoveAttr Uses

func RemoveAttr(f func(start xml.StartElement, attr xml.Attr) bool) Transformer

RemoveAttr returns a Transformer that removes attributes from xml.StartElement's if f matches.

func RemoveElement Uses

func RemoveElement(f func(start xml.StartElement) bool) Transformer

RemoveElement returns a Transformer that removes entire elements (and their children) if f matches the elements start token.

Code:

removeLangEn := xmlstream.RemoveElement(func(start xml.StartElement) bool {
    // TODO: Probably be more specific and actually check the name.
    if len(start.Attr) > 0 && start.Attr[0].Value == "en" {
        return true
    }
    return false
})

d := removeLangEn(xml.NewDecoder(strings.NewReader(`
<quote>
<p xml:lang="en">Thus the whirligig of time brings in his revenges.</p>
<p xml:lang="fr">et c’est ainsi que la roue du temps amène les occasions de revanche.</p>
</quote>
`)))

buf := new(bytes.Buffer)
e := xml.NewEncoder(buf)
for t, err := d.Token(); err == nil; t, err = d.Token() {
    e.EncodeToken(t)
}
e.Flush()
fmt.Println(buf.String())

Output:

<quote>

<p xml:lang="fr">et c’est ainsi que la roue du temps amène les occasions de revanche.</p>
</quote>

type WriterTo Uses

type WriterTo interface {
    WriteXML(TokenWriter) (n int, err error)
}

WriterTo writes tokens to w until there are no more tokens to write or when an error occurs. The return value n is the number of tokens written. Any error encountered during the write is also returned.

The Copy function uses WriterTo if available.

Bugs

Multiple uses of RemoveAttr will iterate over the attr list multiple times.

Package xmlstream imports 10 packages (graph) and is imported by 9 packages. Updated 2018-11-14. Refresh now. Tools for package owners.