Compare commits

..

No commits in common. "main" and "v0.3.0" have entirely different histories.
main ... v0.3.0

24 changed files with 479 additions and 796 deletions

165
README.md
View File

@ -1,26 +1,23 @@
# atom # atom
An extensible Atom feed generator library that aims to be very close to RFC4287. An extensible Atom feed generator library that aims to be very close to
It diligently checks for compliance with the standard and provides functions for RFC4287. It diligently checks for compliance with the standard and provides
easy creation, extension and deletion of elements. functions for easy creation and extension of elements.
## Installation ## Installation
To install the latest version of the module, use the following command: To install the latest version of the module, use the following command:
```sh ```
go get git.streifling.com/jason/atom@latest go get git.streifling.com/jason/atom@latest
``` ```
## Usage ## Usage
### Basic Feed This library provides easy to use functions to create and extend elements of an
Atom feed. The intended way of using it entails using these functions.
This library provides convenient functions to safely create, extend and delete ```
elements and attributes of Atom feeds. It also provides checks for all
constructs' adherence to RFC4287.
```go
package main package main
import ( import (
@ -31,26 +28,31 @@ import (
) )
func main() { func main() {
feed := atom.NewFeed("Example Feed") feed, err := atom.NewFeed("Example Feed")
if err := feed.Check(); err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
author := atom.NewPerson("John Doe") author := atom.NewPerson("John Doe")
author.Email = "john.doe@example.com" author.Email = "john.doe@example.com"
if err := author.Check(); err != nil {
log.Fatalln(err)
}
feed.AddAuthor(author) feed.AddAuthor(author)
entry := atom.NewEntry("First Entry") entry, err := atom.NewEntry("First Entry")
entry.Content = atom.NewContent(atom.InlineText, "text", "This is the content of the first entry.") if err != nil {
if err := entry.Check(); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
content, err := atom.NewContent(atom.InlineText, "text", "This is the content of the first entry.")
if err != nil {
log.Fatalln(err)
}
entry.Content = content
feed.AddEntry(entry) feed.AddEntry(entry)
feedString, err := feed.ToXML("UTF-8") if err := feed.Check(); err != nil {
log.Fatalln(err)
}
feedString, err := feed.ToXML("utf-8")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -58,10 +60,10 @@ func main() {
} }
``` ```
It is also possible to use this library in a way similar to what other libraries It is also possible to use this library in a way similar to what other
provide. libraries would provide.
```go ```
package main package main
import ( import (
@ -70,7 +72,6 @@ import (
"time" "time"
"git.streifling.com/jason/atom" "git.streifling.com/jason/atom"
"github.com/google/uuid"
) )
func main() { func main() {
@ -81,7 +82,7 @@ func main() {
Type: "text", Type: "text",
Text: "Example Feed", Text: "Example Feed",
}, },
ID: &atom.ID{URI: fmt.Sprint("urn:uuid:", uuid.New())}, ID: &atom.ID{URI: "urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6"},
Updated: &atom.Date{DateTime: atom.DateTime(now)}, Updated: &atom.Date{DateTime: atom.DateTime(now)},
Authors: []*atom.Person{ Authors: []*atom.Person{
{ {
@ -95,7 +96,7 @@ func main() {
Type: "text", Type: "text",
Text: "First Entry", Text: "First Entry",
}, },
ID: &atom.ID{URI: fmt.Sprint("urn:uuid:", uuid.New())}, ID: &atom.ID{URI: "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a"},
Updated: &atom.Date{DateTime: atom.DateTime(now)}, Updated: &atom.Date{DateTime: atom.DateTime(now)},
Content: &atom.InlineTextContent{ Content: &atom.InlineTextContent{
Type: "text", Type: "text",
@ -105,7 +106,7 @@ func main() {
}, },
} }
feedString, err := feed.ToXML("UTF-8") feedString, err := feed.ToXML("utf-8")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -113,10 +114,10 @@ func main() {
} }
``` ```
The output of both ways of using it is an RFC4287 compliant Atom feed. The output of both ways of using it being an RFC4287 compliant Atom feed:
```xml ```
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"> <feed xmlns="http://www.w3.org/2005/Atom">
<author> <author>
<name>John Doe</name> <name>John Doe</name>
@ -133,109 +134,3 @@ The output of both ways of using it is an RFC4287 compliant Atom feed.
</entry> </entry>
</feed> </feed>
``` ```
### Compliance and Error Checking
All Atom constructs have their own ```construct.Check()``` method. It checks for
compliance with RFC4287 and errors. This allows one to be certain that only
compliant constructs are added to their respective parent construct. It also
means that not the entire feed has to be checked every time a new construct is
added. Instead one only checks the current construct and all of its
sub-constructs with a single ```construct.Check()``` call.
```go
package main
import (
"fmt"
"log"
"git.streifling.com/jason/atom"
)
func main() {
feed := atom.NewFeed("Example Feed")
if err := feed.Check(); err != nil {
log.Fatalln(err)
}
entry := atom.NewEntry("One")
entry.Content = atom.NewContent(atom.InlineText, "text", "Entry One")
entry.AddAuthor(atom.NewPerson("John Doe"))
if err := entry.Check(); err != nil {
log.Fatalln(err)
}
feed.AddEntry(entry)
feedString, err := feed.ToXML("UTF-8")
if err != nil {
log.Fatalln(err)
}
fmt.Println(feedString)
}
```
### Adding and Deleting Items
To add elements to any slice one calls the appropriate
```someone.AddSomething(toBeAdded)``` method. It returns the item's index
number. To delete the item one calls the Delete method.
```go
package main
import (
"fmt"
"log"
"git.streifling.com/jason/atom"
)
func main() {
feed := atom.NewFeed("Feed")
if err := feed.Check(); err != nil {
log.Fatalln(err)
}
e1 := atom.NewEntry("One")
e1.Content = atom.NewContent(atom.InlineText, "text", "Entry One")
if err := e1.Check(); err != nil {
log.Fatalln(err)
}
i1 := feed.AddEntry(e1)
e2 := atom.NewEntry("Two")
e2.Content = atom.NewContent(atom.InlineText, "text", "Entry Two")
if err := e2.Check(); err != nil {
log.Fatalln(err)
}
feed.AddEntry(e2)
if err := feed.DeleteEntry(i1); err != nil {
log.Fatalln(err)
}
feedString, err := feed.ToXML("UTF-8")
if err != nil {
log.Fatalln(err)
}
fmt.Println(feedString)
}
```
The output of this example looks like this. It only shows the second entry.
```xml
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>urn:uuid:ae89d343-b535-447d-ac14-4b80d3e02a2f</id>
<title type="text">Example Feed</title>
<updated>2024-10-20T14:57:02+02:00</updated>
<entry>
<content type="text">Entry Two</content>
<id>urn:uuid:620c6f73-ee1d-4c1e-be98-b0b1ad7a053f</id>
<title type="text">Two</title>
<updated>2024-10-20T14:57:02+02:00</updated>
</entry>
</feed>
```

90
atom.go
View File

@ -1,9 +1,7 @@
package atom package atom
import ( import (
"encoding/xml"
"fmt" "fmt"
"html"
"mime" "mime"
"regexp" "regexp"
"strings" "strings"
@ -12,41 +10,21 @@ import (
"golang.org/x/text/language" "golang.org/x/text/language"
) )
type Countable interface { type (
*xml.Attr | *Person | *Category | *Link | *ExtensionElement | *Entry EmailAddress string
} LanguageTag string
MediaType string
// addToSlice adds a Countable countable to to a *[]Countable slice. It returns IRI string
// the index as an int. )
func addToSlice[C Countable](slice *[]C, countable C) int {
if *slice == nil {
*slice = make([]C, 0)
}
*slice = append(*slice, countable)
return len(*slice) - 1
}
// deleteFromSlice deletes the Countable at index from the *[]Countable slice.
// It returns an error.
func deleteFromSlice[C Countable](slice *[]C, index int) error {
length := len(*slice)
if index > length {
return fmt.Errorf("id %v out of range %v", index, length)
}
*slice = append((*slice)[:index], (*slice)[index+1:]...)
return nil
}
// isValidIRI checks whether an IRI is valid or not. It returns a bool. // isValidIRI checks whether an IRI is valid or not. It returns a bool.
// https://www.w3.org/2011/04/XMLSchema/TypeLibrary-IRI-RFC3987.xsd // https://www.w3.org/2011/04/XMLSchema/TypeLibrary-IRI-RFC3987.xsd
func isValidIRI(iri string) bool { func isValidIRI(iri IRI) bool {
pattern := `((([A-Za-z])[A-Za-z0-9+\-\.]*):((//(((([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽!$&'()*+,;=:]|(%[0-9A-Fa-f][0-9A-Fa-f]))*@))?((\[((((([0-9A-Fa-f]{0,4}:)){6}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|(::(([0-9A-Fa-f]{0,4}:)){5}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|(([0-9A-Fa-f]{0,4})?::(([0-9A-Fa-f]{0,4}:)){4}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:))?[0-9A-Fa-f]{0,4}))?::(([0-9A-Fa-f]{0,4}:)){3}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,2}[0-9A-Fa-f]{0,4}))?::(([0-9A-Fa-f]{0,4}:)){2}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,3}[0-9A-Fa-f]{0,4}))?::[0-9A-Fa-f]{0,4}:(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,4}[0-9A-Fa-f]{0,4}))?::(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,5}[0-9A-Fa-f]{0,4}))?::[0-9A-Fa-f]{0,4})|((((([0-9A-Fa-f]{0,4}:)){0,6}[0-9A-Fa-f]{0,4}))?::))|(v[0-9A-Fa-f]+\.[A-Za-z0-9\-\._~!$&'()*+,;=:]+))\])|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))|(([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=]))*)((:[0-9]*))?)((/(([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))*))*)|(/(((([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))+((/(([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))*))*))?)|((([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))+((/(([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))*))*)|)((\?(([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@])|[-󰀀-󿿽􀀀-􏿽/?])*))?((#((([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@])|/|\?))*))?)` pattern := `((([A-Za-z])[A-Za-z0-9+\-\.]*):((//(((([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽!$&'()*+,;=:]|(%[0-9A-Fa-f][0-9A-Fa-f]))*@))?((\[((((([0-9A-Fa-f]{0,4}:)){6}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|(::(([0-9A-Fa-f]{0,4}:)){5}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|(([0-9A-Fa-f]{0,4})?::(([0-9A-Fa-f]{0,4}:)){4}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:))?[0-9A-Fa-f]{0,4}))?::(([0-9A-Fa-f]{0,4}:)){3}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,2}[0-9A-Fa-f]{0,4}))?::(([0-9A-Fa-f]{0,4}:)){2}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,3}[0-9A-Fa-f]{0,4}))?::[0-9A-Fa-f]{0,4}:(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,4}[0-9A-Fa-f]{0,4}))?::(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,5}[0-9A-Fa-f]{0,4}))?::[0-9A-Fa-f]{0,4})|((((([0-9A-Fa-f]{0,4}:)){0,6}[0-9A-Fa-f]{0,4}))?::))|(v[0-9A-Fa-f]+\.[A-Za-z0-9\-\._~!$&'()*+,;=:]+))\])|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))|(([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=]))*)((:[0-9]*))?)((/(([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))*))*)|(/(((([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))+((/(([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))*))*))?)|((([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))+((/(([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))*))*)|)((\?(([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@])|[-󰀀-󿿽􀀀-􏿽/?])*))?((#((([A-Za-z0-9\-\._~ -퟿豈-﷏ﷰ-￯𐀀-🿽𠀀-𯿽𰀀-𿿽񀀀-񏿽񐀀-񟿽񠀀-񯿽񰀀-񿿽򀀀-򏿽򐀀-򟿽򠀀-򯿽򰀀-򿿽󀀀-󏿽󐀀-󟿽󡀀-󯿽]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@])|/|\?))*))?)`
return regexp.MustCompile(pattern).MatchString(iri) return regexp.MustCompile(pattern).MatchString(string(iri))
} }
// isCorrectlyEscaped checks whether the text is correctly escaped as per // isCorrectlyEscaped checks whether a string is correctly escaped as per
// RFC4287. It returns a bool. // RFC4287. It returns a bool.
func isCorrectlyEscaped(text string) bool { func isCorrectlyEscaped(text string) bool {
relevantEntities := []string{"&amp;", "&lt;", "&gt;", "&quot;", "&apos;"} relevantEntities := []string{"&amp;", "&lt;", "&gt;", "&quot;", "&apos;"}
@ -60,10 +38,10 @@ func isCorrectlyEscaped(text string) bool {
return true return true
} }
// isCompositeMediaType checks whether the string m is a composite media type. // isCompositeMediaType checks whether a string is a composite media type. It
// It returns a bool. // returns a bool.
func isCompositeMediaType(m string) bool { func isCompositeMediaType(mediaType string) bool {
mediaType, _, err := mime.ParseMediaType(m) mediaType, _, err := mime.ParseMediaType(mediaType)
if err != nil { if err != nil {
return false return false
} }
@ -71,10 +49,10 @@ func isCompositeMediaType(m string) bool {
return strings.HasPrefix(mediaType, "multipart/") || strings.HasPrefix(mediaType, "message/") return strings.HasPrefix(mediaType, "multipart/") || strings.HasPrefix(mediaType, "message/")
} }
// isXMLMediaType checks whether the string m is an xml media type. It returns a // isXMLMediaType checks whether a string is an xml media type. It returns a
// bool. // bool.
func isXMLMediaType(m string) bool { func isXMLMediaType(mediaType string) bool {
mediaType, _, err := mime.ParseMediaType(m) mediaType, _, err := mime.ParseMediaType(mediaType)
if err != nil { if err != nil {
return false return false
} }
@ -82,37 +60,35 @@ func isXMLMediaType(m string) bool {
return strings.HasSuffix(mediaType, "/xml") || strings.HasSuffix(mediaType, "+xml") return strings.HasSuffix(mediaType, "/xml") || strings.HasSuffix(mediaType, "+xml")
} }
// isValidMediaType checks whether the string m is a valid media type. It // isValidMediaType checks whether a string is a valid media type. It returns a
// returns a bool. // bool.
func isValidMediaType(m string) bool { func isValidMediaType(mediaType string) bool {
mediaType, _, err := mime.ParseMediaType(m) mediaType, _, err := mime.ParseMediaType(mediaType)
if err != nil { if err != nil {
return false return false
} }
typeParts := strings.Split(mediaType, "/") typeParts := strings.Split(mediaType, "/")
return len(typeParts) == 2 && typeParts[0] != "" && typeParts[1] != "" if len(typeParts) != 2 || typeParts[0] == "" || typeParts[1] == "" {
return false
} }
// isValidLanguageTag checks whether the string languageTag is valid. It returns return true
// a bool. }
func isValidLanguageTag(languageTag string) bool {
_, err := language.Parse(languageTag) // isValidLanguageTag checks whether a LanguageTag is valid. It returns a bool.
func isValidLanguageTag(tag LanguageTag) bool {
_, err := language.Parse(string(tag))
return err == nil return err == nil
} }
// isValidAttribute checks whether the string attribute is valid. It returns a // isValidAttribute checks whether an Attribute is valid. It returns a bool.
// bool.
func isValidAttribute(attribute string) bool { func isValidAttribute(attribute string) bool {
return regexp.MustCompile(`^[a-zA-Z0-9_]+="[^"]*"$`).MatchString(attribute) regex := regexp.MustCompile(`^[a-zA-Z0-9_]+="[^"]*"$`)
return regex.MatchString(attribute)
} }
// NewURN generates an new valid IRI based on a UUID. It returns an IRI. // NewURN generates an new valid IRI based on a UUID. It returns an IRI.
func NewURN() string { func NewURN() IRI {
return fmt.Sprint("urn:uuid:", uuid.New()) return IRI(fmt.Sprint("urn:uuid:", uuid.New()))
}
// Unescape unescapes the string s. It returns an IRI.
func Unescape(s string) string {
return html.UnescapeString(s)
} }

View File

@ -2,41 +2,44 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"html"
) )
type Category struct { type Category struct {
XMLName xml.Name `xml:"category"` XMLName xml.Name `xml:"category"`
*CommonAttributes *CommonAttributes
Term string `xml:"term,attr"` Term string `xml:"term,attr"`
Scheme string `xml:"scheme,attr,omitempty"` // IRI Scheme IRI `xml:"scheme,attr,omitempty"`
Label string `xml:"label,attr,omitempty"` // Must be unescaped Label string `xml:"label,attr,omitempty"`
} }
// NewCategory creates a new Category. It takes in a string term and returns a // NewCategory creates a new Category. It returns a *Category.
// *Category.
func NewCategory(term string) *Category { func NewCategory(term string) *Category {
return &Category{ return &Category{Term: term}
CommonAttributes: NewCommonAttributes(),
Term: term,
} }
// SetLabel sets the label of the Category.
func (c *Category) SetLabel(label string) {
c.Label = html.UnescapeString(label)
} }
// Check checks the Category for incompatibilities with RFC4287. It returns an // Check checks the Category for incompatibilities with RFC4287. It returns an
// error. // error.
func (c *Category) Check() error { func (c *Category) Check() error {
if c.Term == "" { if c.Term == "" {
return fmt.Errorf("term attribute of category %v empty", c) return errors.New("term attribute of category empty")
} }
if c.Scheme != "" { if c.Scheme != "" {
if !isValidIRI(c.Scheme) { if !isValidIRI(c.Scheme) {
return fmt.Errorf("scheme attribute of category %v not correctly formatted", c) return fmt.Errorf("scheme attribute %v of category not correctly formatted", c.Scheme)
} }
} }
if !isCorrectlyEscaped(c.Label) { if !isCorrectlyEscaped(c.Label) {
return fmt.Errorf("label attribute of category %v not correctly escaped", c) return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label)
} }
return nil return nil

View File

@ -2,12 +2,11 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"fmt"
) )
type CommonAttributes struct { type CommonAttributes struct {
Base string `xml:"base,attr,omitempty"` // IRI Base IRI `xml:"base,attr,omitempty"`
Lang string `xml:"lang,attr,omitempty"` // LanguageTag Lang LanguageTag `xml:"lang,attr,omitempty"`
UndefinedAttributes []*xml.Attr `xml:",attr,omitempty"` UndefinedAttributes []*xml.Attr `xml:",attr,omitempty"`
} }
@ -17,32 +16,12 @@ func NewCommonAttributes() *CommonAttributes {
return new(CommonAttributes) return new(CommonAttributes)
} }
// AddAttribute adds an attribute to the CommonAttributes. It takes in the // AddExtensionAttribute adds the ExtensionAttribute to the CommonAttributes.
// strings name and value and returns the index as an int. func (c *CommonAttributes) AddExtensionAttribute(name, value string) {
func (c *CommonAttributes) AddAttribute(name, value string) int { if c.UndefinedAttributes == nil {
return addToSlice(&c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value}) c.UndefinedAttributes = make([]*xml.Attr, 1)
} c.UndefinedAttributes[0] = &xml.Attr{Name: xml.Name{Local: name}, Value: value}
} else {
// DeleteAttribute deletes the attribute at index from the CommonAttributes. It c.UndefinedAttributes = append(c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
// returns an error.
func (c *CommonAttributes) DeleteAttribute(index int) error {
if err := deleteFromSlice(&c.UndefinedAttributes, index); err != nil {
return fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", index, c, err)
}
return nil
}
// Check checks the CommonAttributes for incompatibilities with RFC4287. It
// returns an error.
func (c *CommonAttributes) Check() error {
for i, u := range c.UndefinedAttributes {
if u.Name.Local == "" {
return fmt.Errorf("xml name of undefined attribute %v empty", i)
}
if u.Value == "" {
return fmt.Errorf("value of undefined attribute %v empty", i)
} }
} }
return nil
}

View File

@ -1,5 +1,9 @@
package atom package atom
import (
"fmt"
)
const ( const (
InlineText = iota InlineText = iota
InlineXHTML InlineXHTML
@ -14,21 +18,18 @@ type Content interface {
Check() error Check() error
} }
// NewContent creates a new Content. It takes in an int contentType, a string // NewContent creates a new Content. It returns a Content and an error.
// mediaType and an any content and returns a Content. func NewContent(contentType int, mediaType string, content any) (Content, error) {
//
// If contentType is invalid, it returns nil.
func NewContent(contentType int, mediaType string, content any) Content {
switch contentType { switch contentType {
case 0: case 0:
return newInlineTextContent(mediaType, content.(string)) return newInlineTextContent(mediaType, content)
case 1: case 1:
return newInlineXHTMLContent(mediaType, content.(*XHTMLDiv)) return newInlineXHTMLContent(mediaType, content)
case 2: case 2:
return newInlineOtherContent(mediaType, content) return newInlineOtherContent(mediaType, content)
case 3: case 3:
return newOutOfLineContent(mediaType, content.(string)) return newOutOfLineContent(mediaType, content)
default: default:
return nil return nil, fmt.Errorf("%v is not a valid text type", contentType)
} }
} }

11
date.go
View File

@ -10,18 +10,15 @@ type Date struct {
DateTime string `xml:",chardata"` DateTime string `xml:",chardata"`
} }
// DateTime formats the time.Time t to a string as defined by RFC3339. It // DateTime formats a time.Time to string formated as defined by RFC3339. It
// returns a string. // returns a string.
func DateTime(t time.Time) string { func DateTime(t time.Time) string {
return t.Format(time.RFC3339) return string(t.Format(time.RFC3339))
} }
// NewDate creates a new Date. It takes in a time.Time t and returns a *Date. // NewDate creates a new Date. It returns a *Date.
func NewDate(t time.Time) *Date { func NewDate(t time.Time) *Date {
return &Date{ return &Date{DateTime: DateTime(t)}
CommonAttributes: NewCommonAttributes(),
DateTime: DateTime(t),
}
} }
// Check checks the Date for incompatibilities with RFC4287. It returns an // Check checks the Date for incompatibilities with RFC4287. It returns an

174
entry.go
View File

@ -2,6 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -30,26 +31,25 @@ type Entry struct {
} }
// checkAuthors checks the entry's authors for incompatibilities with RFC4287. // checkAuthors checks the entry's authors for incompatibilities with RFC4287.
// It takes in a bool authorIsInFeed and returns an errors. // It returns an errors.
//
// atom:entry elements MUST contain one or more atom:author elements, unless // 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 // 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 // element or, in an Atom Feed Document, the atom:feed element contains an
// atom:author element itself. // atom:author element itself.
func (e *Entry) checkAuthors(authorIsInFeed bool) error { func (e *Entry) checkAuthors(authorInFeed bool) error {
if e.Authors == nil { if e.Authors == nil {
if !authorIsInFeed { if !authorInFeed {
if e.Source == nil { if e.Source == nil {
return fmt.Errorf("no authors set in entry %v", e.ID.URI) return errors.New("no authors set in entry")
} }
if e.Source.Authors == nil { if e.Source.Authors == nil {
return fmt.Errorf("no authors set in entry %v", e.ID.URI) return errors.New("no authors set in entry")
} }
} }
} else { } else {
for i, a := range e.Authors { for i, a := range e.Authors {
if err := a.Check(); err != nil { if err := a.Check(); err != nil {
return fmt.Errorf("author element %v of entry %v: %v", i, e.ID.URI, err) return fmt.Errorf("author element %v of entry: %v", i, err)
} }
} }
} }
@ -57,120 +57,93 @@ func (e *Entry) checkAuthors(authorIsInFeed bool) error {
return nil return nil
} }
// update sets the Updated time to time.Now. // NewEntry creates a new Entry. It returns a *Entry and an error.
func (e *Entry) update() { func NewEntry(title string) (*Entry, error) {
if e.Updated == nil { text, err := NewText("text", title)
e.Updated = NewDate(time.Now()) if err != nil {
return nil, fmt.Errorf("error creating new entry: %v", err)
}
id, err := NewID(NewURN())
if err != nil {
return nil, fmt.Errorf("error creating new entry: %v", err)
}
return &Entry{
ID: id,
Title: text,
Updated: NewDate(time.Now()),
}, nil
}
// AddAuthor adds the Person as an author to the Entry.
func (e *Entry) AddAuthor(p *Person) {
if e.Authors == nil {
e.Authors = make([]*Person, 1)
e.Authors[0] = p
} else { } else {
e.Authors = append(e.Authors, p)
}
e.Updated.DateTime = DateTime(time.Now()) e.Updated.DateTime = DateTime(time.Now())
} }
// AddCategory adds the Category to the Entry.
func (e *Entry) AddCategory(c *Category) {
if e.Categories == nil {
e.Categories = make([]*Category, 1)
e.Categories[0] = c
} else {
e.Categories = append(e.Categories, c)
} }
// NewEntry creates a new Entry. It takes in a string title and returns a e.Updated.DateTime = DateTime(time.Now())
// *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 a as an author to the Entry. It returns the index // AddContributor adds the Person as a contributor to the Entry.
// as an int. func (e *Entry) AddContributor(c *Person) {
func (e *Entry) AddAuthor(a *Person) int { if e.Contributors == nil {
e.update() e.Contributors = make([]*Person, 1)
return addToSlice(&e.Authors, a) e.Contributors[0] = c
} else {
e.Contributors = append(e.Contributors, c)
} }
// DeleteAuthor deletes the Person at index from the Entry. It returns an error. e.Updated.DateTime = DateTime(time.Now())
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.update() // AddLink adds the Link to the Entry.
return nil func (e *Entry) AddLink(l *Link) {
if e.Links == nil {
e.Links = make([]*Link, 1)
e.Links[0] = l
} else {
e.Links = append(e.Links, l)
} }
// AddCategory adds the Category c to the Entry. It returns the index as an int. e.Updated.DateTime = DateTime(time.Now())
func (e *Entry) AddCategory(c *Category) int {
e.update()
return addToSlice(&e.Categories, c)
} }
// DeleteCategory deletes the Category at index from the Entry. It returns an // AddExtension adds the ExtensionElement to the Entry.
// error. func (e *Entry) AddExtension(x *ExtensionElement) {
func (e *Entry) DeleteCategory(index int) error { if e.Extensions == nil {
if err := deleteFromSlice(&e.Categories, index); err != nil { e.Extensions = make([]*ExtensionElement, 1)
return fmt.Errorf("error deleting category %v from entry %v: %v", index, e.ID.URI, err) e.Extensions[0] = x
} else {
e.Extensions = append(e.Extensions, x)
} }
e.update() e.Updated.DateTime = DateTime(time.Now())
return nil
}
// AddContributor adds the Person c as a contributor to the Entry. It returns
// the index as an int.
func (e *Entry) AddContributor(c *Person) int {
e.update()
return addToSlice(&e.Contributors, c)
}
// DeleteContributor deletes the Person at index from the Entry. It returns 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.update()
return nil
}
// AddLink adds the Link l to the Entry. It returns the index as an int.
func (e *Entry) AddLink(l *Link) int {
e.update()
return addToSlice(&e.Links, l)
}
// DeleteLink deletes the Link at index from the Entry. It returns 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.update()
return nil
}
// AddExtension adds the ExtensionElement x to the Entry. It returns the index
// as an int.
func (e *Entry) AddExtension(x *ExtensionElement) int {
e.update()
return addToSlice(&e.Extensions, x)
}
// DeleteExtension deletes the Extension at index from the Entry. It returns 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.update()
return nil
} }
// Check checks the Entry for incompatibilities with RFC4287. It returns an // Check checks the Entry for incompatibilities with RFC4287. It returns an
// error. // error.
func (e *Entry) Check() error { func (e *Entry) Check() error {
if e.ID == nil { if e.ID == nil {
return fmt.Errorf("no id element of entry %v", e) return errors.New("no id element of entry")
} else { } else {
if err := e.ID.Check(); err != nil { if err := e.ID.Check(); err != nil {
return fmt.Errorf("id element of entry %v: %v", e, err) return fmt.Errorf("id element of entry: %v", err)
} }
} }
@ -193,7 +166,7 @@ func (e *Entry) Check() error {
// contain at least one atom:link element with a rel attribute value of // contain at least one atom:link element with a rel attribute value of
// "alternate". // "alternate".
if !alternateRelExists(e.Links) { if !alternateRelExists(e.Links) {
return fmt.Errorf("no content element of entry %v and no link element with rel \"alternate\"", e.ID.URI) return errors.New("no content element of entry %v and no link element with rel \"alternate\"")
} }
} }
@ -209,7 +182,7 @@ func (e *Entry) Check() error {
} }
} }
if hasAlternateDuplicateLinks(e.Links) { 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) return fmt.Errorf("links with 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 e.Published != nil {
@ -277,12 +250,11 @@ func (e *Entry) Check() error {
return nil return nil
} }
// ToXML converts the Feed to XML. It takes in a string encoding and returns a // ToXML converts the Feed to XML. It returns a string and an error.
// string and an error.
func (e *Entry) ToXML(encoding string) (string, error) { func (e *Entry) ToXML(encoding string) (string, error) {
xml, err := xml.MarshalIndent(e, "", " ") xml, err := xml.MarshalIndent(e, "", " ")
if err != nil { if err != nil {
return "", fmt.Errorf("error xml encoding entry %v: %v", e.ID.URI, err) return "", fmt.Errorf("error xml encoding entry: %v", err)
} }
return fmt.Sprintln(`<?xml version="1.0" encoding="`+encoding+`"?>`) + string(xml), nil return fmt.Sprintln(`<?xml version="1.0" encoding="`+encoding+`"?>`) + string(xml), nil

View File

@ -2,7 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"fmt" "errors"
) )
type ExtensionElement struct { type ExtensionElement struct {
@ -10,8 +10,8 @@ type ExtensionElement struct {
XMLName xml.Name XMLName xml.Name
} }
// NewExtensionElement creates a new ExtensionElement. It takes in a string name // NewExtensionElement creates a new ExtensionElement. It returns a
// and any value and returns a *ExtensionElement. // *ExtensionElement.
func NewExtensionElement(name string, value any) *ExtensionElement { func NewExtensionElement(name string, value any) *ExtensionElement {
return &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value} return &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value}
} }
@ -19,12 +19,8 @@ func NewExtensionElement(name string, value any) *ExtensionElement {
// Check checks the ExtensionElement for incompatibilities with RFC4287. It // Check checks the ExtensionElement for incompatibilities with RFC4287. It
// returns an error. // returns an error.
func (e *ExtensionElement) Check() error { func (e *ExtensionElement) Check() error {
if e.XMLName.Local == "" {
return fmt.Errorf("xml name of extension %v empty", e)
}
if e.Value == nil { if e.Value == nil {
return fmt.Errorf("value of extension %v empty", e) return errors.New("value element of extension element empty")
} }
return nil return nil

203
feed.go
View File

@ -2,6 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"time" "time"
) )
@ -25,161 +26,105 @@ type Feed struct {
Entries []*Entry `xml:",omitempty"` Entries []*Entry `xml:",omitempty"`
} }
// update sets the Updated time to time.Now. // NewFeed creates a new Feed. It returns a *Feed and an error.
func (f *Feed) update() { func NewFeed(title string) (*Feed, error) {
if f.Updated == nil { text, err := NewText("text", title)
f.Updated = NewDate(time.Now()) if err != nil {
return nil, fmt.Errorf("error creating new feed: %v", err)
}
id, err := NewID(NewURN())
if err != nil {
return nil, fmt.Errorf("error creating new feed: %v", err)
}
return &Feed{
ID: id,
Title: text,
Updated: NewDate(time.Now()),
}, nil
}
// AddAuthor adds the Person as an author to the Feed.
func (f *Feed) AddAuthor(p *Person) {
if f.Authors == nil {
f.Authors = make([]*Person, 1)
f.Authors[0] = p
} else { } else {
f.Authors = append(f.Authors, p)
}
f.Updated.DateTime = DateTime(time.Now()) f.Updated.DateTime = DateTime(time.Now())
} }
// AddCategory adds the Category to the Feed.
func (f *Feed) AddCategory(c *Category) {
if f.Categories == nil {
f.Categories = make([]*Category, 1)
f.Categories[0] = c
} else {
f.Categories = append(f.Categories, c)
} }
// NewFeed creates a new Feed. It takes in a string title and returns a *Feed. f.Updated.DateTime = DateTime(time.Now())
func NewFeed(title string) *Feed {
return &Feed{
CommonAttributes: NewCommonAttributes(),
ID: NewID(NewURN()),
Title: NewText("text", title),
Updated: NewDate(time.Now()),
}
} }
// AddAuthor adds the Person a as an author to the Feed. It returns the index as // AddContributor adds the Person as a contributor to the Feed.
// an int. func (f *Feed) AddContributor(c *Person) {
func (f *Feed) AddAuthor(a *Person) int { if f.Contributors == nil {
f.update() f.Contributors = make([]*Person, 1)
return addToSlice(&f.Authors, a) f.Contributors[0] = c
} else {
f.Contributors = append(f.Contributors, c)
} }
// DeleteAuthor deletes the Person at index from the Feed. It returns an error. f.Updated.DateTime = DateTime(time.Now())
func (f *Feed) DeleteAuthor(index int) error {
if err := deleteFromSlice(&f.Authors, index); err != nil {
return fmt.Errorf("error deleting author %v from entry %v: %v", index, f.ID.URI, err)
} }
f.update() // AddLink adds the Link to the Feed. There should be one Link with Rel "self".
return nil func (f *Feed) AddLink(l *Link) {
if f.Links == nil {
f.Links = make([]*Link, 1)
f.Links[0] = l
} else {
f.Links = append(f.Links, l)
} }
// AddCategory adds the Category c to the Feed. It returns the index as an int. f.Updated.DateTime = DateTime(time.Now())
func (f *Feed) AddCategory(c *Category) int {
f.update()
return addToSlice(&f.Categories, c)
} }
// DeleteCategory deletes the Category at index from the Feed. It returns an // AddExtension adds the Extension to the Feed.
// error. func (f *Feed) AddExtension(e *ExtensionElement) {
func (f *Feed) DeleteCategory(index int) error { if f.Extensions == nil {
if err := deleteFromSlice(&f.Categories, index); err != nil { f.Extensions = make([]*ExtensionElement, 1)
return fmt.Errorf("error deleting category %v from entry %v: %v", index, f.ID.URI, err) f.Extensions[0] = e
} else {
f.Extensions = append(f.Extensions, e)
} }
f.update() f.Updated.DateTime = DateTime(time.Now())
return nil
} }
// AddContributor adds the Person c as a contributor to the Feed. It returns the // AddEntry adds the Entry to the Feed.
// index as an int. func (f *Feed) AddEntry(e *Entry) {
func (f *Feed) AddContributor(c *Person) int { if f.Entries == nil {
f.update() f.Entries = make([]*Entry, 1)
return addToSlice(&f.Contributors, c) f.Entries[0] = e
} else {
f.Entries = append(f.Entries, e)
} }
// DeleteContributor deletes the Person at index from the Feed. It returns an f.Updated.DateTime = DateTime(time.Now())
// error.
func (f *Feed) DeleteContributor(index int) error {
if err := deleteFromSlice(&f.Contributors, index); err != nil {
return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, f.ID.URI, err)
}
f.update()
return nil
}
// AddLink adds the Link l to the Feed. It returns the index as an int.
//
// There should be one Link with Rel "self".
func (f *Feed) AddLink(l *Link) int {
f.update()
return addToSlice(&f.Links, l)
}
// DeleteLink deletes the Link at index from the Feed. It returns an error.
func (f *Feed) DeleteLink(index int) error {
if err := deleteFromSlice(&f.Links, index); err != nil {
return fmt.Errorf("error deleting link %v from entry %v: %v", index, f.ID.URI, err)
}
f.update()
return nil
}
// AddExtension adds the Extension e to the Feed. It returns the index as an
// int.
func (f *Feed) AddExtension(e *ExtensionElement) int {
f.update()
return addToSlice(&f.Extensions, e)
}
// DeleteExtension deletes the Extension at index from the Feed. It returns an
// error.
func (f *Feed) DeleteExtension(index int) error {
if err := deleteFromSlice(&f.Extensions, index); err != nil {
return fmt.Errorf("error deleting extension %v from entry %v: %v", index, f.ID.URI, err)
}
f.update()
return nil
}
// AddEntry adds the Entry e to the Feed. It returns the index as an int.
func (f *Feed) AddEntry(e *Entry) int {
f.update()
return addToSlice(&f.Entries, e)
}
// DeleteEntry deletes the Entry at index from the Feed. It returns an error.
func (f *Feed) DeleteEntry(index int) error {
if err := deleteFromSlice(&f.Entries, index); err != nil {
return fmt.Errorf("error deleting entry %v from entry %v: %v", index, f.ID.URI, err)
}
f.update()
return nil
}
// DeleteEntryByURI deletes the Entry from the Feed. It takes in a string uri
// and returns an error.
func (f *Feed) DeleteEntryByURI(uri string) error {
if !isValidIRI(uri) {
return fmt.Errorf("error deleting entry from feed %v: uri %v invalid", f.ID.URI, uri)
}
index := -1
for i, e := range f.Entries {
if e.ID.URI == uri {
index = i
break
}
}
if index < 0 {
return fmt.Errorf("error deleting entry from feed %v: id %v not found", f.ID.URI, uri)
}
f.Entries = append(f.Entries[:index], f.Entries[index+1:]...)
f.update()
return nil
} }
// Check checks the Feed for incompatibilities with RFC4287. It returns an // Check checks the Feed for incompatibilities with RFC4287. It returns an
// error. // error.
func (f *Feed) Check() error { func (f *Feed) Check() error {
if f.ID == nil { if f.ID == nil {
return fmt.Errorf("no id element of feed %v", f) return errors.New("no id element of feed")
} else { } else {
if err := f.ID.Check(); err != nil { if err := f.ID.Check(); err != nil {
return fmt.Errorf("id element of feed %v: %v", f, err) return fmt.Errorf("id element of feed: %v", err)
} }
} }
@ -230,7 +175,7 @@ func (f *Feed) Check() error {
} }
} }
if hasAlternateDuplicateLinks(f.Links) { if hasAlternateDuplicateLinks(f.Links) {
return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in feed %v", f.ID.URI) return fmt.Errorf("links with with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in feed %v", f.ID.URI)
} }
if f.Logo != nil { if f.Logo != nil {
@ -260,7 +205,7 @@ func (f *Feed) Check() error {
} }
if f.Updated == nil { if f.Updated == nil {
return fmt.Errorf("no updated element of feed %v", f.ID.URI) return fmt.Errorf("no updated element of feed %v", f.ID)
} else { } else {
if err := f.Updated.Check(); err != nil { if err := f.Updated.Check(); err != nil {
return fmt.Errorf("updated element of feed %v: %v", f.ID.URI, err) return fmt.Errorf("updated element of feed %v: %v", f.ID.URI, err)
@ -286,7 +231,7 @@ func (f *Feed) Check() error {
func (f *Feed) ToXML(encoding string) (string, error) { func (f *Feed) ToXML(encoding string) (string, error) {
xml, err := xml.MarshalIndent(f, "", " ") xml, err := xml.MarshalIndent(f, "", " ")
if err != nil { if err != nil {
return "", fmt.Errorf("error xml encoding feed %v: %v", f.ID.URI, err) return "", fmt.Errorf("error xml encoding feed: %v", err)
} }
return fmt.Sprintln(`<?xml version="1.0" encoding="`+encoding+`"?>`) + string(xml), nil return fmt.Sprintln(`<?xml version="1.0" encoding="`+encoding+`"?>`) + string(xml), nil

View File

@ -2,6 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"html" "html"
) )
@ -9,18 +10,14 @@ import (
type Generator struct { type Generator struct {
XMLName xml.Name `xml:"generator"` XMLName xml.Name `xml:"generator"`
*CommonAttributes *CommonAttributes
URI string `xml:"uri,attr,omitempty"` // IRI URI IRI `xml:"uri,attr,omitempty"`
Version string `xml:"version,attr,omitempty"` Version string `xml:"version,attr,omitempty"`
Text string `xml:",chardata"` Text string `xml:",chardata"`
} }
// NewGenerator creates a new Generator. It takes in a string text and returns a // NewGenerator creates a new Generator. It returns a *Generator.
// *Generator.
func NewGenerator(text string) *Generator { func NewGenerator(text string) *Generator {
return &Generator{ return &Generator{Text: html.UnescapeString(text)}
CommonAttributes: NewCommonAttributes(),
Text: html.UnescapeString(text),
}
} }
// Check checks the Generator for incompatibilities with RFC4287. It returns an // Check checks the Generator for incompatibilities with RFC4287. It returns an
@ -28,16 +25,16 @@ func NewGenerator(text string) *Generator {
func (g *Generator) Check() error { func (g *Generator) Check() error {
if g.URI != "" { if g.URI != "" {
if !isValidIRI(g.URI) { if !isValidIRI(g.URI) {
return fmt.Errorf("uri attribute of generator %v not correctly formatted", g) return fmt.Errorf("uri attribute %v of generator not correctly formatted", g.URI)
} }
} }
if g.Text == "" { if g.Text == "" {
return fmt.Errorf("text element of generator %v empty", g) return errors.New("text element of generator empty")
} }
if !isCorrectlyEscaped(g.Text) { if !isCorrectlyEscaped(g.Text) {
return fmt.Errorf("text element of generator %v not correctly escaped", g) return fmt.Errorf("text element %v of generator not correctly escaped", g.Text)
} }
return nil return nil

15
icon.go
View File

@ -9,15 +9,16 @@ import (
type Icon struct { type Icon struct {
XMLName xml.Name `xml:"icon"` XMLName xml.Name `xml:"icon"`
*CommonAttributes *CommonAttributes
URI string `xml:",chardata"` // IRI URI IRI `xml:",chardata"`
} }
// NewIcon creates a new Icon. It takes in a string uri and returns a *Icon. // NewIcon creates a new Icon. It returns a *Icon and an error.
func NewIcon(uri string) *Icon { func NewIcon(uri IRI) (*Icon, error) {
return &Icon{ if !isValidIRI(uri) {
CommonAttributes: NewCommonAttributes(), return nil, fmt.Errorf("uri %v not correctly formatted", uri)
URI: uri,
} }
return &Icon{URI: uri}, nil
} }
// Check checks the Icon for incompatibilities with RFC4287. It returns an // Check checks the Icon for incompatibilities with RFC4287. It returns an
@ -27,7 +28,7 @@ func (i *Icon) Check() error {
return errors.New("uri element of icon empty") return errors.New("uri element of icon empty")
} else { } else {
if !isValidIRI(i.URI) { if !isValidIRI(i.URI) {
return fmt.Errorf("uri attribute of icon %v not correctly formatted", i) return fmt.Errorf("uri attribute %v of icon not correctly formatted", i.URI)
} }
} }

15
id.go
View File

@ -9,15 +9,16 @@ import (
type ID struct { type ID struct {
XMLName xml.Name `xml:"id"` XMLName xml.Name `xml:"id"`
*CommonAttributes *CommonAttributes
URI string `xml:",chardata"` // IRI URI IRI `xml:",chardata"`
} }
// NewID creates a new ID. It takes in a string uri and returns a *ID. // NewID creates a new ID. It returns a *ID and an error.
func NewID(uri string) *ID { func NewID(uri IRI) (*ID, error) {
return &ID{ if !isValidIRI(uri) {
CommonAttributes: NewCommonAttributes(), return nil, fmt.Errorf("uri %v not correctly formatted", uri)
URI: uri,
} }
return &ID{URI: IRI(uri)}, nil
} }
// Check checks the ID for incompatibilities with RFC4287. It returns an error. // Check checks the ID for incompatibilities with RFC4287. It returns an error.
@ -26,7 +27,7 @@ func (i *ID) Check() error {
return errors.New("uri element of id empty") return errors.New("uri element of id empty")
} else { } else {
if !isValidIRI(i.URI) { if !isValidIRI(i.URI) {
return fmt.Errorf("uri element of id %v not correctly formatted", i) return fmt.Errorf("uri element %v of id not correctly formatted", i.URI)
} }
} }

View File

@ -2,6 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"mime" "mime"
) )
@ -10,19 +11,17 @@ type InlineOtherContent struct {
XMLName xml.Name `xml:"content"` XMLName xml.Name `xml:"content"`
*CommonAttributes *CommonAttributes
AnyElement any `xml:",chardata"` AnyElement any `xml:",chardata"`
Type string `xml:"type,attr,omitempty"` // MediaType Type MediaType `xml:"type,attr,omitempty"`
} }
// newInlineOtherContent creates a new InlineOtherContent. It takes in the string // newInlineOtherContent creates a new InlineOtherContent. It returns a
// mediaType and any content and returns a *InlineOtherContent and an error. // *InlineOtherContent and an error.
func newInlineOtherContent(mediaType string, content any) *InlineOtherContent { func newInlineOtherContent(mediaType string, content any) (*InlineOtherContent, error) {
mediaType, _, _ = mime.ParseMediaType(mediaType) if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
return nil, fmt.Errorf("media type %v incompatible with inline other content", mediaType)
return &InlineOtherContent{
CommonAttributes: NewCommonAttributes(),
Type: mediaType,
AnyElement: content,
} }
return &InlineOtherContent{Type: MediaType(mediaType), AnyElement: content}, nil
} }
// isContent checks whether the InlineOtherContent is a Content. It returns a // isContent checks whether the InlineOtherContent is a Content. It returns a
@ -34,19 +33,19 @@ func (i *InlineOtherContent) isContent() bool { return true }
func (i *InlineOtherContent) hasSRC() bool { return false } func (i *InlineOtherContent) hasSRC() bool { return false }
// getType returns the Type of the InlineOtherContent as a string. // getType returns the Type of the InlineOtherContent as a string.
func (i *InlineOtherContent) getType() string { return i.Type } func (i *InlineOtherContent) getType() string { return string(i.Type) }
// Check checks the InlineOtherContent for incompatibilities with RFC4287. It // Check checks the InlineOtherContent for incompatibilities with RFC4287. It
// returns an error. // returns an error.
func (i *InlineOtherContent) Check() error { func (i *InlineOtherContent) Check() error {
mediaType := i.getType() mediaType := i.getType()
if !isValidMediaType(mediaType) { if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
return fmt.Errorf("type attribute of inline other content %v invalid media type", i) return fmt.Errorf("type attribute %v incompatible with inline other content", mediaType)
} }
if isCompositeMediaType(mediaType) { if isCompositeMediaType(mediaType) {
return fmt.Errorf("type attribute of inline other content %v must not be a composite type", i) return errors.New("type attribute of inline other content must not be a composite type")
} }
return nil return nil

View File

@ -2,6 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
) )
@ -12,14 +13,19 @@ type InlineTextContent struct {
Text string `xml:",chardata"` Text string `xml:",chardata"`
} }
// newInlineTextContent creates a new InlineTextContent. It takes in the strings // newInlineTextContent creates a new InlineTextContent. It returns a
// mediaType and text and returns a *InlineTextContent. // *InlineTextContent and an error.
func newInlineTextContent(mediaType, text string) *InlineTextContent { func newInlineTextContent(mediaType string, content any) (*InlineTextContent, error) {
return &InlineTextContent{ if mediaType != "text" && mediaType != "html" && mediaType != "" {
CommonAttributes: NewCommonAttributes(), return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType)
Type: mediaType,
Text: text,
} }
text, ok := content.(string)
if !ok {
return nil, fmt.Errorf("content type %T incompatible with inline text content", content)
}
return &InlineTextContent{Type: mediaType, Text: text}, nil
} }
// isContent checks whether the InlineTextContent is a Content. It returns a // isContent checks whether the InlineTextContent is a Content. It returns a
@ -37,7 +43,7 @@ func (i *InlineTextContent) getType() string { return i.Type }
// returns an error. // returns an error.
func (i *InlineTextContent) Check() error { func (i *InlineTextContent) Check() error {
if i.Type != "" && i.Type != "text" && i.Type != "html" { if i.Type != "" && i.Type != "text" && i.Type != "html" {
return fmt.Errorf("type attribute of inline text content %v must be text or html if not omitted", i) return errors.New("type attribute of inline text content must be text or html if not omitted")
} }
return nil return nil

View File

@ -2,6 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
) )
@ -12,14 +13,19 @@ type InlineXHTMLContent struct {
Type string `xml:"type,attr"` Type string `xml:"type,attr"`
} }
// newInlineXHTMLContent creates a new InlineXHTMLContent. It takes in the // newInlineXHTMLContent creates a new InlineXHTMLContent. It returns a
// string mediaType and the XHTMLDiv div and returns a *InlineXHTMLContent. // *InlineXHTMLContent and an error.
func newInlineXHTMLContent(mediaType string, div *XHTMLDiv) *InlineXHTMLContent { func newInlineXHTMLContent(mediaType string, content any) (*InlineXHTMLContent, error) {
return &InlineXHTMLContent{ if mediaType != "xhtml" {
CommonAttributes: NewCommonAttributes(), return nil, fmt.Errorf("media type %v incompatible with inline xhtml content", mediaType)
Type: mediaType,
XHTMLDiv: div,
} }
xhtmlDiv, ok := content.(*XHTMLDiv)
if !ok {
return nil, fmt.Errorf("content type %T incompatible with inline xhtml content", content)
}
return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: xhtmlDiv}, nil
} }
// isContent checks whether the InlineXHTMLContent is a Content. It returns a // isContent checks whether the InlineXHTMLContent is a Content. It returns a
@ -37,11 +43,11 @@ func (i *InlineXHTMLContent) getType() string { return i.Type }
// returns an error. // returns an error.
func (i *InlineXHTMLContent) Check() error { func (i *InlineXHTMLContent) Check() error {
if i.Type != "xhtml" { if i.Type != "xhtml" {
return fmt.Errorf("type attribute of inline xhtml content %v must be xhtml", i) return errors.New("type attribute of inline xhtml content must be xhtml")
} }
if err := i.XHTMLDiv.Check(); err != nil { if err := i.XHTMLDiv.Check(); err != nil {
return fmt.Errorf("xhtml div element of inline xhtml content %v: %v", i, err) return fmt.Errorf("xhtml div element %v of inline xhtml content %v: %v", i.XHTMLDiv, i, err)
} }
return nil return nil

28
link.go
View File

@ -2,6 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"strings" "strings"
) )
@ -10,47 +11,44 @@ type Link struct {
XMLName xml.Name `xml:"link"` XMLName xml.Name `xml:"link"`
*CommonAttributes *CommonAttributes
Title string `xml:"title,attr,omitempty"` Title string `xml:"title,attr,omitempty"`
Href string `xml:"href,attr"` // IRI Href IRI `xml:"href,attr"`
Rel string `xml:"rel,attr,omitempty"` Rel string `xml:"rel,attr,omitempty"`
Type string `xml:"type,attr,omitempty"` // MediaType Type MediaType `xml:"type,attr,omitempty"`
HrefLang string `xml:"hreflang,attr,omitempty"` // LanguageTag HrefLang LanguageTag `xml:"hreflang,attr,omitempty"`
Length uint `xml:"length,attr,omitempty"` Length uint `xml:"length,attr,omitempty"`
} }
// NewLink creates a new Link. It takes in the string href and returns a *Link. // NewLink creates a new Link. It returns a *Link.
func NewLink(href string) *Link { func NewLink(href string) *Link {
return &Link{ return &Link{Href: IRI(href)}
CommonAttributes: NewCommonAttributes(),
Href: href,
}
} }
// Check checks the Link for incompatibilities with RFC4287. It returns an // Check checks the Link for incompatibilities with RFC4287. It returns an
// error. // error.
func (l *Link) Check() error { func (l *Link) Check() error {
if l.Href == "" { if l.Href == "" {
return fmt.Errorf("href attribute of link %v empty", l) return errors.New("href attribute of link empty")
} else { } else {
if !isValidIRI(l.Href) { if !isValidIRI(l.Href) {
return fmt.Errorf("href attribute of link %v not correctly formatted", l) return fmt.Errorf("href attribute %v of link not correctly formatted", l.Href)
} }
} }
if l.Rel != "" { if l.Rel != "" {
if strings.Contains(l.Rel, ":") && !isValidIRI(l.Rel) { if strings.Contains(l.Rel, ":") && !isValidIRI(IRI(l.Rel)) {
return fmt.Errorf("rel attribute of link %v not correctly formatted", l) return fmt.Errorf("rel attribute %v of link %v not correctly formatted", l.Rel, l.Href)
} }
} }
if l.Type != "" { if l.Type != "" {
if !isValidMediaType(l.Type) { if !isValidMediaType(string(l.Type)) {
return fmt.Errorf("type attribute of link %v invalid media type", l) return fmt.Errorf("type attribute %v of link %v invalid media type", l.Type, l.Href)
} }
} }
if l.HrefLang != "" { if l.HrefLang != "" {
if !isValidLanguageTag(l.HrefLang) { if !isValidLanguageTag(l.HrefLang) {
return fmt.Errorf("hreflang attribute of link %v invalid language tag", l) return fmt.Errorf("hreflang attribute %v of link %v invalid language tag", l.Type, l.HrefLang)
} }
} }

18
logo.go
View File

@ -2,31 +2,33 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
) )
type Logo struct { type Logo struct {
XMLName xml.Name `xml:"logo"` XMLName xml.Name `xml:"logo"`
*CommonAttributes *CommonAttributes
URI string `xml:",chardata"` // IRI URI IRI `xml:",chardata"`
} }
// NewLogo creates a new Logo. It takes in a string uri and returns a *Logo. // NewLogo creates a new Logo. It returns a *Logo.
func NewLogo(uri string) *Logo { func NewLogo(uri IRI) (*Logo, error) {
return &Logo{ if !isValidIRI(uri) {
CommonAttributes: NewCommonAttributes(), return nil, fmt.Errorf("uri %v not correctly formatted", uri)
URI: uri,
} }
return &Logo{URI: uri}, nil
} }
// Check checks the Logo for incompatibilities with RFC4287. It returns an // Check checks the Logo for incompatibilities with RFC4287. It returns an
// error. // error.
func (l *Logo) Check() error { func (l *Logo) Check() error {
if l.URI == "" { if l.URI == "" {
return fmt.Errorf("uri element of logo %v empty", l) return errors.New("uri element of logo empty")
} else { } else {
if !isValidIRI(l.URI) { if !isValidIRI(l.URI) {
return fmt.Errorf("uri element of logo %v not correctly formatted", l) return fmt.Errorf("uri element %v of logo not correctly formatted", l.URI)
} }
} }

View File

@ -2,6 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"mime" "mime"
) )
@ -9,20 +10,27 @@ import (
type OutOfLineContent struct { type OutOfLineContent struct {
XMLName xml.Name `xml:"content"` XMLName xml.Name `xml:"content"`
*CommonAttributes *CommonAttributes
Type string `xml:"type,attr,omitempty"` // MediaType Type MediaType `xml:"type,attr,omitempty"`
SRC string `xml:"src,attr"` // IRI SRC IRI `xml:"src,attr"`
} }
// newOutOfLineContent creates a new OutOfLineContent. It takes in the strings // newOutOfLineContent creates a new OutOfLineContent. It returns a
// mediaType and src and returns a *OutOfLineContent. // *OutOfLineContent and an error.
func newOutOfLineContent(mediaType, src string) *OutOfLineContent { func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, error) {
mediaType, _, _ = mime.ParseMediaType(mediaType) if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
return nil, fmt.Errorf("media type %v incompatible with out of line content", mediaType)
return &OutOfLineContent{
CommonAttributes: NewCommonAttributes(),
Type: mediaType,
SRC: src,
} }
iri, ok := content.(IRI)
if !ok {
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
}
if !isValidIRI(iri) {
return nil, errors.New("content not a valid uri")
}
return &OutOfLineContent{Type: MediaType(mediaType), SRC: iri}, nil
} }
// isContent checks whether the OutOfLineContent is a Content. It returns a // isContent checks whether the OutOfLineContent is a Content. It returns a
@ -34,23 +42,23 @@ func (o *OutOfLineContent) isContent() bool { return true }
func (o *OutOfLineContent) hasSRC() bool { return true } func (o *OutOfLineContent) hasSRC() bool { return true }
// getType returns the Type of the OutOfLineContent as a string. // getType returns the Type of the OutOfLineContent as a string.
func (o *OutOfLineContent) getType() string { return o.Type } func (o *OutOfLineContent) getType() string { return string(o.Type) }
// Check checks the OutOfLineContent for incompatibilities with RFC4287. It // Check checks the OutOfLineContent for incompatibilities with RFC4287. It
// returns an error. // returns an error.
func (o *OutOfLineContent) Check() error { func (o *OutOfLineContent) Check() error {
mediaType := o.getType() mediaType := o.getType()
if !isValidMediaType(mediaType) { if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
return fmt.Errorf("type attribute of out of line content %v invalid media type", o) return fmt.Errorf("type attribute %v incompatible with out of line content", mediaType)
} }
if isCompositeMediaType(mediaType) { if isCompositeMediaType(mediaType) {
return fmt.Errorf("type attribute of out of line content %v must not be a composite type", o) return errors.New("type attribute of out of line content must not be a composite type")
} }
if o.SRC == "" { if o.SRC == "" {
return fmt.Errorf("src attribute of out of line content %v empty", o) return errors.New("src attribute of out of line content empty")
} }
return nil return nil

View File

@ -1,6 +1,7 @@
package atom package atom
import ( import (
"errors"
"fmt" "fmt"
"net/mail" "net/mail"
) )
@ -8,57 +9,50 @@ import (
type Person struct { type Person struct {
*CommonAttributes *CommonAttributes
Name string `xml:"name"` Name string `xml:"name"`
URI string `xml:"uri,omitempty"` // IRI URI IRI `xml:"uri,omitempty"`
Email string `xml:"email,omitempty"` // EmailAddress Email EmailAddress `xml:"email,omitempty"`
Extensions []*ExtensionElement `xml:",any,omitempty"` Extensions []*ExtensionElement `xml:",any,omitempty"`
} }
// NewPerson creates a new Person. It takes in a string name and returns a // NewPerson creates a new Person. It returns a *Person.
// *Person.
func NewPerson(name string) *Person { func NewPerson(name string) *Person {
return &Person{ return &Person{Name: name}
CommonAttributes: NewCommonAttributes(),
Name: name,
}
} }
// AddExtension adds the Extension e to the Person. It returns the index as an // AddExtension adds the Extension to the Person.
// int. func (p *Person) AddExtension(e *ExtensionElement) {
func (p *Person) AddExtension(e *ExtensionElement) int { if p.Extensions == nil {
return addToSlice(&p.Extensions, e) p.Extensions = make([]*ExtensionElement, 1)
p.Extensions[0] = e
} else {
p.Extensions = append(p.Extensions, e)
} }
// DeleteExtension deletes the Extension at index from the Person. It returns an
// error.
func (p *Person) DeleteExtension(index int) error {
if err := deleteFromSlice(&p.Extensions, index); err != nil {
return fmt.Errorf("error deleting extension %v from person %v: %v", index, p, err)
}
return nil
} }
// Check checks the Person for incompatibilities with RFC4287. It returns an // Check checks the Person for incompatibilities with RFC4287. It returns an
// error. // error.
func (p *Person) Check() error { func (p *Person) Check() error {
if p.Name == "" { if p.Name == "" {
return fmt.Errorf("name element of person %v empty", p) return errors.New("name element of person element empty")
} }
if p.URI != "" { if p.URI != "" {
if !isValidIRI(p.URI) { if !isValidIRI(p.URI) {
return fmt.Errorf("uri element of person %v not correctly formatted", p) return fmt.Errorf("uri element of person %v not correctly formatted", p.Name)
} }
} }
if p.Email != "" { if p.Email != "" {
if _, err := mail.ParseAddress(p.Email); err != nil { if _, err := mail.ParseAddress(string(p.Email)); err != nil {
return fmt.Errorf("email element of person %v not correctly formatted", p) return fmt.Errorf("email element of person %v not correctly formatted", p.Name)
} }
} }
if p.Extensions != nil {
for i, e := range p.Extensions { for i, e := range p.Extensions {
if err := e.Check(); err != nil { if err := e.Check(); err != nil {
return fmt.Errorf("extension element %v of person %v: %v", i, p, err) return fmt.Errorf("extension element %v of person %v: %v", i, p.Name, err)
}
} }
} }

View File

@ -1,8 +1,6 @@
package atom package atom
import ( import "errors"
"fmt"
)
type PlainText struct { type PlainText struct {
*CommonAttributes *CommonAttributes
@ -13,29 +11,19 @@ type PlainText struct {
// isText checks whether the PlainText is a Text. It returns a bool. // isText checks whether the PlainText is a Text. It returns a bool.
func (p *PlainText) isText() bool { return true } func (p *PlainText) isText() bool { return true }
// newPlainText creates a new PlainText. It takes in the strings textType and
// content and returns a *PlainText.
func newPlainText(textType, content string) *PlainText {
return &PlainText{
CommonAttributes: NewCommonAttributes(),
Type: textType,
Text: content,
}
}
// Check checks the PlainText for incompatibilities with RFC4287. It returns an // Check checks the PlainText for incompatibilities with RFC4287. It returns an
// error. // error.
func (p *PlainText) Check() error { func (p *PlainText) Check() error {
if p.Type != "" && p.Type != "text" && p.Type != "html" { if p.Type != "" && p.Type != "text" && p.Type != "html" {
return fmt.Errorf("type attribute of plain text %v must be text or html if not omitted", p) return errors.New("type attribute of plain text must be text or html if not omitted")
} }
if p.Type == "html" && !isCorrectlyEscaped(p.Text) { if p.Type == "html" && !isCorrectlyEscaped(p.Text) {
return fmt.Errorf("text element of plain text %v not correctly escaped", p) return errors.New("text element of plain text not correctly escaped")
} }
if p.Text == "" { if p.Text == "" {
return fmt.Errorf("text element of plain text %v empty", p) return errors.New("text element of plain text empty")
} }
return nil return nil

103
source.go
View File

@ -23,161 +23,84 @@ type Source struct {
Extensions []*ExtensionElement `xml:",any,omitempty"` Extensions []*ExtensionElement `xml:",any,omitempty"`
} }
// NewSource creates a new Source. It returns a *Source.
func NewSource() *Source {
return &Source{CommonAttributes: NewCommonAttributes()}
}
// AddAuthor adds the Person a as an author to the Source. It returns the index
// as an int.
func (s *Source) AddAuthor(a *Person) int {
return addToSlice(&s.Authors, a)
}
// DeleteAuthor deletes the Person at index from the Source. It returns an
// error.
func (s *Source) DeleteAuthor(index int) error {
if err := deleteFromSlice(&s.Authors, index); err != nil {
return fmt.Errorf("error deleting author %v from source %v: %v", index, s, err)
}
return nil
}
// AddCategory adds the Category c to the Source. It returns the index as an int.
func (s *Source) AddCategory(c *Category) int {
return addToSlice(&s.Categories, c)
}
// DeleteCategory deletes the Category at index from the Source. It returns an
// error.
func (s *Source) DeleteCategory(index int) error {
if err := deleteFromSlice(&s.Categories, index); err != nil {
return fmt.Errorf("error deleting category %v from source %v: %v", index, s, err)
}
return nil
}
// AddContributor adds the Person c as a contributor to the Source. It returns
// the index as an int.
func (s *Source) AddContributor(c *Person) int {
return addToSlice(&s.Contributors, c)
}
// DeleteContributor deletes the Person at index from the Source. It returns an
// error.
func (s *Source) DeleteContributor(index int) error {
if err := deleteFromSlice(&s.Contributors, index); err != nil {
return fmt.Errorf("error deleting contributor %v from source %v: %v", index, s, err)
}
return nil
}
// AddLink adds the Link l to the Source. It returns the index as an int.
func (s *Source) AddLink(l *Link) int {
return addToSlice(&s.Links, l)
}
// DeleteLink deletes the Link at index from the Source. It returns an error.
func (s *Source) DeleteLink(index int) error {
if err := deleteFromSlice(&s.Links, index); err != nil {
return fmt.Errorf("error deleting link %v from source %v: %v", index, s, err)
}
return nil
}
// AddExtension adds the ExtensionElement e to the Source. It returns the index
// as an int.
func (s *Source) AddExtension(e *ExtensionElement) int {
return addToSlice(&s.Extensions, e)
}
// DeleteExtension deletes the Extension at index from the Source. It returns an
// error.
func (s *Source) DeleteExtension(index int) error {
if err := deleteFromSlice(&s.Extensions, index); err != nil {
return fmt.Errorf("error deleting extension %v from source %v: %v", index, s, err)
}
return nil
}
// Check checks the Source for incompatibilities with RFC4287. It returns an // Check checks the Source for incompatibilities with RFC4287. It returns an
// error. // error.
func (s *Source) Check() error { func (s *Source) Check() error {
for i, a := range s.Authors { for i, a := range s.Authors {
if err := a.Check(); err != nil { if err := a.Check(); err != nil {
return fmt.Errorf("author element %v of source %v: %v", i, s, err) return fmt.Errorf("author element %v of source %v: %v", i, s.ID.URI, err)
} }
} }
for i, c := range s.Categories { for i, c := range s.Categories {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("category element %v of source %v: %v", i, s, err) return fmt.Errorf("category element %v of source %v: %v", i, s.ID.URI, err)
} }
} }
for i, c := range s.Contributors { for i, c := range s.Contributors {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("contributor element %v of source %v: %v", i, s, err) return fmt.Errorf("contributor element %v of source %v: %v", i, s.ID.URI, err)
} }
} }
if s.Generator != nil { if s.Generator != nil {
if err := s.Generator.Check(); err != nil { if err := s.Generator.Check(); err != nil {
return fmt.Errorf("generator element of source %v: %v", s, err) return fmt.Errorf("generator element of source %v: %v", s.ID.URI, err)
} }
} }
if s.Icon != nil { if s.Icon != nil {
if err := s.Icon.Check(); err != nil { if err := s.Icon.Check(); err != nil {
return fmt.Errorf("icon element of source %v: %v", s, err) return fmt.Errorf("icon element of source %v: %v", s.ID.URI, err)
} }
} }
if s.ID != nil { if s.ID != nil {
if err := s.ID.Check(); err != nil { if err := s.ID.Check(); err != nil {
return fmt.Errorf("id element of source %v: %v", s, err) return fmt.Errorf("id element of source %v: %v", s.ID.URI, err)
} }
} }
for i, l := range s.Links { for i, l := range s.Links {
if err := l.Check(); err != nil { if err := l.Check(); err != nil {
return fmt.Errorf("link element %v of source %v: %v", i, s, err) return fmt.Errorf("link element %v of source %v: %v", i, s.ID.URI, err)
} }
} }
if s.Logo != nil { if s.Logo != nil {
if err := s.Logo.Check(); err != nil { if err := s.Logo.Check(); err != nil {
return fmt.Errorf("logo element of source %v: %v", s, err) return fmt.Errorf("logo element of source %v: %v", s.ID.URI, err)
} }
} }
if s.Rights != nil { if s.Rights != nil {
if err := s.Rights.Check(); err != nil { if err := s.Rights.Check(); err != nil {
return fmt.Errorf("rights element of source %v: %v", s, err) return fmt.Errorf("rights element of source %v: %v", s.ID.URI, err)
} }
} }
if s.Subtitle != nil { if s.Subtitle != nil {
if err := s.Subtitle.Check(); err != nil { if err := s.Subtitle.Check(); err != nil {
return fmt.Errorf("subtitle element of source %v: %v", s, err) return fmt.Errorf("subtitle element of source %v: %v", s.ID.URI, err)
} }
} }
if s.Title != nil { if s.Title != nil {
if err := s.Title.Check(); err != nil { if err := s.Title.Check(); err != nil {
return fmt.Errorf("title element of source %v: %v", s, err) return fmt.Errorf("title element of source %v: %v", s.ID.URI, err)
} }
} }
if s.Updated != nil { if s.Updated != nil {
if err := s.Updated.Check(); err != nil { if err := s.Updated.Check(); err != nil {
return fmt.Errorf("updated element of source %v: %v", s, err) return fmt.Errorf("updated element of source %v: %v", s.ID.URI, err)
} }
} }
for i, e := range s.Extensions { for i, e := range s.Extensions {
if err := e.Check(); err != nil { if err := e.Check(); err != nil {
return fmt.Errorf("extension element %v of source %v: %v", i, s, err) return fmt.Errorf("extension element %v of source %v: %v", i, s.ID.URI, err)
} }
} }

26
text.go
View File

@ -1,25 +1,31 @@
package atom package atom
import "html" import (
"fmt"
"html"
)
type Text interface { type Text interface {
isText() bool isText() bool
Check() error Check() error
} }
// NewText creates a new Text. It takes in the strings textType and content and // NewText creates a new Text. It returns a Text and an error.
// returns a Text. func NewText(textType, content string) (Text, error) {
//
// If textType is invalid it returns nil.
func NewText(textType, content string) Text {
switch textType { switch textType {
case "text", "": case "text", "":
return newPlainText(textType, content) return &PlainText{Type: textType, Text: content}, nil
case "html": case "html":
return newPlainText(textType, html.UnescapeString(content)) return &PlainText{Type: textType, Text: html.UnescapeString(content)}, nil
case "xhtml": case "xhtml":
return newXHTMLText(textType, content) return &XHTMLText{
Type: textType,
XHTMLDiv: &XHTMLDiv{
XMLNS: "http://www.w3.org/1999/xhtml",
Content: content,
},
}, nil
default: default:
return nil return nil, fmt.Errorf("%v is not a valid text type", textType)
} }
} }

View File

@ -2,7 +2,7 @@ package atom
import ( import (
"encoding/xml" "encoding/xml"
"fmt" "errors"
) )
type XHTMLDiv struct { type XHTMLDiv struct {
@ -11,8 +11,7 @@ type XHTMLDiv struct {
Content string `xml:",innerxml"` Content string `xml:",innerxml"`
} }
// NewXHTMLDiv creates a new XHTMLDiv. It takes in a string content and returns // NewXHTMLDiv creates a new XHTMLDiv. It returns a *XHTMLDiv.
// a *XHTMLDiv.
func NewXHTMLDiv(content string) *XHTMLDiv { func NewXHTMLDiv(content string) *XHTMLDiv {
return &XHTMLDiv{ return &XHTMLDiv{
XMLNS: "http://www.w3.org/1999/xhtml", XMLNS: "http://www.w3.org/1999/xhtml",
@ -24,7 +23,7 @@ func NewXHTMLDiv(content string) *XHTMLDiv {
// error. // error.
func (x *XHTMLDiv) Check() error { func (x *XHTMLDiv) Check() error {
if x.XMLNS != "http://www.w3.org/1999/xhtml" { if x.XMLNS != "http://www.w3.org/1999/xhtml" {
return fmt.Errorf("xmlns attribute of xhtml text %v must be http://www.w3.org/1999/xhtml", x) return errors.New("xmlns attribute of xhtml text must be http://www.w3.org/1999/xhtml")
} }
return nil return nil

View File

@ -1,6 +1,7 @@
package atom package atom
import ( import (
"errors"
"fmt" "fmt"
) )
@ -13,21 +14,11 @@ type XHTMLText struct {
// isText checks whether the XHTMLText is a Text. It returns a bool. // isText checks whether the XHTMLText is a Text. It returns a bool.
func (x *XHTMLText) isText() bool { return true } func (x *XHTMLText) isText() bool { return true }
// newPlainText creates a new PlainText. It takes in the strings textType and
// content and returns a *PlainText.
func newXHTMLText(textType, content string) *XHTMLText {
return &XHTMLText{
CommonAttributes: NewCommonAttributes(),
Type: textType,
XHTMLDiv: NewXHTMLDiv(content),
}
}
// Check checks the XHTMLText for incompatibilities with RFC4287. It returns an // Check checks the XHTMLText for incompatibilities with RFC4287. It returns an
// error. // error.
func (x *XHTMLText) Check() error { func (x *XHTMLText) Check() error {
if x.Type != "xhtml" { if x.Type != "xhtml" {
return fmt.Errorf("type attribute of xhtml text %v must be xhtml", x) return errors.New("type attribute of xhtml text must be xhtml")
} }
if err := x.XHTMLDiv.Check(); err != nil { if err := x.XHTMLDiv.Check(); err != nil {