26 November 2007

XMLClassUtil Project


/*
* XMLClassUtil.java
*
* Copyright (2007)
* by Emory Merryman, III
* http://indicativeserviceconcierge.blogspot.com/

* (For Contact Information See Web Page.
*
* All Rights Reserved.
*
* Software is confidential and copyrighted.
* Title to Software and all associated intellectual property
* rights is retained by Emory Merryman, III.
* You acknowledge that Licensed Software is not designed or
* intended for use in the design, construction, operation or
* maintenance of any nuclear facility.
*
* Emory Merryman, III disclaims any express or implied warranty
* of fitness for such uses.
* No right, title or interest in or to any trademark, service
* mark, logo or trade name of Emory Merryman, III is granted.
*
* All express or implied conditions, representations and warranties,
* including any implied warranty of merchantability, fitness
* for a particular purpose or non infringement are disclaimed,
* except to the extent that these disclaimers are held
* to be legally invalid.
*
* To the extent not prohibited by law, in no event will Emory
* Merryman, III be liable for any lost revenue, profit or data,
* or for special indirect, consequential, incidental or punitive
* damages, arising out of or related to the use or inability to
* use software if if Emory Merryman, III has been advised of such
* damages.
* In no event will Emory Merryman III's liability to you, whether
* in contract, tort (including negligence), or otherwise, exceed
* the amount paid by you for this software.
*/

