I am working on an Android application (Java) that uses the Yamaha Blu-ray Player API through HTTP POST. The player has a strict set of commands in XML format. Commands follow a hierarchy: while most external XML elements are always the same, the structure inside belongs to the type of player function. For example, the play / pause / stop functions have the same path in XML, while the skip functions have different parent elements. You can see what I mean in the following code example.
public enum BD_A1010 implements YamahaCommand { POWER_ON ("<Main_Zone><Power_Control><Power>On</Power></Power_Control></Main_Zone>"), POWER_OFF ("<Main_Zone><Power_Control><Power>Network Standby</Power></Power_Control></Main_Zone>"), TRAY_OPEN ("<Main_Zone><Tray_Control><Tray>Open</Tray></Tray_Control></Main_Zone>"), TRAY_CLOSE ("<Main_Zone><Tray_Control><Tray>Close</Tray></Tray_Control></Main_Zone>"), PLAY ("<Main_Zone><Play_Control><Play>Play</Play></Play_Control></Main_Zone>"), PAUSE ("<Main_Zone><Play_Control><Play>Pause</Play></Play_Control></Main_Zone>"), STOP ("<Main_Zone><Play_Control><Play>Stop</Play></Play_Control></Main_Zone>"), SKIP_REVERSE ("<Main_Zone><Play_Control><Skip>Rev</Skip></Play_Control></Main_Zone>"), SKIP_FORWARD ("<Main_Zone><Play_Control><Skip>Fwd</Skip></Play_Control></Main_Zone>"); private String command; private BD_A1010 (String command) { this.command = command; } public String toXml () { return "<?xml version=\"1.0\" encoding=\"utf-8\"?><YAMAHA_AV cmd=\"PUT\">" + this.command + "</YAMAHA_AV>"; } }
As you can see, I tried a flat enumeration option that works fine. I can use the enumerations with my RemoteControl class like this:
remoteControl.execute(BD_A1010.PLAY);
The enum.toXml () method returns the full XML code needed to send to the player. Now here is my problem: I need a better way to build a hierarchy of functions in Java classes. I want to use it like this:
remoteControl.execute(BD_A1010.Main_Zone.Power_Control.Power.On);
Like nested enumerations or classes. Each level of the team must define its own XML element within itself. In addition, each command on the path can only determine the subcommands that are possible: for example, after Main_Zone.Play_Control I can use only .Play or .Skip, and not Tray or anything else. At the end of the chain, I like to call .toXml () to get the full XML command.
What is the best way in Java to define this hiarachy as (nested) classes? It should be easily defined - as little code as possible.
Later it should be possible to combine two or more commands to get the combined XML as follows - but this is not so important for the first attempt.
remoteControl.execute( BD_A1010.Main_Zone.Power_Control.Power.On, BD_A1010.Main_Zone.Play_Control.Skip.Rev, BD_A1010.Main_Zone.Play_Control.Play.Pause ); <Main_Zone> <Power_Control> <Power>On</Power> </Power_Control> <Play_Control> <Skip>Rev</Skip> <Play>Pause</Play> </Play_Control> </Main_Zone>