Commits

Doug Burke committed 876ec3b

Formatting: more minor refactoring

  • Participants
  • Parent commits 38be4cb

Comments (0)

Files changed (3)

File src/Swish/RDF/Formatter/Internal.hs

     , showScopedName
     , formatScopedName
     , formatPrefixLines
+    , maybeExtractList
+    , formatPlainLit
+    , formatLangLit
+    , formatTypedLit
     )
 where
 
                        , quote
                        , quoteT
                        )
+import Swish.RDF.Vocabulary (LanguageTag, fromLangTag, xsdBoolean, xsdDecimal, xsdInteger, xsdDouble)
 
 import Data.List (delete, foldl', groupBy)
 import Data.Monoid (mconcat)
       pref (Just p,u) = mconcat ["@prefix ", B.fromText p, ": <", quoteB True (show u), "> ."]
       pref (_,u)      = mconcat ["@prefix : <", quoteB True (show u), "> ."]
       
+
+maybeExtractList :: 
+  SubjTree RDFLabel
+  -> PredTree RDFLabel
+  -> LabelContext
+  -> RDFLabel
+  -> Maybe ([RDFLabel], SubjTree RDFLabel, PredTree RDFLabel)
+maybeExtractList osubjs oprops lctxt ln =
+  let mlst = getCollection osubjs' ln
+
+      -- we only want to send in rdf:first/rdf:rest here
+      fprops = filter ((`elem` [resRdfFirst, resRdfRest]) . fst) oprops
+
+      osubjs' =
+          case lctxt of
+            SubjContext -> (ln, fprops) : osubjs
+            _ -> osubjs 
+
+  in case mlst of
+    Just (sl, ls, _) -> 
+      let oprops' = if lctxt == SubjContext
+                    then filter ((`notElem` [resRdfFirst, resRdfRest]) . fst) oprops
+                    else oprops
+      in Just (ls, sl, oprops')
+
+    _ -> Nothing
+
+formatPlainLit :: T.Text -> B.Builder
+formatPlainLit = quoteText
+
+formatLangLit :: T.Text -> LanguageTag -> B.Builder
+formatLangLit lit lcode = mconcat [quoteText lit, "@", B.fromText (fromLangTag lcode)]
+
+-- The canonical notation for xsd:double in XSD, with an upper-case E,
+-- does not match the syntax used in N3, so we need to convert here.     
+-- Rather than converting back to a Double and then displaying that       
+-- we just convert E to e for now.      
+--      
+formatTypedLit :: T.Text -> ScopedName -> B.Builder
+formatTypedLit lit dtype
+    | dtype == xsdDouble = B.fromText $ T.toLower lit
+    | dtype `elem` [xsdBoolean, xsdDecimal, xsdInteger] = B.fromText lit
+    | otherwise = mconcat [quoteText lit, "^^", showScopedName dtype]
+                           
 	       
 --------------------------------------------------------------------------------
 --

File src/Swish/RDF/Formatter/N3.hs

                                     , LabelContext(..)
                                     , NodeGenState(..), emptyNgs
                                     , findMaxBnode
-                                    , getCollection
                                     , processArcs
 				    , quoteB
-				    , quoteText
-				    , showScopedName
 				    , formatScopedName
 				    , formatPrefixLines
+                                    , maybeExtractList
+				    , formatPlainLit
+				    , formatLangLit
+				    , formatTypedLit
 				    ) 
 
 import Swish.Namespace (ScopedName)
   setNamespaces, getNamespaces,
   getFormulae,
   emptyRDFGraph
-  , resRdfFirst, resRdfRest
   )
 
 import Swish.RDF.Vocabulary (
-  fromLangTag, 
   rdfType,
   rdfNil,
   owlSameAs, logImplies
-  , xsdBoolean, xsdDecimal, xsdInteger, xsdDouble 
   )
 
-import Control.Monad (liftM, when, void)
+import Control.Monad (liftM, void)
 import Control.Monad.State (State, modify, get, put, runState)
 
 import Data.Char (isDigit)
 extractList lctxt ln = do
   osubjs <- getSubjs
   oprops <- getProps
-  let mlst = getCollection osubjs' ln
+  case maybeExtractList osubjs oprops lctxt ln of
+    Just (ls, osubjs', oprops') -> do
+      setSubjs osubjs'
+      setProps oprops' -- TODO: this used to be optional; is this valid?
+      return (Just ls)
 
-      -- we only want to send in rdf:first/rdf:rest here
-      fprops = filter ((`elem` [resRdfFirst, resRdfRest]) . fst) oprops
-
-      osubjs' =
-          case lctxt of
-            SubjContext -> (ln, fprops) : osubjs
-            _ -> osubjs 
-
-      -- tr = "extractList " ++ show ln ++ " (" ++ show lctxt ++ ")\n -> osubjs= " ++ show osubjs ++ "\n -> opreds= " ++ show oprops ++ "\n -> mlst= " ++ show mlst ++ "\n"
-  -- addTrace tr
-  case mlst of
-    -- sl is guaranteed to be free of (ln,fprops) here if lctxt is SubjContext
-    Just (sl,ls,_) -> do
-              setSubjs sl
-              when (lctxt == SubjContext) $ setProps $ filter ((`notElem` [resRdfFirst, resRdfRest]) . fst) oprops
-              return (Just ls)
-
-    Nothing -> return Nothing
+    _ -> return Nothing
   
 ----------------------------------------------------------------------
 --  Define a top-level formatter function:
       queueFormula lab
       return $ formatScopedName sn pr
 
