atom/category.go

50 lines
1.2 KiB
Go

package atom
import (
"errors"
"fmt"
"html"
)
type Category struct {
*CommonAttributes
Content string `xml:",chardata"` // undefinedContent in RFC4287
Term string `xml:"term,attr"`
Scheme IRI `xml:"scheme,attr,omitempty"`
Label string `xml:"label,attr,omitempty"`
}
// NewCategory creates a new Category. It returns a *Category and an error.
func NewCategory(term, content string) (*Category, error) {
return &Category{Term: term, Content: content}, nil
}
// SetLabel sets the label of the Category.
func (c *Category) SetLabel(label string) {
c.Label = html.UnescapeString(label)
}
// Check checks the Category for incompatibilities with RFC4287. It returns an
// error.
func (c *Category) Check() error {
if c.Term == "" {
return errors.New("term attribute of category empty")
}
if c.Scheme != "" {
if !isValidIRI(c.Scheme) {
return fmt.Errorf("scheme attribute %v of category not correctly formatted", c.Scheme)
}
}
if !isCorrectlyEscaped(c.Label) {
return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label)
}
if c.Content == "" {
return errors.New("content element of category empty")
}
return nil
}