[Fencommits] fenfire-hs: done: relativize URIs on saving

Benja Fallenstein benja.fallenstein at gmail.com
Sat Feb 24 16:35:29 EET 2007


Sat Feb 24 16:35:03 EET 2007  Benja Fallenstein <benja.fallenstein at gmail.com>
  * done: relativize URIs on saving
diff -rN -u old-fenfire-hs/Fenfire.fhs new-fenfire-hs/Fenfire.fhs
--- old-fenfire-hs/Fenfire.fhs	2007-02-24 16:35:29.000000000 +0200
+++ new-fenfire-hs/Fenfire.fhs	2007-02-24 16:35:29.000000000 +0200
@@ -28,7 +28,7 @@
 import Paths_fenfire (getDataFileName)
 
 import qualified Raptor (filenameToTriples, uriToTriples,
-                         triplesToFilename, Identifier(..))
+                         triplesToFilename, filenameToURI, Identifier(..))
 
 import qualified Data.Map as Map
 import qualified Data.Set as Set
@@ -41,6 +41,7 @@
 import Data.Monoid(Monoid(mempty, mconcat), Dual(Dual), getDual)
 
 import Control.Applicative
+import qualified Control.Exception
 import Control.Monad (when, guard, mplus, msum, liftM, join)
 import Control.Monad.Reader (ReaderT, runReaderT, local, ask, asks)
 import Control.Monad.State (StateT, get, gets, modify, put, execStateT)
@@ -57,7 +58,8 @@
                                styleGetText, styleGetBase, 
                                styleGetAntiAliasing)
 
-import qualified Control.Exception
+import qualified Network.URI
+
 import System.Directory (canonicalizePath)
 import System.Environment (getArgs, getProgName)
 import System.Mem.StableName
@@ -359,18 +361,18 @@
     let graph = listToGraph [(home, rdfs_label, PlainLiteral "")]
     return (graph, Path home [])
     
-findStartPath :: (?vs :: ViewSettings) => Graph -> Path
-findStartPath g = let ?graph = g in result where
+findStartPath :: (?vs :: ViewSettings) => Node -> Graph -> Path
+findStartPath self g = let ?graph = g in result where
     result :: (?graph :: Graph) => Path
     result = head $ catMaybes $ startNode:topic:triples where
-    self = URI "ex:graph" -- ought to be what the empty URI <> is expanded to
 
-    startNode = fmap getRot $ getTriple self ffv_startNode
-    topic = fmap getRot $ getTriple self foaf_primaryTopic
+    startNode = fmap getRot' $ getTriple self ffv_startNode
+    topic = fmap getRot' $ getTriple self foaf_primaryTopic
     triples = map (Just . getRot) $ graphToList g
     
     getTriple s p = fmap (\o -> (s,p,o)) $ getOne g s p Pos
-    getRot (s,p,o) = Path s [Conn p Pos o]
+    getRot  (s,p,o) = Path s [Conn p Pos o]
+    getRot' (s,p,o) = Path o [Conn p Neg s]
     
     ffv_startNode = URI "http://fenfire.org/rdf-v/2003/05/ff#startNode"
     foaf_primaryTopic = URI "http://xmlns.com/foaf/0.1/primaryTopic"
@@ -392,15 +394,19 @@
         f (Raptor.Blank s) = URI $ "blank:" ++ s
     (raptorTriples, namespaces) <- if Data.List.isPrefixOf "http:" fileName
         then Raptor.uriToTriples fileName Nothing
-        else Raptor.filenameToTriples fileName (Just "fakebase:/")
+        else Raptor.filenameToTriples fileName Nothing
     triples <- return $ map convert raptorTriples
     return $ foldr (uncurry addNamespace) (listToGraph triples) namespaces
 
 saveGraph :: Graph -> FilePath -> IO ()
 saveGraph graph fileName = do
     --writeFile fileName $ toNTriples $ reverse graph
+    uri <- liftM (fromJust . Network.URI.parseURI)
+                 (Raptor.filenameToURI fileName)
     let convert (s,p,o) = (f s, f p, f o)
-        f (URI s) = Raptor.Uri s
+        f (URI s) = Raptor.Uri $ fromMaybe s $ do
+                        u <- Network.URI.parseURI s
+                        return $ show $ Network.URI.relativeFrom u uri
         f (PlainLiteral s) = Raptor.Literal s
         triples = graphToList graph
         namespaces = Map.toAscList $ graphNamespaces graph
