{-# 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 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, forall a. Show a => a -> String
show URI
b1) forall a. Ord a => a -> a -> Ordering
`compare` (Maybe Text
a2, forall a. Show a => a -> String
show URI
b2)
instance Show Namespace where
show :: Namespace -> String
show (Namespace (Just Text
p) URI
u) = forall a. Show a => a -> String
show Text
p forall a. [a] -> [a] -> [a]
++ String
":<" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show URI
u forall a. [a] -> [a] -> [a]
++ String
">"
show (Namespace Maybe Text
_ URI
u) = String
"<" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show URI
u 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) =
forall a. Monoid a => [a] -> a
mconcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Text -> Builder
B.fromText
[ Text
"@prefix ", forall a. a -> Maybe a -> a
fromMaybe Text
"" Maybe Text
pre, Text
": <", String -> Text
T.pack (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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopedName -> Namespace
getScopeNamespace
getScopeURI :: ScopedName -> URI
getScopeURI :: ScopedName -> URI
getScopeURI = Namespace -> URI
getNamespaceURI forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopedName -> Namespace
getScopeNamespace
instance IsString ScopedName where
fromString :: String -> ScopedName
fromString String
s =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. HasCallStack => String -> a
error (String
"Unable to convert " forall a. [a] -> [a] -> [a]
++ String
s 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 forall a. Eq a => a -> a -> Bool
== ScopedName -> QName
getQName ScopedName
sn2
instance Ord ScopedName where
compare :: ScopedName -> ScopedName -> Ordering
compare = 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 forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [Text
pre, Text
":", LName -> Text
getLName LName
l]
Maybe Text
_ -> 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 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 forall a. Eq a => a -> a -> Bool
== 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 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