diff --git a/atom.go b/atom.go index b5e6671..f00314f 100644 --- a/atom.go +++ b/atom.go @@ -1,6 +1,7 @@ package atom import ( + "encoding/xml" "fmt" "mime" "regexp" @@ -86,3 +87,9 @@ func isValidLanguageTag(tag LanguageTag) bool { func NewURN() IRI { return IRI(fmt.Sprint("urn:uuid:", uuid.New())) } + +// isValidXML checks whether a string is valid XML. It returns a bool. +func isValidXML(input string) bool { + var v interface{} + return xml.Unmarshal([]byte(input), &v) == nil +} diff --git a/category.go b/category.go index 8ffc7ae..5600ac6 100644 --- a/category.go +++ b/category.go @@ -16,6 +16,10 @@ type Category struct { // NewCategory creates a new Category. It returns a *Category and an error. func NewCategory(term, content string) (*Category, error) { + if !isValidXML(content) { + return nil, fmt.Errorf("%v not valid XML", content) + } + return &Category{Term: term, Content: content}, nil } @@ -41,8 +45,10 @@ func (c *Category) Check() error { return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label) } - if c.Content == "" { - return errors.New("content element of category empty") + if c.Content != "" { + if !isValidXML(c.Content) { + return fmt.Errorf("content element %v of category not valid XML", c.Content) + } } return nil diff --git a/link.go b/link.go index 5aa70ee..f132f99 100644 --- a/link.go +++ b/link.go @@ -19,6 +19,10 @@ type Link struct { // NewLink creates a new Link. It returns a *Link and an error. func NewLink(href, content string) (*Link, error) { + if !isValidXML(content) { + return nil, fmt.Errorf("%v not valid XML", content) + } + return &Link{Href: IRI(href), Content: content}, nil } @@ -51,8 +55,10 @@ func (l *Link) Check() error { } } - if l.Content == "" { - return errors.New("content element of link empty") + if l.Content != "" { + if !isValidXML(l.Content) { + return fmt.Errorf("content element %v of link not valid XML", l.Content) + } } return nil