1. Doug Burke
  2. swish

Commits

Doug Burke  committed e6255f3

Formatter: more refactoring work

  • Participants
  • Parent commits ea7402e
  • Branches default

Comments (0)

Files changed (3)

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

View file
     , mapBlankNode_
     , formatPrefixes_
     , formatGraph_
+    , formatSubjects_
+    , formatProperties_
+    , formatObjects_
     )
 where
 
       return $ mconcat [fp, fr, end]
     else return fp
 
+formatSubjects_ ::
+    State a RDFLabel     -- ^ next subject
+    -> (LabelContext -> RDFLabel -> State a B.Builder)  -- ^ convert label into text
+    -> (a -> PredTree RDFLabel) -- ^ extract properties
+    -> (RDFLabel -> B.Builder -> State a B.Builder)   -- ^ format properties
+    -> (a -> SubjTree RDFLabel) -- ^ extract subjects
+    -> (B.Builder -> State a B.Builder) -- ^ next line
+    -> State a B.Builder
+formatSubjects_ nextSubject formatLabel props formatProperties subjs nextLine = do
+  sb    <- nextSubject
+  sbstr <- formatLabel SubjContext sb
+  
+  flagP <- hasMore props
+  if flagP
+    then do
+      prstr <- formatProperties sb sbstr
+      flagS <- hasMore subjs
+      if flagS
+        then do
+          fr <- formatSubjects_ nextSubject formatLabel props formatProperties subjs nextLine
+          return $ mconcat [prstr, " .", fr]
+        else return prstr
+           
+    else do
+      txt <- nextLine sbstr
+    
+      flagS <- hasMore subjs
+      if flagS
+        then do
+          fr <- formatSubjects_ nextSubject formatLabel props formatProperties subjs nextLine
+          return $ mconcat [txt, " .", fr]
+        else return txt
+
+
+{-
+TODO: now we are throwing a Builder around it is awkward to
+get the length of the text to calculate the indentation
+
+So
+
+  a) change the indentation scheme
+  b) pass around text instead of builder
+
+mkIndent :: L.Text -> L.Text
+mkIndent inVal = L.replicate (L.length inVal) " "
+-}
+
+hackIndent :: B.Builder
+hackIndent = "    "
+
+formatProperties_ :: 
+    (RDFLabel -> State a RDFLabel)        -- ^ next property for the given subject
+    -> (LabelContext -> RDFLabel -> State a B.Builder) -- ^ convert label into text
+    -> (RDFLabel -> RDFLabel -> B.Builder -> State a B.Builder) -- ^ format objects
+    -> (a -> PredTree RDFLabel) -- ^ extract properties
+    -> (B.Builder -> State a B.Builder) -- ^ next line
+    -> RDFLabel             -- ^ property being processed
+    -> B.Builder            -- ^ current output
+    -> State a B.Builder
+formatProperties_ nextProperty formatLabel formatObjects props nextLine sb sbstr = do
+  pr <- nextProperty sb
+  prstr <- formatLabel PredContext pr
+  obstr <- formatObjects sb pr $ mconcat [sbstr, " ", prstr]
+  more  <- hasMore props
+  let sbindent = hackIndent -- mkIndent sbstr
+  if more
+    then do
+      fr <- formatProperties_ nextProperty formatLabel formatObjects props nextLine sb sbindent
+      nl <- nextLine $ obstr `mappend` " ;"
+      return $ nl `mappend` fr
+    else nextLine obstr
+
+formatObjects_ :: 
+    (RDFLabel -> RDFLabel -> State a RDFLabel) -- ^ get the next object for the (subject,property) pair
+    -> (LabelContext -> RDFLabel -> State a B.Builder) -- ^ format a label
+    -> (a -> [RDFLabel]) -- ^ extract objects
+    -> (B.Builder -> State a B.Builder) -- ^ new line
+    -> RDFLabel      -- ^ subject
+    -> RDFLabel      -- ^ property
+    -> B.Builder     -- ^ current text
+    -> State a B.Builder
+formatObjects_ nextObject formatLabel objs nextLine sb pr prstr = do
+  ob    <- nextObject sb pr
+  obstr <- formatLabel ObjContext ob
+  more  <- hasMore objs
+  if more
+    then do
+      let prindent = hackIndent -- mkIndent prstr
+      fr <- formatObjects_ nextObject formatLabel objs nextLine sb pr prindent
+      nl <- nextLine $ mconcat [prstr, " ", obstr, ","]
+      return $ nl `mappend` fr
+    else return $ mconcat [prstr, " ", obstr]
+
 --------------------------------------------------------------------------------
 --
 --  Copyright (c) 2003, Graham Klyne, 2009 Vasili I Galchin,

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

View file
                                     , mapBlankNode_
                                     , formatPrefixes_
                                     , formatGraph_
+                                    , formatSubjects_
+                                    , formatProperties_
+                                    , formatObjects_
 				    ) 
 
 import Swish.Namespace (ScopedName)
 formatPrefixes :: NamespaceMap -> Formatter B.Builder
 formatPrefixes = formatPrefixes_ nextLine
 
-{-
-NOTE:
-I expect there to be confusion below where I need to
-convert from Text to Builder
--}
-
 formatSubjects :: Formatter B.Builder