final class XMLClassUtil
{
private static final int CAFEBABE = -889275714 ;

private static final void diff ( final java . lang . String fn1 , final java . lang . String fn2 ) throws java . lang . Exception
{
final java . io . InputStream in1 = new java . io . FileInputStream ( new java . io . File ( fn1 ) ) ;
final java . io . InputStream in2 = new java . io . FileInputStream ( new java . io . File ( fn2 ) ) ;
int i , r1 , r2 ;
for ( i = 0 , r1 = in1 . read ( ) , r2 = in2 . read ( ) ; ( r1 != -1 ) || ( r2 != -1 ) ; i ++ , r1 = in1 . read ( ) , r2 = in2 . read ( ) )
{
java . lang . System . out . println ( i + "\t" + r1 + "\t" + r2 + "\t" + ( r1 == r2 ) ) ;
}
in1 .close ( ) ;
in2 . close ( ) ;
}

public static final void main ( final java . lang . String [ ] args ) throws java . lang . Exception
{
// diff ( args [ 1 ] , args [ 2 ] ) ;
// java . lang . System . exit ( 0 ) ;
final java . io . DataInputStream input = new java . io . DataInputStream ( new java . io . FileInputStream ( new java . io . File ( args [ 1 ] ) ) ) ;
final java . io . DataOutputStream output = new java . io . DataOutputStream ( new java . io . FileOutputStream ( new java. io . File ( args [ 2 ] ) ) ) ;
final XMLClassUtil util = new XMLClassUtil ( ) ;
if ( args [ 0 ] . equals ( "toXML" ) )
{
final ClassFile classFile = util . dataInputStreamFactory . make ( input ) ;
final java . io . PrintStream pOut = new java . io . PrintStream ( output ) ;
final org . w3c . dom . Document classDocument = util . xmlClass ( classFile ) ;
util . print ( pOut , classDocument ) ;
}
else if ( args [ 0 ] . equals ( "toClass" ) )
{
final javax . xml . parsers . DocumentBuilderFactory dbf = javax . xml . parsers . DocumentBuilderFactory . newInstance ( ) ;
final javax . xml . parsers . DocumentBuilder db = dbf . newDocumentBuilder ( ) ;
final org . w3c . dom . Document document = db . parse ( input ) ;
final org . w3c . dom . Element node1 = document . getDocumentElement ( ) ;
final org . w3c . dom . NodeList nodes = node1 . getElementsByTagName ( "classFile" ) ;
final org . w3c . dom . Node node2 = nodes . item ( 0 ) ;
final ClassFile classFile = util . xmlReader . read ( node2 ) ;
util . byteWriter . write ( classFile , output ) ;
}
input . close ( ) ;
output . close ( ) ;
input . close ( ) ;
}

private final ClassFile loadClass ( final java . io . DataInputStream input ) throws XMLClassUtilException
{
final ClassFile classFile = dataInputStreamFactory . make ( input ) ;
return ( classFile ) ;
}

private final org . w3c . dom . Document xmlClass ( final ClassFile classFile ) throws java . lang . Exception
{
final javax . xml . parsers . DocumentBuilderFactory dbf = javax . xml . parsers . DocumentBuilderFactory . newInstance ( ) ;
final javax . xml . parsers . DocumentBuilder db = dbf . newDocumentBuilder ( ) ;
final org . w3c . dom . Document document = db . newDocument ( ) ;
final org . w3c . dom . Node element = document . createElement ( "xml" ) ;
document . appendChild ( element ) ;
classFileWriter . write ( classFile , document , element ) ;
return ( document ) ;
}

private interface Factory < R , P , E extends java . lang . Exception >
{
public abstract R make ( final P p ) throws E ;
}

private interface ByteWriter < P , E extends java . lang . Exception >
{
public abstract void write ( final P p , final java . io . DataOutputStream output ) throws E ;
}

private interface ClassFile
{
public abstract int getMagic ( ) ;

public abstract short getMinorVersion ( ) ;

public abstract short getMajorVersion ( ) ;

public abstract java . util . List < ConstantPool > getConstantPool ( ) ;

public abstract short getAccessFlags ( ) ;

public abstract short getThisClass ( ) ;

public abstract short getSuperClass ( ) ;

public abstract short [ ] getInterfaces ( ) ;

public abstract java . util . List < FieldInfo > getFields ( ) ;

public abstract java . util . List < MethodInfo > getMethods ( ) ;

public abstract java . util . List < AttributeInfo > getAttributes ( ) ;
}

private interface ConstantPool
{
public abstract byte getTag ( ) ;

public abstract byte [ ] getInfo ( ) ;
}

private interface FieldMethodInfo
{
public abstract java . util . List < ConstantPool > getConstantPool ( ) ;

public abstract short getAccessFlags ( ) ;

public abstract short getNameIndex ( ) ;

public abstract short getDescriptorIndex ( ) ;

public abstract java . util . List < AttributeInfo > getAttributes ( ) ;
}

private interface FieldInfo extends FieldMethodInfo
{
}

private interface MethodInfo extends FieldMethodInfo
{
}

private interface FMInfo extends FieldInfo , MethodInfo
{
}

private interface AttributeInfo
{
public abstract java . util . List < ConstantPool > getConstantPool ( ) ;

public abstract short getAttributeNameIndex ( ) ;

public abstract byte [ ] getInfo ( ) ;
}

public interface ConstantClassInfo
{
public abstract short getNameIndex ( ) ;
}

public interface ConstantFieldRef
{
public abstract short getClassIndex ( ) ;

public abstract short getNameAndTypeIndex ( ) ;
}

public interface ConstantMethodRef
{
public abstract short getClassIndex ( ) ;

public abstract short getNameAndTypeIndex ( ) ;
}

public interface ConstantInterfaceMethodRef
{
public abstract short getClassIndex ( ) ;

public abstract short getNameAndTypeIndex ( ) ;
}

public interface ConstantString
{
public abstract short getStringIndex ( ) ;
}

public interface ConstantInteger
{
public abstract int getBytes ( ) ;
}

public interface ConstantFloat
{
public abstract int getBytes ( ) ;
}

public interface ConstantLong
{
public abstract int getHighBytes ( ) ;

public abstract int getLowBytes ( ) ;
}

public interface ConstantDouble
{
public abstract int getHighBytes ( ) ;

public abstract int getLowBytes ( ) ;
}

public interface ConstantNameAndType
{
public abstract short getNameIndex ( ) ;

public abstract short getDescriptorIndex ( ) ;
}

public interface ConstantUTF8
{
public abstract byte [ ] getBytes ( ) ;
}

public interface XMLWriter < T >
{
public abstract void write ( final T t , final org . w3c . dom . Document document , final org . w3c . dom . Node parent ) ;
}

public interface XMLReader < T >
{
public abstract T read ( final org . w3c . dom . Node node ) ;
}

private final class XMLClassUtilException extends java . lang . Exception
{
private XMLClassUtilException ( final java . lang . String message , final java . lang . Throwable cause )
{
super ( message , cause ) ;
}

private XMLClassUtilException ( final java . lang . String message )
{
super ( message ) ;
}

private XMLClassUtilException ( final java . lang . Throwable cause )
{
super ( cause ) ;
}
}

private final void error ( final java . lang . String message , final java . lang . Throwable cause ) throws XMLClassUtilException
{
final XMLClassUtilException error = new XMLClassUtilException ( message , cause ) ;
throw ( error ) ;
}

private final void error ( final java . lang . Throwable cause ) throws XMLClassUtilException
{
final XMLClassUtilException error = new XMLClassUtilException ( cause ) ;
throw ( error ) ;
}

private final void error ( final java . lang . String message ) throws XMLClassUtilException
{
final XMLClassUtilException error = new XMLClassUtilException ( message ) ;
throw ( error ) ;
}

private final Factory < ClassFile , java . io . DataInputStream , XMLClassUtilException > dataInputStreamFactory = new Factory < ClassFile , java . io . DataInputStream , XMLClassUtilException > ( )
{
public final ClassFile make ( final java . io . DataInputStream input ) throws XMLClassUtilException
{
ClassFile classFile = null ;
try
{
classFile = this . tryMake ( input ) ;
}
catch ( final java . io . IOException cause )
{
error ( cause ) ;
}
catch ( final java . lang . Exception cause )
{
error ( cause ) ;
}
return ( classFile ) ;
}

private final ClassFile tryMake ( final java . io . DataInputStream input ) throws java . io . IOException , XMLClassUtilException , java . lang . Exception
{
final int [ ] floor = new int [ 1 ] ;
floor [ 0 ] = 1 ;
final class AbstractListFactory < R > implements Factory < java . util . List < R > , java . io . DataInputStream , java . lang . Exception >
{
private AbstractListFactory ( final Factory < R , java . io . DataInputStream , java . lang . Exception > rFactory )
{
super ( ) ;
this . rFactory = rFactory ;
}

private Factory < R , java . io . DataInputStream , java . lang . Exception > rFactory ;

public final java . util . List < R > make ( final java . io . DataInputStream input ) throws java . lang . Exception
{
final short size = u2 ( input ) ;
final java . util . List < R > list = new java . util . ArrayList < R > ( ) ;
final int f = floor [ 0 ] ;
for ( int i = f ; i < size ; i ++ )
{
final R r = rFactory . make ( input ) ;
list . add ( r ) ;
}
return ( list ) ;
}
}
final Factory < ConstantPool , java . io . DataInputStream , java . lang . Exception > constantPoolFactory = new Factory < ConstantPool , java . io . DataInputStream , java . lang . Exception > ( )
{
public final ConstantPool make ( final java . io . DataInputStream input ) throws java . io . IOException , XMLClassUtilException
{
final byte tag = u1 ( input ) ;
// java . lang . System . out . print ( "\t" + tag ) ;
byte [ ] b = null ;
switch ( tag )
{
case 7 :
case 8 :
b = new byte [ 2 ] ;
break ;
case 9 :
case 10 :
case 11 :
case 3 :
case 4 :
case 12 :
b = new byte [ 4 ] ;
break ;
case 5 :
case 6 :
b = new byte [ 8 ] ;
break ;
case 1 :
short size = u2 ( input ) ;
b = new byte [ size ] ;
break ;
default :
error ( "Undefined Tag (" + tag + ")" ) ;
break ;
} ;
input . read ( b ) ;
final byte [ ] info = b ;
final ConstantPool cp = new ConstantPool ( )
{
public final byte getTag ( )
{
return ( tag ) ;
}

public final byte [ ] getInfo ( )
{
return ( info ) ;
}
} ;
return ( cp ) ;
}
} ;
final Factory < java . util . List < ConstantPool > , java . io . DataInputStream , java . lang . Exception > constantPoolListFactory = new AbstractListFactory < ConstantPool > ( constantPoolFactory ) ;
final int magic = u4 ( input ) ;
if ( magic != CAFEBABE )
{
error ( "Incorrect Magic Number (" + magic + ")" ) ;
}
final short minorVersion = u2 ( input ) ;
final short majorVersion = u2 ( input ) ;
final java . util . List < ConstantPool > constantPool = constantPoolListFactory . make ( input ) ;
floor [ 0 ] = 0 ;
final Factory < AttributeInfo , java . io . DataInputStream , java . lang . Exception > attributeInfoFactory = new Factory < AttributeInfo , java . io . DataInputStream , java . lang . Exception > ( )
{
public final AttributeInfo make ( final java . io . DataInputStream input ) throws java . io . IOException
{
final short attributeNameIndex = u2 ( input ) ;
final int attributeLength = u4 ( input ) ;
final byte [ ] info = bytes ( input , attributeLength ) ;
// java . lang . System . out . println ( attributeNameIndex + "\t" + attributeLength ) ;
final AttributeInfo attributeInfo = new AttributeInfo ( )
{
public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAttributeNameIndex ( )
{
return ( attributeNameIndex ) ;
}

public final byte [ ] getInfo ( )
{
return ( info ) ;
}
} ;
return ( attributeInfo ) ;
}
} ;
final Factory < java . util . List < AttributeInfo > , java . io . DataInputStream , java . lang . Exception > attributeInfoListFactory = new AbstractListFactory < AttributeInfo > ( attributeInfoFactory ) ;
final Factory < FMInfo , java . io . DataInputStream , java . lang . Exception > fmInfoFactory = new Factory < FMInfo , java . io . DataInputStream , java . lang . Exception > ( )
{
public final FMInfo make ( final java . io . DataInputStream input ) throws java . lang . Exception
{
final short accessFlags = u2 ( input ) ;
final short nameIndex = u2 ( input ) ;
final short descriptorIndex = u2 ( input ) ;
final java . util . List < AttributeInfo > attributes = attributeInfoListFactory . make ( input ) ;
// java . lang . System . out . println ( accessFlags + "\t" + nameIndex + "\t" + descriptorIndex ) ;
final FMInfo fmInfo = new FMInfo ( )
{
public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAccessFlags ( )
{
return ( accessFlags ) ;
}

public final short getNameIndex ( )
{
return ( nameIndex ) ;
}

public final short getDescriptorIndex ( )
{
return ( descriptorIndex ) ;
}

public final java . util . List < AttributeInfo > getAttributes ( )
{
return ( attributes ) ;
}
} ;
return ( fmInfo ) ;
}
} ;
final Factory < FieldInfo , java . io . DataInputStream , java . lang . Exception > fieldInfoFactory = new Factory < FieldInfo , java . io . DataInputStream , java . lang . Exception > ( )
{
public final FieldInfo make ( final java . io . DataInputStream input ) throws java . lang . Exception
{
final FMInfo r = fmInfoFactory . make ( input ) ;
return ( r ) ;
}
} ;
final Factory < java . util . List < FieldInfo > , java . io . DataInputStream , java . lang . Exception > fieldInfoListFactory = new AbstractListFactory < FieldInfo > ( fieldInfoFactory ) ;
final Factory < MethodInfo , java . io . DataInputStream , java . lang . Exception > methodInfoFactory = new Factory < MethodInfo , java . io . DataInputStream , java . lang . Exception > ( )
{
public final MethodInfo make ( final java . io . DataInputStream input ) throws java . lang . Exception
{
final FMInfo r = fmInfoFactory . make ( input ) ;
return ( r ) ;
}
} ;
final Factory < java . util . List < MethodInfo > , java . io . DataInputStream , java . lang . Exception > methodInfoListFactory = new AbstractListFactory < MethodInfo > ( methodInfoFactory ) ;
final short accessFlags = u2 ( input ) ;
final short thisClass = u2 ( input ) ;
final short superClass = u2 ( input ) ;
final short interfacesCount = u2 ( input ) ;
final short [ ] interfaces = new short [ interfacesCount ] ;
for ( int i = 1 ; i < interfacesCount ; i ++ )
{
interfaces [ i ] = u2 ( input ) ;
}
final java . util . List < FieldInfo > fields = fieldInfoListFactory . make ( input ) ;
final java . util . List < MethodInfo > methods = methodInfoListFactory . make ( input ) ;
final java . util . List < AttributeInfo > attributes = attributeInfoListFactory . make ( input ) ;
final ClassFile classFile = new ClassFile ( )
{
public final int getMagic ( )
{
return ( magic ) ;
}

public final short getMinorVersion ( )
{
return ( minorVersion ) ;
}

public final short getMajorVersion ( )
{
return ( majorVersion ) ;
}

public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAccessFlags ( )
{
return ( accessFlags ) ;
}

public final short getThisClass ( )
{
return ( thisClass ) ;
}

public final short getSuperClass ( )
{
return ( superClass ) ;
}

public final short [ ] getInterfaces ( )
{
return ( interfaces ) ;
}

public final java . util . List < FieldInfo > getFields ( )
{
return ( fields ) ;
}

public final java . util . List < MethodInfo > getMethods ( )
{
return ( methods ) ;
}

public final java . util . List < AttributeInfo > getAttributes ( )
{
return ( attributes ) ;
}
} ;
return ( classFile ) ;
}
} ;

private final byte u1 ( final java . io . DataInputStream input ) throws java . io . IOException
{
final int i = input . readByte ( ) ;
final byte b = ( byte ) ( i ) ;
// java . lang . System . out . print ( "\t" + b ) ;
return ( b ) ;
}

private final short u2 ( final java . io . DataInputStream input ) throws java . io . IOException
{
final int i = input . readShort ( ) ;
final short s = ( short ) ( i ) ;
java . lang . System . out . print ( "\t" + s ) ;
return ( s ) ;
}

private final int u4 ( final java . io . DataInputStream input ) throws java . io . IOException
{
final int i = input . readInt ( ) ;
return ( i ) ;
}

private final byte [ ] bytes ( final java . io . DataInputStream input , final int len ) throws java . io . IOException
{
final byte [ ] b = new byte [ len ] ;
input . read ( b ) ;
return ( b ) ;
}

private final ByteWriter < ClassFile , java . io . IOException > byteWriter = new ByteWriter < ClassFile , java . io . IOException > ( )
{
public final void write ( final ClassFile classFile , final java . io . DataOutputStream output ) throws java . io . IOException
{
final short [ ] floor = new short [ 1 ] ;
floor [ 0 ] = 1 ;
final class AbstractListWriter < R > implements ByteWriter < java . util . List < R > , java . io . IOException >
{
AbstractListWriter ( final ByteWriter < R , java . io . IOException > rWriter )
{
super ( ) ;
this . rWriter = rWriter ;
}

private ByteWriter < R , java . io . IOException > rWriter ;

public final void write ( final java . util . List < R > list , final java . io . DataOutputStream output ) throws java . io . IOException
{
final short count = ( short ) ( list . size ( ) + floor [ 0 ] ) ;
java . lang . System . out . print ( "{" ) ;
u2 ( output , count ) ;
java . lang . System . out . print ( "}" ) ;
final java . util . Iterator < R > iterator = list . iterator ( ) ;
while ( iterator . hasNext ( ) )
{
final R r = iterator . next ( ) ;
rWriter . write ( r , output ) ;
}
}
}
final ByteWriter < ConstantPool , java . io . IOException > constantPoolWriter = new ByteWriter < ConstantPool , java . io . IOException > ( )
{
public final void write ( final ConstantPool cp , final java . io . DataOutputStream output ) throws java . io . IOException
{
final byte tag = cp . getTag ( ) ;
// java . lang . System . out . print ( "\t" + tag ) ;
final byte [ ] info = cp . getInfo ( ) ;
u1 ( output , tag ) ;
if ( tag == 1 )
{
bytes ( output , info , 2 ) ;
}
else
{
bytes ( output , info , 0 ) ;
}
}
} ;
final ByteWriter < java . util . List < ConstantPool > , java . io . IOException > constantPoolListWriter = new AbstractListWriter < ConstantPool > ( constantPoolWriter ) ;
final ByteWriter < AttributeInfo , java . io . IOException > attributeInfoWriter = new ByteWriter < AttributeInfo , java . io . IOException > ( )
{
public final void write ( final AttributeInfo attributeInfo , final java . io . DataOutputStream output ) throws java . io . IOException
{
final short attributeNameIndex = attributeInfo . getAttributeNameIndex ( ) ;
final byte [ ] info = attributeInfo . getInfo ( ) ;
// java . lang . System . out . println ( attributeNameIndex + "\t" + info . length ) ;
u2 ( output , attributeNameIndex ) ;
bytes ( output , info , 4 ) ;
}
} ;
final ByteWriter < java . util . List < AttributeInfo > , java . io . IOException > attributeInfoListWriter = new AbstractListWriter < AttributeInfo > ( attributeInfoWriter ) ;
final ByteWriter < FieldMethodInfo , java . io . IOException > fmInfoWriter = new ByteWriter < FieldMethodInfo , java . io . IOException > ( )
{
public final void write ( final FieldMethodInfo fmInfo , final java . io . DataOutputStream output ) throws java . io . IOException
{
final short accessFlags = fmInfo . getAccessFlags ( ) ;
final short nameIndex = fmInfo . getNameIndex ( ) ;
final short descriptorIndex = fmInfo . getDescriptorIndex ( ) ;
// java . lang . System . out . println ( accessFlags + "\t" + nameIndex + "\t" + descriptorIndex ) ;
final java . util . List < AttributeInfo > attributes = fmInfo . getAttributes ( ) ;
java . lang . System . out . print ( "[" ) ;
u2 ( output , accessFlags ) ;
u2 ( output , nameIndex ) ;
u2 ( output , descriptorIndex ) ;
attributeInfoListWriter . write ( attributes , output ) ;
java . lang . System . out . print ( "]" ) ;
}
} ;
final ByteWriter < FieldInfo , java . io . IOException > fieldInfoWriter = new ByteWriter < FieldInfo , java . io . IOException > ( )
{
public final void write ( final FieldInfo fieldInfo , final java . io . DataOutputStream output ) throws java . io . IOException
{
fmInfoWriter . write ( fieldInfo , output ) ;
}
} ;
final ByteWriter < java . util . List < FieldInfo > , java . io . IOException > fieldInfoListWriter = new AbstractListWriter < FieldInfo > ( fieldInfoWriter ) ;
final ByteWriter < MethodInfo , java . io . IOException > methodInfoWriter = new ByteWriter < MethodInfo , java . io . IOException > ( )
{
public final void write ( final MethodInfo methodInfo , final java . io . DataOutputStream output ) throws java . io . IOException
{
fmInfoWriter . write ( methodInfo , output ) ;
}
} ;
final ByteWriter < java . util . List < MethodInfo > , java . io . IOException > methodInfoListWriter = new AbstractListWriter < MethodInfo > ( methodInfoWriter ) ;
final int magic = classFile . getMagic ( ) ;
final short minorVersion = classFile . getMinorVersion ( ) ;
final short majorVersion = classFile . getMajorVersion ( ) ;
final java . util . List < ConstantPool > constantPool = classFile . getConstantPool ( ) ;
final short accessFlags = classFile . getAccessFlags ( ) ;
final short thisClass = classFile . getThisClass ( ) ;
final short superClass = classFile . getSuperClass ( ) ;
final short [ ] interfaces = classFile . getInterfaces ( ) ;
final java . util . List < FieldInfo > fields = classFile . getFields ( ) ;
final java . util . List < MethodInfo > methods = classFile . getMethods ( ) ;
final java . util . List < AttributeInfo > attributes = classFile . getAttributes ( ) ;
u4 ( output , magic ) ;
u2 ( output , minorVersion ) ;
u2 ( output , majorVersion ) ;
constantPoolListWriter . write ( constantPool , output ) ;
floor [ 0 ] = 0 ;
u2 ( output , accessFlags ) ;
u2 ( output , thisClass ) ;
u2 ( output , superClass ) ;
final short interfacesCount = ( short ) ( interfaces . length ) ;
u2 ( output , interfacesCount ) ;
for ( int i = 0 ; i < interfaces . length ; i ++ )
{
u2 ( output , interfaces [ i ] ) ;
}
fieldInfoListWriter . write ( fields , output ) ;
methodInfoListWriter . write ( methods , output ) ;
attributeInfoListWriter . write ( attributes , output ) ;
}

private final void u1 ( final java . io . DataOutputStream output , final byte b ) throws java . io . IOException
{
output . writeByte ( b ) ;
// java . lang . System . out . print ( "\t" + b ) ;
}

private final void u2 ( final java . io . DataOutputStream output , final short s ) throws java . io . IOException
{
output . writeShort ( s ) ;
java . lang . System . out . print ( "\t" + s + "" ) ;
if ( s == 11 )
{
final java . lang . RuntimeException error = new java . lang . RuntimeException ( "FUCK" ) ;
// throw ( error ) ;
}
}

private final void u4 ( final java . io . DataOutputStream output , final int i ) throws java . io . IOException
{
output . writeInt ( i ) ;
}

private final void bytes ( final java . io . DataOutputStream output , final byte [ ] buff , final int size ) throws java . io . IOException
{
final int n = buff . length ;
byte b = -1 ;
short s = -1 ;
int i = -1 ;
switch ( size )
{
case 0 :
break ;
case 1 :
b = ( byte ) ( n ) ;
u1 ( output , b ) ;
break ;
case 2 :
s = ( short ) ( n ) ;
u2 ( output , s ) ;
break ;
case 4 :
i = ( int ) ( n ) ;
u4 ( output , i ) ;
break ;
case 100 :
i = ( int ) ( n + 1 ) ;
u4 ( output , i ) ;
break ;
}
output . write ( buff ) ;
}
} ;

private final XMLWriter < ClassFile > classFileWriter = new XMLWriter < ClassFile > ( )
{
public final void write ( final ClassFile classFile , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
final class AbstractListWriter < R > implements XMLWriter < java . util . List < R > >
{
private AbstractListWriter ( final XMLWriter < R > rWriter )
{
super ( ) ;
this . rWriter = rWriter ;
}

private XMLWriter < R > rWriter ;

public final void write ( final java . util . List < R > list , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
final java . util . Iterator < R > iterator = list . iterator ( ) ;
while ( iterator . hasNext ( ) )
{
final R r = iterator . next ( ) ;
rWriter . write ( r , document , parent ) ;
}
}
}
final XMLWriter < ConstantPool > constantPoolWriter = new XMLWriter < ConstantPool > ( )
{
public final void write ( ConstantPool cp , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
final org . w3c . dom . Element element = document . createElement ( "constantPool" ) ;
final byte tag = cp . getTag ( ) ;
final byte [ ] info = cp . getInfo ( ) ;
u1 ( "tag" , tag , document , element ) ;
bytes ( info , document , element ) ;
parent . appendChild ( element ) ;
}
} ;
final XMLWriter < java . util . List < ConstantPool > > constantPoolListWriter = new AbstractListWriter ( constantPoolWriter ) ;
final XMLWriter < AttributeInfo > attributeInfoWriter = new XMLWriter < AttributeInfo > ( )
{
public final void write ( AttributeInfo attributeInfo , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
final org . w3c . dom . Element element = document . createElement ( "attribute" ) ;
final short attributeNameIndex = attributeInfo . getAttributeNameIndex ( ) ;
final byte [ ] info = attributeInfo . getInfo ( ) ;
u2 ( "attributeNameIndex" , attributeNameIndex , document , element ) ;
bytes ( info , document , element ) ;
parent . appendChild ( element ) ;
}
} ;
final XMLWriter < java . util . List < AttributeInfo > > attributeInfoListWriter = new AbstractListWriter ( attributeInfoWriter ) ;
final class FMWriter implements XMLWriter < FieldMethodInfo >
{
private FMWriter ( final java . lang . String name )
{
this . name = name ;
}

private java . lang . String name ;

public final void write ( FieldMethodInfo fmInfo , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
final org . w3c . dom . Element element = document . createElement ( name ) ;
final short accessFlags = fmInfo . getAccessFlags ( ) ;
final short nameIndex = fmInfo . getNameIndex ( ) ;
final short descriptorIndex = fmInfo . getDescriptorIndex ( ) ;
final java . util . List < AttributeInfo > attributes = fmInfo . getAttributes ( ) ;
u2 ( "accessFlags" , accessFlags , document , element ) ;
u2 ( "nameIndex" , nameIndex , document , element ) ;
u2 ( "descriptorIndex" , descriptorIndex , document , element ) ;
attributeInfoListWriter . write ( attributes , document , element ) ;
parent . appendChild ( element ) ;
}
} ;
final XMLWriter < FieldMethodInfo > fieldInfoWriter1 = new FMWriter ( "field" ) ;
final XMLWriter < FieldInfo > fieldInfoWriter = new XMLWriter < FieldInfo > ( )
{
public final void write ( FieldInfo fieldInfo , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
fieldInfoWriter1 . write ( fieldInfo , document , parent ) ;
}
} ;
final XMLWriter < java . util . List < FieldInfo > > fieldInfoListWriter = new AbstractListWriter ( fieldInfoWriter ) ;
final XMLWriter < FieldMethodInfo > methodInfoWriter1 = new FMWriter ( "field" ) ;
final XMLWriter < MethodInfo > methodInfoWriter = new XMLWriter < MethodInfo > ( )
{
public final void write ( MethodInfo methodInfo , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
methodInfoWriter1 . write ( methodInfo , document , parent ) ;
}
} ;
final XMLWriter < java . util . List < MethodInfo > > methodInfoListWriter = new AbstractListWriter ( methodInfoWriter ) ;
final int magic = classFile . getMagic ( ) ;
final short minorVersion = classFile . getMinorVersion ( ) ;
final short majorVersion = classFile . getMajorVersion ( ) ;
final java . util . List < ConstantPool > constantPool = classFile . getConstantPool ( ) ;
final short accessFlags = classFile . getAccessFlags ( ) ;
final short thisClass = classFile . getThisClass ( ) ;
final short superClass = classFile . getSuperClass ( ) ;
final short [ ] interfaces = classFile . getInterfaces ( ) ;
final java . util . List < FieldInfo > fields = classFile . getFields ( ) ;
final java . util . List < MethodInfo > methods = classFile . getMethods ( ) ;
final java . util . List < AttributeInfo > attributes = classFile . getAttributes ( ) ;
final org . w3c . dom . Element element = document . createElement ( "classFile" ) ;
u4 ( "magic" , magic , document , element ) ;
u2 ( "minorVersion" , minorVersion , document , element ) ;
u2 ( "majorVersion" , majorVersion , document , element ) ;
constantPoolListWriter . write ( constantPool , document , element ) ;
u2 ( "accessFlags" , accessFlags , document , element ) ;
u2 ( "thisClass" , thisClass , document , element ) ;
u2 ( "superClass" , superClass , document , element ) ;
for ( int i = 0 ; i < interfaces . length ; i ++ )
{
final org . w3c . dom . Element e = document . createElement ( "interface" ) ;
u2 ( "constantPoolIndex" , interfaces [ i ] , document , e ) ;
element . appendChild ( e ) ;
}
fieldInfoListWriter . write ( fields , document , element ) ;
methodInfoListWriter . write ( methods , document , element ) ;
attributeInfoListWriter . write ( attributes , document , element ) ;
parent . appendChild ( element ) ;
}

private final void u1 ( final java . lang . String name , final byte value , final org . w3c . dom . Document document , final org . w3c . dom . Element element )
{
final org . w3c . dom . Attr attribute = document . createAttribute ( name ) ;
final java . lang . String v = java . lang . Byte . toString ( value ) ;
attribute . setNodeValue ( v ) ;
element . setAttributeNode ( attribute ) ;
}

private final void u2 ( final java . lang . String name , final short value , final org . w3c . dom . Document document , final org . w3c . dom . Element element )
{
final org . w3c . dom . Attr attribute = document . createAttribute ( name ) ;
final java . lang . String v = java . lang . Short . toString ( value ) ;
attribute . setNodeValue ( v ) ;
element . setAttributeNode ( attribute ) ;
}

private final void u4 ( final java . lang . String name , final int value , final org . w3c . dom . Document document , final org . w3c . dom . Element element )
{
final org . w3c . dom . Attr attribute = document . createAttribute ( name ) ;
final java . lang . String v = java . lang . Integer . toString ( value ) ;
attribute . setNodeValue ( v ) ;
element . setAttributeNode ( attribute ) ;
}

private final void bytes ( final byte [ ] value , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
java . lang . String v = "" ;
if ( value . length > 0 )
{
v += value [ 0 ] ;
}
for ( int i = 1 ; i < value . length ; i ++ )
{
v += "," + value [ i ] ;
}
final org . w3c . dom . CDATASection cData = document . createCDATASection ( v ) ;
parent . appendChild ( cData ) ;
}
} ;

private final XMLReader < ClassFile > xmlReader = new XMLReader < ClassFile > ( )
{
public ClassFile read ( final org . w3c . dom . Node node )
{
class AbstractListReader < T > implements XMLReader < java . util . List < T > >
{
AbstractListReader ( final java . lang . String name , final XMLReader < T > tReader )
{
super ( ) ;
this . name = name ;
this . tReader = tReader ;
}

private java . lang . String name ;

private XMLReader < T > tReader ;

public final java . util . List < T > read ( final org . w3c . dom . Node node )
{
final org . w3c . dom . Element element = ( org . w3c . dom . Element ) ( node ) ;
final java . util . List < T > list = this . read ( element ) ;
return ( list ) ;
}

private final java . util . List < T > read ( final org . w3c . dom . Element element )
{
final java . util . List < T > list = new java . util . ArrayList < T > ( ) ;
final org . w3c . dom . NodeList nodes = getElementsByTagName ( element , this . name ) ;
for ( int i = 0 ; i < nodes . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node node = nodes . item ( i ) ;
T t = this . tReader . read ( node ) ;
list . add ( t ) ;
}
return ( list ) ;
}

private final org . w3c . dom . NodeList getElementsByTagName ( final org . w3c . dom . Node node , final java . lang . String tag )
{
final org . w3c . dom . NodeList children = node . getChildNodes ( ) ;
final java . util . List < org . w3c . dom . Node > nodes = new java . util . ArrayList < org . w3c . dom . Node > ( ) ;
for ( int i = 0 ; i < children . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node child = children . item ( i ) ;
final java . lang . String name = child . getNodeName ( ) ;
if ( tag . equals ( name ) )
{
nodes . add ( child ) ;
}
}
final org . w3c . dom . NodeList elements = new org . w3c . dom . NodeList ( )
{
public final int getLength ( )
{
final int length = nodes . size ( ) ;
return ( length ) ;
}

public final org . w3c . dom . Node item ( final int index )
{
final org . w3c . dom . Node item = nodes . get ( index ) ;
return ( item ) ;
}
} ;
return ( elements ) ;
}
}
final XMLReader < ConstantPool > constantPoolReader = new XMLReader ( )
{
public final ConstantPool read ( final org . w3c . dom . Node node )
{
final org . w3c . dom . Element element = ( org . w3c . dom . Element ) ( node ) ;
final ConstantPool cp = this . read ( element ) ;
return ( cp ) ;
}

private final ConstantPool read ( final org . w3c . dom . Element element )
{
final org . w3c . dom . NamedNodeMap attr = element . getAttributes ( ) ;
final byte tag = u1 ( attr , "tag" ) ;
final byte [ ] info = bytes ( element ) ;
final ConstantPool cp = new ConstantPool ( )
{
public final byte getTag ( )
{
return ( tag ) ;
}

public final byte [ ] getInfo ( )
{
return ( info ) ;
}
} ;
return ( cp ) ;
}
} ;
final XMLReader < java . util . List < ConstantPool > > constantPoolListReader = new AbstractListReader ( "constantPool" , constantPoolReader ) ;
final org . w3c . dom . NamedNodeMap attr = node . getAttributes ( ) ;
final int magic = u4 ( attr , "magic" ) ;
final short minorVersion = u2 ( attr , "minorVersion" ) ;
final short majorVersion = u2 ( attr , "majorVersion" ) ;
final java . util . List < ConstantPool > constantPool = constantPoolListReader . read ( node ) ;
final XMLReader < AttributeInfo > attributeInfoReader = new XMLReader < AttributeInfo > ( )
{
public final AttributeInfo read ( final org . w3c . dom . Node node )
{
final org . w3c . dom . NamedNodeMap attr = node . getAttributes ( ) ;
final short attributeNameIndex = u2 ( attr , "attributeNameIndex" ) ;
final byte [ ] info = bytes ( node ) ;
final AttributeInfo attributeInfo = new AttributeInfo ( )
{
public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAttributeNameIndex ( )
{
return ( attributeNameIndex ) ;
}

public final byte [ ] getInfo ( )
{
return ( info ) ;
}
} ;
return ( attributeInfo ) ;
}
} ;
final XMLReader < java . util . List < AttributeInfo > > attributeInfoListReader = new AbstractListReader ( "attribute" , attributeInfoReader ) ;
final XMLReader < FMInfo > fmInfoReader = new XMLReader ( )
{
public final FMInfo read ( final org . w3c . dom . Node node )
{
final org . w3c . dom . NamedNodeMap attr = node . getAttributes ( ) ;
final short accessFlags = u2 ( attr , "accessFlags" ) ;
final short nameIndex = u2 ( attr , "nameIndex" ) ;
final short descriptorIndex = u2 ( attr , "descriptorIndex" ) ;
final java . util . List < AttributeInfo > attributes = attributeInfoListReader . read ( node ) ;
final FMInfo fmInfo = new FMInfo ( )
{
public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAccessFlags ( )
{
return ( accessFlags ) ;
}

public final short getNameIndex ( )
{
return ( nameIndex ) ;
}

public final short getDescriptorIndex ( )
{
return ( descriptorIndex ) ;
}

public final java . util . List < AttributeInfo > getAttributes ( )
{
return ( attributes ) ;
}
} ;
return ( fmInfo ) ;
}
} ;
final XMLReader < FieldInfo > fieldInfoReader = new XMLReader < FieldInfo > ( )
{
public final FieldInfo read ( final org . w3c . dom . Node node )
{
final FieldInfo fieldInfo = fmInfoReader . read ( node ) ;
return ( fieldInfo ) ;
}
} ;
final XMLReader < java . util . List < FieldInfo > > fieldInfoListReader = new AbstractListReader ( "field" , fieldInfoReader ) ;
final XMLReader < MethodInfo > methodInfoReader = new XMLReader < MethodInfo > ( )
{
public final MethodInfo read ( final org . w3c . dom . Node node )
{
final MethodInfo methodInfo = fmInfoReader . read ( node ) ;
return ( methodInfo ) ;
}
} ;
final XMLReader < java . util . List < MethodInfo > > methodInfoListReader = new AbstractListReader ( "method" , methodInfoReader ) ;
final short accessFlags = u2 ( attr , "accessFlags" ) ;
final short thisClass = u2 ( attr , "thisClass" ) ;
final short superClass = u2 ( attr , "superClass" ) ;
final org . w3c . dom . NodeList interfaceList = ( ( org . w3c . dom . Element ) ( node ) ) . getElementsByTagName ( "interface" ) ;
final short [ ] interfaces = new short [ interfaceList . getLength ( ) ] ;
for ( int i = 0 ; i < interfaceList . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node intrfcNode = interfaceList . item ( i ) ;
final org . w3c . dom . NamedNodeMap intrfcAttr = intrfcNode . getAttributes ( ) ;
interfaces [ i ] = u2 ( intrfcAttr , "constantPoolIndex" ) ;
}
final java . util . List < FieldInfo > fields = fieldInfoListReader . read ( node ) ;
final java . util . List < MethodInfo > methods = methodInfoListReader . read ( node ) ;
final java . util . List < AttributeInfo > attributes = attributeInfoListReader . read ( node ) ;
final ClassFile classFile = new ClassFile ( )
{
public final int getMagic ( )
{
return ( magic ) ;
}

public final short getMinorVersion ( )
{
return ( minorVersion ) ;
}

public final short getMajorVersion ( )
{
return ( majorVersion ) ;
}

public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAccessFlags ( )
{
return ( accessFlags ) ;
}

public final short getThisClass ( )
{
return ( thisClass ) ;
}

public final short getSuperClass ( )
{
return ( superClass ) ;
}

public final short [ ] getInterfaces ( )
{
return ( interfaces ) ;
}

public final java . util . List < FieldInfo > getFields ( )
{
return ( fields ) ;
}

public final java . util . List < MethodInfo > getMethods ( )
{
return ( methods ) ;
}

public final java . util . List < AttributeInfo > getAttributes ( )
{
return ( attributes ) ;
}
} ;
return ( classFile ) ;
}

private final byte u1 ( final org . w3c . dom . NamedNodeMap attributes , final java . lang . String key )
{
final org . w3c . dom . Node node = attributes . getNamedItem ( key ) ;
final java . lang . String v = node . getNodeValue ( ) ;
final byte b = java . lang . Byte . parseByte ( v ) ;
return ( b ) ;
}

private final short u2 ( final org . w3c . dom . NamedNodeMap attributes , final java . lang . String key )
{
final org . w3c . dom . Node node = attributes . getNamedItem ( key ) ;
final java . lang . String v = node . getNodeValue ( ) ;
final short b = java . lang . Short . parseShort ( v ) ;
return ( b ) ;
}

private final int u4 ( final org . w3c . dom . NamedNodeMap attributes , final java . lang . String key )
{
final org . w3c . dom . Node node = attributes . getNamedItem ( key ) ;
final java . lang . String v = node . getNodeValue ( ) ;
final int b = java . lang . Integer . parseInt ( v ) ;
return ( b ) ;
}

private final byte [ ] bytes ( final org . w3c . dom . Node node )
{
byte [ ] b = null ;
final org . w3c . dom . NodeList childNodes = node . getChildNodes ( ) ;
for ( int i = 0 ; i < childNodes . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node child = childNodes . item ( i ) ;
if ( child instanceof org . w3c . dom . CDATASection )
{
final java . lang . String string = child . getNodeValue ( ) ;
final java . util . StringTokenizer counter = new java . util . StringTokenizer ( string , "," , false ) ;
int count = 0 ;
for ( count = 0 ; counter . hasMoreTokens ( ) ; count ++ )
{
counter . nextToken ( ) ;
}
b = new byte [ count ] ;
final java . util . StringTokenizer tokenizer = new java . util . StringTokenizer ( string , "," , false ) ;
for ( int j = 0 ; j < count ; j ++ )
{
final java . lang . String token = tokenizer . nextToken ( ) ;
final byte t = java . lang . Byte . parseByte ( token ) ;
b [ j ] = t ;
}
}
}
return ( b ) ;
}
} ;

private final void print ( final java . io . PrintStream out , final org . w3c . dom . NodeList nodes )
{
for ( int i = 0 ; i < nodes . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node node = nodes . item ( i ) ;
this . print ( out , node ) ;
}
}

private final void print ( final java . io . PrintStream out , final org . w3c . dom . NamedNodeMap nodes )
{
for ( int i = 0 ; i < nodes . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node node = nodes . item ( i ) ;
this . print ( out , node ) ;
}
}

private final java . lang . Object printer = new java . lang . Object ( )
{
} ;

private final void print ( final java . io . PrintStream out , final org . w3c . dom . Node node )
{
if ( node instanceof org . w3c . dom . Document )
{
this . print ( out , ( org . w3c . dom . Document ) ( node ) ) ;
}
else if ( node instanceof org . w3c . dom . Element )
{
this . print ( out , ( org . w3c . dom . Element ) ( node ) ) ;
}
else if ( node instanceof org . w3c . dom . Attr )
{
this . print ( out , ( org . w3c . dom . Attr ) ( node ) ) ;
}
else if ( node instanceof org . w3c . dom . CDATASection )
{
this . print ( out , ( org . w3c . dom . CDATASection ) ( node ) ) ;
}
}

private final void print ( final java . io . PrintStream out , final org . w3c . dom . Document node )
{
final org . w3c . dom . NodeList childNodes = node . getChildNodes ( ) ;
this . print ( out , childNodes ) ;
}

private final void print ( final java . io . PrintStream out , final org . w3c . dom . Element node )
{
final java . lang . String tagName = node . getTagName ( ) ;
out . print ( "<" + tagName ) ;
final org . w3c . dom . NamedNodeMap namedNodeMap = node . getAttributes ( ) ;
print ( out , namedNodeMap ) ;
out . println ( ">" ) ;
final org . w3c . dom . NodeList childNodes = node . getChildNodes ( ) ;
this . print ( out , childNodes ) ;
out . println ( "</" + tagName + ">" ) ;
}

private final void print ( final java . io . PrintStream out , final org . w3c . dom . Attr node )
{
final java . lang . String name = node . getName ( ) ;
final java . lang . String value = node . getValue ( ) ;
out . print ( " " + name + "=\"" + value + "\"" ) ;
}

private final void print ( final java . io . PrintStream out , final org . w3c . dom . CDATASection node )
{
final java . lang . String value = node . getNodeValue ( ) ;
out . println ( "<![CDATA[" + value + "]]>" ) ;
}
}

