2024-10-16 21:28:04 +02:00
|
|
|
package atom
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-10-16 16:51:39 +02:00
|
|
|
"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
|
2024-10-17 18:11:06 +02:00
|
|
|
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.
|
2024-10-17 18:11:06 +02:00
|
|
|
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-17 18:39:57 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2024-10-16 16:51:39 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-10-16 16:48:44 +02:00
|
|
|
if c.Scheme != "" {
|
2024-10-16 17:33:25 +02:00
|
|
|
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)
|
2024-10-16 16:48:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 16:51:39 +02:00
|
|
|
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)
|
2024-10-16 16:51:39 +02:00
|
|
|
}
|
|
|
|
|
2024-10-17 18:39:57 +02:00
|
|
|
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
|
|
|
|
}
|