46 lines
1008 B
Go
46 lines
1008 B
Go
package atom
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
"html"
|
|
)
|
|
|
|
type Category struct {
|
|
XMLName xml.Name `xml:"category"`
|
|
*CommonAttributes
|
|
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.
|
|
func NewCategory(term string) *Category {
|
|
return &Category{Term: term}
|
|
}
|
|
|
|
// 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 fmt.Errorf("term attribute of category %v empty", c)
|
|
}
|
|
|
|
if c.Scheme != "" {
|
|
if !isValidIRI(c.Scheme) {
|
|
return fmt.Errorf("scheme attribute of category %v not correctly formatted", c)
|
|
}
|
|
}
|
|
|
|
if !isCorrectlyEscaped(c.Label) {
|
|
return fmt.Errorf("label attribute of category %v not correctly escaped", c)
|
|
}
|
|
|
|
return nil
|
|
}
|