@@ -569,9 +575,10 @@
         "open"  -> confirmSave modified $ do 
             result <- liftIO $ openFile filepath
             maybeDo result $ \(g',fp') -> do
-                let ts = containsInfoTriples (URI $ "file://" ++ fp') g'
+                uri <- liftM URI $ liftIO $ Raptor.filenameToURI fp'
+                let ts = containsInfoTriples uri g'
                     g'' = foldr insertVirtual g' ts
-                put $ newState g'' (findStartPath g'') fp' focus
+                put $ newState g'' (findStartPath uri g'') fp' focus
         "loadURI" -> case node of 
                          URI uri -> do 
                              g <- liftIO $ loadGraph uri
@@ -585,9 +592,9 @@
                          _ -> unhandledEvent
         "revert" | filepath /= "" -> confirmRevert modified $ do
             g' <- liftIO $ loadGraph filepath
-            let gNode = URI ("file://" ++ filepath)
-                g'' = foldr insertVirtual g' $ containsInfoTriples gNode g'
-            put $ newState g'' (findStartPath g'') filepath focus
+            gNode <- liftM URI $ liftIO $ Raptor.filenameToURI filepath
+            let g'' = foldr insertVirtual g' $ containsInfoTriples gNode g'
+            put $ newState g'' (findStartPath gNode g'') filepath focus
         "save" | filepath /= "" -> do 
                      liftIO $ saveGraph graph filepath
                      modify $ \s -> s { fsGraphModified = False }
@@ -799,11 +806,12 @@
             fileName:fileNames <- mapM f xs
             g' <- loadGraph fileName
             gs <- mapM loadGraph fileNames
-            let uris = map (URI . ("file://" ++)) fileNames
-                ts = concatMap (uncurry containsInfoTriples) $
-                     (URI $ "file://" ++ fileName, g') : zip uris gs
+            uri <- Raptor.filenameToURI fileName
+            uris <- mapM Raptor.filenameToURI fileNames
+            let ts = concatMap (uncurry containsInfoTriples) $
+                         (URI uri, g') : zip (map URI uris) gs
                 graph = foldr insertVirtual (foldl mergeGraphs g' gs) ts
-            newIORef $ newState graph (findStartPath graph) fileName False
+            newIORef $ newState graph (findStartPath (URI uri) graph) fileName False
 
     -- start:
 
diff -rN -u old-fenfire-hs/README new-fenfire-hs/README
--- old-fenfire-hs/README	2007-02-24 16:35:29.000000000 +0200
+++ new-fenfire-hs/README	2007-02-24 16:35:29.000000000 +0200
@@ -32,7 +32,7 @@
   alex   2.0.1  (A lexical analyser generator for Haskell)
 
 (Packages in Debian: ghc6 libghc6-gtk-dev libraptor1-dev c2hs libghc6-harp-dev 
- libghc6-haxml-dev happy alex)
+ libghc6-haxml-dev happy alex libghc6-network-dev)
 
 
 Running a precompiled binary only requires:
diff -rN -u old-fenfire-hs/Raptor.chs new-fenfire-hs/Raptor.chs
--- old-fenfire-hs/Raptor.chs	2007-02-24 16:35:29.000000000 +0200
+++ new-fenfire-hs/Raptor.chs	2007-02-24 16:35:29.000000000 +0200
@@ -193,6 +193,13 @@
   {# call serialize_end #} serializer
   {# call free_serializer #} serializer
   {# call finish #}
+  
+filenameToURI :: String -> IO String
+filenameToURI filename = do
+  uri_str <- withCString filename uri_filename_to_uri_string
+  r <- peekCString uri_str
+  {# call free_memory #} (castPtr uri_str)
+  return r  
 
 -- | Parse a file with the given filename into triples
 --
diff -rN -u old-fenfire-hs/fenfire.cabal new-fenfire-hs/fenfire.cabal
--- old-fenfire-hs/fenfire.cabal	2007-02-24 16:35:29.000000000 +0200
+++ new-fenfire-hs/fenfire.cabal	2007-02-24 16:35:29.000000000 +0200
@@ -11,7 +11,8 @@
 Category:       User Interfaces
 Stability:      alpha
 Homepage:       http://fenfire.org/
-Build-Depends:  base, HaXml, gtk, mtl, unix, cairo, harp, template-haskell, glib
+Build-Depends:  base, HaXml, gtk, mtl, unix, cairo, harp, template-haskell,
+                glib, network
 Data-Files:     data/logo.svg data/icon16.png
 
 Executable:     fenfire




More information about the Fencommits mailing list