2024-10-16 21:28:04 +02:00
|
|
|
package atom
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-10-16 18:31:13 +02:00
|
|
|
"strings"
|
2024-10-13 17:19:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
*CommonAttributes
|
2024-10-16 18:31:13 +02:00
|
|
|
Title string `xml:"title,attr,omitempty"`
|
2024-10-16 17:33:25 +02:00
|
|
|
Href IRI `xml:"href,attr"`
|
2024-10-13 17:19:40 +02:00
|
|
|
Rel string `xml:"rel,attr,omitempty"`
|
|
|
|
Type MediaType `xml:"type,attr,omitempty"`
|
|
|
|
HrefLang LanguageTag `xml:"hreflang,attr,omitempty"`
|
|
|
|
Length uint `xml:"length,attr,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-10-17 18:59:24 +02:00
|
|
|
// NewLink creates a new Link. It returns a *Link.
|
|
|
|
func NewLink(href string) *Link {
|
2024-10-17 18:57:43 +02:00
|
|
|
return &Link{Href: IRI(href)}
|
2024-10-16 17:17:41 +02:00
|
|
|
}
|
|
|
|
|
2024-10-16 19:59:28 +02:00
|
|
|
// Check checks the Link for incompatibilities with RFC4287. It returns an
|
|
|
|
// error.
|
2024-10-13 17:19:40 +02:00
|
|
|
func (l *Link) Check() error {
|
|
|
|
if l.Href == "" {
|
|
|
|
return errors.New("href attribute of link empty")
|
2024-10-16 18:31:13 +02:00
|
|
|
} else {
|
|
|
|
if !isValidIRI(l.Href) {
|
|
|
|
return fmt.Errorf("href attribute %v of link not correctly formatted", l.Href)
|
|
|
|
}
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-17 05:18:02 +02:00
|
|
|
if l.Rel != "" {
|
|
|
|
if strings.Contains(l.Rel, ":") && !isValidIRI(IRI(l.Rel)) {
|
|
|
|
return fmt.Errorf("rel attribute %v of link %v not correctly formatted", l.Rel, l.Href)
|
|
|
|
}
|
2024-10-16 18:31:13 +02:00
|
|
|
}
|
|
|
|
|
2024-10-17 05:18:02 +02:00
|
|
|
if l.Type != "" {
|
|
|
|
if !isValidMediaType(string(l.Type)) {
|
|
|
|
return fmt.Errorf("type attribute %v of link %v invalid media type", l.Type, l.Href)
|
|
|
|
}
|
2024-10-16 18:31:13 +02:00
|
|
|
}
|
|
|
|
|
2024-10-17 05:18:02 +02:00
|
|
|
if l.HrefLang != "" {
|
|
|
|
if !isValidLanguageTag(l.HrefLang) {
|
|
|
|
return fmt.Errorf("hreflang attribute %v of link %v invalid language tag", l.Type, l.HrefLang)
|
|
|
|
}
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-15 18:44:02 +02:00
|
|
|
|
2024-10-16 19:59:28 +02:00
|
|
|
// hasAlternateDuplicateLinks checks whether multiple Links with Rel
|
|
|
|
// "alternate" also have Type and HrefLang in common. It returns a bool.
|
2024-10-15 18:44:02 +02:00
|
|
|
// atom:feed/entry elements MUST NOT contain more than one atom:link element
|
|
|
|
// with a rel attribute value of "alternate" that has the same combination of
|
|
|
|
// type and hreflang attribute values.
|
|
|
|
func hasAlternateDuplicateLinks(l []*Link) bool {
|
|
|
|
linkMap := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, link := range l {
|
|
|
|
if link.Rel == "alternate" {
|
|
|
|
key := fmt.Sprint(link.Type, "|", link.HrefLang)
|
|
|
|
if linkMap[key] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
linkMap[key] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2024-10-16 19:59:28 +02:00
|
|
|
|
|
|
|
// alternateRelExists checks whether multiple Links with Rel "alternate" exist.
|
|
|
|
// It returns a bool.
|
|
|
|
func alternateRelExists(l []*Link) bool {
|
|
|
|
for _, link := range l {
|
|
|
|
if link.Rel == "alternate" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|