github.com/boki/go-xmp@v1.0.1/models/dc/model.go (about)

     1  // Copyright (c) 2017-2018 Alexander Eichhorn
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  // Package dc implements Dublin Core metadata as defined in XMP Specification Part 1.
    16  package dc
    17  
    18  import (
    19  	"fmt"
    20  	"trimmer.io/go-xmp/xmp"
    21  )
    22  
    23  var (
    24  	NsDc = xmp.NewNamespace("dc", "http://purl.org/dc/elements/1.1/", NewModel)
    25  )
    26  
    27  func init() {
    28  	xmp.Register(NsDc, xmp.XmpMetadata)
    29  }
    30  
    31  func NewModel(name string) xmp.Model {
    32  	return &DublinCore{}
    33  }
    34  
    35  func MakeModel(d *xmp.Document) (*DublinCore, error) {
    36  	m, err := d.MakeModel(NsDc)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	x, _ := m.(*DublinCore)
    41  	return x, nil
    42  }
    43  
    44  func FindModel(d *xmp.Document) *DublinCore {
    45  	if m := d.FindModel(NsDc); m != nil {
    46  		return m.(*DublinCore)
    47  	}
    48  	return nil
    49  }
    50  
    51  // Note: differences from vanilla Dublin Core are
    52  // - Date is an array instead of a simple string
    53  // - Kanguage is an array instead of a simple string
    54  type DublinCore struct {
    55  	Contributor xmp.StringArray `xmp:"dc:contributor"`
    56  	Coverage    string          `xmp:"dc:coverage"`
    57  	Creator     xmp.StringList  `xmp:"dc:creator"`
    58  	Date        xmp.DateList    `xmp:"dc:date"`
    59  	Description xmp.AltString   `xmp:"dc:description"`
    60  	Format      string          `xmp:"dc:format"`
    61  	Identifier  string          `xmp:"dc:identifier"`
    62  	Language    LocaleArray     `xmp:"dc:language"`
    63  	Publisher   xmp.StringArray `xmp:"dc:publisher"`
    64  	Relation    xmp.StringArray `xmp:"dc:relation"`
    65  	Rights      xmp.AltString   `xmp:"dc:rights"`
    66  	Source      string          `xmp:"dc:source"`
    67  	Subject     xmp.StringArray `xmp:"dc:subject"`
    68  	Title       xmp.AltString   `xmp:"dc:title"`
    69  	Type        xmp.StringArray `xmp:"dc:type"`
    70  }
    71  
    72  func (x DublinCore) Can(nsName string) bool {
    73  	return NsDc.GetName() == nsName
    74  }
    75  
    76  func (x DublinCore) Namespaces() xmp.NamespaceList {
    77  	return xmp.NamespaceList{NsDc}
    78  }
    79  
    80  func (x *DublinCore) SyncModel(d *xmp.Document) error {
    81  	return nil
    82  }
    83  
    84  func (x *DublinCore) SyncFromXMP(d *xmp.Document) error {
    85  	return nil
    86  }
    87  
    88  func (x DublinCore) SyncToXMP(d *xmp.Document) error {
    89  	return nil
    90  }
    91  
    92  func (x *DublinCore) CanTag(tag string) bool {
    93  	_, err := xmp.GetNativeField(x, tag)
    94  	return err == nil
    95  }
    96  
    97  func (x *DublinCore) GetTag(tag string) (string, error) {
    98  	if v, err := xmp.GetNativeField(x, tag); err != nil {
    99  		return "", fmt.Errorf("%s: %v", NsDc.GetName(), err)
   100  	} else {
   101  		return v, nil
   102  	}
   103  }
   104  
   105  func (x *DublinCore) SetTag(tag, value string) error {
   106  	if err := xmp.SetNativeField(x, tag, value); err != nil {
   107  		return fmt.Errorf("%s: %v", NsDc.GetName(), err)
   108  	}
   109  	return nil
   110  }