May 25, 2013

Generating Collada loader with JAXB

Collada is a XML 3D Model data format. I was looking for a library that can automatically load in structure. I found JAXB that does exactly what I was looking for. However, there was a little trouble generating Java source code.

After struggling for a few hour, I was able to generate JAXB source codes from Collada Schema v1.5.
Here is how I did and it is for anybody who may have the problem I was having.

I got the Collada schema file from here.
At my first try, I got errors:
> xjc.exe -p collada collada_schema_1_5.txt
[ERROR] Property "Source" is already defined. Use <jaxb:property> to resolve this conflict.
[ERROR] The following location is relevant to the above error
This error is due to name conflict. The name "source" is defined for an attribute and inner element. It can be like this:
<skin_type source="somekindofURL"> <source></source> </skin_type>
 The generated Java source code from JAXB will have a member method "getSource()" for both the element and the attribute so does the conflict happens.

This issue can be resolved by providing "binding" information that simply rename one of them from "source" to something else. The binding information looks like this:
<bindings br="" xmlns="http://java.sun.com/xml/ns/jaxb">          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          version="2.1">
    <bindings schemalocation="collada_schema_1_5.xsd" version="1.0">

        <!-- rename the value element -->
        <bindings node="//xs:complexType[@name='skin_type']">
            <bindings node=".//xs:attribute[@name='source']">
                <property name="source_attr">
            </property></bindings>
        </bindings>
       
        <bindings node="//xs:complexType[@name='morph_type']">
            <bindings node=".//xs:attribute[@name='source']">
                <property name="source_attr">
            </property></bindings>
        </bindings>

    </bindings></bindings>
But this solves only half of the problem.
> xjc.exe -b collada_binding.xjc -p collada collada_schema_1_5.xsd
[ERROR] Property "MiOrMoOrMn" is already defined. Use <jaxb:property> to resolve this conflict.
 This error was easier to resolve by internet searching.
https://jaxb.java.net/guide/Dealing_with_errors.html
https://weblogs.java.net/blog/kohsuke/archive/2006/03/simple_and_bett.html

This issue can be solved by having an extension file:
<xs:schema br="" targetnamespace="no-such-thing">    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
    xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">
  <xs:annotation>
      <xs:appinfo>
        <jaxb:globalbindings>
            <xjc:simple>
        </xjc:simple></jaxb:globalbindings>
      </xs:appinfo>
  </xs:annotation></xs:schema>
 Now JXB should be able to generate the Java source files from Collada schema.
> xjc.exe -extension simpleMode.xjc -b collada_binding.xjc -p collada collada_schema_1_5.xsd
 I am not sure what kind of magic this extension file is playing but it works for me for now...

May 23, 2013

McAfee Online Backup

Yesterday I purchased McAfee Online Backup service.
I think it was a good deal; *unlimited* online backup storage for $60/year.

Their webpage didn't give me much information about their service. I was wondering what kind of options I can tweak, how many computers I can use, how can I restore files, how long they will keep my data after my license expires and so on.

What I found after buying it are:
  1. $60/year is only for one computer. I need to add another $60/year for another computer but it seems that they give some discount from 3rd computer.
  2. They delete every backup data as soon as my license expires.
  3. Since my internet uploading speed is not as fast as downloading speed, it seems to take for a while to consume big amount of promised backup storage.
  4. One license covers only one computer to be using the backup software. But it seems that I can download any backup files from any other computers via web user interface. I think this is very useful and otherwise I will be complaining a lot.

The Backup Options:
The backup software runs as a windows tray icon. It doesn't give me complex tweaks. Basically it lets me select which folders to be backed up. It also has some network traffic controlling and CPU utilization checkup to be a less intrusive background task.

The Web Restore:
A picture above is take from their web restore user interface. I can select files to download, then the system generate a ZIP file that includes the files. It seems to take some time to generate the file, which is understandable; when the data size is big, the system will have to spend some time to compress them. The web user interface doesn't seems to allow me to download more than 20GB at once, which is a minor limitation for me.

