{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Swish.Namespace
( Namespace
, makeNamespace, makeNamespaceQName
, getNamespacePrefix, getNamespaceURI, getNamespaceTuple
, ScopedName
, getScopeNamespace, getScopeLocal
, getScopePrefix, getScopeURI
, getQName, getScopedNameURI
, matchName
, makeScopedName
, makeQNameScopedName
, makeURIScopedName
, makeNSScopedName
, nullScopedName
, namespaceToBuilder
)
where
import Swish.QName (QName, LName, newQName, getLName, emptyLName, getQNameURI, getNamespace, getLocalName)
import Data.Maybe (fromMaybe)
#if (!defined(__GLASGOW_HASKELL__)) || (__GLASGOW_HASKELL__ < 710)
import Data.Monoid (Monoid(..))
#endif
import Data.Ord (comparing)
import Data.String (IsString(..))
import Network.URI (URI(..), parseURIReference, nullURI)
import qualified Data.Text as T
import qualified Data.Text.Lazy.Builder as B
data Namespace = Namespace (Maybe T.Text) URI
getNamespacePrefix :: Namespace -> Maybe T.Text
getNamespacePrefix :: Namespace -> Maybe Text
getNamespacePrefix (Namespace Maybe Text
p URI
_) = Maybe Text
p
getNamespaceURI :: Namespace -> URI
getNamespaceURI :: Namespace -> URI
getNamespaceURI (Namespace Maybe Text
_ URI
u) = URI
u
getNamespaceTuple :: Namespace -> (Maybe T.Text, URI)
getNamespaceTuple :: Namespace -> (Maybe Text, URI)
getNamespaceTuple (Namespace Maybe Text
p URI
u) = (Maybe Text
p, URI
u)
instance Eq Namespace where
(Namespace Maybe Text
_ URI
u1) == :: Namespace -> Namespace -> Bool
== (Namespace Maybe Text
_ URI
u2) = URI
u1 URI -> URI -> Bool
forall a. Eq a => a -> a -> Bool
== URI
u2
instance Ord Namespace where
(Namespace Maybe Text
a1 URI
b1) compare :: Namespace -> Namespace -> Ordering
`compare` (Namespace Maybe Text
a2 URI
b2) =
(Maybe Text
a1, URI -> String
forall a. Show a => a -> String
show URI
b1) (Maybe Text, String) -> (Maybe Text, String) -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` (Maybe Text
a2, URI -> String
forall a. Show a => a -> String
show URI
b2)
instance Show Namespace where
show :: Namespace -> String
show (Namespace (Just Text
p) URI
u) = Text -> String
forall a. Show a => a -> String
show Text
p String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
":<" String -> ShowS
forall a. [a] -> [a] -> [a]
++ URI -> String
forall a. Show a => a -> String
show URI
u String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
">"
show (Namespace Maybe Text
_ URI
u) = String
"<" String -> ShowS
forall a. [a] -> [a] -> [a]
++ URI -> String
forall a. Show a => a -> String
show URI
u String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
">"
makeNamespace ::
Maybe T.Text
-> URI
-> Namespace
makeNamespace :: Maybe Text -> URI -> Namespace
makeNamespace = Maybe Text -> URI -> Namespace
Namespace
makeNamespaceQName ::
Namespace
-> LName
-> QName
makeNamespaceQName :: Namespace -> LName -> QName
makeNamespaceQName (Namespace Maybe Text
_ URI
uri) = URI -> LName -> QName
newQName URI
uri
namespaceToBuilder :: Namespace -> B.Builder
namespaceToBuilder :: Namespace -> Builder
namespaceToBuilder (Namespace Maybe Text
pre URI
uri) =
[Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ([Builder] -> Builder) -> [Builder] -> Builder
forall a b. (a -> b) -> a -> b
$ (Text -> Builder) -> [Text] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Builder
B.fromText
[ Text
"@prefix ", Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" Maybe Text
pre, Text
": <", String -> Text
T.pack (URI -> String
forall a. Show a => a -> String
show URI
uri), Text
"> .\n"]
data ScopedName = ScopedName !QName Namespace LName
getScopeLocal :: ScopedName -> LName
getScopeLocal :: ScopedName -> LName
getScopeLocal (ScopedName QName
_ Namespace
_ LName
l) = LName
l
getScopeNamespace :: ScopedName -> Namespace
getScopeNamespace :: ScopedName -> Namespace
getScopeNamespace (ScopedName QName
_ Namespace
ns LName
_) = Namespace
ns
getScopePrefix :: ScopedName -> Maybe T.Text
getScopePrefix :: ScopedName -> Maybe Text
getScopePrefix = Namespace -> Maybe Text
getNamespacePrefix (Namespace -> Maybe Text)
-> (ScopedName -> Namespace) -> ScopedName -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopedName -> Namespace
getScopeNamespace
getScopeURI :: ScopedName -> URI
getScopeURI :: ScopedName -> URI
getScopeURI = Namespace -> URI
getNamespaceURI (Namespace -> URI)
-> (ScopedName -> Namespace) -> ScopedName -> URI
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopedName -> Namespace
getScopeNamespace
instance IsString ScopedName where
fromString :: String -> ScopedName
fromString String
s =
ScopedName -> (URI -> ScopedName) -> Maybe URI -> ScopedName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> ScopedName
forall a. HasCallStack => String -> a
error (String
"Unable to convert " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" into a ScopedName"))
URI -> ScopedName
makeURIScopedName (String -> Maybe URI
parseURIReference String
s)
instance Eq ScopedName where
ScopedName
sn1 == :: ScopedName -> ScopedName -> Bool
== ScopedName
sn2 = ScopedName -> QName
getQName ScopedName
sn1 QName -> QName -> Bool
forall a. Eq a => a -> a -> Bool
== ScopedName -> QName
getQName ScopedName
sn2
instance Ord ScopedName where
compare :: ScopedName -> ScopedName -> Ordering
compare = (ScopedName -> QName) -> ScopedName -> ScopedName -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing ScopedName -> QName
getQName
instance Show ScopedName where
show :: ScopedName -> String
show (ScopedName QName
qn Namespace
n LName
l) = case Namespace -> Maybe Text
getNamespacePrefix Namespace
n of
Just Text
pre -> Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat [Text
pre, Text
":", LName -> Text
getLName LName
l]
Maybe Text
_ -> QName -> String
forall a. Show a => a -> String
show QName
qn
getQName :: ScopedName -> QName
getQName :: ScopedName -> QName
getQName (ScopedName QName
qn Namespace
_ LName
_) = QName
qn
getScopedNameURI :: ScopedName -> URI
getScopedNameURI :: ScopedName -> URI
getScopedNameURI = QName -> URI
getQNameURI (QName -> URI) -> (ScopedName -> QName) -> ScopedName -> URI
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopedName -> QName
getQName
matchName :: String -> ScopedName -> Bool
matchName :: String -> ScopedName -> Bool
matchName String
str ScopedName
nam = String
str String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== ScopedName -> String
forall a. Show a => a -> String
show ScopedName
nam
makeScopedName ::
Maybe T.Text
-> URI
-> LName
-> ScopedName
makeScopedName :: Maybe Text -> URI -> LName -> ScopedName
makeScopedName Maybe Text
pre URI
nsuri LName
local =
QName -> Namespace -> LName -> ScopedName
ScopedName (URI -> LName -> QName
newQName URI
nsuri LName
local)
(Maybe Text -> URI -> Namespace
Namespace Maybe Text
pre URI
nsuri)
LName
local
makeQNameScopedName ::
Maybe T.Text
-> QName
-> ScopedName
makeQNameScopedName :: Maybe Text -> QName -> ScopedName
makeQNameScopedName Maybe Text
pre QName
qn = QName -> Namespace -> LName -> ScopedName
ScopedName QName
qn (Maybe Text -> URI -> Namespace
Namespace Maybe Text
pre (QName -> URI
getNamespace QName
qn)) (QName -> LName
getLocalName QName
qn)
makeURIScopedName :: URI -> ScopedName
makeURIScopedName :: URI -> ScopedName
makeURIScopedName URI
uri = Maybe Text -> URI -> LName -> ScopedName
makeScopedName Maybe Text
forall a. Maybe a
Nothing URI
uri LName
emptyLName
makeNSScopedName ::
Namespace
-> LName
-> ScopedName
makeNSScopedName :: Namespace -> LName -> ScopedName
makeNSScopedName Namespace
ns LName
local =
QName -> Namespace -> LName -> ScopedName
ScopedName (URI -> LName -> QName
newQName (Namespace -> URI
getNamespaceURI Namespace
ns) LName
local) Namespace
ns LName
local
nullScopedName :: ScopedName
nullScopedName :: ScopedName
nullScopedName = URI -> ScopedName
makeURIScopedName URI
nullURI