{- |
Module      :  ./OWL2/Translate.hs
Description :  translate string to OWL2 valid names
Copyright   :  (c) C. Maeder, DFKI GmbH 2012
License     :  GPLv2 or higher, see LICENSE.txt

Maintainer  :  Christian.Maeder@dfki.de
Stability   :  provisional
Portability :  portable
-}

module OWL2.Translate where

import Common.IRI
import Common.Id
import Common.ProofUtils

import Data.Char

import OWL2.Parse

-- now provided in Common.IRI
--idToIRI :: Id -> IRI
--idToIRI = idToAnonIRI False

idToAnonIRI :: Bool -> Id -> IRI
idToAnonIRI :: Bool -> Id -> IRI
idToAnonIRI = Int -> Bool -> Id -> IRI
idToAnonNumberedIRI (-1)

idToNumberedIRI :: Id -> Int -> IRI
idToNumberedIRI :: Id -> Int -> IRI
idToNumberedIRI i :: Id
i n :: Int
n = Int -> Bool -> Id -> IRI
idToAnonNumberedIRI Int
n Bool
False Id
i

idToAnonNumberedIRI :: Int -> Bool -> Id -> IRI
idToAnonNumberedIRI :: Int -> Bool -> Id -> IRI
idToAnonNumberedIRI n :: Int
n b :: Bool
b i :: Id
i = IRI
nullIRI
  { iriPath :: Id
iriPath = String -> Id
stringToId ((if Bool
b then ('_' Char -> String -> String
forall a. a -> [a] -> [a]
:) else String -> String
forall a. a -> a
id) (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String -> String
transString (Id -> String
forall a. Show a => a -> String
show Id
i)
      String -> String -> String
forall a. [a] -> [a] -> [a]
++ if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0 then "" else '_' Char -> String -> String
forall a. a -> [a] -> [a]
: Int -> String
forall a. Show a => a -> String
show Int
n)
  , iriPos :: Range
iriPos = Id -> Range
rangeOfId Id
i
  , isAbbrev :: Bool
isAbbrev = Bool
True }

-- | translate to a valid OWL string
transString :: String -> String
transString :: String -> String
transString str :: String
str = let
    x :: Char
x = 'x'
    replaceChar1 :: Char -> String
replaceChar1 d :: Char
d | Char
d Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
x = [Char
x, Char
x]  -- code out existing x!
                   | Char -> Bool
iunreserved Char
d = [Char
d]
                   | Bool
otherwise = Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: Char -> String
replaceChar Char
d
    in case String
str of
    "" -> [Char
x]
    c :: Char
c : s :: String
s -> let l :: String
l = Char -> String
replaceChar1 Char
c in
             (if Char -> Bool
isDigit Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '_' then [Char
x, Char
c]
             else String
l) String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Char -> String) -> String -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Char -> String
replaceChar1 String
s

-- | injective replacement of special characters
replaceChar :: Char -> String
-- <http://www.htmlhelp.com/reference/charset/>
replaceChar :: Char -> String
replaceChar c :: Char
c = if Char -> Bool
isAlphaNum Char
c then [Char
c] else Char -> String
lookupCharMap Char
c