atom/generator.go

44 lines
968 B
Go
Raw 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"
"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 string `xml:"uri,attr,omitempty"` // IRI
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
}
// NewGenerator creates a new Generator. It returns a *Generator.
func NewGenerator(text string) *Generator {
return &Generator{
CommonAttributes: newCommonAttributes(),
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) {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("uri attribute of generator %v not correctly formatted", g)
}
}
2024-10-13 17:19:40 +02:00
if g.Text == "" {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("text element of generator %v empty", g)
2024-10-13 17:19:40 +02:00
}
if !isCorrectlyEscaped(g.Text) {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("text element of generator %v not correctly escaped", g)
}
2024-10-13 17:19:40 +02:00
return nil
}