2024-10-16 21:28:04 +02:00
|
|
|
package atom
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
import (
|
2024-10-17 20:10:18 +02:00
|
|
|
"encoding/xml"
|
2024-10-19 12:28:09 +02:00
|
|
|
"errors"
|
2024-10-13 17:19:40 +02:00
|
|
|
"fmt"
|
2024-10-16 16:51:39 +02:00
|
|
|
"html"
|
2024-10-13 17:19:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Category struct {
|
2024-10-17 20:10:18 +02:00
|
|
|
XMLName xml.Name `xml:"category"`
|
2024-10-13 17:19:40 +02:00
|
|
|
*CommonAttributes
|
2024-10-17 18:57:43 +02:00
|
|
|
Term string `xml:"term,attr"`
|
2024-10-19 12:28:09 +02:00
|
|
|
Scheme string `xml:"scheme,attr,omitempty"` // IRI
|
2024-10-17 18:57:43 +02:00
|
|
|
Label string `xml:"label,attr,omitempty"`
|
2024-10-15 22:17:10 +02:00
|
|
|
}
|
|
|
|
|
2024-10-19 12:28:09 +02:00
|
|
|
// NewCategory creates a new Category. It returns a *Category and an error.
|
|
|
|
func NewCategory(term string) (*Category, error) {
|
|
|
|
if term == "" {
|
|
|
|
return nil, errors.New("error creating new category: term string empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Category{Term: term}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTerm sets the Term attribute of the Category. It returns an error.
|
|
|
|
func (c *Category) SetTerm(t string) error {
|
|
|
|
if t == "" {
|
|
|
|
return errors.New("error setting term of category: t string empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Term = t
|
|
|
|
return nil
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-19 12:28:09 +02:00
|
|
|
// SetScheme sets the Scheme attribute of the Category. It returns an error.
|
|
|
|
func (c *Category) SetScheme(s string) error {
|
|
|
|
if !isValidIRI(s) {
|
|
|
|
return fmt.Errorf("scheme %v not correctly formatted", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Scheme = s
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLabel sets the Label attribute of the Category. It returns an error.
|
|
|
|
func (c *Category) SetLabel(label string) error {
|
|
|
|
if label == "" {
|
|
|
|
return errors.New("error setting label of category: label string empty")
|
|
|
|
}
|
|
|
|
|
2024-10-16 16:51:39 +02:00
|
|
|
c.Label = html.UnescapeString(label)
|
2024-10-19 12:28:09 +02:00
|
|
|
return nil
|
2024-10-16 16:51:39 +02:00
|
|
|
}
|
|
|
|
|
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 == "" {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("term attribute of category %v empty", c)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
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-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("scheme attribute of category %v not correctly formatted", c)
|
2024-10-16 16:48:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 16:51:39 +02:00
|
|
|
if !isCorrectlyEscaped(c.Label) {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("label attribute of category %v not correctly escaped", c)
|
2024-10-16 16:51:39 +02:00
|
|
|
}
|
|
|
|
|
2024-10-13 17:19:40 +02:00
|
|
|
return nil
|
|
|
|
}
|