Archive

Archive for the ‘Useful stuff’ Category

Ajax wrapper class

April 22nd, 2008

I’m not really a big fan of “AJAX” ( yes, I put it in quotes ). To be honest, I’ve being doing AJAX type stuff since the dawn of Javscript and IFRAMES, but now it has a name and everyone is using it. My pet hate is AJAX for the sake of AJAX, reloading 5mb of data when a simple page refresh will do.

Saying all that, what AJAX I do now, I do with this great wrapper class

http://www.cmarshall.net/MySoftware/ajax/

Works like a charm and really simple to impliment. If I get the time, I’ll add in some very simple PHP examples on how to make re-usable AJAX type code for populating lists etc.

[-]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Javascript

Handling bookmarks / named anchors in an IFRAME

February 14th, 2008

So, you are using IFRAMES, good on you. You have a nice document with lots of names anchors, but you find when you click on the bookmark link, the whole window, parent and all, scrolls into view. Pain in the ass. To get around it, use Javascript as such …

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
    while(1)
    {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)break;
        obj = obj.offsetParent;
    }
  else if(obj.y)
    curtop += obj.y;
  return curtop;
}
 
 
function scroll(element){
  p = findPosY(document.getElementById(element));
  window.scrollBy(0,p);
}

The findPosY is not my code, sorry, cant remember the original author - if I find you I will credit you.

In your HTML use

<a href="javascript:scroll('A');">A

and for the named anchor use any tag and add the ID for the link, ie

<h3 id="A">A

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Javascript

Converting a Midlet to a COD file for OTA installation

February 14th, 2008

God, RIM sucks. Thought I’d mention that. I hate Blackberries with passion. Really.

Now, if you are converting a Midlet to a COD file, you will need to use the infamous rapc tool provided with the RIM JDE ( in the bin directory if you are looking for it ) . Now, along with issues with multiple versions, bugs and what have you, one big pain is if you want the RIM version of the JAD file created for OTA installation, rapc doesn’t give you all the parameters if you use the -midlet paramater. If you DON’T use it, you get a nice JAD but the COD will not work.

So, to get around this, I created a small app to extract the RIM specfic information from the COD file generated by rapc.

Generated the COD using

rapc import="%jde%\lib\net_rim_api.jar" codename=%jar% -midlet jad=%jar%.jad %jar%.jar

where %jde% is your JDE path, %jar% is the name of your module.

Extract JAD information from a COD file

To run:

java -jar cod2jad.jar {cod file} {module name}

This will dump the extra information to stdout, so you can just append it to your JAD file with >>, ie

java -jar cod2jad.jar path.cod myapp >> path.jad

Remember, once you have done this, open the COD file in winzip ( or whatever ) and extract the seperate elements to your webserver. Once note, if you COD is only made of one module, this will probably fail ( sorry ).

Oh, and if you are using Netbeans components, and/or getting verification errors, try turning down your obfusication level in your project - rapc hates the splash screen and wait screen when obfusicated for some reason. The other option is to try including the net_rim_api.jar in your project ( but don’t include in the package ).

And even nicer of me, here is the source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.magnetmobile.tools.rim;
 
/**
 *
 * @author Sime when drunk ... yay drunk code
 */
import java.io.*;
import java.util.*;
import java.util.jar.*;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
public class Cod2Jad {
 
    public static void main(String args[])throws NoSuchAlgorithmException {
 
        FileInputStream fis = null;
        try {
            int count = 0;
            if (args.length != 2) {
                System.out.println("usage: {path to COD} {Module name}");
                System.exit(-1);
            }
            Hashtable htSizes = new Hashtable();
            Hashtable htJarContents = new Hashtable();
            fis = new FileInputStream(args[0]);
 
 
            BufferedInputStream bis = new BufferedInputStream(fis);
            ZipInputStream zis = new ZipInputStream(bis);
            ZipEntry ze = null;
            while ((ze = zis.getNextEntry()) != null) {
                if (ze.isDirectory()) {
                    continue;
                }
 
                int size = (int) ze.getSize();
                // -1 means unknown size.
                if (size == -1) {
                    size = ((Integer) htSizes.get(ze.getName())).intValue();
                }
                byte[] b = new byte[(int) size];
                int rb = 0;
                int chunk = 0;
                while (((int) size - rb) > 0) {
                    chunk = zis.read(b, rb, (int) size - rb);
                    if (chunk == -1) {
                        break;
                    }
                    rb += chunk;
                }
                htJarContents.put(ze.getName(), b);
                process(ze, count, b);
                count++;
            }
 
            System.out.println("RIM-COD-Module-Name:" + args[1]);
            System.out.println("RIM-COD-Module-Dependencies: net_rim_cldc");
            System.out.println("RIM-COD-Creation-Time: 1202924527");
 
            zis.close();
            bis.close();
            fis.close();
        } catch (IOException ex) {
            System.out.println("Could not access file "+args[0]);
            System.exit(-1);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(Cod2Jad.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
 
    private static void process(Object obj, int count,byte[] data) throws NoSuchAlgorithmException,
UnsupportedEncodingException {
        ZipEntry entry = (ZipEntry) obj;
        String name = entry.getName();
        long size = entry.getSize();
        long compressedSize = entry.getCompressedSize();
 
        String output = "RIM-COD-URL" + (count > 0 ? ("-" + count) : "") + ": " + name;
        System.out.println(output);
 
        output = "RIM-COD-Size" + (count > 0 ? ("-" + count) : "") + ": " + compressedSize;
        System.out.println(output);
 
        output = "RIM-COD-SHA1" + (count > 0 ? ("-" + count) : "") + ": ";
        output +=  SHA1(data, true);
        System.out.println(output);
    }
 
    private static String convertToHex(byte[] data, boolean spaced) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i <
                data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9)) {
                    buf.append((char) ('0' + halfbyte));
                } else {
                    buf.append((char) ('a' + (halfbyte - 10)));
                }
 
                halfbyte = data[i] & 0x0F;
            } while (two_halfs++ < 1);
 
