atom/outOfLineContent.go

66 lines
1.8 KiB
Go

package atom
import (
"encoding/xml"
"fmt"
"mime"
)
type OutOfLineContent struct {
XMLName xml.Name `xml:"content"`
*CommonAttributes
Type string `xml:"type,attr,omitempty"` // MediaType
SRC string `xml:"src,attr"` // IRI
}
// newOutOfLineContent creates a new OutOfLineContent. It returns a
// *OutOfLineContent and an error.
func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, error) {
if !isValidMediaType(mediaType) {
return nil, fmt.Errorf("error creating new out of line content: media type %v invalid", mediaType)
}
mediaType, _, _ = mime.ParseMediaType(mediaType)
iri, ok := content.(string)
if !ok {
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
}
if !isValidIRI(iri) {
return nil, fmt.Errorf("content %v not a valid uri", iri)
}
return &OutOfLineContent{Type: mediaType, SRC: iri}, nil
}
// isContent checks whether the OutOfLineContent is a Content. It returns a
// bool.
func (o *OutOfLineContent) isContent() bool { return true }
// hasSRC checks whether the OutOfLineContent has a SRC attribute. It returns a
// bool.
func (o *OutOfLineContent) hasSRC() bool { return true }
// getType returns the Type of the OutOfLineContent as a string.
func (o *OutOfLineContent) getType() string { return o.Type }
// Check checks the OutOfLineContent for incompatibilities with RFC4287. It
// returns an error.
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) {
return fmt.Errorf("type attribute of out of line content %v must not be a composite type", o)
}
if o.SRC == "" {
return fmt.Errorf("src attribute of out of line content %v empty", o)
}
return nil
}