/*
* XMLClassUtil.java
*/
I have been working on this project for a little bit.
The program is still very rough.
This is an outline of what it is supposed to do.

You can use this program to read in a java class file.
It will read all the bytes and organize them into an XML file.
The program will print the XML file.

You can easily read the XML file.
You can make minor changes.

The program can also read in a xml file and write out bytes.

Project Goal:  to better understand the class file format.

Project Status:  the program works almost.
I have used HelloWorld.class as a simple test case.
I converted HelloWorld.class to HelloWorld.xml and reconverted HelloWorld.xml to HelloWorld.class.1.
HelloWorld.class is the same length as HelloWorld.class.1.
Most of the bytes are the same.
In fact, the first 70 some percent bytes exactly match.
At some point, if you shift, then they would exactly match.
(Shifting=put x byte at end, x+1 byte at x, x+2 byte at x+1, ...., end byte at end-1.)

22 November 2007

A Simple Robot Application

public final class HawkingRobot
{
private static final int LEFT = 37 ;

private static final int UP = 38 ;

private static final int RIGHT = 39 ;

private static final int DOWN = 40 ;

private static final int PLUS = 61 ;

private static final int MINUS = 45 ;

private static final int ASTERISK = 56 ;

private static final int SLASH = 47 ;

private static final int ENTER = java . awt . event . KeyEvent . VK_ENTER ;

private static final int ESC = 27 ;

private static final int TOGGLEMOUSE = java . awt . event . KeyEvent . VK_SPACE ;

private static final int MOUSEHOME = java . awt . event . KeyEvent . VK_H ;

private static final int MOUSEMOVE = java . awt . event . KeyEvent . VK_M ;

private static final int MOUSEPRESS = java . awt . event . KeyEvent . VK_Q ;

private static final int MOUSERELEASE = java . awt . event . KeyEvent . VK_W ;

private static final int KEYPRESS = java . awt . event . KeyEvent . VK_E ;

private static final int KEYRELEASE = java . awt . event . KeyEvent . VK_R ;

private static final int GETPIXELCOLOR = java . awt . event . KeyEvent . VK_C ;

private static final int CREATESCREENCAPTURE = java . awt . event . KeyEvent . VK_S ;

private static final int AUTOWAIT = java . awt . event . KeyEvent . VK_A ;

private static final int DELAY = java . awt . event . KeyEvent . VK_D ;

private static final int IDLE = java . awt . event . KeyEvent . VK_I ;

public static final void main ( final java . lang . String [ ] args ) throws java . lang . Exception
{
final java . lang . Runnable exitRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
java . lang . System . exit ( 0 ) ;
}
} ;
final javax . swing . JFrame jFrame = new javax . swing . JFrame ( ) ;
final java . awt . Container contentPane = jFrame . getContentPane ( ) ;
final javax . swing . JDesktopPane jDesktopPane = new javax . swing . JDesktopPane ( ) ;
contentPane . add ( jDesktopPane ) ;
final java . awt . Robot robot = new java . awt . Robot ( ) ;
final java . lang . Runnable robotRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
final boolean recordMode [ ] = new boolean [ 1 ] ;
final java . util . List < java . lang . Runnable > runnables = new java . util . ArrayList < java . lang . Runnable > ( ) ;
final int [ ] point = new int [ 2 ] ;
final boolean [ ] mouseMode = new boolean [ 1 ] ;
mouseMode [ 0 ] = true ;
final int [ ] keyMode = new int [ 2 ] ;
keyMode [ 0 ] = 1 ;
final int [ ] sleep = new int [ 1 ] ;
sleep [ 0 ] = 1 ;
final javax . swing . JInternalFrame jInternalFrame = new javax . swing . JInternalFrame ( "Robot" , true , true , true , true ) ;
final javax . swing . JButton jButton = new javax . swing . JButton ( "Mouse" ) ;
jButton . setFocusable ( true ) ;
jButton . setRequestFocusEnabled ( true ) ;
jInternalFrame . getContentPane ( ) . add ( jButton ) ;
final java . lang . Runnable toggleMouseModeRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
mouseMode [ 0 ] = ! mouseMode [ 0 ] ;
}
} ;
final java . lang . Runnable mouseToPointRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
final int x = point [ 0 ] ;
final int y = point [ 1 ] ;
robot . mouseMove ( x , y ) ;
}
} ;
final java . lang . Runnable mouseMoveRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
final int x = point [ 0 ] ;
final int y = point [ 1 ] ;
final java . lang . Runnable runnable = new java . lang . Runnable ( )
{
public final void run ( )
{
robot . mouseMove ( x , y ) ;
}
} ;
runnables . add ( runnable ) ;
}
} ;
final java . lang . Runnable mousePressRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
final int [ ] pressOrRelease = new int [ 1 ] ;
final int [ ] buttons = new int [ 1 ] ;
final java . util . Map < java . lang . Integer , java . lang . String > codeMap = new CodeMap ( ) ;
codeMap . put ( java . awt . event . InputEvent . BUTTON1_MASK , "Button 1" ) ;
codeMap . put ( java . awt . event . InputEvent . BUTTON2_MASK , "Button 2" ) ;
codeMap . put ( java . awt . event . InputEvent . BUTTON3_MASK , "Button 3" ) ;
final int x = point [ 0 ] ;
final int y = point [ 1 ] ;
mouseMode [ 0 ] = false ;
radio ( jInternalFrame , codeMap , pressOrRelease , buttons ) ;
toggleMouseModeRunnable . run ( ) ;
final java . lang . Runnable runnable = new java . lang . Runnable ( )
{
public final void run ( )
{
robot . mouseMove ( x , y ) ;
if ( pressOrRelease [ 0 ] == 1 )
{
robot . mousePress ( buttons [ 0 ] ) ;
}
else if ( pressOrRelease [ 0 ] == 2 )
{
robot . mouseRelease ( buttons [ 0 ] ) ;
}
else if ( pressOrRelease [ 0 ] == 3 )
{
robot . mousePress ( buttons [ 0 ] ) ;
robot . mouseRelease ( buttons [ 0 ] ) ;
}
}
} ;
runnables . add ( runnable ) ;
runnable . run ( ) ;
jButton . requestFocus ( ) ;
}
} ;
final java . lang . Runnable keyPressRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
final int [ ] pressOrRelease = new int [ 1 ] ;
final int [ ] buttons = new int [ 1 ] ;
final java . util . Map < java . lang . Integer , java . lang . String > codeMap = new CodeMap ( ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_A , "A" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_B , "B" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_C , "C" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_D , "D" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_E , "E" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_F , "F" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_G , "G" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_H , "H" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_I , "I" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_J , "J" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_K , "K" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_L , "L" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_M , "M" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_N , "N" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_O , "O" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_P , "P" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_Q , "Q" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_R , "R" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_S , "S" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_T , "T" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_U , "U" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_V , "V" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_W , "W" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_X , "X" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_Y , "Y" ) ;
codeMap . put ( java . awt . event . KeyEvent . VK_Z , "Z" ) ;
final int x = point [ 0 ] ;
final int y = point [ 1 ] ;
mouseMode [ 0 ] = false ;
radio ( jInternalFrame , codeMap , pressOrRelease , buttons ) ;
toggleMouseModeRunnable . run ( ) ;
final java . lang . Runnable runnable = new java . lang . Runnable ( )
{
public final void run ( )
{
robot . mouseMove ( x , y ) ;
if ( pressOrRelease [ 0 ] == 1 )
{
robot . keyPress ( buttons [ 0 ] ) ;
}
else if ( pressOrRelease [ 0 ] == 2 )
{
robot . keyRelease ( buttons [ 0 ] ) ;
}
else if ( pressOrRelease [ 0 ] == 3 )
{
robot . keyPress ( buttons [ 0 ] ) ;
robot . keyRelease ( buttons [ 0 ] ) ;
}
}
} ;
runnables . add ( runnable ) ;
runnable . run ( ) ;
jButton . requestFocus ( ) ;
}
} ;
final java . lang . Runnable delayRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
final java . lang . Runnable runnable = new java . lang . Runnable ( )
{
final java . lang . String delayString = javax . swing . JOptionPane . showInputDialog ( jInternalFrame , "Delay (in milliseconds)" ) ;
final int delay = java . lang . Integer . parseInt ( delayString ) ;
public final void run ( )
{
robot . delay ( delay ) ;
}
} ;
runnables . add ( runnable ) ;
}
} ;
final java . awt . event . KeyListener keyListener = new java . awt . event . KeyAdapter ( )
{
public final void keyPressed ( final java . awt . event . KeyEvent e )
{
final int keyCode = e . getKeyCode ( ) ;
if ( keyCode == TOGGLEMOUSE )
{
toggleMouseModeRunnable . run ( ) ;
}
else if ( keyCode == MOUSEHOME )
{
point [ 0 ] = 0 ;
point [ 1 ] = 1 ;
}
else if ( keyCode == MOUSEMOVE )
{
mouseMoveRunnable . run ( ) ;
}
else if ( keyCode == MOUSEPRESS )
{
mousePressRunnable . run ( ) ;
}
else if ( keyCode == KEYPRESS )
{
keyPressRunnable . run ( ) ;
}
else if ( keyCode == DELAY )
{
delayRunnable . run ( ) ;
}
else if ( ( keyCode == LEFT ) || ( keyCode == RIGHT ) || ( keyCode == UP ) || ( keyCode == DOWN ) )
{
keyMode [ 0 ] = 1 ;
keyMode [ 1 ] = keyCode ;
}
else if ( keyCode == PLUS )
{
sleep [ 0 ] ++ ;
}
else if ( keyCode == MINUS )
{
sleep [ 0 ] -- ;
}
else if ( keyCode == ASTERISK )
{
sleep [ 0 ] *= 2 ;
}
else if ( keyCode == SLASH )
{
sleep [ 0 ] /= 2 ;
}
else if ( keyCode == ESC )
{
recordMode [ 0 ] = false ;
jButton . removeKeyListener ( this ) ;
}
if ( sleep [ 0 ] <= 0 )
{
sleep [ 0 ] = 1 ;
}
else if ( sleep [ 0 ] > 2048 )
{
sleep [ 0 ] = 2048 ;
}
}

public final void keyReleased ( final java . awt . event . KeyEvent e )
{
keyMode [ 0 ] = 0 ;
}
} ;
final java . lang . Runnable recordRobotRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
jButton . requestFocus ( ) ;
jButton . addKeyListener ( keyListener ) ;
recordMode [ 0 ] = true ;
while ( recordMode [ 0 ] )
{
if ( ( keyMode [ 0 ] == 1 ) && ( mouseMode [ 0 ] ) )
{
if ( keyMode [ 1 ] == LEFT )
{
point [ 0 ] -- ;
}
else if ( keyMode [ 1 ] == RIGHT )
{
point [ 0 ] ++ ;
}
else if ( keyMode [ 1 ] == UP )
{
point [ 1 ] -- ;
}
else if ( keyMode [ 1 ] == DOWN )
{
point [ 1 ] ++ ;
}
else
{
keyMode [ 0 ] = 0 ;
}
}
if ( mouseMode [ 0 ] == true )
{
mouseToPointRunnable . run ( ) ;
robot . delay ( sleep [ 0 ] ) ;
}
}
}
} ;
final java . lang . Runnable runRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
final java . util . Iterator < java . lang . Runnable > iter = runnables . iterator ( ) ;
while ( iter . hasNext ( ) )
{
final java . lang . Runnable next = iter . next ( ) ;
next . run ( ) ;
}
}
} ;
final java . lang . Runnable rerunRunnable = new java . lang . Runnable ( )
{
public final void run ( )
{
while ( true )
{
runRunnable . run ( ) ;
try
{
java . lang . Thread . sleep ( 60 * 1000 ) ;
}
catch ( final java . lang . Exception cause )
{
cause . printStackTrace ( ) ;
}
}
}
} ;
jInternalFrame . setSize ( 160 , 160 ) ;
final javax . swing . JMenuBar jMenuBar = new javax . swing . JMenuBar ( ) ;
final javax . swing . JMenu jMenu = new javax . swing . JMenu ( "Robot" ) ;
action ( recordRobotRunnable , "Record Robot" , jMenu ) ;
action ( runRunnable , "Run" , jMenu ) ;
action ( rerunRunnable , "Continuously Run" , jMenu ) ;
jMenuBar . add ( jMenu ) ;
jInternalFrame . setJMenuBar ( jMenuBar ) ;
jInternalFrame . setVisible ( true ) ;
jDesktopPane . add ( jInternalFrame ) ;
}
} ;
jFrame . setSize ( 200 , 200 ) ;
final javax . swing . JMenuBar jMenuBar = new javax . swing . JMenuBar ( ) ;
final javax . swing . JMenu jMenu = new javax . swing . JMenu ( "File" ) ;
action ( robotRunnable , "Robot" , jMenu ) ;
action ( exitRunnable , "Exit" , jMenu ) ;
jMenuBar . add ( jMenu ) ;
jFrame . setJMenuBar ( jMenuBar ) ;
final java . awt . event . WindowListener windowListener = new java . awt . event . WindowAdapter ( )
{
public final void windowClosing ( final java . awt . event . WindowEvent e )
{
start ( exitRunnable ) ;
}
} ;
jFrame . addWindowListener ( windowListener ) ;
jFrame . setVisible ( true ) ;
}

