module Data.RDF.Namespace(
Namespace(..), mkPlainNS, mkPrefixedNS, mkPrefixedNS',
PrefixMapping(PrefixMapping), PrefixMappings(PrefixMappings), toPMList,
mergePrefixMappings,
mkUri,
prefixOf, uriOf,
rdf, rdfs, dc, dct, owl, xsd, skos, foaf, ex, ex2, standard_ns_mappings
)
where
import Data.RDF.Utils
import Text.Printf
import Data.Map(Map)
import qualified Data.Map as Map
import qualified Data.List as List
import Data.ByteString.Lazy.Char8(ByteString)
import qualified Data.ByteString.Lazy.Char8 as B
standard_namespaces :: [Namespace]
standard_namespaces = [rdf, rdfs, dc, dct, owl, xsd, skos, foaf, ex, ex2]
standard_ns_mappings :: PrefixMappings
standard_ns_mappings = PrefixMappings $ Map.fromList $
map (\(PrefixedNS pre uri) -> (pre, uri)) standard_namespaces
rdf :: Namespace
rdf = mkPrefixedNS' "rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
rdfs :: Namespace
rdfs = mkPrefixedNS' "rdfs" "http://www.w3.org/2000/01/rdf-schema#"
dc :: Namespace
dc = mkPrefixedNS' "dc" "http://purl.org/dc/elements/1.1/"
dct :: Namespace
dct = mkPrefixedNS' "dct" "http://purl.org/dc/terms/"
owl :: Namespace
owl = mkPrefixedNS' "owl" "http://www.w3.org/2002/07/owl#"
xsd :: Namespace
xsd = mkPrefixedNS' "xsd" "http://www.w3.org/2001/XMLSchema#"
skos :: Namespace
skos = mkPrefixedNS' "skos" "http://www.w3.org/2004/02/skos/core#"
foaf :: Namespace
foaf = mkPrefixedNS' "foaf" "http://xmlns.com/foaf/0.1/"
ex :: Namespace
ex = mkPrefixedNS' "ex" "http://www.example.org/"
ex2 :: Namespace
ex2 = mkPrefixedNS' "ex2" "http://www2.example.org/"
newtype PrefixMappings = PrefixMappings (Map ByteString ByteString)
deriving (Eq, Ord)
instance Show PrefixMappings where
show (PrefixMappings pmap) = printf "PrefixMappings [%s]" mappingsStr
where showPM = show . PrefixMapping
mappingsStr = List.intercalate ", " (map showPM (Map.toList pmap))
mergePrefixMappings :: PrefixMappings -> PrefixMappings -> PrefixMappings
mergePrefixMappings (PrefixMappings p1s) (PrefixMappings p2s) =
PrefixMappings $ Map.union p1s p2s
toPMList :: PrefixMappings -> [(ByteString, ByteString)]
toPMList (PrefixMappings m) = Map.toList m
newtype PrefixMapping = PrefixMapping (ByteString, ByteString)
deriving (Eq, Ord)
instance Show PrefixMapping where
show (PrefixMapping (prefix, uri)) = printf "PrefixMapping (%s, %s)" (b2s prefix) (b2s uri)
mkUri :: Namespace -> ByteString -> ByteString
mkUri ns local = uriOf ns `B.append` local
data Namespace = PrefixedNS ByteString ByteString
| PlainNS ByteString
mkPlainNS :: ByteString -> Namespace
mkPlainNS = PlainNS
mkPrefixedNS :: ByteString -> ByteString -> Namespace
mkPrefixedNS = PrefixedNS
mkPrefixedNS' :: String -> String -> Namespace
mkPrefixedNS' s1 s2 = mkPrefixedNS (B.pack s1) (B.pack s2)
instance Eq Namespace where
(PrefixedNS _ u1) == (PrefixedNS _ u2) = u1 == u2
(PlainNS u1) == (PlainNS u2) = u1 == u2
(PrefixedNS _ u1) == (PlainNS u2) = u1 == u2
(PlainNS u1) == (PrefixedNS _ u2) = u1 == u2
instance Show Namespace where
show (PlainNS uri) = B.unpack uri
show (PrefixedNS prefix uri) = printf "(PrefixNS %s %s)" (B.unpack prefix) (B.unpack uri)
uriOf :: Namespace -> ByteString
uriOf (PlainNS uri) = uri
uriOf (PrefixedNS _ uri) = uri
prefixOf :: Namespace -> Maybe ByteString
prefixOf (PlainNS _) = Nothing
prefixOf (PrefixedNS p _) = Just p