Commits

Doug Burke committed b8ad62c

Formatter: more minor refactoring

Comments (0)

Files changed (4)

src/Swish/RDF/Formatter/Internal.hs

     , formatLangLit
     , formatTypedLit
     , insertList
+    , nextLine_
+    , mapBlankNode_
     )
 where
 
 import Swish.RDF.Vocabulary (LanguageTag, fromLangTag, xsdBoolean, xsdDecimal, xsdInteger, xsdDouble)
 
 import Control.Monad (liftM)
-import Control.Monad.State (State, get, put)
+import Control.Monad.State (State, get, gets, put)
 
 import Data.List (delete, foldl', groupBy, intersperse, partition)
 import Data.Monoid (Monoid(..), mconcat)
 insertList f xs = do
     ls <- mapM f xs
     return $ mconcat ("( " : intersperse " " ls) `mappend` " )" 
-    
+
+nextLine_ :: (a -> B.Builder) -> (a -> Bool) -> (Bool -> State a ()) -> B.Builder -> State a B.Builder
+nextLine_ indent lineBreak setLineBreak str = do
+  ind <- gets indent
+  brk <- gets lineBreak
+  if brk
+    then return $ ind `mappend` str
+    else do
+      --  After first line, always insert line break
+      setLineBreak True
+      return str
+         
+mapBlankNode_ :: (a -> NodeGenState) -> (NodeGenState -> State a ()) -> RDFLabel -> State a B.Builder
+mapBlankNode_ nodeGenSt setNgs lab = do
+  ngs <- gets nodeGenSt
+  let (lval, mngs) = getBNodeLabel lab ngs
+  case mngs of
+    Just ngs' -> setNgs ngs'
+    _ -> return ()
+  return lval
+
 --------------------------------------------------------------------------------
 --
 --  Copyright (c) 2003, Graham Klyne, 2009 Vasili I Galchin,

src/Swish/RDF/Formatter/N3.hs

                                     , changeState
                                     , hasMore
                                     , emptyNgs
-                                    , getBNodeLabel
                                     , findMaxBnode
                                     , splitOnLabel
                                     , processArcs
 				    , formatLangLit
 				    , formatTypedLit
 				    , insertList
+                                    , nextLine_
+                                    , mapBlankNode_
 				    ) 
 
 import Swish.Namespace (ScopedName)
 formatGraph ind end dobreak dopref gr = do
   setIndent ind
   setLineBreak dobreak
-  setGraph gr
+  modify (newState gr)
   
   fp <- if dopref
         then formatPrefixes (getNamespaces gr)
            , bNodesCheck   = bNodes
            }
 
-setGraph :: RDFGraph -> Formatter ()
-setGraph = modify . newState
-
 nextSubject :: Formatter RDFLabel
 nextSubject = 
     changeState $ \st -> 
         in (ob, nst)
 
 nextLine :: B.Builder -> Formatter B.Builder
-nextLine str = do
-  ind <- gets indent
-  brk <- gets lineBreak
-  if brk
-    then return $ ind `mappend` str
-    else do
-      --  After first line, always insert line break
-      setLineBreak True
-      return str
+nextLine = nextLine_ indent lineBreak setLineBreak
 
 --  Format a label
 --  Most labels are simply displayed as provided, but there are a
 
 {-
 The "[..]" conversion is done last, after "()" and "{}" checks.
+
+TODO: look at the (_:_) check on the blank string; why is this needed?
 -}
 formatLabel lctxt lab@(Blank (_:_)) = do
   mlst <- extractList lctxt lab
 formatNodeId other = error $ "formatNodeId not expecting a " ++ show other -- to shut up -Wall
 
 mapBlankNode :: RDFLabel -> Formatter B.Builder
-mapBlankNode lab = do
-  ngs <- gets nodeGenSt
-  let (lval, mngs) = getBNodeLabel lab ngs
-  case mngs of
-    Just ngs' -> setNgs ngs'
-    _ -> return ()
-  return lval
+mapBlankNode = mapBlankNode_ nodeGenSt setNgs
 
 --------------------------------------------------------------------------------
 --

src/Swish/RDF/Formatter/NTriples.hs

 
 import Swish.RDF.Formatter.Internal ( NodeGenState(..)
                                     , emptyNgs
-                                    , getBNodeLabel
+                                    , mapBlankNode_
                                     )
 
 import Swish.GraphClass (Arc(..))
 formatLabel lab = return $ B.fromString $ show lab
 
 mapBlankNode :: RDFLabel -> Formatter B.Builder
-mapBlankNode lab = do
-  ngs <- get
-  let (lval, mngs) = getBNodeLabel lab ngs
-  case mngs of
-    Just ngs' -> put ngs'
-    _ -> return ()
-  return lval
-  
+mapBlankNode = mapBlankNode_ id put
 
 -- TODO: can we use Network.URI to protect the URI?
 showScopedName :: ScopedName -> B.Builder

src/Swish/RDF/Formatter/Turtle.hs

                                     , changeState
                                     , hasMore
                                     , emptyNgs
-                                    , getBNodeLabel
                                     , findMaxBnode
                                     , splitOnLabel
                                     , processArcs
                                     , formatLangLit
                                     , formatTypedLit
                                     , insertList
+                                    , nextLine_
+                                    , mapBlankNode_
 				    )
 
 import Swish.RDF.Graph (
 formatGraph ind end dobreak dopref gr = do
   setIndent ind
   setLineBreak dobreak
-  setGraph gr
+  modify (newState gr)
   
   fp <- if dopref
         then formatPrefixes (getNamespaces gr)
            , bNodesCheck   = bNodes
            }
 
-setGraph :: RDFGraph -> Formatter ()
-setGraph = modify . newState
-
 nextSubject :: Formatter RDFLabel
 nextSubject = 
     changeState $ \st -> 
         in (ob, nst)
 
 nextLine :: B.Builder -> Formatter B.Builder
-nextLine str = do
-  ind <- gets indent
-  brk <- gets lineBreak
-  if brk
-    then return $ ind `mappend` str
-    else do
-      --  After first line, always insert line break
-      setLineBreak True
-      return str
+nextLine = nextLine_ indent lineBreak setLineBreak
 
 --  Format a label
 --  Most labels are simply displayed as provided, but there are a
 
 {-
 The "[..]" conversion is done last, after "()" and "{}" checks.
+
+TODO: look at the (_:_) check on the blank string; why is this needed?
 -}
 formatLabel lctxt lab@(Blank (_:_)) = do
   mlst <- extractList lctxt lab
 formatNodeId other = error $ "formatNodeId not expecting a " ++ show other -- to shut up -Wall
 
 mapBlankNode :: RDFLabel -> Formatter B.Builder
-mapBlankNode lab = do
-  ngs <- gets nodeGenSt
-  let (lval, mngs) = getBNodeLabel lab ngs
-  case mngs of
-    Just ngs' -> setNgs ngs'
-    _ -> return ()
-  return lval
+mapBlankNode = mapBlankNode_ nodeGenSt setNgs
 
 --------------------------------------------------------------------------------
 --