Skip to content

Commit

Permalink
Parse top level XML
Browse files Browse the repository at this point in the history
Ignore-this: c065825fd94b8d3f6043fcf308bbd468

darcs-hash:20110118170812-1786f-fde5818bc4b4c644cf55f0816d5d68963362806e.gz
  • Loading branch information
amccausl committed Jan 18, 2011
1 parent 8a09933 commit 7fa7b38
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
2 changes: 2 additions & 0 deletions rdf4h.cabal
Expand Up @@ -52,6 +52,7 @@ library
, network >= 2.2.0.0 && < 2.3 , network >= 2.2.0.0 && < 2.3
, HTTP >= 4000.0.0 && < 4000.2 , HTTP >= 4000.0.0 && < 4000.2
, hxt >= 9.0.0 && < 9.1 , hxt >= 9.0.0 && < 9.1
, MissingH >= 1.0.0 && < 1.1
other-modules: Data.RDF.Utils other-modules: Data.RDF.Utils
, Text.RDF.RDF4H.ParserUtils , Text.RDF.RDF4H.ParserUtils
, Text.RDF.RDF4H.Interact , Text.RDF.RDF4H.Interact
Expand All @@ -69,6 +70,7 @@ executable rdf4h
, network >= 2.2.0.0 && < 2.3 , network >= 2.2.0.0 && < 2.3
, HTTP >= 4000.0.0 && < 4000.2 , HTTP >= 4000.0.0 && < 4000.2
, hxt >= 9.0.0 && < 9.1 , hxt >= 9.0.0 && < 9.1
, MissingH >= 1.0.0 && < 1.1
hs-source-dirs: src hs-source-dirs: src
extensions: BangPatterns extensions: BangPatterns
ghc-options: -O2 -Wall -fglasgow-exts -fno-warn-name-shadowing -fno-warn-missing-signatures -fno-warn-unused-do-bind ghc-options: -O2 -Wall -fglasgow-exts -fno-warn-name-shadowing -fno-warn-missing-signatures -fno-warn-unused-do-bind
Expand Down
1 change: 1 addition & 0 deletions src/Data/RDF/MGraph.hs
Expand Up @@ -35,6 +35,7 @@ import Data.List
-- --
-- * 'query' : O(log n) -- * 'query' : O(log n)
newtype MGraph = MGraph (SPOMap, Maybe BaseUrl, PrefixMappings) newtype MGraph = MGraph (SPOMap, Maybe BaseUrl, PrefixMappings)
deriving (Show)


instance RDF MGraph where instance RDF MGraph where
baseUrl = baseUrl' baseUrl = baseUrl'
Expand Down
39 changes: 35 additions & 4 deletions src/Text/RDF/RDF4H/XmlParser.hs
@@ -1,23 +1,54 @@
{-# LANGUAGE RankNTypes #-} {-# LANGUAGE Arrows, RankNTypes #-}
-- |An parser for the RDF/XML format -- |An parser for the RDF/XML format
-- <http://www.w3.org/TR/REC-rdf-syntax/>. -- <http://www.w3.org/TR/REC-rdf-syntax/>.


module Text.RDF.RDF4H.XmlParser( module Text.RDF.RDF4H.XmlParser(
parseXmlRDF parseXmlRDF, getRDF
) where ) where


import qualified Data.Map as Map
import Data.RDF import Data.RDF
import Data.RDF.Namespace
import Control.Arrow


import Text.XML.HXT.Core import Text.XML.HXT.Core
import Data.Tree.NTree.TypeDefs (NTree(..))


import Data.ByteString.Lazy.Char8(ByteString) import Data.ByteString.Lazy.Char8(ByteString)
import Data.String.Utils


-- |Parse a xml ByteString to an RDF representation
parseXmlRDF :: forall rdf. (RDF rdf) parseXmlRDF :: forall rdf. (RDF rdf)
=> Maybe BaseUrl -- ^ The base URL for the RDF if required => Maybe BaseUrl -- ^ The base URL for the RDF if required
-> Maybe ByteString -- ^ DocUrl: The request URL for the RDF if available -> Maybe ByteString -- ^ DocUrl: The request URL for the RDF if available
-> ByteString -- ^ The contents to parse -> ByteString -- ^ The contents to parse
-> Either ParseFailure rdf -- ^ The RDF representation of the triples or ParseFailure -> Either ParseFailure rdf -- ^ The RDF representation of the triples or ParseFailure
parseXmlRDF bUrl dUrl xmlStr = Left (ParseFailure "XML parsing not implemented") parseXmlRDF bUrl dUrl xmlStr = Left (ParseFailure "XML parsing not implemented")
where xml = runLA xread (b2s xmlStr)

-- |Arrow that translates HXT XmlTree to an RDF representation
getRDF :: forall rdf a. (RDF rdf, ArrowXml a) => a XmlTree rdf
getRDF = proc xml -> do
rdf <- hasName "rdf:RDF" <<< isElem <<< getChildren -< xml
bUrl <- arr (Just . BaseUrl . s2b) <<< getAttrValue "transfer-URI" -< xml
prefixMap <- arr toPrefixMap <<< toAttrMap -< rdf
triples <- (parseDescription <<< hasName "rdf:Description" <<< isElem <<< getChildren) >. id -< rdf
returnA -< mkRdf triples bUrl prefixMap
where toAttrMap = (getAttrl >>> (getName &&& (getChildren >>> getText))) >. id
toPrefixMap = PrefixMappings . Map.fromList . map (\(n, m) -> (s2b (drop 6 n), s2b m)) . filter (startswith "xmlns:" . fst)
toUNode = unode . s2b
parseDescription = proc desc -> do
s <- getAttrValue "rdf:about" -< desc
(p, o) <- ((getName >>> isA (/= "rdf:about")) &&& (getChildren >>> getText)) <<< getAttrl -< desc
returnA -< Triple (toUNode s) (toUNode p) (toUNode o)

fakeDoc :: ArrowXml a => a XmlTree XmlTree
fakeDoc = mkelem "/"
[ sattr "source" "../../data/xml/example07.rdf"
, sattr "transfer-URI" "http://www.w3.org/TR/REC-rdf-syntax/example07.rdf"
, sattr "transfer-Message" "OK"
, sattr "transfer-Status" "200"
, sattr "transfer-MimeType" "text/rdf"
, sattr "transfer-Encoding" "ISO-8859-1"
]
[ arr id ]


File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions testsuite/tests/Test.hs
Expand Up @@ -7,8 +7,8 @@ import Test.Framework.Providers.QuickCheck (testProperty)
import Test.QuickCheck import Test.QuickCheck
import Test.HUnit import Test.HUnit


import Text.RDF.RDF4H.MGraph_Test import Data.RDF.MGraph_Test
import Text.RDF.RDF4H.TriplesGraph_Test import Data.RDF.TriplesGraph_Test
import Text.RDF.RDF4H.TurtleParser_ConformanceTest import Text.RDF.RDF4H.TurtleParser_ConformanceTest


main :: IO () main :: IO ()
Expand Down

0 comments on commit 7fa7b38

Please sign in to comment.