Code Nest
Software issues and opinons by Darin Tanaka
March 1st, 2012 Comments: 0

Sorry it's been a while since my last post, I've been pretty busy so I wasn't able to update this blog at all. I wanted to show the easiest way I could find to convert an XML file to a Java object. There are many ways to do this however I wanted to find just a simple solution that wouldn't require me downloading extra jar files. Also I wanted to avoid a long complicated process to do this. I choose to use JAXB instead of something like XStream just for the fact that JAXB is included in Java 1.6, this way I could avoid having to download a third party jar (especially since I'm using ANT). Also I wanted to do this using an XSD (XML Schema) because this way the XML schema can be changed by someone who doesn't understand JAXB annotations or Java even. Ok so just to be clear I'm going to show the way I converted XML to Java using:

  • An XSD file
  • JAXB

1. First get your XSD file ready (example XSD from http://www.w3schools.com/schema/schema_example.asp)

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="shiporder">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="orderperson" type="xs:string"/>
          <xs:element name="shipto">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="address" type="xs:string"/>
                <xs:element name="city" type="xs:string"/>
                <xs:element name="country" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="item" maxOccurs="unbounded">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="title" type="xs:string"/>
                <xs:element name="note" type="xs:string" minOccurs="0"/>
                <xs:element name="quantity" type="xs:positiveInteger"/>
                <xs:element name="price" type="xs:decimal"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
       </xs:sequence>
      <xs:attribute name="orderid" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

2. Run the xjc.exe/xjc.sh on the XSD file to generate the JAXB annotated class and object factory

xjc -p net.mytestname.helloworld shiporder.xsd

This xjc executable is located in the ${JAVA_HOME}/bin folder if your using JDK1.6 (remember it's only in the JDK). The -p option is the name of the package you wish the generated class to use. The last argument is the name of the XSD file to use.

3. After generating the java class file and the object factory java file, place these files into the appropriate folder in your java project (Whatever you defined your package name to be)

4. Once you have generated this java files it's actually very easy now with JAXB, just create a JAXBContext and then create a unmarshaller from the JAXBContext. Then just simply unmarshall the xml file to a java object.

JAXBContext context = JAXBContext.newInstance("net.mytestname.helloworld");
Unmarshaller unmarshaller = context.createUnmarshaller(); 
ShipOrder order = unmarshaller.unmarshal(new FileReader("C:\shiporder.xml"));

And that's all there is to it with JAXB. This was the easiest way I could find using an XSD file.

July 30th, 2011 Comments: 0

Here's a quick tip if your having trouble with an embed flash player rendering over content that you want shown in your web page. You need to set the wmode variable to "transparent" Here's an example with a embed youtube player.

<object width="560" height="349" 
classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" 
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="allowFullScreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="src" value="http://www.youtube.com/v/QPyVwPO3h4U?version=3&amp;hl=en_US&amp;rel=0" />
<param name="allowfullscreen" value="true" />
<embed width="560" height="349" type="application/x-shockwave-flash" 
src="http://www.youtube.com/v/QPyVwPO3h4U?version=3&amp;hl=en_US&amp;rel=0" 
allowFullScreen="true" allowscriptaccess="always" allowfullscreen="true" 
wmode="transparent" /></object>

June 6th, 2011 Comments: 0

Here's a quick note about css I found. I wanted to create a fixed height <div> without having to use the position:absolute tag. In order to accomplish this I needed to set the height of the html and body attributes to 100%. Like below:

html {
  height: 100%;
}

body {
  height:100%; /* this is the key! */
  margin: 0 auto;
}

div.ecard{
  float: left;
  font-size: 1.5em;
  width: 80%;
  height: 80%;
}
May 11th, 2011 Comments: 0

When I was developing my Android app Call Me Not, I struggled a bit trying to figure out how to use the ADB Android provides with it's SDK. The developer guide has a pretty good page on all the different features, however I was really confused on how to even get started. It seemed like the page was geared towards Linux users. This may be useless to many people however I myself had a hard time getting started with ADB so I'll give a quick guide how to access your sqlite3 database for your emulator using ADB in Windows. Ok so first of all, the ADB is in the android-sdk-windowsplatform-tools folder, that or you could just search for "ADB" and the executable will show up. However don't be like me I think that adb.exe is a GUI, it can only be accessed through the CMD.

1. Open up cmd.exe

2. Goto the 'android-sdk-windowsplatform-tools' directory, if your not familiar with cmd.exe, "dir" lists all files and directories, and "cd" goes into that directory.

3. You should see adb.exe if you enter in the "dir" command after navigating to the 'platform-tools' folder The only way you can use adb.exe is if an instance of an Android emulator is running. In order to determine this enter in the command "adb.exe devices" in the cmd. See picture below

ADB devices

If you don't see any devices listed this means that your emulator is not running and you won't be able to use adb.

4. In order to access the sqlite3 database for your emulator you need to enter the adb shell. To do this you need to add the "-s" option along with the emulator name and the "shell" option. For example see below:

ADB Shell

the emulator name will be the one listed from the "adb.exe devices" command. You should see the cursor now next to the "#" sign.

5. Once your in the shell you need to use the sqlite3 command to access the sqlite3 database, it takes one argument, the path of the database. The path will have the format: /data/data/<your package name>/databases/<your database name> <your package name>: this is just the package name for your Android app <your database name>: assuming your using SQLiteOpenHelper this is the database name you use for the constructor, ex "items.db" See below for an example:

ADB sqlite3

6. Now that your in the database, you can start issuing commands to see what's in your tables. Commands I use: .schema - shows all the different tables and their schema structure .tables - lists all tables in the database From there you can just issue SQL commands like "select * from prices;" and it'll list all the rows in that table. See picture below for an example: ADB database

I hope this helps anyone that's not sure how to use the ADB in the Android SDK for Windows.

February 28th, 2011 Comments: 1

About a year ago for my senior design project I needed to setup Java for Ubuntu Linux to communicate with an RS-232 port. It took me some time to figure out how to do this so I thought I would share my solution with anybody that had a similar need. This setup requires a USB-to-Serial cable.

1. Download Java Comm API from http://www.oracle.com/technetwork/java/index-jsp-141752.html

2. Follow the steps in this blog post to setup USB-to-Serial cable for linux

3. Copy libLinuxSerialParallel.so to /usr/lib/

4. Copy javax.comm.properties to [JDK-directory]/jre/lib/

5. Copy comm.jar to [JDK-directory]/lib/

6. Setup classpath by typing the below in a terminal:

> export CLASSPATH=$CLASSPATH:[location of comm.jar]

Example

> export CLASSPATH=$CLASSPATH:/usr/lib/jvm/java-6-openjdk /jre/lib/ext/comm.jar

Notes: Apprantely Linux by default automatically converts LF bytes into two bytes LF and CR whenever sending data through a serial port. Also by default whenever it receives a CR it converts it into an LF byte. This has been a well known issue and there exists a fix. By running the below commands we were able to get rid of this problem. "stty -F /dev/ttyUSB0 -onclr" "stty -F /dev/ttyUSB0 -icrnl" /dev/ttyUSB0 can be whatever file descriptor you are using. - Install JDK 1.6 as earlier versions has had historical problems with the Java Comm API - When testing, launching the Jar file in Netbeans gave no problem, however when trying to launch the jar file in the command line I received the below error:

> java -jar carInterface.jar javax.comm: Error loading javax.comm.properties! ... ...

I was able to fix this by copying the javax.comm.properties file into the same directory as the jar file. Which is weird because according to the javadoc the first place javax.comm looks is the [JDK-directory]/jre/lib directory.

January 2nd, 2011 Comments: 0

I've been working on an ExtJS project, and just recently discovered something else. I wanted to create a DropTarget inside a BoxComponent, however in my notifyDrop() function I couldn't figure out how to get a reference to the BoxComponent. There was no clear way in the API documentation or on the Sencha forums. I did notice there was a reference to the DOM HTML element for the BoxComponent. Below was how I got the BoxComponent reference when creating my DropTarget

notifyDrop:function(dd, e, data){
  var myBoxComp = Ext.getCmp(Ext.get(this.el).id);
}
December 23rd, 2010 Comments: 0

I recently just got an ExtJS Drag and Drop from a Grid to a BoxComponent to work. The problem with all the examples online was that it didn't have any object oriented fashion of creating a Drag and Drop sequence. All of them were just creating variables sequentially to build the the drag and drop functionality. So what happened was that the container with the DropTarget object needed to have a reference of the RowSelectionModel from the Grid. This makes it difficult to implement object-oriented concepts. Also there's this obscure 'data' object that gets passed into a event function such as notifyDrop() as below.

var dd = new Ext.dd.DropTarget(this.el.dom, {
  // must be same as for tree
  ddGroup:'tl'

  // what to do when user drops a node here
  ,notifyDrop:function(dd, e, data) {
    alert(data.selections[0].data['ACID']);
    return true;
  } // eo function notifyDrop

});

"What the hell is this data object?" The ExtJS documentation doesn't have any clear place where it is and what it's about. But apparently it has an array 'selections' which then contains an array with the data that was selected. I was able to find out how to use it by scurrying forums everywhere. Why can't the documentation clearly state what this object is all about instead of just

data : Object
An object containing arbitrary data supplied by the drag source

Thank you Sencha that's very clear!

November 7th, 2010 Comments: 0

When developing my webapp, I ran into a problem where IE (and only IE) would cache my ajax requests I made to a certain PHP file. Basically if IE detects that the browser is making the same AJAX request twice to the same URL it won't it even execute the succeeding one. For certain design structures this could be a problem where you need to be able to poll a server for data. In order to stop IE from doing this I needed to create a function called makeid()

function makeid() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for( var i=0; i < 5; i++ )?
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

Which basically just generates a random string, and I pass this string into my GET ?AJAX request. However if your using the jQuery library like I am you can set caching to false when you set up your $.ajax function

$.ajax({
  url: "test.html",
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});

jQuery passes in the current time rather than a random string

Newer Older