Saturday, December 11, 2010

Parsing XML using linq with c# 3.5

In this post i will include the c# (linq) code necessary to read an xml file containing definition of a list of device Ids and descriptions.
input:  String localDirectory pointing to the location of xml file to be read and parsed.
output:  ArrayList of devices parsed from the XML file.

Note: a device can be any object you want.

  
 mkrs1000
 rs1000 with output
  
  
  
 rs1100
 rs1100 demo
  



// code converting xml file to arrarylist of devices
XDocument document = XDocument.Load(localDirectory);

List<device> listOfDevices = document
   .Descendants("device")
   .Select(device => new Device
   {
       DeviceID = device.Element("deviceId").Value,
       DeviceDescription = device.Element("deviceDesc").Value
   })
   .ToList();


foreach (Device device in listOfDevices)
{
 string s = String.Format("ID: {0,-20} : Desc = {1}", device.DeviceID, device.DeviceDescription);
 Console.WriteLine(s);

}

// class representing an xml node (can be any object you want)
class Device
{
    public string DeviceID { get; set; }
    public string DeviceDescription { get; set; }
}

Wednesday, November 24, 2010

How to fit a number in an array of numbers

The following code try to fit a number inside a list of numbers using the java language. 
  • The inputs to this program are the number to search: numberToSearch, and the List of numbers to fit the number in: targetList .
  •  The outputs are the lower bound: lowerBound , and the upper bound: upperBound , which are numbers from the list which encloses numberToSearch
The functions toArray and itemAt are taken from the OpenGTS (open source vehicle tracking system), whereas the function indexOfStable is based on the indexOf  function from the OpenGTS source code, and was modified by me in order to search the index of an object in an array by doing a comparison by reference not by value as the indexOf  function does.
..................................................................................................................................
..................................................................................................................................
  • Input: numberToSearch to fit into targetList.
  • Output: lowerBound <= numberToSearch <= upperBound where lowerBound and upperBound are conntained in targetList.


long numberToSearch= ....
List targetList= .....

Long lowerBound = targetList.get(0);
Long upperBound = targetList.get(targetList.size()-1);


Long numberToSearchByReference= new Long(numberToSearch);targetList.add(numberToSearchByReference);
Collections.sort(targetList);Long [] targetArray= ListTools.toArray(targetList, Long.class);
int index = ListTools.indexOfStable(targetArray, numberToSearchByReference);
       
if(index <= 0)
     return null;
else {
     lowerBound = ListTools.itemAt(targetArray, index -1, lowerBound);
     upperBound = ListTools.itemAt(targetArray, index +1, upperBound);               
}
 


...........................................................................................................................................................
...........................................................................................................................................................
The following function is based on the OpenGTS source code (ListTools.java) but was modified by me: 
public static <t> int indexOfStable(T list[], T item)
     {
         if (list == null) {

             /* no list */
             return -1;

         } else {

             /* constrain offset/length */
             int alen = (list != null)? list.length : 0;

             /* loop through array checking for item */
             for (int i = 0; i < alen; i++) {
                 if (list[i] == item) { // also takes care of 'null == null'
                     return i;
                 } 
             }

             /* still not found */
             return -1;

         }
     }
  
.......................................................................................................................
.......................................................................................................................
The following function is taked from the OpenGTS source code (ListTools.java)

public static <t> T itemAt(Collection<t> c, int ndx, T dft)
    {
        if ((c == null) || (ndx < 0) || (ndx >= c.size())) {
            return dft;
        } else
        if (c instanceof java.util.List) {
            // Randomly addressable list
            return ((java.util.List<t>)c).get(ndx);
        } else {
            // Serialized collection, iterate to item #ndx
            for (T obj : c) {
                if (ndx-- == 0) {
                    return obj;
                }
            }
            return dft;
        }
    }

.......................................................................................................................
.......................................................................................................................
The following function is taked from the OpenGTS source code (ListTools.java)

public static <t> T[] toArray(Collection list, Class<t> type)
    {
        if (type == null) { type = (Class<t>)Object.class; }
        if (list != null) {
            T array[] = (T[])Array.newInstance(type, list.size());  // "unchecked cast"
            return list.toArray(array);
        } else {
            return (T[])Array.newInstance(type, 0);  // "unchecked cast"
        }
    }

Friday, April 30, 2010

How to configure eclipse and tomcat to run remote debugging:

Check this link: http://wiki.apache.org/tomcat/FAQ/Developing and this link
http://it.newinstance.it/2005/10/04/remote-debugging-tomcat-with-eclipse/

http://confluence.sakaiproject.org/display/BOOT/Setting+Up+Tomcat+For+Remote+Debugging


1)open catalina.bat and add the following lines :
set JPDA_TRANSPORT=dt_socket
set JPDA_ADDRESS=7945
Now stop tomcat from the services window if you have it installed as a service. Then run the following from a console:

[path to tomcat]\bin>catalina jpda start

In eclipse press Run->Debug Configurations..->Remote Java Application->press new button(on top)->
select the project : moodle-transfer -> enter as port : 7945 -> press Debug.
Now run the application in a browser after you have set a breakpoint in the source code,
then eclipse should propose you to turn to debug perspective, choose yes, then you should begin the debugging.
Note: if you get the following error: failed to connect to remote vm. connection refused. connection refused connect eclipse
then change the port number because it may be used by another application.

Monday, April 26, 2010

How to post XML code on blogspot

if you want to add XML code on a post in blogspot, then you have to follow this trick (unless the xml code will be hided) actually i have found two methods which achieves the same target:

  1. download notepad++, which can be downloaded from the following link:
    (remember to download the latest version because it contains a plugin manager.
    install notepad++, then go to Plugins-> Plugin Manager-> Show Plugin Manager.
    in tab Available choose XML Tools, check it out then press install. you will have to restart Notepad++ (i'm not sure)
    Now the most important part: paste the xml code in notepad++, select the xml code then go to Plugins->XML Tools->Convert selection XML to text(<> => <>)
    Now copy the converted XML text to a post in blogspot and it should work.

  2. Another method is to use this publicly available tool on the web:http://centricle.com/tools/html-entities/
    paste your code in the text box then press encode, and voila the xml code is converted to html entity, all you have to do is copy the encoded text and paste it in Compose in blogspot.

Friday, April 23, 2010

How to install tomcat as a service in windows 7 or windows vista

When installing tomcat 6 as a service in windows 7 or windows vista using the method we use in windows xp it fails, So what to do? The answer is:  
  • Press Windows+(Pause/Break), then press Advanced system settings, press Environment Variables, add a user variable with name : CATALINA_HOME, with value path to tomcat\tomcat6. Press Ok three times
  • Go to the desktop, right click, then New->shortcut, in Type the location of the item enter: path to tomcat\tomcat6\service.bat, click next then enter a name for the shortcut, press finish. In the desktop right click the shortcut then choose Properties, in the target add to it: install [name you want for tomcat], press Ok, then right click on it, and choose Run as administrator. 
  • go to services and tomcat will be added to the list, you can then set it to run automatically, and stop it or restart the service.