jx/internal/folio/parseduri.go

103 lines
2.1 KiB
Go
Raw Normal View History

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package folio
import (
"net/url"
"decl/internal/transport"
"decl/internal/data"
"decl/internal/identifier"
"strings"
)
var (
)
type ParsedURI url.URL
func NewParsedURI(uri URI) *ParsedURI {
return uri.Parse().(*ParsedURI)
}
func CastParsedURI(u *url.URL) *ParsedURI {
return (*ParsedURI)(u)
}
func (uri *ParsedURI) URL() *url.URL {
return (*url.URL)(uri)
}
func (uri *ParsedURI) Hostname() string {
return (*url.URL)(uri).Hostname()
}
func (uri *ParsedURI) NewResource(document data.Document) (newResource data.Resource, err error) {
if document == nil {
declaration := NewDeclaration()
if err = declaration.NewResourceFromParsedURI(uri); err == nil {
return declaration.Attributes, err
}
} else {
newResource, err = document.NewResourceFromParsedURI(uri)
return
}
return
}
func (uri *ParsedURI) ConstructResource(res data.Resource) (err error) {
if ri, ok := res.(data.ResourceInitializer); ok {
err = ri.Init(uri)
} else {
err = res.SetParsedURI(uri)
}
return
}
func (uri *ParsedURI) Converter() (converter data.Converter, err error) {
return DocumentRegistry.ConverterTypes.NewFromParsedURI(uri.URL())
}
func (uri *ParsedURI) Exists() bool {
return transport.Exists(uri.URL())
}
func (uri *ParsedURI) ContentReaderStream() (*transport.Reader, error) {
return transport.NewReader(uri.URL())
}
func (uri *ParsedURI) ContentWriterStream() (*transport.Writer, error) {
return transport.NewWriter(uri.URL())
}
func (uri *ParsedURI) String() string {
return uri.URL().String()
}
func (uri *ParsedURI) SetURL(url *url.URL) {
*uri = *(*ParsedURI)(url)
}
func (uri *ParsedURI) Extension() (string, string) {
return (identifier.ID)(uri.Path).Extension()
}
func (uri *ParsedURI) ContentType() string {
var ext strings.Builder
exttype, fileext := uri.Extension()
if fileext == "" {
return exttype
}
ext.WriteString(exttype)
ext.WriteRune('.')
ext.WriteString(fileext)
return ext.String()
}
func (uri *ParsedURI) IsEmpty() bool {
if uri == nil || (identifier.ID)(uri.String()).IsEmpty() {
return true
}
return false
}