Simple Example for RDF Data Model

The previous example focused on Document Object Model (DOM) and this example explores the Resource Description Framework (RDF) Data Model. RDF uses XML as a language but it gives the meta-data about the resource that it is describing. RDF Data model is based on statements that contains three parts: resource, property, and value. We extended the previous example by adding a vCard information of each student. vCard is an electronic bussiness card and recently there are studies to serialize this information with RDF (See Representing vCard v3.0 in RDF). In this example, each student element contains a vCard element which gives the name of vCard file related to this student. Example builds the similar JTable representation of class list with one additional column (vCard) where a button is used to trigger to fetch the vCard informatin, process it through RDF, and show some of the information in the another window. Example uses the following DTD description for student records.

<!ELEMENT street  (#PCDATA)>
<!ELEMENT city    (#PCDATA)>
<!ELEMENT state   (#PCDATA)>
<!ELEMENT aphone  (#PCDATA)>
<!ELEMENT name    (#PCDATA)>
<!ELEMENT surname (#PCDATA)>
<!ELEMENT vcard   (#PCDATA)>
<!ELEMENT phones  (aphone*)>
<!ELEMENT address (street,city,state?,country)>
<!ELEMENT student (name,surname,address,vcard,phones?)>
<!ATTLIST student stdid ID #REQUIRED>
<!ELEMENT students student+>

One record in class list looks like the following.

<?xml version="1.0">
<students>
<student stdid="A1231999">
 <name>Bugs</name>
 <surname>Bunny</surname>
 <address>
  <street>Walnut</street>
  <city>Carrotville</city>
  <country>Cartoon Land</country>
 </address>
 <vcard>vcard\bunny.rdf</vcard>
 <phones>
  <aphone>1CC-615-AD123</aphone>
 </phones>
</student>
</students>

A possible bunny.rdf file looks as follows:

<?xml version="1.0"?>
<bunny>
<rdf:RDF>
  <rdf:Description about="vcard/bunny.rdf">
    <vCard:Family>Bugs</vCard:Family>
    <vCard:Given>Rabit the brave </vCard:Given>
    <vCard:Prefix>Mr.</vCard:Prefix>
  </rdf:Description>
</rdf:RDF>
</bunny>

This example contains the following files:

DOMDemo.java
This provides the Swing based GUI.
StudentTable.java
This object encapsulates the table for student records.
StudentRecord.java
This object represents one student's information like a database record.

This example uses the iwt.rdf interfaces with edu.npac.iwt.rdf package. iwt.rdf interfaces are defined for easy RDF programming.

Figure 1: A class diagram of iwt.rdf package.

In this example, StudentTable object is slightly modified to be able to handle the mouse clicks on the vCard column. This mouse click starts a process for reading RDF description.

Figure 2:

A snapshot of GUI after user clicks on Studnet.sdb folder. It contains one more column called vCard to pop up the vCard information window.

The mouse click on vCard column starts a process for reading RDF description and displaying some infomation in the pop up window. This operation is handled in createVCard() method of StudentTable,

/** Creates the vCard representation for the given Student */
JDialog createVCard(StudentRecord aRec)
{
    JDialog ret = null;

    try
    {
       // Define the url for vCard
       URL url = new URL("file:"+ System.getProperty("user.dir")
                                + System.getProperty("file.separator")
                                + aRec.getVCard());

       // Define RDFFactory object
       RDFFactory rdfFac = new RDFFactory();

       // Obtain the RDF of the document
       RDFElement dbRoot = rdfFac.getRDFElement(url);

       // If RDF exists
       if( dbRoot!=null )
       {
          // Get list of Descriptions
          DescriptionList dList = dbRoot.getDescriptionElements();

          if( dList!=null && dList.getLength()>0)
          {
             int i=0;
             while( i<dList.getLength() )
             {
                DescriptionElement current = dList.item(i++);

                // Find the Description defining vCard
                if( current!=null && current.isAbout() &&
                    current.getAbout().equals(aRec.getVCard()) )
                {
                   // Put these information into the JDialog if possible
                   JPanel  box = showVCard(current.getProperties());
                   .......
                }// end of if
             }// end of while
          }// end of if

        } else ....

    } catch (Exception e) {  ......  }

   return ret;
}// end of createVCard()

RDFElement object is obtained by using RDFFactory pattern. Once RDFElement is obtained, it can give the list of DescriptionElement objects (DescriptionList). Code goes through these description elements and finds the one related to this vCard resource. Then it extracts the properties of this DescriptionElement through getProperties(). showVCard() method of StudentTable defines a pop up window after extracting some information from the PropertyList object that contains the PropertyElement objects in the DescriptionElement.

/** Extracts the vCard properties.  */
JPanel showVCard(PropertyList props)
{
   JLabel label ;
   JLabel value ;
   ....
   if( props!=null && props.getLength()>0 )
   {
      int i=0;
      while( i<props.getLength() )
      {
         PropertyElement pElem = props.item(i++);
         if( pElem!=null && !pElem.isResource() )
         {
            if( pElem.getTagName().equals("vCard:Family") ||
                pElem.getTagName().equals("Family") )
            {
                label = new JLabel("Family Name");
                value = new JLabel("");
                .....
                Node aText = pElem.getValue();
                if( aText.getNodeType()==org.w3c.dom.Node.TEXT_NODE )
                    value.setText(((Text)aText).getData());
                ...
            }
            ....
         }// end of element
      }// end of while
   }// end of if
   ....
}// end of showVCard()

Code loops through the PropertyList and it checks the PropertyElement object with property name-value pair since it is possible that this PropertyElement might be defining a statement with another resource. Code basically extracts the value of Family property in vCard description. After this process finishes successfully, the program displays a pop up window:

Figure 3: A snapshot of GUI after user clicks on the button in the vCard column.

Exercises

  1. Handle the photo property of vCard.