private static final void start ( final java . lang . Runnable runnable )
{
final java . lang . Thread thread = new java . lang . Thread ( runnable ) ;
thread . start ( ) ;
}

private static final void action ( final java . lang . Runnable runnable , final java . lang . String name , final javax . swing . JMenu jMenu )
{
final javax . swing . Action action = action ( runnable , name ) ;
jMenu . add ( action ) ;
}

private static final javax . swing . Action action ( final java . lang . Runnable runnable , final java . lang . String name )
{
final javax . swing . Action action = new javax . swing . AbstractAction ( name )
{
public final void actionPerformed ( final java . awt . event . ActionEvent e )
{
start ( runnable ) ;
}
} ;
return ( action ) ;
}

private static final void radio ( final javax . swing . ButtonGroup buttonGroup , final javax . swing . JPanel jPanel , final java . lang . String name , final int [ ] code , final int codeValue )
{
final javax . swing . Action action = new javax . swing . AbstractAction ( name )
{
public final void actionPerformed ( final java . awt . event . ActionEvent e )
{
code [ 0 ] = codeValue ;
}
} ;
final javax . swing . JRadioButton jRadioButton = new javax . swing . JRadioButton ( action ) ;
buttonGroup . add ( jRadioButton ) ;
jPanel . add ( jRadioButton ) ;
}

