What is Swush
Overview
Swush (pronounce s-woo-sh) is a simple data exchange language that is designed to be easy for people to read and manipulate (unlike XML, which is designed to be difficult for both people and for machines)
A Swush file can be as simple as a key:value list, or a complex nested structure.
Specification
Before I get into details, here is a sample swush file:
The following is an example of a swush file with all of the above elements:
@swush 1.0 utf-8 server{ connector{ /* multiline comment */ protocol : http // singleline comment port : 8080 # singleline comment host : localhost admin_emails { root@localhost john@dawg.com } } log{ output{ file : /var/log/server.log format : %t%s } } contact{ name: "Omry Yadan" email: john@dawg.com } }
Swush ignores whitespaces, unless they are inside a quoted string.
Each node is a list of Swush elements, each element can one of:
Each node is a list of Swush elements, each element can be:
- a key-value pair, for example, this the following line key is format and the value is %s%t :
format:%s%t - a String, for example, the following node is named drinks and contains 3 string elements:
drinks{ Coffee Tea “Bloody marry” } - a name followed by a swush node inside curly brackets, for example – the following swush node is named server, and contains a key:value pair:
server{
host:localhost
}
A swush file must begin with an header line:
@swush 1.0 [encoding]
where encoding is optional and is the encoding of the file (default is utf-8)
Comments
Swush supports two types of comments:
- C Multiline comments
- C++/Bash style single line comments
Some examples:
/* a multil line comment */ // Single line comment # Another single line comment key:value # Trailing single line comment
Selectors
Swush implementation supports a simple selector language that allow the user to easily access nested nodes.
for example, given supposed we have the following swush file:
@swush 1.0 addressbook{ entry{ name: "John dog" email: dog@john.com } entry{ name: "Jane bitch" email: jane@bitch.org } }
the following selector will select the two entries:
addressbook.entry
an empty selector matches all the nodes in the root of the file.
in the future, it’s possible that the selectors language will become more powerful.
Java Implementation
Usage example:
java -jar swush-x.y.jar swush.file selector
Programmatic usage example:
public class Example { public static void main(String[] args) throws SwushException, IOException { // You can construct a swush object in several ways: // by parsing a file Swush fromFile = new Swush(new File("test-data/test1/file.swush")); // straight from a string Swush fromString = Swush.fromString("swush:rocks node{a:b}"); // programatically Swush root = Swush.node("node1"); root.addPair("key","value"); Swush node2 = root.addNode("node2"); node2.addPair("key", "value1"); node2.addPair("key", "value2"); node2.addArray("array", "a", "b", "c"); // This will select the two key-value nodes and the array node in node2: List selected = root.select("node2.key"); // Selects the first key in node2 Swush first = root.selectFirst("node2.key"); // Selects the value of the first node2.key element ("value1") String value = root.selectProperty("node2.key"); } }
You can download swush from here.