2024-10-16 21:28:04 +02:00
|
|
|
package atom
|
2024-10-13 17:19:40 +02:00
|
|
|
|
2024-10-16 16:58:58 +02:00
|
|
|
import (
|
2024-10-17 20:10:18 +02:00
|
|
|
"encoding/xml"
|
2024-10-16 16:58:58 +02:00
|
|
|
"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
|
2024-10-19 12:28:09 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-19 14:12:51 +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 16:58:58 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-10-16 16:58:58 +02:00
|
|
|
if g.URI != "" {
|
2024-10-16 17:33:25 +02:00
|
|
|
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-16 16:58:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-16 16:58:58 +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-16 16:58:58 +02:00
|
|
|
}
|
|
|
|
|
2024-10-13 17:19:40 +02:00
|
|
|
return nil
|
|
|
|
}
|