private static final void radio ( final javax . swing . JInternalFrame jInternalFrame , final java . util . Map < java . lang . Integer , java . lang . String > codeMap , final int [ ] pressOrRelease , final int [ ] code )
{
final javax . swing . JPanel prPanel = new javax . swing . JPanel ( ) ;
final javax . swing . JPanel cdPanel = new javax . swing . JPanel ( ) ;
final javax . swing . ButtonGroup prGroup = new javax . swing . ButtonGroup ( ) ;
final javax . swing . ButtonGroup cdGroup = new javax . swing . ButtonGroup ( ) ;
radio ( prGroup , prPanel , "Press" , pressOrRelease , 1 ) ;
radio ( prGroup , prPanel , "Release" , pressOrRelease , 2 ) ;
radio ( prGroup , prPanel , "Press And Release" , pressOrRelease , 3 ) ;
final java . util . Set < java . lang . Integer > keySet = codeMap . keySet ( ) ;
final java . util . Iterator < java . lang . Integer > iterator = keySet . iterator ( ) ;
while ( iterator . hasNext ( ) )
{
final java . lang . Integer key = iterator . next ( ) ;
final int k = key . intValue ( ) ;
final java . lang . String name = codeMap . get ( key ) ;
radio ( cdGroup , cdPanel , name , code , k ) ;
}
final javax . swing . JPanel jPanel = new javax . swing . JPanel ( ) ;
jPanel . add ( prPanel ) ;
jPanel . add ( cdPanel ) ;
javax . swing . JOptionPane . showMessageDialog ( jInternalFrame , jPanel ) ;
}

