package atom import ( "encoding/xml" "fmt" "strings" "time" ) // It is advisable that each atom:entry element contain a non-empty atom:title // element, a non-empty atom:content element when that element is present, and // a non-empty atom:summary element when the entry contains no atom:content // element. type Entry struct { XMLName xml.Name `xml:"entry"` *CommonAttributes Authors []*Person `xml:"author,omitempty"` Categories []*Category `xml:",omitempty"` Content Content `xml:",omitempty"` Contributors []*Person `xml:"contributors,omitempty"` ID *ID Links []*Link `xml:",omitempty"` Published *Date `xml:"published,omitempty"` Rights Text `xml:"rights,omitempty"` Source *Source `xml:",omitempty"` Summary Text `xml:"summary,omitempty"` Title Text `xml:"title"` Updated *Date `xml:"updated"` Extensions []*ExtensionElement `xml:",any,omitempty"` } // checkAuthors checks the entry's authors for incompatibilities with RFC4287. // It returns an errors. // atom:entry elements MUST contain one or more atom:author elements, unless // the atom:entry contains an atom:source element that contains an atom:author // element or, in an Atom Feed Document, the atom:feed element contains an // atom:author element itself. func (e *Entry) checkAuthors(authorInFeed bool) error { if e.Authors == nil { if !authorInFeed { if e.Source == nil { return fmt.Errorf("no authors set in entry %v", e.ID.URI) } if e.Source.Authors == nil { return fmt.Errorf("no authors set in entry %v", e.ID.URI) } } } else { for i, a := range e.Authors { if err := a.Check(); err != nil { return fmt.Errorf("author element %v of entry %v: %v", i, e.ID.URI, err) } } } return nil } // NewEntry creates a new Entry. It returns a *Entry. func NewEntry(title string) *Entry { return &Entry{ CommonAttributes: newCommonAttributes(), ID: NewID(NewURN()), Title: NewText("text", title), Updated: NewDate(time.Now()), } } // AddAuthor adds the Person as an author to the Entry. It returns an int. func (e *Entry) AddAuthor(p *Person) int { e.Updated = NewDate(time.Now()) return addToSlice(&e.Authors, p) } // DeleteAuthor deletes the Person at index from the Entry. It return an error. func (e *Entry) DeleteAuthor(index int) error { if err := deleteFromSlice(&e.Authors, index); err != nil { return fmt.Errorf("error deleting author %v from entry %v: %v", index, e.ID.URI, err) } e.Updated = NewDate(time.Now()) return nil } // AddCategory adds the Category to the Entry. It returns an int. func (e *Entry) AddCategory(c *Category) int { e.Updated = NewDate(time.Now()) return addToSlice(&e.Categories, c) } // DeleteCategory deletes the Category at index from the Entry. It return an // error. func (e *Entry) DeleteCategory(index int) error { if err := deleteFromSlice(&e.Categories, index); err != nil { return fmt.Errorf("error deleting category %v from entry %v: %v", index, e.ID.URI, err) } e.Updated = NewDate(time.Now()) return nil } // AddContributor adds the Person as a contributor to the Entry. It returns an // int. func (e *Entry) AddContributor(c *Person) int { e.Updated = NewDate(time.Now()) return addToSlice(&e.Contributors, c) } // DeleteContributor deletes the Person at index from the Entry. It return an // error. func (e *Entry) DeleteContributor(index int) error { if err := deleteFromSlice(&e.Contributors, index); err != nil { return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, e.ID.URI, err) } e.Updated = NewDate(time.Now()) return nil } // AddLink adds the Link to the Entry. It returns an int. func (e *Entry) AddLink(l *Link) int { e.Updated = NewDate(time.Now()) return addToSlice(&e.Links, l) } // DeleteLink deletes the Link at index from the Entry. It return an error. func (e *Entry) DeleteLink(index int) error { if err := deleteFromSlice(&e.Links, index); err != nil { return fmt.Errorf("error deleting link %v from entry %v: %v", index, e.ID.URI, err) } e.Updated = NewDate(time.Now()) return nil } // AddExtension adds the ExtensionElement to the Entry. It returns an int. func (e *Entry) AddExtension(x *ExtensionElement) int { e.Updated = NewDate(time.Now()) return addToSlice(&e.Extensions, x) } // DeleteExtension deletes the Extension at index from the Entry. It return an // error. func (e *Entry) DeleteExtension(index int) error { if err := deleteFromSlice(&e.Extensions, index); err != nil { return fmt.Errorf("error deleting extension %v from entry %v: %v", index, e.ID.URI, err) } e.Updated = NewDate(time.Now()) return nil } // Check checks the Entry for incompatibilities with RFC4287. It returns an // error. func (e *Entry) Check() error { if e.ID == nil { return fmt.Errorf("no id element of entry %v", e) } else { if err := e.ID.Check(); err != nil { return fmt.Errorf("id element of entry %v: %v", e, err) } } if err := e.checkAuthors(true); err != nil { return fmt.Errorf("entry %v: %v", e.ID.URI, err) } for i, c := range e.Categories { if err := c.Check(); err != nil { return fmt.Errorf("category element %v of entry %v: %v", i, e.ID.URI, err) } } if e.Content != nil { if err := e.Content.Check(); err != nil { return fmt.Errorf("content element of entry %v: %v", e.ID.URI, err) } } else { // atom:entry elements that contain no child atom:content element MUST // contain at least one atom:link element with a rel attribute value of // "alternate". if !alternateRelExists(e.Links) { return fmt.Errorf("no content element of entry %v and no link element with rel \"alternate\"", e.ID.URI) } } for i, c := range e.Contributors { if err := c.Check(); err != nil { return fmt.Errorf("contributor element %v of entry %v: %v", i, e.ID.URI, err) } } for i, l := range e.Links { if err := l.Check(); err != nil { return fmt.Errorf("link element %v of entry %v: %v", i, e.ID.URI, err) } } if hasAlternateDuplicateLinks(e.Links) { return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in entry %v", e.ID.URI) } if e.Published != nil { if err := e.Published.Check(); err != nil { return fmt.Errorf("published element of entry %v: %v", e.ID.URI, err) } } if e.Rights != nil { if err := e.Rights.Check(); err != nil { return fmt.Errorf("rights element of entry %v: %v", e.ID.URI, err) } } if e.Source != nil { if err := e.Source.Check(); err != nil { return fmt.Errorf("source element of entry %v: %v", e.ID.URI, err) } } if e.Summary != nil { if err := e.Summary.Check(); err != nil { return fmt.Errorf("summary element of entry %v: %v", e.ID.URI, err) } } else { // atom:entry elements MUST contain an atom:summary element in either // of the following cases: // the atom:entry contains an atom:content that has a "src" attribute // (and is thus empty). if e.Content.hasSRC() { return fmt.Errorf("no summary element of entry %v but content of type out of line content", e.ID.URI) } // the atom:entry contains content that is encoded in Base64; i.e., the // "type" attribute of atom:content is a MIME media type [MIMEREG], but // is not an XML media type [RFC3023], does not begin with "text/", and // does not end with "/xml" or "+xml". mediaType := e.Content.getType() if isValidMediaType(mediaType) && !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") { return fmt.Errorf("no summary element of entry %v but media type not xml", e.ID.URI) } } if e.Title == nil { return fmt.Errorf("no title element of entry %v", e.ID.URI) } else { if err := e.Title.Check(); err != nil { return fmt.Errorf("title element of entry %v: %v", e.ID.URI, err) } } if e.Updated == nil { return fmt.Errorf("no updated element of entry %v", e.ID.URI) } else { if err := e.Updated.Check(); err != nil { return fmt.Errorf("updated element of entry %v: %v", e.ID.URI, err) } } for i, x := range e.Extensions { if err := x.Check(); err != nil { return fmt.Errorf("extension element %v of entry %v: %v", i, e.ID.URI, err) } } return nil } // ToXML converts the Feed to XML. It returns a string and an error. func (e *Entry) ToXML(encoding string) (string, error) { xml, err := xml.MarshalIndent(e, "", " ") if err != nil { return "", fmt.Errorf("error xml encoding entry %v: %v", e.ID.URI, err) } return fmt.Sprintln(``) + string(xml), nil }