--- The canonical notation for xsd:double in XSD, with an upper-case E,
--- does not match the syntax used in N3, so we need to convert here.     
--- Rather than converting back to a Double and then displaying that       
--- we just convert E to e for now.      
---
-formatLabel _ (TypedLit lit dtype)
-    | dtype == xsdDouble = return $ B.fromText $ T.toLower lit
-    | dtype `elem` [xsdBoolean, xsdDecimal, xsdInteger] = return $ B.fromText lit
-    | otherwise = return $ quoteText lit `mappend` "^^" `mappend` showScopedName dtype
-formatLabel _ (LangLit lit lcode) =
-    return $ quoteText lit `mappend` "@" `mappend` B.fromText (fromLangTag lcode)
-formatLabel _ (Lit lit) = return $ quoteText lit
+formatLabel _ (Lit lit)            = return $ formatPlainLit lit
+formatLabel _ (LangLit lit lcode)  = return $ formatLangLit lit lcode
+formatLabel _ (TypedLit lit dtype) = return $ formatTypedLit lit dtype
 
 formatLabel _ lab = return $ B.fromString $ show lab
 

File src/Swish/RDF/Formatter/Turtle.hs

                                     , LabelContext(..)
                                     , NodeGenState(..), emptyNgs
                                     , findMaxBnode
-                                    , getCollection
                                     , processArcs
- 				    , quoteText
-				    , showScopedName
                                     , formatScopedName
                                     , formatPrefixLines
+                                    , maybeExtractList
+                                    , formatPlainLit
+                                    , formatLangLit
+                                    , formatTypedLit
 				    )
 
 import Swish.RDF.Graph (
   , NamespaceMap
   , getNamespaces
   , emptyRDFGraph
-  , resRdfFirst, resRdfRest
   )
 
-import Swish.RDF.Vocabulary ( fromLangTag 
-                            , rdfType
-                            , rdfNil
-                            , xsdBoolean, xsdDecimal, xsdInteger, xsdDouble 
-                            )
+import Swish.RDF.Vocabulary (rdfType, rdfNil)
 
-import Control.Monad (liftM, when)
+import Control.Monad (liftM)
 import Control.Monad.State (State, modify, get, put, runState)
 
 import Data.Char (isDigit)
 extractList lctxt ln = do
   osubjs <- getSubjs
   oprops <- getProps
-  let mlst = getCollection osubjs' ln
+  case maybeExtractList osubjs oprops lctxt ln of
+    Just (ls, osubjs', oprops') -> do
+      setSubjs osubjs'
+      setProps oprops' -- TODO: this used to be optional; is this valid?
+      return (Just ls)
 
-      -- we only want to send in rdf:first/rdf:rest here
-      fprops = filter ((`elem` [resRdfFirst, resRdfRest]) . fst) oprops
-
-      osubjs' =
-          case lctxt of
-            SubjContext -> (ln, fprops) : osubjs
-            _ -> osubjs 
-
-      -- tr = "extractList " ++ show ln ++ " (" ++ show lctxt ++ ")\n -> osubjs= " ++ show osubjs ++ "\n -> opreds= " ++ show oprops ++ "\n -> mlst= " ++ show mlst ++ "\n"
-  -- addTrace tr
-  case mlst of
-    -- sl is guaranteed to be free of (ln,fprops) here if lctxt is SubjContext
-    Just (sl,ls,_) -> do
-              setSubjs sl
-              when (lctxt == SubjContext) $ setProps $ filter ((`notElem` [resRdfFirst, resRdfRest]) . fst) oprops
-              return (Just ls)
-
-    Nothing -> return Nothing
+    _ -> return Nothing
   
 ----------------------------------------------------------------------
 --  Define a top-level formatter function:
   pr <- getPrefixes
   return $ formatScopedName sn pr
 
--- The canonical notation for xsd:double in XSD, with an upper-case E,
--- does not match the syntax used in N3, so we need to convert here.     
--- Rather than converting back to a Double and then displaying that       
--- we just convert E to e for now.      
---      
-formatLabel _ (Lit lit) = return $ quoteText lit
-formatLabel _ (LangLit lit lcode) = return $ quoteText lit `mappend` "@" `mappend` B.fromText (fromLangTag lcode)
-formatLabel _ (TypedLit lit dtype)
-    | dtype == xsdDouble = return $ B.fromText $ T.toLower lit
-    | dtype `elem` [xsdBoolean, xsdDecimal, xsdInteger] = return $ B.fromText lit
-    | otherwise = return $ quoteText lit `mappend` "^^" `mappend` showScopedName dtype
+formatLabel _ (Lit lit) = return $ formatPlainLit lit
+formatLabel _ (LangLit lit lcode) = return $ formatLangLit lit lcode
+formatLabel _ (TypedLit lit dtype) = return $ formatTypedLit lit dtype
 
 formatLabel _ lab = return $ B.fromString $ show lab