private static final class CodeMap extends java . util . Hashtable < java . lang . Integer , java . lang . String >
{
public final void put ( final int key , final java . lang . Object value )
{
final java . lang . Integer k = new java . lang . Integer ( key ) ;
final java . lang . String v = value . toString ( ) ;
super . put ( k , v ) ;
}
}
}

This is a simle Robot application. It makes use of the class java.awt.Robot.

You can use it to record a robot.
When you record a robot, you use the keyboard to control input to the system.
For example, you use the up, down, left, and right arrow keys to control the mouse.
The application will record your inputs.

Then later, you can play them back.

For now, I have abandoned this project.

15 November 2007

Emory Merryman, III

Education

  • Master of Science (3.45 GPA) in Computer Science, McGill University, Montreal, Quebec, Canada, 2005.
    • Topic: High Performance Distributed Database Systems
    • Databases Studied: DB2, Oracle
  • Master of Science in Applied Statistics (3.35 GPA), Bowling Green State University, Bowling Green, Ohio, United States of America, 1999.
  • Bachelor of Science in Psychology (Cum Laude), James Madison University, Harrisonburg, Virginia, United States of America, 1996.
    • Recipient American Society for Quality Control Applied Statistics Award

Experience

  • Senior Lecturer, Missouri State University Branch Campus Dalian, Dalian, Liaoning, China, 2007.
    • Taught Students how to use Microsoft Office: Excel, Word, PowerPoint, Access
    • Taught Students simple web page design (html)
  • Foreign Expert, Southwest University of Science and Technology, Mianyang, Sichuan, China, 2005 -- 2007.
    • Taught Students Database Systems (Microsoft Access, Microsoft SQLServer)
  • Teaching Assistant, McGill University, Montreal, Quebec, Canada, 2003 -- 2004.
    • Graded Papers and Tutored Students for Introduction to Computing (Java) and Database Systems (Oracle)
  • Consulting Analyst, NDCHealth (now McKesson), Philadelphia, Pennsylvania, United States of America, 2000 -- 2001.
    • Synthesized and Integrate Data to Derive New Products, Maps, and Tools
    • Prepared Excel and Access Tables
    • Statistical Programming and Analysis using SAS
    • Presented Analyses to Non-Technical Audience
  • Mathematical Statistician, Census Bureau, Washington, DC, United States of America, 1999 -- 2000.
    • Developed a SAS/ASSIST tutorial
    • Statistical Programming on the StEPS project (a large Data Warehouse) using SAS
  • Graduate Research Assistant, Department of Applied Statistics and Operations Research, Bowling Green State University, Bowling Green, Ohio, United States of America (1997-1999).
    • Developed and Maintained the Departmental web page
    • Assisted Faculty and Graduate Students (often in non technical disciplines) with Statistical Programming (SAS and SPSS) in the Statistical Consulting Center

