Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed May 12, 2024
1 parent c98b380 commit 5a3c7ce
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Commonmeta reads and/or writes these metadata formats:
| Format | Name | Content Type | Read | Write |
| ------------------------------------------------------------------------------------------------ | ------------- | -------------------------------------- | ------- | ------- |
| [Commonmeta](https://docs.commonmeta.org) | commonmeta | application/vnd.commonmeta+json | yes | yes |
| [CrossRef XML](https://www.crossref.org/schema/documentation/unixref1.1/unixref1.1.html) | crossrefxml | application/vnd.crossref.unixref+xml | yes | later |
| [CrossRef XML](https://www.crossref.org/schema/documentation/unixref1.1/unixref1.1.html) | crossrefxml | application/vnd.crossref.unixref+xml | yes | yes |
| [Crossref](https://api.crossref.org) | crossref | application/vnd.crossref+json | yes | n/a |
| [DataCite](https://api.datacite.org/) | datacite | application/vnd.datacite.datacite+json | yes | yes |
| [Schema.org (in JSON-LD)](http://schema.org/) | schemaorg | application/vnd.schemaorg.ld+json | later | yes |
Expand Down
3 changes: 0 additions & 3 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,4 @@ commonmeta 10.5555/12345678`,

func init() {
rootCmd.AddCommand(convertCmd)

convertCmd.PersistentFlags().StringP("from", "f", "", "the format to convert from")
convertCmd.PersistentFlags().StringP("to", "t", "commonmeta", "the format to convert to")
}
22 changes: 19 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ var listCmd = &cobra.Command{
hasArchive, _ := cmd.Flags().GetBool("has-archive")
sample := false

depositor, _ := cmd.Flags().GetString("depositor")
email, _ := cmd.Flags().GetString("email")
registrant, _ := cmd.Flags().GetString("registrant")

if input != "" {
_, err = os.Stat(input)
if err != nil {
Expand Down Expand Up @@ -93,16 +97,28 @@ var listCmd = &cobra.Command{
output, jsErr = csl.WriteAll(data)
} else if to == "datacite" {
output, jsErr = datacite.WriteAll(data)
} else if to == "crossrefxml" {
account := crossrefxml.Account{
Depositor: depositor,
Email: email,
Registrant: registrant,
}
output, jsErr = crossrefxml.WriteAll(data, account)
} else if to == "schemaorg" {
output, jsErr = schemaorg.WriteAll(data)
}

if err != nil {
fmt.Println(err)
}
var out bytes.Buffer
json.Indent(&out, output, "", " ")
fmt.Println(out.String())

if to == "crossrefxml" {
fmt.Printf("%s\n", output)
} else {
var out bytes.Buffer
json.Indent(&out, output, "", " ")
fmt.Println(out.String())
}

if jsErr != nil {
fmt.Println(jsErr)
Expand Down
90 changes: 65 additions & 25 deletions crossrefxml/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ import (

type StringMap map[string]string

type Depositor struct {
DepositorName string `xml:"depositor_name"`
Email string `xml:"email_address"`
}

type Head struct {
DOIBatchID string `xml:"doi_batch_id"`
Timestamp string `xml:"timestamp"`
Depositor Depositor `xml:"depositor"`
Registrant string `xml:"registrant"`
}

type DOIBatch struct {
XMLName xml.Name `xml:"doi_batch"`
Xmlns string `xml:"xmlns,attr"`
Version string `xml:"version,attr"`
Xsi string `xml:"xsi,attr"`
SchemaLocation string `xml:"schemaLocation,attr"`
Head Head `xml:"head"`
Body []Crossref `xml:"body"`
}

type Account struct {
Depositor string `xml:"depositor"`
Email string `xml:"email"`
Expand Down Expand Up @@ -49,8 +71,8 @@ var CMToCRMappings = map[string]string{
}

// Convert converts Commonmeta metadata to Crossrefxml metadata
func Convert(data commonmeta.Data) (*Crossref, error) {
c := &Crossref{}
func Convert(data commonmeta.Data) (Crossref, error) {
c := Crossref{}
abstract := []Abstract{}
if len(data.Descriptions) > 0 {
for _, description := range data.Descriptions {
Expand Down Expand Up @@ -308,33 +330,13 @@ func Convert(data commonmeta.Data) (*Crossref, error) {

// Write writes Crossrefxml metadata.
func Write(data commonmeta.Data, account Account) ([]byte, []gojsonschema.ResultError) {
type Depositor struct {
DepositorName string `xml:"depositor_name"`
Email string `xml:"email_address"`
}

type Head struct {
DOIBatchID string `xml:"doi_batch_id"`
Timestamp string `xml:"timestamp"`
Depositor Depositor `xml:"depositor"`
Registrant string `xml:"registrant"`
}

type DOIBatch struct {
XMLName xml.Name `xml:"doi_batch"`
Xmlns string `xml:"xmlns,attr"`
Version string `xml:"version,attr"`
Xsi string `xml:"xsi,attr"`
SchemaLocation string `xml:"schemaLocation,attr"`
Head Head `xml:"head"`
Body Crossref `xml:"body"`
}

crossref, err := Convert(data)
if err != nil {
fmt.Println(err)
}

body := []Crossref{}
body = append(body, crossref)
depositor := Depositor{
DepositorName: account.Depositor,
Email: account.Email,
Expand All @@ -352,7 +354,7 @@ func Write(data commonmeta.Data, account Account) ([]byte, []gojsonschema.Result
Xsi: "http://www.w3.org/2001/XMLSchema-instance",
SchemaLocation: "http://www.crossref.org/schema/5.3.1 ",
Head: head,
Body: *crossref,
Body: body,
}

output, err := xml.MarshalIndent(doiBatch, "", " ")
Expand All @@ -362,3 +364,41 @@ func Write(data commonmeta.Data, account Account) ([]byte, []gojsonschema.Result
output = []byte(xml.Header + string(output))
return output, nil
}

// WriteAll writes a list of commonmeta metadata.
func WriteAll(list []commonmeta.Data, account Account) ([]byte, []gojsonschema.ResultError) {
var body []Crossref
for _, data := range list {
crossref, err := Convert(data)
if err != nil {
fmt.Println(err)
}
body = append(body, crossref)
}
depositor := Depositor{
DepositorName: account.Depositor,
Email: account.Email,
}
uuid, _ := uuid.NewRandom()
head := Head{
DOIBatchID: uuid.String(),
Timestamp: time.Now().Format(dateutils.CrossrefDateTimeFormat),
Depositor: depositor,
Registrant: account.Registrant,
}
doiBatch := DOIBatch{
Xmlns: "http://www.crossref.org/schema/5.3.1",
Version: "5.3.1",
Xsi: "http://www.w3.org/2001/XMLSchema-instance",
SchemaLocation: "http://www.crossref.org/schema/5.3.1 ",
Head: head,
Body: body,
}

output, _ := xml.MarshalIndent(doiBatch, "", " ")
// if err == nil {
// // fmt.Println(err)
// }
output = []byte(xml.Header + string(output))
return output, nil
}

0 comments on commit 5a3c7ce

Please sign in to comment.