The Price:
My internet uploading speed is about 0.1MByte/s, which means I can upload 8GByte/day and 3TByte/year. So assuming that I have lots of data to backup 24/7, this is a good deal. Because it is like I am paying $60 for 3TB for the first year and in the next year I will be paying another $60 for another 3TB top of the 3TB I used the previous year.

Comparing to HDD price, I think this service is a good deal although I thought that $60 is little too much at first thought. These days the HDD price is around $90~$140 for 1~3TByte. Although HDD last more than just one year, most of time the limited-warranty is 1/2 years. After 3years later, HDD may stop working and there is a risk to lose all the back up data.

The real question for me at this point is whether I actually have data to backup that much unless I want to securely backup all the HD movie files.

*PS: I tried to access Network Mounted Drive from McAfee Backup software but it didn't show the drive at all. I believe there gotta be a way to trick the software and allow me to access remote files without another license.

May 11, 2013

Java lamda expression in version 8 next year.

Java will have Lamda expression on the coming new version 8 next year. This info is from the official java web site: here

This anonymous class feature was from the day one.
printMembers(
    roster,
    new CheckMember() {
        public boolean test(Member p) {
            return p.getGender() == Member.Sex.MALE
                && p.getAge() >= 18
                && p.getAge() <= 25;
        }
    }
);

But with the new Lamda expression it will become like this:

printMembers(
    roster,
    (Member p) -> p.getGender() == Member.Sex.MALE
        && p.getAge() >= 18
        && p.getAge() <= 25
);

Here is how the magik works.
The interface "CheckMember" should have only one member method. The interface that has only one member method is called "functional interface".

interface CheckMember {
    boolean test(Member p);
}

Because functional interface has only one member method, we can omit the obvious function name.

May 5, 2013

iTunes auto sync over Wifi fixed..

I have been having a trouble on auto sync feature between my iTunes and my iPhone.
When I was searching Google, many people fixed the same problem by de-selecting "prevent iPods,iPhones, and iPads from syncing automatically" or selecting "Sync with this iPhone over Wi-Fi".

But it didn't fix my problem. One of guys claimed that he fixed the issue by clean uninstalling iTunes with registry cleaner. It didn't work for me too.

Today I fixed the problem. My problem was on Bonjour. It was installed at "C:\Program Files (x86)\Bonjour" and it didn't go away even after I uninstalled every Apple applications. It turned out that it didn't go away because one of DLL files in the folder was still in use by AirDisplay.

Deleting DLL was little tricky because Windows doesn't allow users to alter a file while it is open by any processes. Somehow there were more than AirDisplay was using the Bonjour DLL. I opened CMD window and renamed the DLL in the Bonjour folder and delete the folder. I restarted Windows7 and re-installed iTunes and AirDisplay.

Mileage will vary but this way fixed my problem and I hope this info can help others too.

Reusable Logic Flow code can be separated from platform depended code implementation

Yesterday I came up with an interesting idea about programming language.
The idea is from a question how I can separate detail code implementation from abstract logic.

For example, Java introduced a powerful concept called Interface. An interface is nothing but a collection of method signature. It cannot contain any implementation or any variables. First time when I saw it, I thought it is useless because it does nothing for me without any implementation. But the important aspect of interface was to separate the abstract layer of programming code from detail implementation. It also allowed us to reuse the abstract layer a lot more and also allowed us to replace old implementation without affecting other code that are relying on the interface.


I didn't extend my idea from interface but later I found the similarity. And it is easier to understand from the idea of Interface. A function is usually mixed with Logic and implementation details. Let me show us a code example first. This code is taken from a random Google search: http://www.programmingsimplified.com/c-program-copy-file

#include "stdio.h"
#include "stdlib.h"
 