Computer Skills

Statistical Programming
Office
Programming Languages
Databases
SAS
R
S+
SPSS
Word
Excel
PowerPoint
Access
Java
C++
C
SQL
JDBC
MySQL

SQLServer
Oracle
DB2

Graduate Level Course Work

Mathematical Statistics
Computer Science
Probability
Exploratory Data Analysis
Regression Analysis
Statistical Quality Control
Survival Analysis
Experimental Design
Applied Nonparametric Statistics
Applied Multivariate Statistics
Sample Design
Time Series Analysis
Statistical Packages
Experimental Design
Cryptography and Data Security
Distributed Database Systems
Database Systems
Distributed Algorithms
Probabilistic Analysis of Algorithms
Fault Tolerant Computing
Computational Geometry
Object Oriented Software Development
Computer Networks

References Available Upon Request


Installing Knowledge Tree

Knowledge Tree is a document management system.
A document management systems is a computer system used to track and store electronic documents and or scanned copies of paper documents.
Document management systems are becoming increasingly important.
The theoretical paperless office would depend on a document management system.

Yesterday, I reviewed Knowledge Tree.
Jam Warehouse developed Knowledge Tree and maintains the Knowledge Tree web page.
From this web page, I downloaded the Knowledge Tree source code and documentation.

There are at least four Knowledge Tree editions:
  1. Knowledge Tree SMB
  2. Knowledge Tree Enterprise Edition
  3. Knowledge Tree Live
  4. Knowledge Tree Open Source Edition
