{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Swish.RDF.Vocabulary
(
namespaceRDFD
, namespaceXsdType
, namespaceMATH
, namespaceLOG
, namespaceDAML
, namespaceDefault
, namespaceSwish
, scopeRDF
, scopeRDFS
, scopeRDFD
, LanguageTag
, toLangTag
, fromLangTag
, isBaseLang
, swishName
, rdfdGeneralRestriction
, rdfdOnProperties, rdfdConstraint, rdfdMaxCardinality
, logImplies
, defaultBase
, module Swish.RDF.Vocabulary.RDF
, module Swish.RDF.Vocabulary.OWL
, module Swish.RDF.Vocabulary.XSD
)
where
import Swish.Namespace (Namespace, ScopedName, makeNamespace, makeNSScopedName)
import Swish.QName (LName, getLName)
import Swish.RDF.Vocabulary.RDF
import Swish.RDF.Vocabulary.OWL
import Swish.RDF.Vocabulary.XSD
import Control.Monad (guard)
import Data.Char (isDigit, isAsciiLower)
import Data.List (isPrefixOf)
import Data.List.NonEmpty (NonEmpty(..))
#if (!defined(__GLASGOW_HASKELL__)) || (__GLASGOW_HASKELL__ < 710)
import Data.Monoid (mappend, mconcat)
#endif
import Data.Maybe (fromJust, fromMaybe)
import Data.String (IsString(..))
import Network.URI (URI, parseURI)
import qualified Data.List.NonEmpty as NE
import qualified Data.Text as T
toNS :: T.Text -> T.Text -> Namespace
toNS :: Text -> Text -> Namespace
toNS Text
p Text
utxt =
let ustr :: String
ustr = Text -> String
T.unpack Text
utxt
uri :: URI
uri = URI -> Maybe URI -> URI
forall a. a -> Maybe a -> a
fromMaybe (String -> URI
forall a. HasCallStack => String -> a
error (String
"Unable to convert " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ustr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" to a URI")) (Maybe URI -> URI) -> Maybe URI -> URI
forall a b. (a -> b) -> a -> b
$
String -> Maybe URI
parseURI String
ustr
in Maybe Text -> URI -> Namespace
makeNamespace (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
p) URI
uri
toNSU :: T.Text -> URI -> Namespace
toNSU :: Text -> URI -> Namespace
toNSU Text
p = Maybe Text -> URI -> Namespace
makeNamespace (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
p)
namespaceXsdType ::
LName
-> Namespace
namespaceXsdType :: LName -> Namespace
namespaceXsdType LName
lbl =
let dtn :: Text
dtn = LName -> Text
getLName LName
lbl
in Text -> Text -> Namespace
toNS (Text
"xsd_" Text -> Text -> Text
forall a. Monoid a => a -> a -> a
`mappend` Text
dtn)
([Text] -> Text
forall a. Monoid a => [a] -> a
mconcat [Text
"http://id.ninebynine.org/2003/XMLSchema/", Text
dtn, Text
"#"])
namespaceRDFD :: Namespace
namespaceRDFD :: Namespace
namespaceRDFD = Text -> URI -> Namespace
toNSU Text
"rdfd" URI
namespaceRDFDURI
namespaceMATH :: Namespace
namespaceMATH :: Namespace
namespaceMATH = Text -> Text -> Namespace
toNS Text
"math" Text
"http://www.w3.org/2000/10/swap/math#"
namespaceLOG :: Namespace
namespaceLOG :: Namespace
namespaceLOG = Text -> URI -> Namespace
toNSU Text
"log" URI
namespaceLOGURI
namespaceDAML :: Namespace
namespaceDAML :: Namespace
namespaceDAML = Text -> Text -> Namespace
toNS Text
"daml" Text
"http://www.daml.org/2000/10/daml-ont#"
namespaceSwish :: Namespace
namespaceSwish :: Namespace
namespaceSwish = Text -> URI -> Namespace
toNSU Text
"swish" URI
namespaceSwishURI
namespaceDefault :: Namespace
namespaceDefault :: Namespace
namespaceDefault = Text -> URI -> Namespace
toNSU Text
"default" URI
namespaceDefaultURI
tU :: String -> URI
tU :: String -> URI
tU = URI -> Maybe URI -> URI
forall a. a -> Maybe a -> a
fromMaybe (String -> URI
forall a. HasCallStack => String -> a
error String
"Internal error processing namespace URI") (Maybe URI -> URI) -> (String -> Maybe URI) -> String -> URI
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe URI
parseURI
namespaceRDFDURI,
namespaceLOGURI,
namespaceSwishURI,
namespaceDefaultURI :: URI
namespaceRDFDURI :: URI
namespaceRDFDURI = String -> URI
tU String
"http://id.ninebynine.org/2003/rdfext/rdfd#"
namespaceLOGURI :: URI
namespaceLOGURI = String -> URI
tU String
"http://www.w3.org/2000/10/swap/log#"
namespaceSwishURI :: URI
namespaceSwishURI = String -> URI
tU String
"http://id.ninebynine.org/2003/Swish/"
namespaceDefaultURI :: URI
namespaceDefaultURI = String -> URI
tU String
"http://id.ninebynine.org/default/"
swishName :: LName -> ScopedName
swishName :: LName -> ScopedName
swishName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceSwish
data LanguageTag =
LanguageTag T.Text (NonEmpty T.Text)
instance Show LanguageTag where
show :: LanguageTag -> String
show = Text -> String
T.unpack (Text -> String) -> (LanguageTag -> Text) -> LanguageTag -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LanguageTag -> Text
fromLangTag
instance IsString LanguageTag where
fromString :: String -> LanguageTag
fromString = Maybe LanguageTag -> LanguageTag
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe LanguageTag -> LanguageTag)
-> (String -> Maybe LanguageTag) -> String -> LanguageTag
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe LanguageTag
toLangTag (Text -> Maybe LanguageTag)
-> (String -> Text) -> String -> Maybe LanguageTag
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack
instance Eq LanguageTag where
LanguageTag Text
_ NonEmpty Text
t1 == :: LanguageTag -> LanguageTag -> Bool
== LanguageTag Text
_ NonEmpty Text
t2 = NonEmpty Text
t1 NonEmpty Text -> NonEmpty Text -> Bool
forall a. Eq a => a -> a -> Bool
== NonEmpty Text
t2
instance Ord LanguageTag where
LanguageTag Text
_ NonEmpty Text
t1 compare :: LanguageTag -> LanguageTag -> Ordering
`compare` LanguageTag Text
_ NonEmpty Text
t2 = NonEmpty Text
t1 NonEmpty Text -> NonEmpty Text -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` NonEmpty Text
t2
toLangTag :: T.Text -> Maybe LanguageTag
toLangTag :: Text -> Maybe LanguageTag
toLangTag Text
lbl = do
let tag :: Text
tag = Text -> Text
T.toLower Text
lbl
toks :: [Text]
toks = (Char -> Bool) -> Text -> [Text]
T.split (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-') Text
tag
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard ((Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\Text
s -> let l :: Int
l = Text -> Int
T.length Text
s in Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
9) [Text]
toks)
case [Text]
toks of
Text
primtag : [Text]
subtags -> do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard ((Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isAsciiLower Text
primtag Bool -> Bool -> Bool
&& (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Char -> Bool) -> Text -> Bool
T.all (\Char
c -> Char -> Bool
isAsciiLower Char
c Bool -> Bool -> Bool
|| Char -> Bool
isDigit Char
c)) [Text]
subtags)
LanguageTag -> Maybe LanguageTag
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LanguageTag -> Maybe LanguageTag)
-> LanguageTag -> Maybe LanguageTag
forall a b. (a -> b) -> a -> b
$ Text -> NonEmpty Text -> LanguageTag
LanguageTag Text
lbl ([Text] -> NonEmpty Text
forall a. HasCallStack => [a] -> NonEmpty a
NE.fromList [Text]
toks)
[] -> Maybe LanguageTag
forall a. Maybe a
Nothing
fromLangTag :: LanguageTag -> T.Text
fromLangTag :: LanguageTag -> Text
fromLangTag (LanguageTag Text
f NonEmpty Text
_) = Text
f
isBaseLang ::
LanguageTag
-> LanguageTag
-> Bool
isBaseLang :: LanguageTag -> LanguageTag -> Bool
isBaseLang (LanguageTag Text
_ (Text
a :| [Text]
as))
(LanguageTag Text
_ (Text
b :| [Text]
bs))
| Text
a Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
b = [Text]
as [Text] -> [Text] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` [Text]
bs
| Bool
otherwise = Bool
False
scopeRDF :: Namespace
scopeRDF :: Namespace
scopeRDF = Text -> Text -> Namespace
toNS Text
"rs_rdf" Text
"http://id.ninebynine.org/2003/Ruleset/rdf#"
scopeRDFS :: Namespace
scopeRDFS :: Namespace
scopeRDFS = Text -> Text -> Namespace
toNS Text
"rs_rdfs" Text
"http://id.ninebynine.org/2003/Ruleset/rdfs#"
scopeRDFD :: Namespace
scopeRDFD :: Namespace
scopeRDFD = Text -> Text -> Namespace
toNS Text
"rs_rdfd" Text
"http://id.ninebynine.org/2003/Ruleset/rdfd#"
toRDFD :: LName -> ScopedName
toRDFD :: LName -> ScopedName
toRDFD = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceRDFD
rdfdGeneralRestriction :: ScopedName
rdfdGeneralRestriction :: ScopedName
rdfdGeneralRestriction = LName -> ScopedName
toRDFD LName
"GeneralRestriction"
rdfdOnProperties :: ScopedName
rdfdOnProperties :: ScopedName
rdfdOnProperties = LName -> ScopedName
toRDFD LName
"onProperties"
rdfdConstraint :: ScopedName
rdfdConstraint :: ScopedName
rdfdConstraint = LName -> ScopedName
toRDFD LName
"constraint"
rdfdMaxCardinality :: ScopedName
rdfdMaxCardinality :: ScopedName
rdfdMaxCardinality = LName -> ScopedName
toRDFD LName
"maxCardinality"
logImplies :: ScopedName
logImplies :: ScopedName
logImplies = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceLOG LName
"implies"
defaultBase :: ScopedName
defaultBase :: ScopedName
defaultBase = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceDefault LName
"base"