bitrazor.com ...THE source for mediocre content                                      

TiVo HME How-To: Read an App Defaults File             

Many applications need to be able to read a set of default values out of a local config file.  Here's an example of how to do that.

  • Create the XML config file using an XML editor like XMLSpy (they have a free home version).  Use my example to get started: app-defaults.xml
  • Use XMLSpy to generate a schema (XSD) from the XML.  Remove any enumerations.  Here's the finished example: app-defaults.xsd
  • Install JWSDP 1.5, which includes JAXB.
  • Create a directory in your Eclipse workspace (my workspace is in My Documents\TiVo Projects) called jaxbAppDefaults.
  • Copy app-defaults.xsd into this new directory.
  • Open a Command Window, cd into this directory, then run the following command:

%JWSDP_HOME%\jaxb\bin\xjc.bat -p com.bitrazor.jaxb.appdefaults app-defaults.xsd

  • This will generate a bunch of Java classes in that directory.
  • Now, go into Eclipse, and create a new project with the same name as the directory you created (jaxbAppDefaults).  Eclipse will discover there's already source there & it will do the right thing.
  • Right-click on the Project, do Properties, Java Build Path, Add External JARs, and add the following JARs:
  • This project should now build in Eclipse without errors (there's one warning I ignore).

  • Now create a second project in Eclipse called AppDefaultsTest.  Create a package called 'com.bitrazor.appdefaults', and add a Main class to it.
  • Add a project reference (right-click on the project folder, select Properties, click the Projects tab, click Add, then select 'jaxbAppDefaults'.
  • Right-click on the Project, do Properties, Java Build Path, Add External JARs, and add the following JARs:
  • Drag app-defaults.xml onto the project folder in Eclipse.  It will be copied into the project to the right spot.
  • Add the following code to main() to finish up.  Run it to verify the output.
package com.bitrazor.appdefaults;

import java.io.FileInputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import com.bitrazor.jaxb.appdefaults.AppDefaults;

public class Main {

public static void main(String[] args) {
    getAppDefaults();
}

public static void getAppDefaults() {
    String jaxbContext1 = "com.bitrazor.jaxb.appdefaults";
    String appDefaultsFilename = "app-defaults.xml";

    JAXBContext jc = null;
    Unmarshaller u = null;
    AppDefaults appDefaults = null;
    try {
        jc = JAXBContext.newInstance(jaxbContext1);
        u = jc.createUnmarshaller();
        appDefaults = (AppDefaults) u.unmarshal(
           new FileInputStream(appDefaultsFilename));
    } catch (Exception e) {
        e.printStackTrace();
    }

    String regionMasterURLString = appDefaults.getRegionMasterURL();
    String showTime = appDefaults.getShowTime();
    String fadeTime = appDefaults.getFadeTime();
    String defaultRegion = appDefaults.getDefaultRegion();

    System.out.println("These values were retrieved from " +
       "the app-defaults file:");
    System.out.println("RegionMasterURL: "+ regionMasterURLString);
    System.out.println("ShowTime: "+ showTime);
    System.out.println("FadeTime: "+ fadeTime);
    System.out.println("DefaultRegion: "+ defaultRegion);
    }
}

 

                                                         Last updated: February 19, 2007