Jam Warehouse prepared a matrix that is useful for comparing the various editions.
I chose to investigate the Knowledge Tree Open Source Edition primarily because it is inexpensive (free). I did not look at the other editions.
However there is a limited free trial offer for Knowledge Tree Enterprise Edition and Knowledge Tree Live.
Curious readers are encouraged to report back the results of their reviews of the other editions.

To download this software, I followed the appropriate links to http://www.knowledgetree.com/node/38.
I filled in a form with my name and email address.
Jam Warehouse sent me an email with a link to an appropriate download site.
I downloaded version 3.4.4.
Since Knowledge Tree Open Source Edition is released under GNU GPL Version 3, if there is a problem with the download site, I am legally able to distribute my copy.

There were five choices on the Download page:
  1. Knowledge Tree Open Source Stack Installer
  2. Knowledge Tree Open Source Source Only
  3. Knowledge Tree Appliance
  4. Knowledge Tree Open Source Development Release
  5. Knowledge Tree Documentation
I downloaded all of the documentation.

At the time, it seemed clear that I wanted to download Knowledge Tree Open Source Source Only.
However, this was a compressed file (.tgz) that was unusable.
However, I found in Knowledge Tree Open Source Development Release a useable zip file.

I installed the software on a computer running Red Hat Linux Fedora Core 7.
The computer had the following services:
  1. Apache/2.2.6
  2. MySQL Server Version 5.0.45
  3. PHP version 4.4.7
The computer has:
  1. 343.8 Mb of RAM memory
  2. a Pentium III Processor
  3. approximately 27 Gigabytes of hard disk memory
I unzipped the files and copied them to /var/www/html.
I recursively changed the ownership of the files to the web user. (chown -R apache:apache /var/www/html).

Then it was necessary to execute some sql scripts supplied in the distribution.
These sql scripts established users, tables, and input some preliminary data.
Personally, I felt it necessary to read the scripts before I would execute them.
I feel that there must be a better way to setup the database.

After that the software was installed.
Perhaps in a future blog, I will review Knowledge Tree operation.