Permalink
Browse files

initial revision

migrated from darcs:

Tue Nov 23 11:12:22 CET 2010  Stefan Wehr <wehr@factisresearch.com>
  * INITIAL checkin
  • Loading branch information...
skogsbaer committed Jan 16, 2011
0 parents commit 024b196308b14ac41dc39520cee13cfd5e543416
Showing with 148 additions and 0 deletions.
  1. +30 −0 LICENSE
  2. +2 −0 Setup.hs
  3. +102 −0 src/Text/XML/Generator.hs
  4. +14 −0 xmlgen.cabal
30 LICENSE
@@ -0,0 +1,30 @@
+Copyright (c)2010, Stefan Schmidt, Stefan Wehr
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Stefan Schmidt, Stefan Wehr nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
@@ -0,0 +1,102 @@
+module Text.XML.Generator where
+
+import qualified Data.ByteString.Lazy as BSL
+import Data.Monoid
+
+import Blaze.ByteString.Builder
+import Blaze.ByteString.Builder.Char.Utf8
+
+newtype Out t = Out { outBuf :: Builder }
+data ATTR = ATTR
+data ELEM = ELEM
+type Attr = Out ATTR
+type Elem = Out ELEM
+
+
+type Prefix = String
+type Uri = String
+data Namespace
+ = DefaultNamespace
+ | QualifiedNamespace Prefix Uri
+
+data OutEnv
+ = OutEnv
+ { outEnv_counter :: Int
+ , outEnv_namespaceMap :: Map Uri Int }
+
+type Trans t = t -> Reader OutEnv t
+
+{-
+class XmlGen t where
+ xattr :: String -> String -> t ATTR -> t ATTR
+ xelem :: String -> (t ATTR -> t ATTR) -> (t ELEM-> t ELEM) -> t ELEM -> t ELEM
+
+instance XmlGen Out where
+ -}
+
+{-
+- Escaping
+- rawText
+- comments
+- processing instructions
+- Xml header
+- syntactic sugar for elements with no attributes/children
+- namespaces
+-}
+
+xattr :: String -> String -> Trans Attr
+xattr key value (Out buffer)
+ = Out (mconcat [buffer, spaceBuilder, keyBuilder, startBuilder, valueBuilder, endBuilder])
+ where
+ spaceBuilder = fromString " "
+ keyBuilder = fromString key
+ valueBuilder = fromString value
+ startBuilder = fromString "=\""
+ endBuilder = fromString "\""
+
+xelem :: String -> Trans Attr -> Trans Elem -> Trans Elem
+xelem name attrs elems (Out outBuffer)
+ = endOut
+ where
+ startOut = Out (mconcat [outBuffer, fromString "<", fromString name])
+ (Out attrsBuffer) = attrs startOut
+ (Out elemsBuffer) = elems (Out $ mappend attrsBuffer (fromString "\n>"))
+ endOut = Out (mconcat [elemsBuffer, fromString "</", fromString name, fromString "\n>"])
+
+xattrQ :: Namespace -> String -> String -> Trans Attr
+xattrQ = undefined
+
+xelemQ :: Namespace -> String -> Trans Attr -> Trans Elem -> TransElem
+xelemQ = undefined
+
+xtext :: String -> Trans Elem
+xtext content (Out buffer)
+ = Out (buffer `mappend` fromString content)
+
+
+(<#>) :: Trans (Out t) -> Trans (Out t) -> Trans (Out t)
+(<#>) comb1 comb2 = comb2 . comb1
+
+
+xrender :: Trans Elem -> Builder
+xrender elem = outBuf (elem (Out $ mempty))
+
+xempty :: Out t -> Out t
+xempty = id
+
+test :: IO ()
+test = BSL.putStr $ toLazyByteString $ xrender xsample
+
+nsFoo = ("http://www.factisresearch.com/ns/foo", "foo")
+
+xelemFoo = xelem nsFoo
+
+xsample :: Elem -> Elem
+xsample =
+ xelem "foo" (xattr "key" "value")
+ (xelem "bar" xempty (xtext "BAR")
+ <#>
+ xelem "spam" xempty (xelem "egg" xempty xempty <#> xtext "this is spam!"))
+
+
+-- sample (Out "")
@@ -0,0 +1,14 @@
+Name: xmlgen
+Version: 0.1.0.0
+Synopsis: Fast XML generation library
+License: BSD3
+License-file: LICENSE
+Author: Stefan Schmidt, Stefan Wehr
+Maintainer: wehr@factisresearch.com
+Category: Text
+Build-type: Simple
+Cabal-version: >=1.2
+
+
+Library
+ Exposed-modules: Text.XML.Generator

0 comments on commit 024b196

Please sign in to comment.