Debian Codegraph @

owner-site/site/docs/xml-support.md

raw symbols SCIP
1 ---
2 layout: docs
3 title: XML support
4 prev_section: configuring
5 next_section: event-support
6 permalink: /docs/xml-support/
7 ---
8
9 Since JDK 1.5 Java properties support XML format via `loadFromXML()` and `storeToXML()`
10
11 There is no reason why OWNER API shouldn't implement XML support as the Properties class.
12
13 But OWNER goes beyond that.
14
15
16 The Java XML Properties format
17 ------------------------------
18
19 The [`java.util.Properties`](http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html)
20 class supports an XML format that looks like this:
21
22 ```xml
23 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
24 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
25 <properties>
26 <comment>this is an example</comment>
27 <entry key="server.ssh.alive.interval">60</entry>
28 <entry key="server.ssh.address">127.0.0.1</entry>
29 <entry key="server.http.port">80</entry>
30 <entry key="server.http.hostname">localhost</entry>
31 <entry key="server.ssh.user">admin</entry>
32 <entry key="server.ssh.port">22</entry>
33 </properties>
34 ```
35
36 Don't you agree that the above example is quite an unstructured XML format?
37
38 OWNER is able to load this XML as configuration properties, but it is also possible to specify an XML more structured,
39 that can be defined from the user on his own taste.
40
41
42 The user's specified XML format
43 -------------------------------
44
45 If you don't like the Java XML Properties format, then you can define an XML of your own:
46
47 ```xml
48 <server>
49 <http port="80">
50 <hostname>localhost</hostname>
51 </http>
52 <ssh port="22">
53 <address>127.0.0.1</address>
54 <alive interval="60"/>
55 <user>admin</user>
56 </ssh>
57 </server>
58 ```
59
60 For OWNER, the above example is equivalent to the example listed in the previous section (the Java XML Properties format)
61
62 XML elements, and XML attributes will be converted to properties names, and attribute values and element values will be
63 converted to properties values.
64
65 This means that the above xml would be equivalent to a properties file like the following:
66
67 ```properties
68 server.http.port=80
69 server.http.hostname=localhost
70 server.ssh.port=22
71 server.ssh.address=127.0.0.1
72 server.ssh.alive.interval=60
73 server.ssh.user=admin
74 ```
75
76 Loading the XML
77 ---------------
78
79 How to load the XML into OWNER Config object? Simple: in the same way as you already do for properties file.
80
81 Suppose you have a *mapping interface* class in a source file called `foo.bar.ServerConfig.java`, just place a
82 resource in your classpath in `foo.bar.ServerConfig.xml`. Or you can specify the `@Sources` annotation with an URL
83 pointing to an `file.xml`. OWNER will notice the `.xml` extension and will load the configuration source as XML.
84
85 The mapping interface for the two above XML examples is the following:
86
87 ```java
88 interface ServerConfig extends Config {
89
90 @Key("server.http.port")
91 int httpPort();
92
93 @Key("server.http.hostname")
94 String httpHostname();
95
96 @Key("server.ssh.port")
97 int sshPort();
98
99 @Key("server.ssh.address")
100 String sshAddress();
101
102 @Key("server.ssh.alive.interval")
103 int aliveInterval();
104
105 @Key("server.ssh.user")
106 String sshUser();
107 }
108 ```
109
110 Storing to XML
111 --------------
112
113 If you are looking for the method to store a Config object into an XML stream, you should have a look at the method
114 [`storeToXML`][storeToXML] in the `Accessible` interface.
115
116 [storeToXML]: http://owner.aeonbits.org/apidocs/latest/org/aeonbits/owner/Accessible.html#storeToXML(java.io.OutputStream,+java.lang.String)
117
118 When saving a Config object to XML, the Java XML Properties format will be used.
119
120 Conclusions
121 -----------
122
123 If you like XML format more than properties format (well, *I don't*), you can use XML to achieve the same result as
124 using properties files.