            if (spaced) {
                buf.append(' ');
            }
        }
 
        return buf.toString();
    }
 
    public static String SHA1(byte[] data, boolean spaced)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md;
        md =
                MessageDigest.getInstance("SHA-1");
        byte[] sha1hash = new byte[40];
        md.update(data, 0, data.length);
        sha1hash =
                md.digest();
        return convertToHex(sha1hash, spaced);
    }
}
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Google] [StumbleUpon]

RIM / Blackberry, Uncategorized

IP reported as 0.0.0.0 in Apache access log

October 30th, 2007

If you are running Apache on a Windows 2000 server, and notice your access logs are coming up with 0.0.0.0 IP addresses

0.0.0.0 - - [30/Oct/2007:19:29:11 -0400] “GET /ad_images/siam.gif HTTP/1.1″ 304 -

or similar, in your WordPress install posts are coming from a 0.0.0.0 IP, check your httpd.conf, and add the following line:

Win32DisableAcceptEx

at the top of the file. Restart Apache and your should be ok!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Windows

Reseting your disks from PIO to DMA

October 26th, 2007

If you are having issues with your PC, noticeable by your Media player crapping out whenever your Hard Disk or CD/DVD is accessed or by your mouse sticking, check this piece of VBscript that resets your devices from PIO ( software ) mode to DMA ( hardware ). Sometimes windows will switch over to software mode without even telling you ( thats nice ) and this will detect and reset those settings.

For the original script check it out here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
' Visual Basic Script program to reset the DMA status of all ATA drives
 
' Copyright © 2006 Hans-Georg Michna
 
' Version 2007-04-04
 
' Works in Windows XP, probably also in Windows 2000 and NT.
' Does no harm if Windows version is incompatible.
 
If MsgBox("This program will now reset the DMA"_
  & " status of all ATA drives with Windows drivers." _
  & vbNewline & "Windows will redetect the status after "_
  & "the next reboot, therefore this procedure" _
  & vbNewline & "should be harmless.", _
    vbOkCancel, "Program start message") _
  = vbOk Then
 
RegPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\"_
  & "Control\Class\{4D36E96A-E325-11CE-BFC1-08002BE10318}\"
ValueName1Master = "MasterIdDataChecksum"
ValueName1Slave = "SlaveIdDataChecksum"
ValueName2Master = "UserMasterDeviceTimingModeAllowed"
ValueName2Slave = "UserSlaveDeviceTimingModeAllowed"
ValueName3 = "ResetErrorCountersOnSuccess"
MessageText = "The following ATA channels have been reset:"
MessageTextLen0 = Len(MessageText)
ConsecutiveMisses = 0
Set WshShell = WScript.CreateObject("WScript.Shell")
 
For i = 0 to 999
  RegSubPath = Right("000" & i, 4) & "\"
 
  ' Master
 
  Err.Clear
  On Error Resume Next
  WshShell.RegRead RegPath & RegSubPath & ValueName1Master
  errMaster = Err.Number
  On Error Goto 0
  If errMaster = 0 Then
    On Error Resume Next
    WshShell.RegDelete RegPath & RegSubPath & ValueName1Master
    WshShell.RegDelete RegPath & RegSubPath & ValueName2Master
    On Error Goto 0
    MessageText = MessageText & vbNewLine & "Master"
  End If
 
  ' Slave
 
  Err.Clear
  On Error Resume Next
  WshShell.RegRead RegPath & RegSubPath & ValueName1Slave
  errSlave = Err.Number
  On Error Goto 0
  If errSlave = 0 Then
    On Error Resume Next
    WshShell.RegDelete RegPath & RegSubPath & ValueName1Slave
    WshShell.RegDelete RegPath & RegSubPath & ValueName2Slave
    On Error Goto 0
    If errMaster = 0 Then
      MessageText = MessageText & " and "
    Else
      MessageText = MessageText & vbNewLine
    End If
    MessageText = MessageText & "Slave"
  End If
 
  If errMaster = 0 Or errSlave = 0 Then
    On Error Resume Next
    WshShell.RegWrite RegPath & RegSubPath & ValueName3, 1, "REG_DWORD"
    On Error Goto 0
    ChannelName = "unnamed channel " & Left(RegSubPath, 4)
    On Error Resume Next
    ChannelName = WshShell.RegRead(RegPath & RegSubPath & "DriverDesc")
    On Error Goto 0
    MessageText = MessageText & " of " & ChannelName & ";"
    ConsecutiveMisses = 0
  Else
    ConsecutiveMisses = ConsecutiveMisses + 1
    If ConsecutiveMisses >= 32 Then Exit For ' Don't search unnecessarily long.
  End If
Next ' i
 
If Len(MessageText) <= MessageTextLen0 Then
  MessageText = "No resettable ATA channels with "_
     & "Windows drivers found. Nothing changed."
Else
  MessageText = MessageText & vbNewline _
    & "Please reboot now to reset and redetect the DMA status."
End If
 
MsgBox MessageText, vbOkOnly, "Program finished normally"
 
End If ' MsgBox(...) = vbOk
 
' End of Visual Basic Script program
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Windows