int main()
{
   char ch, source_file[20], target_file[20];
   FILE *source, *target;
 
   printf("Enter name of file to copy\n");
   gets(source_file);
 
   source = fopen(source_file, "r");
 
   if( source == NULL )
   {
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }
 
   printf("Enter name of target file\n");
   gets(target_file);
 
   target = fopen(target_file, "w");
 
   if( target == NULL )
   {
      fclose(source);
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }
 
   while( ( ch = fgetc(source) ) != EOF )
      fputc(ch, target);
 
   printf("File copied successfully.\n");
 
   fclose(source);
   fclose(target);
 
   return 0;
}

Although the program itself is short and easy to understand, the issue is that functional logic flow and implementation details are mixed.This problem may not be obvious for short program like this. But when we want the program to be running on multiple platforms, we have to replace implementation details.

First remedy for multi-platform support is usually "#ifdef PLATFORM_A / #else / #endif".
It gets harder to read and gets longer to read.

The point of my view is that even when the implementation details can vary, the logic flow should stay.

After I apply my idea the code becomes like this:
class ICopyFileLogic
{
public:
 virtual void GetFilenameToCopy();
 virtual void OpenSourceFile();
 virtual bool IsSourceFileOpen();
 virtual void NoticeOpenFailed();
 virtual void GetFilenameToCreate();
 virtual void OpenTargetFileToWrite();
 virtual bool IsTargetFileOpen();
 virtual void NoticeTargetOpenFailed();
 virtual bool GetOneByte();
 virtual void WriteOneByteOnTargetFile();
 virtual void NoticeCopySuccessful();
 virtual void CloseFileHandles();
};
 
int _tmain(int argc, _TCHAR* argv[])
{
 ICopyFileLogic* cf = new PCopyFile_Win32();

 cf->GetFilenameToCopy();
 cf->OpenSourceFile();
 
 if( !cf->IsSourceFileOpen() )
 {
  cf->NoticeOpenFailed();
  exit(EXIT_FAILURE);
 }

 cf->GetFilenameToCreate();
 cf->OpenTargetFileToWrite();
 
 if( !cf->IsTargetFileOpen() )
 {
  cf->NoticeTargetOpenFailed();
  exit(EXIT_FAILURE);
 }
 
 while( cf->GetOneByte()  )
  cf->WriteOneByteOnTargetFile();

 cf->NoticeCopySuccessful();
 cf->CloseFileHandles();
 return 0;
}

class PCopyFile_Win32 : public ICopyFileLogic
{
 char ch, source_file[20], target_file[20];
 FILE *source, *target;

 void GetFilenameToCopy()
 {
  printf("Enter name of file to copy\n");
  gets(source_file);
 }

 void OpenSourceFile()
 {
  source = fopen(source_file, "r");
 }

 bool IsSourceFileOpen()
 {
  return source != NULL;
 }

 void NoticeOpenFailed()
 {
  printf("Press any key to exit...\n");
 }

 void GetFilenameToCreate()
 {
  printf("Enter name of target file\n");
  gets(target_file);
 }

 void OpenTargetFileToWrite()
 {
  target = fopen(target_file, "w");
 }

 bool IsTargetFileOpen()
 {
  return target != NULL;
 }

 void NoticeTargetOpenFailed()
 {
  fclose(source);
  printf("Press any key to exit...\n");
 }

 bool GetOneByte()
 {
  return ( ch = fgetc(source) ) != EOF;
 }

 void WriteOneByteOnTargetFile()
 {
  fputc(ch, target);
 }

 void NoticeCopySuccessful()
 {
  printf("File copied successfully.\n");
 }

 void CloseFileHandles()
 {
  fclose(source);
  fclose(target);
 }
};
  
When the idea was in my head, it was more interesting than after I wrote them down. The new version of the code is longer and also it utilizes stack memory less than before, which I think not preferable. It will also prevent the compiler from optimizing harder.

But the good part is, as I discussed earlier, the logic flow is reusable so that we can replace the platform dependent code implementation without affecting the logic flow. The "main" function doesn't even need to include stdio.h or stdlib.h.

I may need to find a better example than this to convince anybody else of the usefulness of the idea.