Skip to content

Commit

Permalink
fix validation errors in crossrefxml writer
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed May 13, 2024
1 parent bdd0c79 commit c531fe4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 47 deletions.
16 changes: 2 additions & 14 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ commonmeta 10.5555/12345678`,
var err error
var data commonmeta.Data

loginID, _ := cmd.Flags().GetString("login_id")
loginPassword, _ := cmd.Flags().GetString("login_passwd")
// loginID, _ := cmd.Flags().GetString("login_id")
// loginPassword, _ := cmd.Flags().GetString("login_passwd")
depositor, _ := cmd.Flags().GetString("depositor")
email, _ := cmd.Flags().GetString("email")
registrant, _ := cmd.Flags().GetString("registrant")
Expand Down Expand Up @@ -131,18 +131,6 @@ commonmeta 10.5555/12345678`,

if to == "crossrefxml" {
fmt.Printf("%s\n", output)

if loginID != "" && loginPassword != "" {
account := crossrefxml.Account{
LoginID: loginID,
LoginPassword: loginPassword,
}
message, err := crossrefxml.Upload(output, account)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%s\n", message)
}
} else {
var out bytes.Buffer
json.Indent(&out, output, "", " ")
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var versionCmd = &cobra.Command{
Short: "Print the version number of commonmeta",
Long: `All software has versions. This is commonmeta's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Commonmeta v0.2.15 -- HEAD")
fmt.Println("Commonmeta v0.3 -- HEAD")
},
}

Expand Down
5 changes: 3 additions & 2 deletions crossrefxml/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ type OriginalLanguageTitle struct {

type P struct {
XMLName xml.Name `xml:"p"`
Xmlns string `xml:"xmlns,attr"`
Xmlns string `xml:"xmlns,attr,omitempty"`
Text string `xml:",chardata"`
}

Expand Down Expand Up @@ -527,11 +527,12 @@ type ProceedingsMetadata struct {

type Program struct {
XMLName xml.Name `xml:"program"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
Fr string `xml:"fr,attr,omitempty"`
Name string `xml:"name,attr,omitempty"`
Ai string `xml:"ai,attr,omitempty"`
Rel string `xml:"rel,attr,omitempty"`
Text string `xml:",chardata"`
Assertion []Assertion `xml:"assertion"`
LicenseRef []LicenseRef `xml:"license_ref"`
RelatedItem []RelatedItem `xml:"related_item"`
Expand Down
96 changes: 66 additions & 30 deletions crossrefxml/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"encoding/xml"
"fmt"
"log"
"io"
"mime/multipart"
"net/http"
"net/url"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -35,10 +35,10 @@ type Head struct {

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"`
Xmlns string `xml:"xmlns,attr,omitempty"`
Version string `xml:"version,attr,omitempty"`
Xsi string `xml:"xsi,attr,omitempty"`
SchemaLocation string `xml:"schemaLocation,attr,omitempty"`
Head Head `xml:"head"`
Body []Crossref `xml:"body"`
}
Expand Down Expand Up @@ -83,9 +83,13 @@ func Convert(data commonmeta.Data) (Crossref, error) {
if len(data.Descriptions) > 0 {
for _, description := range data.Descriptions {
if description.Type == "Abstract" {
p := []P{}
p = append(p, P{
Text: description.Description,
})
abstract = append(abstract, Abstract{
Xmlns: "http://www.ncbi.nlm.nih.gov/JATS1",
Text: description.Description,
P: p,
})
}
}
Expand All @@ -101,17 +105,20 @@ func Convert(data commonmeta.Data) (Crossref, error) {
institution := []Institution{}
for _, a := range contributor.Affiliations {
if a.Name != "" {
institutionID := InstitutionID{}
if a.ID != "" {
institutionID = InstitutionID{
institutionID := &InstitutionID{
IDType: "ror",
Text: a.ID,
}
institution = append(institution, Institution{
InstitutionID: institutionID,
InstitutionName: a.Name,
})
} else {
institution = append(institution, Institution{
InstitutionName: a.Name,
})
}
institution = append(institution, Institution{
InstitutionID: &institutionID,
InstitutionName: a.Name,
})
}
}
affiliations := &Affiliations{
Expand All @@ -138,6 +145,10 @@ func Convert(data commonmeta.Data) (Crossref, error) {
})
if len(data.Files) > 0 {
for _, file := range data.Files {
if file.MimeType == "text/markdown" {
// Crossref schema currently doesn't support text/markdown
file.MimeType = "text/plain"
}
items = append(items, Item{
Resource: Resource{
Text: file.URL,
Expand Down Expand Up @@ -207,6 +218,7 @@ func Convert(data commonmeta.Data) (Crossref, error) {
}
program = append(program, &Program{
Name: "fundref",
Xmlns: "http://www.crossref.org/fundref.xsd",
Assertion: assertion,
})
}
Expand All @@ -223,6 +235,7 @@ func Convert(data commonmeta.Data) (Crossref, error) {
})
program = append(program, &Program{
Name: "AccessIndicators",
Xmlns: "http://www.crossref.org/AccessIndicators.xsd",
LicenseRef: licenseRef,
})
}
Expand All @@ -234,8 +247,9 @@ func Convert(data commonmeta.Data) (Crossref, error) {
identifierType = "uri"
}
if slices.Contains(InterWorkRelationTypes, relation.Type) && id != "" {
// Crossref relation types are camel case rather than pascal case
interWorkRelation := &InterWorkRelation{
RelationshipType: relation.Type,
RelationshipType: utils.CamelCaseString(relation.Type),
IdentifierType: strings.ToLower(identifierType),
Text: id,
}
Expand All @@ -258,6 +272,7 @@ func Convert(data commonmeta.Data) (Crossref, error) {
}
program = append(program, &Program{
Name: "relations",
Xmlns: "http://www.crossref.org/relations.xsd",
RelatedItem: relatedItem,
})
}
Expand Down Expand Up @@ -355,17 +370,16 @@ func Write(data commonmeta.Data, account Account) ([]byte, []gojsonschema.Result
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,
Xmlns: "http://www.crossref.org/schema/5.3.1",
Version: "5.3.1",
Head: head,
Body: body,
}

output, err := xml.MarshalIndent(doiBatch, "", " ")
if err == nil {
fmt.Println(err)
// TODO: handle error
// fmt.Println(err)
}
output = []byte(xml.Header + string(output))
return output, nil
Expand Down Expand Up @@ -409,18 +423,40 @@ func WriteAll(list []commonmeta.Data, account Account) ([]byte, []gojsonschema.R
return output, nil
}

func Upload(data commonmeta.Data, account Account) (string, error) {
output, jsErr := Write(data, account)
if jsErr != nil {
fmt.Println(jsErr)
func Upload(content []byte, account Account) (string, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}
bytes.NewReader(output)
resp, err := http.PostForm("https://doi.crossref.org/servlet/deposit",
url.Values{"operation": {"doMDUpload"}, "login_id": {account.LoginID}, "login_passwd": {account.LoginPassword}})
postUrl := "https://doi.crossref.org/servlet/deposit"

var b bytes.Buffer
w := multipart.NewWriter(&b)
part, _ := w.CreateFormFile("fname", "output.xml")
_, err := part.Write(content)
if err != nil {
return "", err
}
w.WriteField("operation", "doMDUpload")
w.WriteField("login_id", account.LoginID)
w.WriteField("login_passwd", account.LoginPassword)
defer w.Close()

req, err := http.NewRequest(http.MethodPost, postUrl, &b)
req.Header.Add("Content-Type", w.FormDataContentType())
if err != nil {
log.Fatalln(err)
return "", err
}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error uploading batch", err)
return "", err
}
defer resp.Body.Close()
message := "Your batch submission was successfully received."
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
fmt.Println(string(body))
message := "Your batch submission was successfully received. " + resp.Status
return message, nil
}
5 changes: 5 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@ func CamelCaseToWords(str string) string {
return strings.ToUpper(words[:1]) + strings.ToLower(words[1:])
}

// CamelCaseString converts a pascal case string to camel case
func CamelCaseString(str string) string {
return strings.ToLower(str[:1]) + str[1:]
}

func EncodeDOI(prefix string) string {
suffix := crockford.Generate(10, 5, true)
return fmt.Sprintf("https://doi.org/%s/%s", prefix, suffix)
Expand Down

0 comments on commit c531fe4

Please sign in to comment.