-formatSubjects = do
-  sb    <- nextSubject
-  sbstr <- formatLabel SubjContext sb
-  
-  flagP <- hasMore props
-  if flagP
-    then do
-      prstr <- formatProperties sb sbstr
-      flagS <- hasMore subjs
-      if flagS
-        then do
-          fr <- formatSubjects
-          return $ mconcat [prstr, " .", fr]
-        else return prstr
-           
-    else do
-      txt <- nextLine sbstr
-    
-      flagS <- hasMore subjs
-      if flagS
-        then do
-          fr <- formatSubjects
-          return $ mconcat [txt, " .", fr]
-        else return txt
-
-{-
-TODO: now we are throwing a Builder around it is awkward to
-get the length of the text to calculate the indentation
-
-So
-
-  a) change the indentation scheme
-  b) pass around text instead of builder
-
-mkIndent :: L.Text -> L.Text
-mkIndent inVal = L.replicate (L.length inVal) " "
--}
-
-hackIndent :: B.Builder
-hackIndent = "    "
+formatSubjects = formatSubjects_ nextSubject formatLabel props formatProperties subjs nextLine
 
 formatProperties :: RDFLabel -> B.Builder -> Formatter B.Builder
-formatProperties sb sbstr = do
-  pr <- nextProperty sb
-  prstr <- formatLabel PredContext pr
-  obstr <- formatObjects sb pr $ mconcat [sbstr, " ", prstr]
-  more  <- hasMore props
-  let sbindent = hackIndent -- mkIndent sbstr
-  if more
-    then do
-      fr <- formatProperties sb sbindent
-      nl <- nextLine $ obstr `mappend` " ;"
-      return $ nl `mappend` fr
-    else nextLine obstr
+formatProperties = formatProperties_ nextProperty formatLabel formatObjects props nextLine
 
 formatObjects :: RDFLabel -> RDFLabel -> B.Builder -> Formatter B.Builder
-formatObjects sb pr prstr = do
-  ob    <- nextObject sb pr
-  obstr <- formatLabel ObjContext ob
-  more  <- hasMore objs
-  if more
-    then do
-      let prindent = hackIndent -- mkIndent prstr
-      fr <- formatObjects sb pr prindent
-      nl <- nextLine $ mconcat [prstr, " ", obstr, ","]
-      return $ nl `mappend` fr
-    else return $ mconcat [prstr, " ", obstr]
+formatObjects = formatObjects_ nextObject formatLabel objs nextLine
 
 insertFormula :: RDFGraph -> Formatter B.Builder
 insertFormula gr = do

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

View file
                                     , mapBlankNode_
                                     , formatPrefixes_
                                     , formatGraph_
+                                    , formatSubjects_
+                                    , formatProperties_
+                                    , formatObjects_
 				    )
 
 import Swish.RDF.Graph (
 formatPrefixes :: NamespaceMap -> Formatter B.Builder
 formatPrefixes = formatPrefixes_ nextLine
 
-{-
-NOTE:
-I expect there to be confusion below where I need to
-convert from Text to Builder
--}
-
 formatSubjects :: Formatter B.Builder
-formatSubjects = do
-  sb    <- nextSubject
-  sbstr <- formatLabel SubjContext sb
-  
-  flagP <- hasMore props
-  if flagP
-    then do
-      prstr <- formatProperties sb sbstr
-      flagS <- hasMore subjs
-      if flagS
-        then do
-          fr <- formatSubjects
-          return $ mconcat [prstr, " .", fr]
-        else return prstr
-           
-    else do
-      txt <- nextLine sbstr
-    
-      flagS <- hasMore subjs
-      if flagS
-        then do
-          fr <- formatSubjects
-          return $ mconcat [txt, " .", fr]
-        else return txt
-
-{-
-TODO: now we are throwing a Builder around it is awkward to
-get the length of the text to calculate the indentation
-
-So
-
-  a) change the indentation scheme
-  b) pass around text instead of builder
-
-mkIndent :: L.Text -> L.Text
-mkIndent inVal = L.replicate (L.length inVal) " "
--}
-
-hackIndent :: B.Builder
-hackIndent = "    "
+formatSubjects = formatSubjects_ nextSubject formatLabel props formatProperties subjs nextLine
 
 formatProperties :: RDFLabel -> B.Builder -> Formatter B.Builder
-formatProperties sb sbstr = do
-  pr <- nextProperty sb
-  prstr <- formatLabel PredContext pr
-  obstr <- formatObjects sb pr $ mconcat [sbstr, " ", prstr]
-  more  <- hasMore props
-  let sbindent = hackIndent -- mkIndent sbstr
-  if more
-    then do
-      fr <- formatProperties sb sbindent
-      nl <- nextLine $ obstr `mappend` " ;"
-      return $ nl `mappend` fr
-    else nextLine obstr
+formatProperties = formatProperties_ nextProperty formatLabel formatObjects props nextLine
 
 formatObjects :: RDFLabel -> RDFLabel -> B.Builder -> Formatter B.Builder
-formatObjects sb pr prstr = do
-  ob    <- nextObject sb pr
-  obstr <- formatLabel ObjContext ob
-  more  <- hasMore objs
-  if more
-    then do
-      let prindent = hackIndent -- mkIndent prstr
-      fr <- formatObjects sb pr prindent
-      nl <- nextLine $ mconcat [prstr, " ", obstr, ","]
-      return $ nl `mappend` fr
-    else return $ mconcat [prstr, " ", obstr]
+formatObjects = formatObjects_ nextObject formatLabel objs nextLine
 
 {-
 Add a blank node inline.