Skip to content

Commit d6ebf34

Browse files
committed
First version
0 parents  commit d6ebf34

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

test/xml-to-string-test.el

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
;;;; xml-to-string-test.el --- ert tests for xml-to-string
2+
3+
(ert-deftest xml-to-string-test-parse-attr-list ()
4+
(let ((input '((class . "red") (id . "content"))))
5+
(should (equal "class=\"red\" id=\"content\""
6+
(xml-to-string-parse-attr-list input)))))
7+
8+
(ert-deftest xml-to-string-test-parse-qname ()
9+
(let ((input "simple"))
10+
(should (equal input (xml-to-string-parse-qname input)))))
11+
12+
(ert-deftest xml-to-string-test-parse-node ()
13+
(let ((input '(dependency nil "\n "
14+
(groupId ((class . "red") (id . "content")) "org.eclipse.jetty")
15+
"\n "
16+
(artifactId nil "jetty-servlet")
17+
"\n "
18+
(version nil "${jetty.version}")
19+
"\n "))
20+
(expected "<dependency>\n <groupId class=\"red\" id=\"content\">org.eclipse.jetty</groupId>\n <artifactId>jetty-servlet</artifactId>\n <version>${jetty.version}</version>\n </dependency>"))
21+
(should (equal expected (xml-to-string-parse-node input)))))
22+
23+
(ert-deftest xml-to-string-test-parse-child-node-list ()
24+
(let ((input '((dependency nil "\n "
25+
(groupId nil "org.eclipse.jetty")
26+
"\n "
27+
(artifactId nil "jetty-servlet")
28+
"\n "
29+
(version nil "${jetty.version}")
30+
"\n ")
31+
(dependency nil "\n "
32+
(groupId nil "jstl")
33+
"\n "
34+
(artifactId nil "jstl")
35+
"\n "
36+
(version nil "${jstl.version}")
37+
"\n ")))
38+
(expected "<dependency>\n <groupId>org.eclipse.jetty</groupId>\n <artifactId>jetty-servlet</artifactId>\n <version>${jetty.version}</version>\n </dependency><dependency>\n <groupId>jstl</groupId>\n <artifactId>jstl</artifactId>\n <version>${jstl.version}</version>\n </dependency>"))
39+
(should (equal expected (xml-to-string-parse-child-node-list input)))))
40+

xml-to-string.el

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
;;; xml-to-string --- convert lisp s-expressions to xml string
2+
3+
;; Copyright (C) 2012 Dave Paroulek
4+
5+
;; Author: Dave Paroulek <[email protected]>
6+
;; Maintainer: Dave Paroulek <[email protected]>
7+
;; Version: 0.1.0
8+
;; Keywords: xml, data
9+
;; URL: http://github.com/upgradingdave/xml-to-string
10+
11+
;; This file is NOT part of GNU Emacs.
12+
13+
;;; License:
14+
15+
;; This program is free software; you can redistribute it and/or modify
16+
;; it under the terms of the GNU General Public License as published by
17+
;; the Free Software Foundation; either version 3, or (at your option)
18+
;; any later version.
19+
20+
;; This program is distributed in the hope that it will be useful,
21+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
;; GNU General Public License for more details.
24+
25+
;; You should have received a copy of the GNU General Public License
26+
;; along with GNU Emacs; see the file COPYING. If not, write to the
27+
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28+
;; Boston, MA 02110-1301, USA.
29+
30+
;;; Commentary:
31+
32+
;; Functions like xml-parse-file and xml-parse-region output s-expressions
33+
;; like this:
34+
;;
35+
;; xml-list ::= (node node ...)
36+
;; node ::= (qname attribute-list . child_node_list)
37+
;; child_node_list ::= child_node child_node ...
38+
;; child_node ::= node | string
39+
;; qname ::= (:namespace-uri . "name") | "name"
40+
;; attribute_list ::= ((qname . "value") (qname . "value") ...)
41+
;; | nil
42+
;; string ::= "..."
43+
44+
;; The functions below take s-exp's like these and output them back into xml strings
45+
46+
;;; Code:
47+
48+
(defun xml-to-string (node-list)
49+
"Convert sexp created by xml-parse-* function back to xml string"
50+
(let ((result))
51+
(dolist (node node-list result)
52+
(setq result (concat result (xml-to-string-parse-node node))))))
53+
54+
(defun xml-to-string-parse-node (node)
55+
"Convert `(qname attribute-list . child_node_list)` to xml string representation"
56+
(concat "<" (xml-to-string-parse-qname (symbol-name (car node)))
57+
(if (cadr node) " ")
58+
(xml-to-string-parse-attr-list (cadr node)) ">"
59+
(xml-to-string-parse-child-node-list (cddr node))
60+
"</" (xml-to-string-parse-qname (symbol-name (car node))) ">"))
61+
62+
(defun xml-to-string-parse-child-node-list (child-node-list)
63+
(let (value)
64+
(dolist (node child-node-list value)
65+
(setq value (concat value (xml-to-string-parse-child-node node))))))
66+
67+
(defun xml-to-string-parse-child-node (child-node)
68+
"Convert `node | string' to string representation"
69+
(if (stringp child-node)
70+
child-node
71+
(xml-to-string-parse-node child-node)))
72+
73+
(defun xml-to-string-parse-qname (node)
74+
"Currently just assumes it's name, need to implement namespace url"
75+
node)
76+
77+
(defun xml-to-string-parse-attr-list (attr-list)
78+
"Convert `((qname . \"value\") (qname . \"value\") ...)' to a
79+
string like \"qname=value qname=value\" ..."
80+
(mapconcat (lambda (elem) (concat (symbol-name (car elem))
81+
"="
82+
"\"" (cdr elem) "\"")) attr-list " "))
83+
84+
85+
(provide 'xml-to-string)
86+

0 commit comments

Comments
 (0)