New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement XML properties support. #5

Closed
lviggiano opened this Issue Jan 20, 2013 · 5 comments

Comments

Projects
None yet
2 participants
@lviggiano
Owner

lviggiano commented Jan 20, 2013

Since JDK 1.5 Java properties support XML format via loadFromXML() and storeToXML()

There is no reason why OWNER API shouldn't implement those two methods.

@maroun-baydoun

This comment has been minimized.

Show comment
Hide comment
@maroun-baydoun

maroun-baydoun Jan 20, 2013

The ability to marshal/unmarshal XML documents is already implemented in JAXB, so maybe that can be a starting point.

maroun-baydoun commented Jan 20, 2013

The ability to marshal/unmarshal XML documents is already implemented in JAXB, so maybe that can be a starting point.

@lviggiano

This comment has been minimized.

Show comment
Hide comment
@lviggiano

lviggiano Jan 20, 2013

Owner

sure thing, but it's easier than that: java.util.Properties have the method already implemented, I just need to forward the method invokation. See PropertyInvocationHandler:

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // I just need to add something like:
    // if (method == storeToXML()) {
    //      properties.storeToXML(args); 
    //      return null;
    // }
    String key = key(method);
    String defaultValue = defaultValue(method);
    String value = properties.getProperty(key, defaultValue);
    if (value == null)
        return null;
    return convert(method.getReturnType(), format(value, args)); // TODO: variable expansion here would be nice
}

Very simple.

Then change the Config interface to add the storeToXML() method.

It's a little bit more complicate intead for the load (which I should implement in the ConfigFactory.load(InputStream))

Owner

lviggiano commented Jan 20, 2013

sure thing, but it's easier than that: java.util.Properties have the method already implemented, I just need to forward the method invokation. See PropertyInvocationHandler:

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // I just need to add something like:
    // if (method == storeToXML()) {
    //      properties.storeToXML(args); 
    //      return null;
    // }
    String key = key(method);
    String defaultValue = defaultValue(method);
    String value = properties.getProperty(key, defaultValue);
    if (value == null)
        return null;
    return convert(method.getReturnType(), format(value, args)); // TODO: variable expansion here would be nice
}

Very simple.

Then change the Config interface to add the storeToXML() method.

It's a little bit more complicate intead for the load (which I should implement in the ConfigFactory.load(InputStream))

@lviggiano

This comment has been minimized.

Show comment
Hide comment
@lviggiano

lviggiano Jan 24, 2013

Owner

Looking at how XML properties are implemented in Java, now I find the reason why not to have them in Owner API.

Not sure this will be available anytime soon.

Owner

lviggiano commented Jan 24, 2013

Looking at how XML properties are implemented in Java, now I find the reason why not to have them in Owner API.

Not sure this will be available anytime soon.

@lviggiano

This comment has been minimized.

Show comment
Hide comment
@lviggiano

lviggiano Feb 9, 2013

Owner

link to #14

Owner

lviggiano commented Feb 9, 2013

link to #14

@lviggiano

This comment has been minimized.

Show comment
Hide comment
@lviggiano

lviggiano Jul 21, 2013

Owner

Almost done :)

on master branch OWNER is able to load xml files into properties. The format can be the one specified by the DTD of the properties class, or an xml specified by the user on his own taste.

Example of xml accepted, as specified by the DTD in properties class:

The ugly java format:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>this is an example</comment>
    <entry key="server.ssh.alive.interval">60</entry>
    <entry key="server.ssh.address">127.0.0.1</entry>
    <entry key="server.http.port">80</entry>
    <entry key="server.http.hostname">localhost</entry>
    <entry key="server.ssh.user">admin</entry>
    <entry key="server.ssh.port">22</entry>
</properties>

A better example (the content is the same as above, and both will work the same way for OWNER):

<server>
    <http port="80">
        <hostname>localhost</hostname>
    </http>
    <ssh port="22">
        <address>127.0.0.1</address>
        <alive interval="60"/>
        <user>admin</user>
    </ssh>
</server>

Example of the mapping interface for the above xml examples

interface ServerConfig extends Config {

    @Key("server.http.port")
    int httpPort();

    @Key("server.http.hostname")
    String httpHostname();

    @Key("server.ssh.port")
    int sshPort();

    @Key("server.ssh.address")
    String sshAddress();

    @Key("server.ssh.alive.interval")
    int aliveInterval();

    @Key("server.ssh.user")
    String sshUser();
}
Owner

lviggiano commented Jul 21, 2013

Almost done :)

on master branch OWNER is able to load xml files into properties. The format can be the one specified by the DTD of the properties class, or an xml specified by the user on his own taste.

Example of xml accepted, as specified by the DTD in properties class:

The ugly java format:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>this is an example</comment>
    <entry key="server.ssh.alive.interval">60</entry>
    <entry key="server.ssh.address">127.0.0.1</entry>
    <entry key="server.http.port">80</entry>
    <entry key="server.http.hostname">localhost</entry>
    <entry key="server.ssh.user">admin</entry>
    <entry key="server.ssh.port">22</entry>
</properties>

A better example (the content is the same as above, and both will work the same way for OWNER):

<server>
    <http port="80">
        <hostname>localhost</hostname>
    </http>
    <ssh port="22">
        <address>127.0.0.1</address>
        <alive interval="60"/>
        <user>admin</user>
    </ssh>
</server>

Example of the mapping interface for the above xml examples

interface ServerConfig extends Config {

    @Key("server.http.port")
    int httpPort();

    @Key("server.http.hostname")
    String httpHostname();

    @Key("server.ssh.port")
    int sshPort();

    @Key("server.ssh.address")
    String sshAddress();

    @Key("server.ssh.alive.interval")
    int aliveInterval();

    @Key("server.ssh.user")
    String sshUser();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment