atom/generator.go

42 lines
913 B
Go
Raw Permalink Normal View History

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"
"errors"
"fmt"
"html"
)
2024-10-13 17:19:40 +02:00
type Generator struct {
2024-10-17 20:10:18 +02:00
XMLName xml.Name `xml:"generator"`
2024-10-13 17:19:40 +02:00
*CommonAttributes
URI IRI `xml:"uri,attr,omitempty"`
2024-10-13 17:19:40 +02:00
Version string `xml:"version,attr,omitempty"`
2024-10-17 19:11:33 +02:00
Text string `xml:",chardata"`
2024-10-13 17:19:40 +02:00
}
2024-10-16 19:59:28 +02:00
// NewGenerator creates a new Generator. It returns a *Generator.
func NewGenerator(text string) *Generator {
return &Generator{Text: html.UnescapeString(text)}
}
2024-10-16 19:59:28 +02:00
// Check checks the Generator for incompatibilities with RFC4287. It returns an
// error.
2024-10-13 17:19:40 +02:00
func (g *Generator) Check() error {
if g.URI != "" {
if !isValidIRI(g.URI) {
return fmt.Errorf("uri attribute %v of generator not correctly formatted", g.URI)
}
}
2024-10-13 17:19:40 +02:00
if g.Text == "" {
return errors.New("text element of generator empty")
}
if !isCorrectlyEscaped(g.Text) {
return fmt.Errorf("text element %v of generator not correctly escaped", g.Text)
}
2024-10-13 17:19:40 +02:00
return nil
}