atom/category.go

58 lines
1.3 KiB
Go
Raw Normal View History

2024-10-16 21:28:04 +02:00
package atom
2024-10-13 17:19:40 +02:00
import (
"errors"
"fmt"
"html"
2024-10-13 17:19:40 +02:00
)
type Category struct {
*CommonAttributes
2024-10-17 18:15:41 +02:00
Content string `xml:",chardata"` // undefinedContent in RFC4287
Term string `xml:"term,attr"`
Scheme IRI `xml:"scheme,attr,omitempty"`
Label string `xml:"label,attr,omitempty"`
2024-10-15 22:17:10 +02:00
}
2024-10-16 19:59:28 +02:00
// NewCategory creates a new Category. It returns a *Category and an error.
func NewCategory(term, content string) (*Category, error) {
2024-10-17 18:40:52 +02:00
if content != "" {
if !isValidXML(content) {
return nil, fmt.Errorf("%v not valid XML", content)
}
}
2024-10-15 22:17:10 +02:00
return &Category{Term: term, Content: content}, nil
2024-10-13 17:19:40 +02:00
}
2024-10-16 19:59:28 +02:00
// SetLabel sets the label of the Category.
func (c *Category) SetLabel(label string) {
c.Label = html.UnescapeString(label)
}
2024-10-16 19:59:28 +02:00
// Check checks the Category for incompatibilities with RFC4287. It returns an
// error.
2024-10-13 17:19:40 +02:00
func (c *Category) Check() error {
if c.Term == "" {
return errors.New("term attribute of category empty")
}
if c.Scheme != "" {
if !isValidIRI(c.Scheme) {
2024-10-16 16:54:26 +02:00
return fmt.Errorf("scheme attribute %v of category not correctly formatted", c.Scheme)
}
}
if !isCorrectlyEscaped(c.Label) {
2024-10-16 16:54:26 +02:00
return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label)
}
if c.Content != "" {
if !isValidXML(c.Content) {
return fmt.Errorf("content element %v of category not valid XML", c.Content)
}
2024-10-13 17:19:40 +02:00
}
return nil
}