atom/outOfLineContent.go

58 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-10-16 21:28:04 +02:00
package atom
2024-10-13 17:19:40 +02:00
2024-10-15 21:13:46 +02:00
import (
2024-10-17 20:10:18 +02:00
"encoding/xml"
2024-10-15 21:13:46 +02:00
"fmt"
"mime"
)
2024-10-13 17:19:40 +02:00
type OutOfLineContent struct {
2024-10-17 20:10:18 +02:00
XMLName xml.Name `xml:"content"`
2024-10-13 17:19:40 +02:00
*CommonAttributes
Type string `xml:"type,attr,omitempty"` // MediaType
SRC string `xml:"src,attr"` // IRI
2024-10-13 17:19:40 +02:00
}
2024-10-16 19:59:28 +02:00
// newOutOfLineContent creates a new OutOfLineContent. It returns a
// *OutOfLineContent.
func newOutOfLineContent(mediaType, src string) *OutOfLineContent {
mediaType, _, _ = mime.ParseMediaType(mediaType)
return &OutOfLineContent{
CommonAttributes: newCommonAttributes(),
Type: mediaType,
SRC: src,
}
2024-10-15 21:13:46 +02:00
}
2024-10-16 19:59:28 +02:00
// isContent checks whether the OutOfLineContent is a Content. It returns a
// bool.
2024-10-15 19:32:14 +02:00
func (o *OutOfLineContent) isContent() bool { return true }
2024-10-16 19:59:28 +02:00
// hasSRC checks whether the OutOfLineContent has a SRC attribute. It returns a
// bool.
2024-10-15 19:32:14 +02:00
func (o *OutOfLineContent) hasSRC() bool { return true }
2024-10-16 19:59:28 +02:00
// getType returns the Type of the OutOfLineContent as a string.
func (o *OutOfLineContent) getType() string { return o.Type }
2024-10-13 17:19:40 +02:00
2024-10-16 19:59:28 +02:00
// Check checks the OutOfLineContent for incompatibilities with RFC4287. It
// returns an error.
2024-10-13 17:19:40 +02:00
func (o *OutOfLineContent) Check() error {
mediaType := o.getType()
if !isValidMediaType(mediaType) {
return fmt.Errorf("type attribute of out of line content %v invalid media type", o)
}
if isCompositeMediaType(mediaType) {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("type attribute of out of line content %v must not be a composite type", o)
}
2024-10-13 17:19:40 +02:00
if o.SRC == "" {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("src attribute of out of line content %v empty", o)
2024-10-13 17:19:40 +02:00
}
return nil
}