atom/generator.go

55 lines
1.2 KiB
Go

package atom
import (
"encoding/xml"
"errors"
"fmt"
"html"
)
type Generator struct {
XMLName xml.Name `xml:"generator"`
*CommonAttributes
URI string `xml:"uri,attr,omitempty"` // IRI
Version string `xml:"version,attr,omitempty"`
Text string `xml:",chardata"`
}
// NewGenerator creates a new Generator. It returns a *Generator and an error.
func NewGenerator(text string) (*Generator, error) {
if text == "" {
return nil, errors.New("error creating new generator: text string empty")
}
return &Generator{Text: html.UnescapeString(text)}, nil
}
// SetURI sets the URI attribute of the Generator. It returns an error.
func (g *Generator) SetURI(uri string) error {
if !isValidIRI(uri) {
return fmt.Errorf("uri %v not correctly formatted", g)
}
return nil
}
// Check checks the Generator for incompatibilities with RFC4287. It returns an
// error.
func (g *Generator) Check() error {
if g.URI != "" {
if !isValidIRI(g.URI) {
return fmt.Errorf("uri attribute of generator %v not correctly formatted", g)
}
}
if g.Text == "" {
return fmt.Errorf("text element of generator %v empty", g)
}
if !isCorrectlyEscaped(g.Text) {
return fmt.Errorf("text element of generator %v not correctly escaped", g)
}
return nil
}