Information on using the JConvert API
The JConvert API is very powerful and allows you to leverage all of JConvert's functionality from within your application. Adding the JConvert API is simple. Place the jconvert-version.jar file in your classpath and use it. There are no dependencies on any other third party jars. Please note that this API was not intended for you to use JConvert's swing GUI. JConvert uses a custom designed logger that will log using log4j if log4j is found in the classpath - so getting the logging from it is very simple.
Using the API
Please use the Java API link here or at the top of the page to access the API information for JConvert. In the source, there are some example classes that show you how to interface with the API. Here is a link to the test section which has some API Examples. Below I will give excerpts and explain.
To get a hold of the conversion data that JConvert uses, simply add this line of code:
List domainData = new DataLoader().loadData();
Next you will want to get a hold of the conversion category/type. In this case I am going to get a handle to the Distance
conversion types. You can externalize this as a helper method, but all we are doing is going through the list of
ConversionTypeData objects that we received by calling the DataLoader and looking for the Distance type.
Once you get this ConversionTypeData object, you can perform your conversions against it. You will need to use the string
representation for the conversion name. You can find these names from the UI.
ConversionTypeData ctd = null;
Iterator iter = domainData.iterator();
while (iter.hasNext()) {
ConversionType type = (ConversionType) iter.next();
if (type.getTypeName().equals("Distance")) {
ctd = new ConversionTypeData(type);
break;
}
}
System.out.println("1 mile = " + ctd.convert(1.0, "mile", "meter") + " meters");
System.out.println("1 mile = " + ctd.convertFraction("1", "mile", "meter") + " meters");
If you use different names, you have a couple of choices. Lets use the example that your name for "meter" is "meters".
Now, you can have your own mapping file that every time you want to convert "meters", you translate it first. So your
code may look like this:
ctd.convert(1.0, "mile", AliasFactory.getInstance().translate("meters"))
You other choice is to add a custom conversion for 1 meter = 1 meters. Then your code would look like:
ctd.convert(1.0, "mile", "meters")
Replacing the currency conversions
Currency conversions was somewhat of a tricky matter. All of the current conversions in JConvert are fixed. Currency conversions change dynamically throughout the day. In order to provide conversions for currency, we must pull the data from somewhere on a regular basis. Currency data is tricky as well. If you want it free, you are going to have to pull it from a site that gives it away for free, but this usually means that the data is stale, like getting 20 minute delayed quotes for the stock market. I have implemented a solution for this that goes out to some public websites and pulls data from a web page. If you are a business, then you probably want more accurate data. While I can consult on this, I have provided an architecture to allow you to develop your own. All you have to do is implement the com.edsdev.jconvert.common.CustomConversionDataInterface. There are examples of this if you look at the source com.edsdev.jconvert.common.CustomConversionCurrency. You can point your implementation at a paysite, an EJB, an RSS feed, or whatever you want.
For the geek at heart, this also provides a way for you update these custom classes if the websites change their format, or experiment with pointing at sites that have a more comprehensive selection of currencies.