From d10176e6f39375655c1322838d0fa421054476f1 Mon Sep 17 00:00:00 2001 From: Michael Lum Date: Thu, 31 Jul 2008 17:53:34 +0000 Subject: [PATCH] added ESF parser, serializer and event types git-svn-id: https://lwes.svn.sourceforge.net/svnroot/lwes/lwes-java/trunk@8 a2f82657-cdd2-4550-bd36-68a8e7111808 --- .classpath | 1 + src/org/lwes/BaseType.java | 166 +++++++ src/org/lwes/Event.java | 21 +- src/org/lwes/EventSystemException.java | 25 + src/org/lwes/NoSuchAttributeTypeException.java | 15 + src/org/lwes/TypeID.java | 178 +++++++ src/org/lwes/db/ESFParser.java | 285 +++++++++++ src/org/lwes/db/ESFParser.jj | 157 ++++++ src/org/lwes/db/ESFParserConstants.java | 29 ++ src/org/lwes/db/ESFParserTokenManager.java | 358 ++++++++++++++ src/org/lwes/db/EventTemplateDB.java | 543 +++++++++++++++++++++ src/org/lwes/db/ParseException.java | 192 ++++++++ src/org/lwes/db/SimpleCharStream.java | 439 +++++++++++++++++ src/org/lwes/db/Token.java | 81 +++ src/org/lwes/db/TokenMgrError.java | 133 +++++ src/org/lwes/emitter/MulticastEventEmitter.java | 6 +- .../lwes/{util => serializer}/Deserializer.java | 48 +- .../{util => serializer}/DeserializerState.java | 4 +- src/org/lwes/{util => serializer}/Serializer.java | 4 +- src/org/lwes/serializer/StringParser.java | 238 +++++++++ src/org/lwes/util/IPAddress.java | 2 +- src/org/lwes/util/Log.java | 77 +++ 22 files changed, 2961 insertions(+), 41 deletions(-) create mode 100644 src/org/lwes/BaseType.java create mode 100644 src/org/lwes/EventSystemException.java create mode 100644 src/org/lwes/NoSuchAttributeTypeException.java create mode 100644 src/org/lwes/TypeID.java create mode 100644 src/org/lwes/db/ESFParser.java create mode 100644 src/org/lwes/db/ESFParser.jj create mode 100644 src/org/lwes/db/ESFParserConstants.java create mode 100644 src/org/lwes/db/ESFParserTokenManager.java create mode 100644 src/org/lwes/db/EventTemplateDB.java create mode 100644 src/org/lwes/db/ParseException.java create mode 100644 src/org/lwes/db/SimpleCharStream.java create mode 100644 src/org/lwes/db/Token.java create mode 100644 src/org/lwes/db/TokenMgrError.java rename src/org/lwes/{util => serializer}/Deserializer.java (88%) rename src/org/lwes/{util => serializer}/DeserializerState.java (93%) rename src/org/lwes/{util => serializer}/Serializer.java (97%) create mode 100644 src/org/lwes/serializer/StringParser.java create mode 100644 src/org/lwes/util/Log.java diff --git a/.classpath b/.classpath index bdb2412..ff68450 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,7 @@ + diff --git a/src/org/lwes/BaseType.java b/src/org/lwes/BaseType.java new file mode 100644 index 0000000..bb0e73d --- /dev/null +++ b/src/org/lwes/BaseType.java @@ -0,0 +1,166 @@ +package org.lwes; + +import org.lwes.serializer.StringParser; +import org.lwes.util.EncodedString; + +/** + * This class provides a base type for the base types in the event system. acts + * partially as an interface and partially to provide encapsulation of the + * TypeIDs used in serialization. + * + * It also provides a sizeof() type method called getByteSize() used to + * determine how many bytes must be used to serialize an object of the given + * type. + * + * @author Anthony Molinaro + */ +public class BaseType { + /** + * The name of this type used in the ESF file + */ + String typeName = null; + + /** + * The type token used during serialization + */ + byte typeToken = TypeID.UNDEFINED_TOKEN; + + /** + * The object stored in this type + */ + Object typeObject = null; + + public BaseType() { + } + + public BaseType(String typeName, byte typeToken) { + this.typeName = typeName; + this.typeToken = typeToken; + } + + public BaseType(String typeName, byte typeToken, Object typeObject) { + this.typeName = typeName; + this.typeToken = typeToken; + this.typeObject = typeObject; + } + + public void setTypeName(String typeName) { + this.typeName = typeName; + } + + public String getTypeName() { + return typeName; + } + + public void setTypeToken(byte typeToken) { + this.typeToken = typeToken; + } + + public byte getTypeToken() { + return typeToken; + } + + public void setTypeObject(Object typeObject) { + this.typeObject = typeObject; + } + + public Object getTypeObject() { + return typeObject; + } + + public int getByteSize(short encoding) throws NoSuchAttributeTypeException { + int size; + switch (typeToken) { + case TypeID.UINT16_TOKEN: + size = 2; + break; + case TypeID.INT16_TOKEN: + size = 2; + break; + case TypeID.UINT32_TOKEN: + size = 4; + break; + case TypeID.INT32_TOKEN: + size = 4; + break; + case TypeID.INT64_TOKEN: + size = 8; + break; + case TypeID.UINT64_TOKEN: + size = 8; + break; + case TypeID.STRING_TOKEN: + String aString = (String) typeObject; + /* add size of string plus two bytes for the length */ + size = EncodedString.getBytes(aString, + Event.ENCODING_STRINGS[encoding]).length + 2; + break; + case TypeID.IPADDR_TOKEN: + size = 4; + break; + case TypeID.BOOLEAN_TOKEN: + size = 1; + break; + default: + throw new NoSuchAttributeTypeException("Unknown size of BaseType " + + typeName); + } + return size; + } + + public int bytesStoreSize(short encoding) { + /* add size of data plus size of token denoting data type */ + try { + return getByteSize(encoding) + 1; + } catch (NoSuchAttributeTypeException e) { + return 0; + } + } + + public Object parseFromString(String string) throws EventSystemException { + Object toReturn = null; + switch (typeToken) { + case TypeID.UINT16_TOKEN: + toReturn = StringParser.fromStringUINT16(string); + break; + case TypeID.INT16_TOKEN: + toReturn = StringParser.fromStringINT16(string); + break; + case TypeID.UINT32_TOKEN: + toReturn = StringParser.fromStringUINT32(string); + break; + case TypeID.INT32_TOKEN: + toReturn = StringParser.fromStringINT32(string); + break; + case TypeID.INT64_TOKEN: + toReturn = StringParser.fromStringINT64(string); + break; + case TypeID.UINT64_TOKEN: + toReturn = StringParser.fromStringUINT64(string); + break; + case TypeID.STRING_TOKEN: + toReturn = StringParser.fromStringSTRING(string); + break; + case TypeID.IPADDR_TOKEN: + toReturn = StringParser.fromStringIPADDR(string); + break; + case TypeID.BOOLEAN_TOKEN: + toReturn = StringParser.fromStringBOOLEAN(string); + break; + default: + throw new NoSuchAttributeTypeException("Unknown size of BaseType " + + typeName); + } + return toReturn; + } + + public BaseType cloneBaseType() { + BaseType newBaseType = new BaseType(getTypeName(), getTypeToken(), + getTypeObject()); + return newBaseType; + } + + public String toString() { + return typeObject.toString(); + } +} diff --git a/src/org/lwes/Event.java b/src/org/lwes/Event.java index f6c35b6..cf4f565 100644 --- a/src/org/lwes/Event.java +++ b/src/org/lwes/Event.java @@ -1,29 +1,38 @@ package org.lwes; +import java.util.concurrent.ConcurrentHashMap; + import org.lwes.util.CharacterEncoding; public class Event { - /* + /** * Encoding variables */ public static final short ISO_8859_1 = 0; public static final short UTF_8 = 1; public static final short DEFAULT_ENCODING = UTF_8; - public static final CharacterEncoding[] ENCODING_STRINGS = - {CharacterEncoding.ISO_8859_1, CharacterEncoding.UTF_8}; - + public static final CharacterEncoding[] ENCODING_STRINGS = { + CharacterEncoding.ISO_8859_1, CharacterEncoding.UTF_8 }; + + /** + * Event data + */ + private ConcurrentHashMap attributes = new ConcurrentHashMap(); + private String name = null; + private short encoding = DEFAULT_ENCODING; + /** * the size of the event in bytes */ private int bytesStoreSize = 0; - + /** * * @return the serialized byte array */ public byte[] serialize() { byte[] bytes = new byte[this.bytesStoreSize]; - + return bytes; } } diff --git a/src/org/lwes/EventSystemException.java b/src/org/lwes/EventSystemException.java new file mode 100644 index 0000000..7b622a1 --- /dev/null +++ b/src/org/lwes/EventSystemException.java @@ -0,0 +1,25 @@ +package org.lwes; + +/** + * This is the parent class of all the Exceptions thrown by the event system + * + * @author Anthony Molinaro + */ +public class EventSystemException extends Exception { + /** + * Overrides Exception constructor + * @param e the parent exception + */ + + public EventSystemException(Exception e) { + super(e); + } + + /** + * Overrides Exception Constructor + * @param s the exception message + */ + public EventSystemException(String s) { + super(s); + } +} diff --git a/src/org/lwes/NoSuchAttributeTypeException.java b/src/org/lwes/NoSuchAttributeTypeException.java new file mode 100644 index 0000000..44ceaa9 --- /dev/null +++ b/src/org/lwes/NoSuchAttributeTypeException.java @@ -0,0 +1,15 @@ +package org.lwes; + +/** + * Thrown when the Type does not exist for a given attribute in an Event. + * + * @author Anthony Molinaro + */ +public class NoSuchAttributeTypeException extends EventSystemException { + /** + * Overrides EventSystemException Constructor + */ + public NoSuchAttributeTypeException(String s) { + super(s); + } +} diff --git a/src/org/lwes/TypeID.java b/src/org/lwes/TypeID.java new file mode 100644 index 0000000..f942e26 --- /dev/null +++ b/src/org/lwes/TypeID.java @@ -0,0 +1,178 @@ +package org.lwes; + +/** + * This class contains some global variables used in various parts of + * the event system. + * @author Anthony Molinaro + * @author Michael P. Lum + */ +public class TypeID +{ + /** + * The token used for undefined types in LWES + */ + public final static byte UNDEFINED_TOKEN = (byte)0xff; + + /** + * The token used by uint16 in the Event Serialization Protocol + */ + public final static byte UINT16_TOKEN = (byte)0x01; + + /** + * The token used by int16 in the Event Serialization Protocol + */ + public final static byte INT16_TOKEN = (byte)0x02; + /** + * The token used by uint32 in the Event Serialization Protocol + */ + public final static byte UINT32_TOKEN = (byte)0x03; + /** + * The token used by int32 in the Event Serialization Protocol + */ + public final static byte INT32_TOKEN = (byte)0x04; + /** + * The token used by string in the Event Serialization Protocol + */ + public final static byte STRING_TOKEN = (byte)0x05; + /** + * The token used by ip_addr in the Event Serialization Protocol + */ + public final static byte IPADDR_TOKEN = (byte)0x06; + /** + * The token used by int64 in the Event Serialization Protocol + */ + public final static byte INT64_TOKEN = (byte)0x07; + /** + * The token used by uint64 in the Event Serialization Protocol + */ + public final static byte UINT64_TOKEN = (byte)0x08; + /** + * The token used by boolean in the Event Serialization Protocol + */ + public final static byte BOOLEAN_TOKEN= (byte)0x09; + + /** + * The string used by uint16 in the Event Serialization Protocol + */ + public final static String UINT16_STRING = "uint16"; + /** + * The string used by int16 in the Event Serialization Protocol + */ + public final static String INT16_STRING = "int16"; + /** + * The string used by uint32 in the Event Serialization Protocol + */ + public final static String UINT32_STRING = "uint32"; + /** + * The string used by int32 in the Event Serialization Protocol + */ + public final static String INT32_STRING = "int32"; + /** + * The string used by string in the Event Serialization Protocol + */ + public final static String STRING_STRING = "string"; + /** + * The string used by ip_addr in the Event Serialization Protocol + */ + public final static String IPADDR_STRING= "ip_addr"; + /** + * The string used by int64 in the Event Serialization Protocol + */ + public final static String INT64_STRING = "int64"; + /** + * The string used by uint64 in the Event Serialization Protocol + */ + public final static String UINT64_STRING = "uint64"; + /** + * The string used by boolean in the Event Serialization Protocol + */ + public final static String BOOLEAN_STRING= "boolean"; + + /** + * This is a regular expression for parsing an integer number from a string + */ + public final static String SIGNED_INTEGER_REGEX = "/-?\\d+/i"; + /** + * This is a regular expression for parsing an unsigned integer number + * from a string + */ + public final static String UNSIGNED_INTEGER_REGEX = "/\\d+(?=\\s|$)/i"; + /** + * This is a regular expression for matching a hexidecimal short from a string + */ + public final static String HEX_SHORT_REGEX = "/0x[0-9a-fA-F]{1,4}(?=\\s|$)/i"; + /** + * This is a regular expression for matching a hexidecimal int from a string + */ + public final static String HEX_INT_REGEX = "/0x[0-9a-fA-F]{5,8}(?=\\s|$)/i"; + /** + * This is a regular expression for matching a hexidecimal long from a string + */ + public final static String HEX_LONG_REGEX = "/0x[0-9a-fA-F]{9,16}(?=\\s|$)/i"; + /** + * This is a regular expression for matching an ip address from a string + */ + public final static String IP_ADDR_REGEX + = "/(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(?=\\s|$)/i"; + /** + * This is a regular expression for matching a boolean from a string + */ + public final static String BOOLEAN_REGEX = "/true|false/"; + + /** + * Simple conversion utility + */ + public static String byteIDToString(byte id) + { + switch(id) + { + case UINT16_TOKEN : + return UINT16_STRING; + case INT16_TOKEN : + return INT16_STRING; + case UINT32_TOKEN : + return UINT32_STRING; + case INT32_TOKEN : + return INT32_STRING; + case STRING_TOKEN : + return STRING_STRING; + case IPADDR_TOKEN : + return IPADDR_STRING; + case INT64_TOKEN : + return INT64_STRING; + case UINT64_TOKEN : + return UINT64_STRING; + case BOOLEAN_TOKEN: + return BOOLEAN_STRING; + default: + return null; + } + } + + /** + * Another conversion utility + */ + public static byte stringToByteID(String id) + { + if ( id.equals(UINT16_STRING) ) + return UINT16_TOKEN; + else if ( id.equals(INT16_STRING) ) + return INT16_TOKEN; + else if ( id.equals(UINT32_STRING) ) + return UINT32_TOKEN; + else if ( id.equals(INT32_STRING) ) + return INT32_TOKEN; + else if ( id.equals(STRING_STRING) ) + return STRING_TOKEN; + else if ( id.equals(IPADDR_STRING) ) + return IPADDR_TOKEN; + else if ( id.equals(INT64_STRING) ) + return INT64_TOKEN; + else if ( id.equals(UINT64_STRING) ) + return UINT64_TOKEN; + else if ( id.equals(BOOLEAN_STRING) ) + return BOOLEAN_TOKEN; + else + return UNDEFINED_TOKEN; + } +} diff --git a/src/org/lwes/db/ESFParser.java b/src/org/lwes/db/ESFParser.java new file mode 100644 index 0000000..ea416cb --- /dev/null +++ b/src/org/lwes/db/ESFParser.java @@ -0,0 +1,285 @@ +/* Generated By:JavaCC: Do not edit this line. ESFParser.java */ +package org.lwes.db; + +public class ESFParser implements ESFParserConstants { + private String currentEvent; + private EventTemplateDB eventTemplateDB; + + public void setEventTemplateDB(EventTemplateDB DB) + { eventTemplateDB = DB; } + + public EventTemplateDB getEventTemplateDB() + { return eventTemplateDB; } + + public void setCurrentEvent(String evt) + { currentEvent = evt; } + + public String getCurrentEvent() + { return currentEvent; } + +/** + * A list of events + */ + final public void eventlist() throws ParseException { + event(); + label_1: + while (true) { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case ID: + ; + break; + default: + jj_la1[0] = jj_gen; + break label_1; + } + event(); + } + jj_consume_token(0); + } + +/** + * a single event + */ + final public void event() throws ParseException { + eventName(); + jj_consume_token(11); + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case ID: + attributeList(); + break; + default: + jj_la1[1] = jj_gen; + ; + } + jj_consume_token(12); + } + +/** + * The name of an event, should be max 256 chars ([a-zA-Z0-9_]*) + */ + final public void eventName() throws ParseException { + Token t; + t = jj_consume_token(ID); + if ( getEventTemplateDB().addEvent(t.image)) + { + setCurrentEvent(t.image); + } + else + { + {if (true) throw new ParseException("Problem adding event "+t.image);} + } + } + + final public void attributeList() throws ParseException { + attribute(); + label_2: + while (true) { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case ID: + ; + break; + default: + jj_la1[2] = jj_gen; + break label_2; + } + attribute(); + } + } + + final public void attribute() throws ParseException { + String aType; + String anAttribute; + aType = type(); + anAttribute = attributeName(); + jj_consume_token(13); + if ( !( aType.equals("uint16") + | aType.equals("int16") + | aType.equals("uint32") + | aType.equals("int32") + | aType.equals("string") + | aType.equals("ip_addr") + | aType.equals("int64") + | aType.equals("uint64") + | aType.equals("boolean") + ) + ) + { + {if (true) throw new ParseException("No such type '"+aType+"'");} + } + String evt = getCurrentEvent(); + if ( evt == null ) {if (true) throw new ParseException("Bad Event");} + if ( !getEventTemplateDB().addEventAttribute(evt,anAttribute,aType)) + { + {if (true) throw new ParseException("Problem adding attribute "+evt+"(" + +aType+","+anAttribute+")");} + } + } + + final public String type() throws ParseException { + Token t; + t = jj_consume_token(ID); + {if (true) return t.image;} + throw new Error("Missing return statement in function"); + } + + final public String attributeName() throws ParseException { + Token t; + t = jj_consume_token(ID); + {if (true) return t.image;} + throw new Error("Missing return statement in function"); + } + + public ESFParserTokenManager token_source; + SimpleCharStream jj_input_stream; + public Token token, jj_nt; + private int jj_ntk; + private int jj_gen; + final private int[] jj_la1 = new int[3]; + static private int[] jj_la1_0; + static { + jj_la1_0(); + } + private static void jj_la1_0() { + jj_la1_0 = new int[] {0x400,0x400,0x400,}; + } + + public ESFParser(java.io.InputStream stream) { + this(stream, null); + } + public ESFParser(java.io.InputStream stream, String encoding) { + try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } + token_source = new ESFParserTokenManager(jj_input_stream); + token = new Token(); + jj_ntk = -1; + jj_gen = 0; + for (int i = 0; i < 3; i++) jj_la1[i] = -1; + } + + public void ReInit(java.io.InputStream stream) { + ReInit(stream, null); + } + public void ReInit(java.io.InputStream stream, String encoding) { + try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } + token_source.ReInit(jj_input_stream); + token = new Token(); + jj_ntk = -1; + jj_gen = 0; + for (int i = 0; i < 3; i++) jj_la1[i] = -1; + } + + public ESFParser(java.io.Reader stream) { + jj_input_stream = new SimpleCharStream(stream, 1, 1); + token_source = new ESFParserTokenManager(jj_input_stream); + token = new Token(); + jj_ntk = -1; + jj_gen = 0; + for (int i = 0; i < 3; i++) jj_la1[i] = -1; + } + + public void ReInit(java.io.Reader stream) { + jj_input_stream.ReInit(stream, 1, 1); + token_source.ReInit(jj_input_stream); + token = new Token(); + jj_ntk = -1; + jj_gen = 0; + for (int i = 0; i < 3; i++) jj_la1[i] = -1; + } + + public ESFParser(ESFParserTokenManager tm) { + token_source = tm; + token = new Token(); + jj_ntk = -1; + jj_gen = 0; + for (int i = 0; i < 3; i++) jj_la1[i] = -1; + } + + public void ReInit(ESFParserTokenManager tm) { + token_source = tm; + token = new Token(); + jj_ntk = -1; + jj_gen = 0; + for (int i = 0; i < 3; i++) jj_la1[i] = -1; + } + + final private Token jj_consume_token(int kind) throws ParseException { + Token oldToken; + if ((oldToken = token).next != null) token = token.next; + else token = token.next = token_source.getNextToken(); + jj_ntk = -1; + if (token.kind == kind) { + jj_gen++; + return token; + } + token = oldToken; + jj_kind = kind; + throw generateParseException(); + } + + final public Token getNextToken() { + if (token.next != null) token = token.next; + else token = token.next = token_source.getNextToken(); + jj_ntk = -1; + jj_gen++; + return token; + } + + final public Token getToken(int index) { + Token t = token; + for (int i = 0; i < index; i++) { + if (t.next != null) t = t.next; + else t = t.next = token_source.getNextToken(); + } + return t; + } + + final private int jj_ntk() { + if ((jj_nt=token.next) == null) + return (jj_ntk = (token.next=token_source.getNextToken()).kind); + else + return (jj_ntk = jj_nt.kind); + } + + private java.util.Vector jj_expentries = new java.util.Vector(); + private int[] jj_expentry; + private int jj_kind = -1; + + public ParseException generateParseException() { + jj_expentries.removeAllElements(); + boolean[] la1tokens = new boolean[14]; + for (int i = 0; i < 14; i++) { + la1tokens[i] = false; + } + if (jj_kind >= 0) { + la1tokens[jj_kind] = true; + jj_kind = -1; + } + for (int i = 0; i < 3; i++) { + if (jj_la1[i] == jj_gen) { + for (int j = 0; j < 32; j++) { + if ((jj_la1_0[i] & (1< SKIP: +{ + "\n" : DEFAULT +| "\r" : DEFAULT +| "\r\n" : DEFAULT +} + + MORE: +{ + < ~[] > +} + + +TOKEN : +{ + < ID: ["a"-"z","A"-"Z","_",":","0"-"9"] (["a"-"z","A"-"Z","_",":","0"-"9"])* > +} + +/** + * A list of events + */ +void eventlist() : +{ +} +{ + event() ( event() )* +} + +/** + * a single event + */ +void event() : +{ +} +{ + eventName() "{" [ attributeList() ] "}" +} + +/** + * The name of an event, should be max 256 chars ([a-zA-Z0-9_]*) + */ +void eventName() : +{ + Token t; +} +{ + t= + { + if ( getEventTemplateDB().addEvent(t.image)) + { + setCurrentEvent(t.image); + } + else + { + throw new ParseException("Problem adding event "+t.image); + } + } +} + +void attributeList() : +{} +{ + attribute() ( attribute() )* +} + +void attribute() : +{ + String aType; + String anAttribute; +} +{ + aType=type() anAttribute=attributeName() ";" { + if ( !( aType.equals("uint16") + | aType.equals("int16") + | aType.equals("uint32") + | aType.equals("int32") + | aType.equals("string") + | aType.equals("ip_addr") + | aType.equals("int64") + | aType.equals("uint64") + | aType.equals("boolean") + ) + ) + { + throw new ParseException("No such type '"+aType+"'"); + } + String evt = getCurrentEvent(); + if ( evt == null ) throw new ParseException("Bad Event"); + if ( !getEventTemplateDB().addEventAttribute(evt,anAttribute,aType)) + { + throw new ParseException("Problem adding attribute "+evt+"(" + +aType+","+anAttribute+")"); + } + } +} + +String type() : +{ + Token t; +} +{ + t= + { + return t.image; + } +} + +String attributeName() : +{ + Token t; +} +{ + t= + { + return t.image; + } +} diff --git a/src/org/lwes/db/ESFParserConstants.java b/src/org/lwes/db/ESFParserConstants.java new file mode 100644 index 0000000..2fc5c46 --- /dev/null +++ b/src/org/lwes/db/ESFParserConstants.java @@ -0,0 +1,29 @@ +/* Generated By:JavaCC: Do not edit this line. ESFParserConstants.java */ +package org.lwes.db; + +public interface ESFParserConstants { + + int EOF = 0; + int ID = 10; + + int DEFAULT = 0; + int IN_LINE_COMMENT = 1; + + String[] tokenImage = { + "", + "\" \"", + "\"\\t\"", + "\"\\n\"", + "\"\\r\"", + "\"#\"", + "\"\\n\"", + "\"\\r\"", + "\"\\r\\n\"", + "", + "", + "\"{\"", + "\"}\"", + "\";\"", + }; + +} diff --git a/src/org/lwes/db/ESFParserTokenManager.java b/src/org/lwes/db/ESFParserTokenManager.java new file mode 100644 index 0000000..894e4f4 --- /dev/null +++ b/src/org/lwes/db/ESFParserTokenManager.java @@ -0,0 +1,358 @@ +/* Generated By:JavaCC: Do not edit this line. ESFParserTokenManager.java */ +package org.lwes.db; + +public class ESFParserTokenManager implements ESFParserConstants +{ + public java.io.PrintStream debugStream = System.out; + public void setDebugStream(java.io.PrintStream ds) { debugStream = ds; } +private final int jjStopStringLiteralDfa_0(int pos, long active0) +{ + switch (pos) + { + default : + return -1; + } +} +private final int jjStartNfa_0(int pos, long active0) +{ + return jjMoveNfa_0(jjStopStringLiteralDfa_0(pos, active0), pos + 1); +} +private final int jjStopAtPos(int pos, int kind) +{ + jjmatchedKind = kind; + jjmatchedPos = pos; + return pos + 1; +} +private final int jjStartNfaWithStates_0(int pos, int kind, int state) +{ + jjmatchedKind = kind; + jjmatchedPos = pos; + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { return pos + 1; } + return jjMoveNfa_0(state, pos + 1); +} +private final int jjMoveStringLiteralDfa0_0() +{ + switch(curChar) + { + case 35: + return jjStopAtPos(0, 5); + case 59: + return jjStopAtPos(0, 13); + case 123: + return jjStopAtPos(0, 11); + case 125: + return jjStopAtPos(0, 12); + default : + return jjMoveNfa_0(0, 0); + } +} +private final void jjCheckNAdd(int state) +{ + if (jjrounds[state] != jjround) + { + jjstateSet[jjnewStateCnt++] = state; + jjrounds[state] = jjround; + } +} +private final void jjAddStates(int start, int end) +{ + do { + jjstateSet[jjnewStateCnt++] = jjnextStates[start]; + } while (start++ != end); +} +private final void jjCheckNAddTwoStates(int state1, int state2) +{ + jjCheckNAdd(state1); + jjCheckNAdd(state2); +} +private final void jjCheckNAddStates(int start, int end) +{ + do { + jjCheckNAdd(jjnextStates[start]); + } while (start++ != end); +} +private final void jjCheckNAddStates(int start) +{ + jjCheckNAdd(jjnextStates[start]); + jjCheckNAdd(jjnextStates[start + 1]); +} +private final int jjMoveNfa_0(int startState, int curPos) +{ + int[] nextStates; + int startsAt = 0; + jjnewStateCnt = 1; + int i = 1; + jjstateSet[0] = startState; + int j, kind = 0x7fffffff; + for (;;) + { + if (++jjround == 0x7fffffff) + ReInitRounds(); + if (curChar < 64) + { + long l = 1L << curChar; + MatchLoop: do + { + switch(jjstateSet[--i]) + { + case 0: + if ((0x7ff000000000000L & l) == 0L) + break; + kind = 10; + jjstateSet[jjnewStateCnt++] = 0; + break; + default : break; + } + } while(i != startsAt); + } + else if (curChar < 128) + { + long l = 1L << (curChar & 077); + MatchLoop: do + { + switch(jjstateSet[--i]) + { + case 0: + if ((0x7fffffe87fffffeL & l) == 0L) + break; + kind = 10; + jjstateSet[jjnewStateCnt++] = 0; + break; + default : break; + } + } while(i != startsAt); + } + else + { + int i2 = (curChar & 0xff) >> 6; + long l2 = 1L << (curChar & 077); + MatchLoop: do + { + switch(jjstateSet[--i]) + { + default : break; + } + } while(i != startsAt); + } + if (kind != 0x7fffffff) + { + jjmatchedKind = kind; + jjmatchedPos = curPos; + kind = 0x7fffffff; + } + ++curPos; + if ((i = jjnewStateCnt) == (startsAt = 1 - (jjnewStateCnt = startsAt))) + return curPos; + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { return curPos; } + } +} +private final int jjMoveStringLiteralDfa0_1() +{ + switch(curChar) + { + case 10: + return jjStopAtPos(0, 6); + case 13: + jjmatchedKind = 7; + return jjMoveStringLiteralDfa1_1(0x100L); + default : + return 1; + } +} +private final int jjMoveStringLiteralDfa1_1(long active0) +{ + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { + return 1; + } + switch(curChar) + { + case 10: + if ((active0 & 0x100L) != 0L) + return jjStopAtPos(1, 8); + break; + default : + return 2; + } + return 2; +} +static final int[] jjnextStates = { +}; +public static final String[] jjstrLiteralImages = { +"", null, null, null, null, null, null, null, null, null, null, "\173", +"\175", "\73", }; +public static final String[] lexStateNames = { + "DEFAULT", + "IN_LINE_COMMENT", +}; +public static final int[] jjnewLexState = { + -1, -1, -1, -1, -1, 1, 0, 0, 0, -1, -1, -1, -1, -1, +}; +static final long[] jjtoToken = { + 0x3c01L, +}; +static final long[] jjtoSkip = { + 0x1feL, +}; +static final long[] jjtoMore = { + 0x200L, +}; +protected SimpleCharStream input_stream; +private final int[] jjrounds = new int[1]; +private final int[] jjstateSet = new int[2]; +protected char curChar; +public ESFParserTokenManager(SimpleCharStream stream){ + if (SimpleCharStream.staticFlag) + throw new Error("ERROR: Cannot use a static CharStream class with a non-static lexical analyzer."); + input_stream = stream; +} +public ESFParserTokenManager(SimpleCharStream stream, int lexState){ + this(stream); + SwitchTo(lexState); +} +public void ReInit(SimpleCharStream stream) +{ + jjmatchedPos = jjnewStateCnt = 0; + curLexState = defaultLexState; + input_stream = stream; + ReInitRounds(); +} +private final void ReInitRounds() +{ + int i; + jjround = 0x80000001; + for (i = 1; i-- > 0;) + jjrounds[i] = 0x80000000; +} +public void ReInit(SimpleCharStream stream, int lexState) +{ + ReInit(stream); + SwitchTo(lexState); +} +public void SwitchTo(int lexState) +{ + if (lexState >= 2 || lexState < 0) + throw new TokenMgrError("Error: Ignoring invalid lexical state : " + lexState + ". State unchanged.", TokenMgrError.INVALID_LEXICAL_STATE); + else + curLexState = lexState; +} + +protected Token jjFillToken() +{ + Token t = Token.newToken(jjmatchedKind); + t.kind = jjmatchedKind; + String im = jjstrLiteralImages[jjmatchedKind]; + t.image = (im == null) ? input_stream.GetImage() : im; + t.beginLine = input_stream.getBeginLine(); + t.beginColumn = input_stream.getBeginColumn(); + t.endLine = input_stream.getEndLine(); + t.endColumn = input_stream.getEndColumn(); + return t; +} + +int curLexState = 0; +int defaultLexState = 0; +int jjnewStateCnt; +int jjround; +int jjmatchedPos; +int jjmatchedKind; + +public Token getNextToken() +{ + int kind; + Token specialToken = null; + Token matchedToken; + int curPos = 0; + + EOFLoop : + for (;;) + { + try + { + curChar = input_stream.BeginToken(); + } + catch(java.io.IOException e) + { + jjmatchedKind = 0; + matchedToken = jjFillToken(); + return matchedToken; + } + + for (;;) + { + switch(curLexState) + { + case 0: + try { input_stream.backup(0); + while (curChar <= 32 && (0x100002600L & (1L << curChar)) != 0L) + curChar = input_stream.BeginToken(); + } + catch (java.io.IOException e1) { continue EOFLoop; } + jjmatchedKind = 0x7fffffff; + jjmatchedPos = 0; + curPos = jjMoveStringLiteralDfa0_0(); + break; + case 1: + jjmatchedKind = 0x7fffffff; + jjmatchedPos = 0; + curPos = jjMoveStringLiteralDfa0_1(); + if (jjmatchedPos == 0 && jjmatchedKind > 9) + { + jjmatchedKind = 9; + } + break; + } + if (jjmatchedKind != 0x7fffffff) + { + if (jjmatchedPos + 1 < curPos) + input_stream.backup(curPos - jjmatchedPos - 1); + if ((jjtoToken[jjmatchedKind >> 6] & (1L << (jjmatchedKind & 077))) != 0L) + { + matchedToken = jjFillToken(); + if (jjnewLexState[jjmatchedKind] != -1) + curLexState = jjnewLexState[jjmatchedKind]; + return matchedToken; + } + else if ((jjtoSkip[jjmatchedKind >> 6] & (1L << (jjmatchedKind & 077))) != 0L) + { + if (jjnewLexState[jjmatchedKind] != -1) + curLexState = jjnewLexState[jjmatchedKind]; + continue EOFLoop; + } + if (jjnewLexState[jjmatchedKind] != -1) + curLexState = jjnewLexState[jjmatchedKind]; + curPos = 0; + jjmatchedKind = 0x7fffffff; + try { + curChar = input_stream.readChar(); + continue; + } + catch (java.io.IOException e1) { } + } + int error_line = input_stream.getEndLine(); + int error_column = input_stream.getEndColumn(); + String error_after = null; + boolean EOFSeen = false; + try { input_stream.readChar(); input_stream.backup(1); } + catch (java.io.IOException e1) { + EOFSeen = true; + error_after = curPos <= 1 ? "" : input_stream.GetImage(); + if (curChar == '\n' || curChar == '\r') { + error_line++; + error_column = 0; + } + else + error_column++; + } + if (!EOFSeen) { + input_stream.backup(1); + error_after = curPos <= 1 ? "" : input_stream.GetImage(); + } + throw new TokenMgrError(EOFSeen, curLexState, error_line, error_column, error_after, curChar, TokenMgrError.LEXICAL_ERROR); + } + } +} + +} diff --git a/src/org/lwes/db/EventTemplateDB.java b/src/org/lwes/db/EventTemplateDB.java new file mode 100644 index 0000000..6e4b3e4 --- /dev/null +++ b/src/org/lwes/db/EventTemplateDB.java @@ -0,0 +1,543 @@ +package org.lwes.db; + +import java.io.InputStream; +import java.io.File; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.Map; +import java.util.Collections; +import java.util.concurrent.ConcurrentHashMap; + +import org.lwes.BaseType; +import org.lwes.EventSystemException; +import org.lwes.TypeID; +import org.lwes.util.IPAddress; +import org.lwes.util.Log; + +/** + * Provides type checking for the event system. Also provides a place for + * globally accessible information. + * + * @author Anthony Molinaro + * @author Michael P. Lum + */ +public class EventTemplateDB { + /** + * the meta event info inherent to every event + */ + private static final String META_EVENT_INFO = "MetaEventInfo"; + + /** + * esfFile for this event system. + */ + private File esfFile = null; + private InputStream esfInputStream = null; + + /** + * System Attributes + */ + private Map> events = null; + private Map knownTypes = null; + private Map reservedWords = null; + + /** + * This is the EventTemplateDB constructor. + */ + public EventTemplateDB() { + events = new ConcurrentHashMap>(); + knownTypes = new ConcurrentHashMap(); + reservedWords = new ConcurrentHashMap(); + initializeKnownTypes(); + } + + /** + * Sets the Event Specification file for this system + * + * @param esfFile + * the ESF file for this system. + */ + public void setESFFile(File anEsfFile) { + esfFile = anEsfFile; + } + + /** + * Gets the ESF file, in case you want to look at it + * + * @return the ESF file used in this system. + */ + public File getESFFile() { + return esfFile; + } + + /** + * Sets the Event Specification file as an InputStream + * + * @param esfInputStream + * the InputStream representing an ESF file + */ + public void setESFInputStream(InputStream esfInputStream) { + this.esfInputStream = esfInputStream; + } + + /** + * Ges the ESF InputStream + * + * @return the ESF InputStream used in the system + */ + public InputStream getESFInputStream() { + return esfInputStream; + } + + /** + * Initializes the EventTemplateDB, assumes that setESFFile() has been + * called. + * + * @return true if the EventTemplateDB initializes correctly; false if it + * does not. false means the event system is unable to perform validation + */ + public synchronized boolean initialize() { + /* + * Call the parser to parse the file. Any errors cause the + * initialization to fail. + */ + ESFParser parser; + try { + if (getESFInputStream() != null) { + parser = new ESFParser(getESFInputStream()); + } else if(getESFFile() != null) { + parser = new ESFParser( + new java.io.FileInputStream(getESFFile())); + } else { + return false; + } + + parser.setEventTemplateDB(this); + parser.eventlist(); + } catch (java.io.FileNotFoundException e) { + Log.warning("File not found ", e); + + /* + * treat this as just a warning and allow things to continue, this + * allows an empty EventTemplateDB to work when type checking is + * turned off + */ + } catch (ParseException e) { + Log.warning("Parser error in ESF file " + getESFFile(), e); + return false; + } catch (Exception e) { + Log.error("Error parsing ESF file " + getESFFile(), e); + /* catch IO, NPEs and other exceptions but still continue */ + return false; + } + + return true; + } + + /** + * Add an Event to the EventTemplateDB + * + * @param anEventName + * the name of the Event to add + * @return true if the event was added, false if it was not + */ + public synchronized boolean addEvent(String anEventName) { + if(anEventName == null) { + return false; + } + + try { + if (anEventName.equals(META_EVENT_INFO)) { + return true; + } + + if (events.containsKey(anEventName)) { + Log.info("Event " + anEventName + " already exists in event DB"); + return false; + } + + Map evtHash = new ConcurrentHashMap(); + if (!(reservedWords.isEmpty())) { + /* insert reserved words into new event */ + Iterator i = reservedWords.keySet().iterator(); + while(i.hasNext()) { + String key = (String) i.next(); + if(key != null) { + evtHash.put(key, reservedWords.get(key)); + } + } + } + + events.put(anEventName, evtHash); + } catch(Exception e) { + Log.warning("Error adding event to EventTemplateDB", e); + } + + return true; + } + + /** + * Add an attribute to an Event in the EventTemplateDB + * + * @param anEventName + * the name of the event to add this attribute to + * @param anAttributeName + * the name of the attribute to add + * @param anAttributeType + * the type of the attribute, should be the name of the type + * given in the ESF Specification. + * @return true if the attribute can be added, false if it can not. + */ + public synchronized boolean addEventAttribute(String anEventName, String anAttributeName, + String anAttributeType) { + if(anEventName == null || anAttributeName == null || anAttributeType == null) { + return false; + } + + try { + if (anEventName.equals(META_EVENT_INFO)) { + if (checkForType(anAttributeType)) { + reservedWords.put(anAttributeName, knownTypes + .get(anAttributeType)); + return true; + } else { + Log.info("Meta keyword " + anEventName + "." + anAttributeName + + "has unknown type " + anAttributeType + ", skipping"); + return false; + } + } + + if (reservedWords.containsKey(anEventName)) { + Log.warning("Unable to add attribute named " + anAttributeName + + "as it is a reserved word, skipping"); + return false; + + } + + if (events.containsKey(anEventName)) { + Map evtHash = (Map) events.get(anEventName); + if (checkForType(anAttributeType)) { + evtHash.put(anAttributeName, knownTypes.get(anAttributeType)); + return true; + } else { + Log.warning("Type " + anAttributeType + " does not exist for " + + anAttributeName + ", skipping"); + return false; + } + } else { + Log.warning("No such event " + anEventName + ", skipping"); + return false; + } + } catch(Exception e) { + Log.error("Error adding attribute " + anAttributeName + " to " + anEventName, e); + return false; + } + } + + /** + * Returns an enumeration of all defined events + * + * @return an enumeration of all defined events + */ + public Enumeration getEventNames() { + return Collections.enumeration(this.events.keySet()); + } + + /** + * Returns true if the type given by aTypeName is a valid type in the DB. + * + * @param aTypeName + * a type name according to the ESF Specification + * @return true if the type exists in the DB, false otherwise + */ + public boolean checkForType(String aTypeName) { + return knownTypes.containsKey(aTypeName); + } + + /** + * Checks to see if an Event exists in the EventTemplateDB + * + * @param anEventName + * the name of the event to check the existence of + * @return true if the event with the name anEventName exists in + * the EventTemplateDB, false otherwise. + */ + public boolean checkForEvent(String anEventName) { + return events.containsKey(anEventName); + } + + /** + * Checks to see if an attribute anAttributeName exists for the + * event anEventName + * + * @param anEventName + * the name of an Event + * @param anAttributeName + * the name of an attribute of Event to check + * @reture true if the attribute exists as a member of event, false + * otherwise + */ + public boolean checkForAttribute(String anEventName, String anAttributeName) { + if (checkForEvent(anEventName)) { + Map evtHash = + (Map) events.get(anEventName); + return evtHash.containsKey(anAttributeName); + } + return false; + } + + /** + * Checks to see if the type of an attribute is proper. (i.e. if the given + * attribute of the given event has the same type assigned to it as the type + * of the given objects value) + * + * @param anEventName + * the name of an Event. + * @param anAttributeName + * the name of the attribute whose type is being checked + * @param anAttributeValue + * the Object containing the possible value of the attribute. + * + * @return true if the event and attribute exist and if the type of the + * attribute matches the type assigned to this attribute in the + * EventTemplateDB, false otherwise. + */ + public boolean checkTypeForAttribute(String anEventName, + String anAttributeName, Object anAttributeValue) { + if (checkForAttribute(anEventName, anAttributeName)) { + Map evtHash = (Map) events.get(anEventName); + Object storedTypeObject = ((BaseType) evtHash.get(anAttributeName)); + byte type1 = ((BaseType) anAttributeValue).getTypeToken(); + byte type2 = ((BaseType) storedTypeObject).getTypeToken(); + if (type1 == type2) { + return true; + } + } + return false; + } + + /** + * Checks to see if the type of an attribute is proper. (i.e. if the given + * attribute of the given event has the same type assigned to it as the type + * given) + * + * @param anEventName + * the name of an Event. + * @param anAttributeName + * the name of the attribute whose type is being checked + * @param anAttributeType + * the String containing the possible type value of the + * attribute. + * + * @return true if the event and attribute exist and if the type of the + * attribute matches the type assigned to this attribute in the + * EventTemplateDB, false otherwise. + */ + public boolean checkTypeForAttribute(String anEventName, + String anAttributeName, String anAttributeType) { + if (checkForAttribute(anEventName, anAttributeName)) { + Map evtHash = (Map) events.get(anEventName); + String storedTypeName = ((BaseType) evtHash.get(anAttributeName)) + .getTypeName(); + if (anAttributeType.equals(storedTypeName)) { + return true; + } + } + return false; + } + + /** + * Given an Object which is the attribute value of the attribute + * attributeName of event eventName, return the internal + * representation (i.e. BaseType) of this Object + * + * @param eventName + * the name of an Event. + * @param attributeName + * the name of an attribute of eventName + * @param attributeValue + * the value of the attribute + * @return the BaseType representation of attributeValue + */ + BaseType getBaseTypeForObjectAttribute(String eventName, + String attributeName, Object attributeValue) { + Map evtHash = (Map) events.get(eventName); + BaseType tmpBaseType = ((BaseType) evtHash.get(attributeName)); + BaseType retBaseType = tmpBaseType.cloneBaseType(); + retBaseType.setTypeObject(attributeValue); + return retBaseType; + } + + /** + * Returns the base types for this event + * @param eventName + * @return a map of event fields to base types + */ + public Map getBaseTypesForEvent(String eventName) { + Map map = (Map) events.get(eventName); + + return map == null ? new ConcurrentHashMap() : map; + } + + /** + * Parses the string representation of an event attribute into the + * appropriate objectt. + * + * @param anEventName + * the name of an Event. + * @param anAttributeName + * the name of the attribute we are parsing + * @param stringAttributeValue + * a string representation of the value of the attribute given by + * anAttributeName. + * @return the object represented by the string + * stringAttributeValue + */ + public Object parseAttribute(String anEventName, String anAttributeName, + String stringAttributeValue) { + Object retObject = null; + + Log.trace("parseAttribute: " + anEventName + "." + anAttributeName + "=" + + stringAttributeValue); + + if (checkForAttribute(anEventName, anAttributeName)) { + Log.trace("parseAttribute: passed first if attribute exists"); + + Map evtHash = (Map) events.get(anEventName); + try { + BaseType bt = (BaseType) evtHash.get(anAttributeName); + if (bt == null) + throw new EventSystemException("Null BaseType for " + + anAttributeName); + + retObject = bt.parseFromString(stringAttributeValue); + Log.trace("parseAttribute: parsed " + retObject); + } catch (EventSystemException btpe) { + Log.error("Unable to parseAttribute", btpe); + } + } + + Log.trace("parseAttribute: returning " + retObject); + return retObject; + } + + /** + * Returns a HTML rendering of the EventTemplateDB + * @return HTML string of the EventTemplateDB + */ + public String toHtmlString() { + StringBuffer sb = new StringBuffer(); + sb.append("\n"); + sb.append("\n"); + for (Iterator res = reservedWords.keySet().iterator(); res.hasNext();) { + String key = (String) res.next(); + BaseType tv = (BaseType) reservedWords.get(key); + String type = tv.getTypeName(); + sb.append("\n"); + } + for (Iterator iter = events.keySet().iterator(); iter.hasNext();) { + String EventKey = (String) iter.next(); + sb.append("\n"); + if (EventKey != null) { + Map event = (Map) events.get(EventKey); + for (Enumeration att = Collections.enumeration(event.keySet()); att + .hasMoreElements();) { + String key = (String) att.nextElement(); + BaseType tv = (BaseType) event.get(key); + String type = tv.getTypeName(); + sb.append("\n"); + } + } + } + + sb.append("
" + META_EVENT_INFO + + "TypeName
" + type + "" + key + + "
" + EventKey + + "TypeName
" + type + "" + key + + "
\n"); + return sb.toString(); + + } + + /** + * Returns a rather long string Representation of the EventTemplateDB + * + * @return a string Representation of the EventTemplateDB + */ + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("\n" + META_EVENT_INFO + "\n{\n"); + String[] reservedKeys = new String[reservedWords.size()]; + int i = 0, j = 0; + + for (Iterator res = reservedWords.keySet().iterator(); res.hasNext();) { + reservedKeys[i] = (String) res.next(); + ++i; + } + Arrays.sort(reservedKeys); + + for (i = 0; i < reservedKeys.length; ++i) { + BaseType tv = (BaseType) reservedWords.get(reservedKeys[i]); + String type = tv.getTypeName(); + sb.append("\t" + type + " " + reservedKeys[i] + ";\n"); + } + sb.append("}\n"); + + String[] eventKeys = new String[events.size()]; + i = 0; + for (Iterator iter = events.keySet().iterator(); iter.hasNext();) { + eventKeys[i] = (String) iter.next(); + ++i; + } + Arrays.sort(eventKeys); + + for (i = 0; i < eventKeys.length; ++i) { + sb.append(eventKeys[i] + "\n{\n"); + if (eventKeys[i] != null) { + Map event = (Map) events.get(eventKeys[i]); + j = 0; + String[] attributeKeys = new String[event.size()]; + for (Enumeration att = Collections.enumeration(event.keySet()); + att.hasMoreElements();) { + attributeKeys[j] = (String) att.nextElement(); + ++j; + } + Arrays.sort(attributeKeys); + + for (j = 0; j < attributeKeys.length; ++j) { + BaseType tv = (BaseType) event.get(attributeKeys[j]); + String type = tv.getTypeName(); + sb.append("\t" + type + " " + attributeKeys[j] + ";\n"); + } + } + sb.append("}\n"); + } + return sb.toString(); + } + + /** + * Creates a map of known types + */ + private void initializeKnownTypes() { + /* initialize the list of known types */ + knownTypes.put(TypeID.UINT16_STRING, new BaseType(TypeID.UINT16_STRING, + TypeID.UINT16_TOKEN, new Integer(0))); + knownTypes.put(TypeID.INT16_STRING, new BaseType(TypeID.INT16_STRING, + TypeID.INT16_TOKEN, new Short((short) 0))); + knownTypes.put(TypeID.UINT32_STRING, new BaseType(TypeID.UINT32_STRING, + TypeID.UINT32_TOKEN, new Long(0))); + knownTypes.put(TypeID.INT32_STRING, new BaseType(TypeID.INT32_STRING, + TypeID.INT32_TOKEN, new Integer(0))); + knownTypes.put(TypeID.STRING_STRING, new BaseType(TypeID.STRING_STRING, + TypeID.STRING_TOKEN, "")); + knownTypes.put(TypeID.IPADDR_STRING, new BaseType(TypeID.IPADDR_STRING, + TypeID.IPADDR_TOKEN, new IPAddress())); + knownTypes.put(TypeID.INT64_STRING, new BaseType(TypeID.INT64_STRING, + TypeID.INT64_TOKEN, new Long(0))); + knownTypes.put(TypeID.UINT64_STRING, new BaseType(TypeID.UINT64_STRING, + TypeID.UINT64_TOKEN, new Long(0))); + knownTypes + .put(TypeID.BOOLEAN_STRING, new BaseType(TypeID.BOOLEAN_STRING, + TypeID.BOOLEAN_TOKEN, new Boolean(true))); + } +} diff --git a/src/org/lwes/db/ParseException.java b/src/org/lwes/db/ParseException.java new file mode 100644 index 0000000..5b67dac --- /dev/null +++ b/src/org/lwes/db/ParseException.java @@ -0,0 +1,192 @@ +/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */ +package org.lwes.db; + +/** + * This exception is thrown when parse errors are encountered. + * You can explicitly create objects of this exception type by + * calling the method generateParseException in the generated + * parser. + * + * You can modify this class to customize your error reporting + * mechanisms so long as you retain the public fields. + */ +public class ParseException extends Exception { + + /** + * This constructor is used by the method "generateParseException" + * in the generated parser. Calling this constructor generates + * a new object of this type with the fields "currentToken", + * "expectedTokenSequences", and "tokenImage" set. The boolean + * flag "specialConstructor" is also set to true to indicate that + * this constructor was used to create this object. + * This constructor calls its super class with the empty string + * to force the "toString" method of parent class "Throwable" to + * print the error message in the form: + * ParseException: + */ + public ParseException(Token currentTokenVal, + int[][] expectedTokenSequencesVal, + String[] tokenImageVal + ) + { + super(""); + specialConstructor = true; + currentToken = currentTokenVal; + expectedTokenSequences = expectedTokenSequencesVal; + tokenImage = tokenImageVal; + } + + /** + * The following constructors are for use by you for whatever + * purpose you can think of. Constructing the exception in this + * manner makes the exception behave in the normal way - i.e., as + * documented in the class "Throwable". The fields "errorToken", + * "expectedTokenSequences", and "tokenImage" do not contain + * relevant information. The JavaCC generated code does not use + * these constructors. + */ + + public ParseException() { + super(); + specialConstructor = false; + } + + public ParseException(String message) { + super(message); + specialConstructor = false; + } + + /** + * This variable determines which constructor was used to create + * this object and thereby affects the semantics of the + * "getMessage" method (see below). + */ + protected boolean specialConstructor; + + /** + * This is the last token that has been consumed successfully. If + * this object has been created due to a parse error, the token + * followng this token will (therefore) be the first error token. + */ + public Token currentToken; + + /** + * Each entry in this array is an array of integers. Each array + * of integers represents a sequence of tokens (by their ordinal + * values) that is expected at this point of the parse. + */ + public int[][] expectedTokenSequences; + + /** + * This is a reference to the "tokenImage" array of the generated + * parser within which the parse error occurred. This array is + * defined in the generated ...Constants interface. + */ + public String[] tokenImage; + + /** + * This method has the standard behavior when this object has been + * created using the standard constructors. Otherwise, it uses + * "currentToken" and "expectedTokenSequences" to generate a parse + * error message and returns it. If this object has been created + * due to a parse error, and you do not catch it (it gets thrown + * from the parser), then this method is called during the printing + * of the final stack trace, and hence the correct error message + * gets displayed. + */ + public String getMessage() { + if (!specialConstructor) { + return super.getMessage(); + } + StringBuffer expected = new StringBuffer(); + int maxSize = 0; + for (int i = 0; i < expectedTokenSequences.length; i++) { + if (maxSize < expectedTokenSequences[i].length) { + maxSize = expectedTokenSequences[i].length; + } + for (int j = 0; j < expectedTokenSequences[i].length; j++) { + expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" "); + } + if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { + expected.append("..."); + } + expected.append(eol).append(" "); + } + String retval = "Encountered \""; + Token tok = currentToken.next; + for (int i = 0; i < maxSize; i++) { + if (i != 0) retval += " "; + if (tok.kind == 0) { + retval += tokenImage[0]; + break; + } + retval += add_escapes(tok.image); + tok = tok.next; + } + retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; + retval += "." + eol; + if (expectedTokenSequences.length == 1) { + retval += "Was expecting:" + eol + " "; + } else { + retval += "Was expecting one of:" + eol + " "; + } + retval += expected.toString(); + return retval; + } + + /** + * The end of line string for this machine. + */ + protected String eol = System.getProperty("line.separator", "\n"); + + /** + * Used to convert raw characters to their escaped version + * when these raw version cannot be used as part of an ASCII + * string literal. + */ + protected String add_escapes(String str) { + StringBuffer retval = new StringBuffer(); + char ch; + for (int i = 0; i < str.length(); i++) { + switch (str.charAt(i)) + { + case 0 : + continue; + case '\b': + retval.append("\\b"); + continue; + case '\t': + retval.append("\\t"); + continue; + case '\n': + retval.append("\\n"); + continue; + case '\f': + retval.append("\\f"); + continue; + case '\r': + retval.append("\\r"); + continue; + case '\"': + retval.append("\\\""); + continue; + case '\'': + retval.append("\\\'"); + continue; + case '\\': + retval.append("\\\\"); + continue; + default: + if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { + String s = "0000" + Integer.toString(ch, 16); + retval.append("\\u" + s.substring(s.length() - 4, s.length())); + } else { + retval.append(ch); + } + continue; + } + } + return retval.toString(); + } + +} diff --git a/src/org/lwes/db/SimpleCharStream.java b/src/org/lwes/db/SimpleCharStream.java new file mode 100644 index 0000000..3acf54f --- /dev/null +++ b/src/org/lwes/db/SimpleCharStream.java @@ -0,0 +1,439 @@ +/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */ +package org.lwes.db; + +/** + * An implementation of interface CharStream, where the stream is assumed to + * contain only ASCII characters (without unicode processing). + */ + +public class SimpleCharStream +{ + public static final boolean staticFlag = false; + int bufsize; + int available; + int tokenBegin; + public int bufpos = -1; + protected int bufline[]; + protected int bufcolumn[]; + + protected int column = 0; + protected int line = 1; + + protected boolean prevCharIsCR = false; + protected boolean prevCharIsLF = false; + + protected java.io.Reader inputStream; + + protected char[] buffer; + protected int maxNextCharInd = 0; + protected int inBuf = 0; + protected int tabSize = 8; + + protected void setTabSize(int i) { tabSize = i; } + protected int getTabSize(int i) { return tabSize; } + + + protected void ExpandBuff(boolean wrapAround) + { + char[] newbuffer = new char[bufsize + 2048]; + int newbufline[] = new int[bufsize + 2048]; + int newbufcolumn[] = new int[bufsize + 2048]; + + try + { + if (wrapAround) + { + System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); + System.arraycopy(buffer, 0, newbuffer, + bufsize - tokenBegin, bufpos); + buffer = newbuffer; + + System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); + System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); + bufline = newbufline; + + System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); + System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); + bufcolumn = newbufcolumn; + + maxNextCharInd = (bufpos += (bufsize - tokenBegin)); + } + else + { + System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); + buffer = newbuffer; + + System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); + bufline = newbufline; + + System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); + bufcolumn = newbufcolumn; + + maxNextCharInd = (bufpos -= tokenBegin); + } + } + catch (Throwable t) + { + throw new Error(t.getMessage()); + } + + + bufsize += 2048; + available = bufsize; + tokenBegin = 0; + } + + protected void FillBuff() throws java.io.IOException + { + if (maxNextCharInd == available) + { + if (available == bufsize) + { + if (tokenBegin > 2048) + { + bufpos = maxNextCharInd = 0; + available = tokenBegin; + } + else if (tokenBegin < 0) + bufpos = maxNextCharInd = 0; + else + ExpandBuff(false); + } + else if (available > tokenBegin) + available = bufsize; + else if ((tokenBegin - available) < 2048) + ExpandBuff(true); + else + available = tokenBegin; + } + + int i; + try { + if ((i = inputStream.read(buffer, maxNextCharInd, + available - maxNextCharInd)) == -1) + { + inputStream.close(); + throw new java.io.IOException(); + } + else + maxNextCharInd += i; + return; + } + catch(java.io.IOException e) { + --bufpos; + backup(0); + if (tokenBegin == -1) + tokenBegin = bufpos; + throw e; + } + } + + public char BeginToken() throws java.io.IOException + { + tokenBegin = -1; + char c = readChar(); + tokenBegin = bufpos; + + return c; + } + + protected void UpdateLineColumn(char c) + { + column++; + + if (prevCharIsLF) + { + prevCharIsLF = false; + line += (column = 1); + } + else if (prevCharIsCR) + { + prevCharIsCR = false; + if (c == '\n') + { + prevCharIsLF = true; + } + else + line += (column = 1); + } + + switch (c) + { + case '\r' : + prevCharIsCR = true; + break; + case '\n' : + prevCharIsLF = true; + break; + case '\t' : + column--; + column += (tabSize - (column % tabSize)); + break; + default : + break; + } + + bufline[bufpos] = line; + bufcolumn[bufpos] = column; + } + + public char readChar() throws java.io.IOException + { + if (inBuf > 0) + { + --inBuf; + + if (++bufpos == bufsize) + bufpos = 0; + + return buffer[bufpos]; + } + + if (++bufpos >= maxNextCharInd) + FillBuff(); + + char c = buffer[bufpos]; + + UpdateLineColumn(c); + return (c); + } + + /** + * @deprecated + * @see #getEndColumn + */ + + public int getColumn() { + return bufcolumn[bufpos]; + } + + /** + * @deprecated + * @see #getEndLine + */ + + public int getLine() { + return bufline[bufpos]; + } + + public int getEndColumn() { + return bufcolumn[bufpos]; + } + + public int getEndLine() { + return bufline[bufpos]; + } + + public int getBeginColumn() { + return bufcolumn[tokenBegin]; + } + + public int getBeginLine() { + return bufline[tokenBegin]; + } + + public void backup(int amount) { + + inBuf += amount; + if ((bufpos -= amount) < 0) + bufpos += bufsize; + } + + public SimpleCharStream(java.io.Reader dstream, int startline, + int startcolumn, int buffersize) + { + inputStream = dstream; + line = startline; + column = startcolumn - 1; + + available = bufsize = buffersize; + buffer = new char[buffersize]; + bufline = new int[buffersize]; + bufcolumn = new int[buffersize]; + } + + public SimpleCharStream(java.io.Reader dstream, int startline, + int startcolumn) + { + this(dstream, startline, startcolumn, 4096); + } + + public SimpleCharStream(java.io.Reader dstream) + { + this(dstream, 1, 1, 4096); + } + public void ReInit(java.io.Reader dstream, int startline, + int startcolumn, int buffersize) + { + inputStream = dstream; + line = startline; + column = startcolumn - 1; + + if (buffer == null || buffersize != buffer.length) + { + available = bufsize = buffersize; + buffer = new char[buffersize]; + bufline = new int[buffersize]; + bufcolumn = new int[buffersize]; + } + prevCharIsLF = prevCharIsCR = false; + tokenBegin = inBuf = maxNextCharInd = 0; + bufpos = -1; + } + + public void ReInit(java.io.Reader dstream, int startline, + int startcolumn) + { + ReInit(dstream, startline, startcolumn, 4096); + } + + public void ReInit(java.io.Reader dstream) + { + ReInit(dstream, 1, 1, 4096); + } + public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, + int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException + { + this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); + } + + public SimpleCharStream(java.io.InputStream dstream, int startline, + int startcolumn, int buffersize) + { + this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); + } + + public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, + int startcolumn) throws java.io.UnsupportedEncodingException + { + this(dstream, encoding, startline, startcolumn, 4096); + } + + public SimpleCharStream(java.io.InputStream dstream, int startline, + int startcolumn) + { + this(dstream, startline, startcolumn, 4096); + } + + public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException + { + this(dstream, encoding, 1, 1, 4096); + } + + public SimpleCharStream(java.io.InputStream dstream) + { + this(dstream, 1, 1, 4096); + } + + public void ReInit(java.io.InputStream dstream, String encoding, int startline, + int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException + { + ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); + } + + public void ReInit(java.io.InputStream dstream, int startline, + int startcolumn, int buffersize) + { + ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); + } + + public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException + { + ReInit(dstream, encoding, 1, 1, 4096); + } + + public void ReInit(java.io.InputStream dstream) + { + ReInit(dstream, 1, 1, 4096); + } + public void ReInit(java.io.InputStream dstream, String encoding, int startline, + int startcolumn) throws java.io.UnsupportedEncodingException + { + ReInit(dstream, encoding, startline, startcolumn, 4096); + } + public void ReInit(java.io.InputStream dstream, int startline, + int startcolumn) + { + ReInit(dstream, startline, startcolumn, 4096); + } + public String GetImage() + { + if (bufpos >= tokenBegin) + return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); + else + return new String(buffer, tokenBegin, bufsize - tokenBegin) + + new String(buffer, 0, bufpos + 1); + } + + public char[] GetSuffix(int len) + { + char[] ret = new char[len]; + + if ((bufpos + 1) >= len) + System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); + else + { + System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, + len - bufpos - 1); + System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); + } + + return ret; + } + + public void Done() + { + buffer = null; + bufline = null; + bufcolumn = null; + } + + /** + * Method to adjust line and column numbers for the start of a token. + */ + public void adjustBeginLineColumn(int newLine, int newCol) + { + int start = tokenBegin; + int len; + + if (bufpos >= tokenBegin) + { + len = bufpos - tokenBegin + inBuf + 1; + } + else + { + len = bufsize - tokenBegin + bufpos + 1 + inBuf; + } + + int i = 0, j = 0, k = 0; + int nextColDiff = 0, columnDiff = 0; + + while (i < len && + bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) + { + bufline[j] = newLine; + nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; + bufcolumn[j] = newCol + columnDiff; + columnDiff = nextColDiff; + i++; + } + + if (i < len) + { + bufline[j] = newLine++; + bufcolumn[j] = newCol + columnDiff; + + while (i++ < len) + { + if (bufline[j = start % bufsize] != bufline[++start % bufsize]) + bufline[j] = newLine++; + else + bufline[j] = newLine; + } + } + + line = bufline[j]; + column = bufcolumn[j]; + } + +} diff --git a/src/org/lwes/db/Token.java b/src/org/lwes/db/Token.java new file mode 100644 index 0000000..3d7d64c --- /dev/null +++ b/src/org/lwes/db/Token.java @@ -0,0 +1,81 @@ +/* Generated By:JavaCC: Do not edit this line. Token.java Version 3.0 */ +package org.lwes.db; + +/** + * Describes the input token stream. + */ + +public class Token { + + /** + * An integer that describes the kind of this token. This numbering + * system is determined by JavaCCParser, and a table of these numbers is + * stored in the file ...Constants.java. + */ + public int kind; + + /** + * beginLine and beginColumn describe the position of the first character + * of this token; endLine and endColumn describe the position of the + * last character of this token. + */ + public int beginLine, beginColumn, endLine, endColumn; + + /** + * The string image of the token. + */ + public String image; + + /** + * A reference to the next regular (non-special) token from the input + * stream. If this is the last token from the input stream, or if the + * token manager has not read tokens beyond this one, this field is + * set to null. This is true only if this token is also a regular + * token. Otherwise, see below for a description of the contents of + * this field. + */ + public Token next; + + /** + * This field is used to access special tokens that occur prior to this + * token, but after the immediately preceding regular (non-special) token. + * If there are no such special tokens, this field is set to null. + * When there are more than one such special token, this field refers + * to the last of these special tokens, which in turn refers to the next + * previous special token through its specialToken field, and so on + * until the first special token (whose specialToken field is null). + * The next fields of special tokens refer to other special tokens that + * immediately follow it (without an intervening regular token). If there + * is no such token, this field is null. + */ + public Token specialToken; + + /** + * Returns the image. + */ + public String toString() + { + return image; + } + + /** + * Returns a new Token object, by default. However, if you want, you + * can create and return subclass objects based on the value of ofKind. + * Simply add the cases to the switch for all those special cases. + * For example, if you have a subclass of Token called IDToken that + * you want to create if ofKind is ID, simlpy add something like : + * + * case MyParserConstants.ID : return new IDToken(); + * + * to the following switch statement. Then you can cast matchedToken + * variable to the appropriate type and use it in your lexical actions. + */ + public static final Token newToken(int ofKind) + { + switch(ofKind) + { + default : return new Token(); + } + } + +} diff --git a/src/org/lwes/db/TokenMgrError.java b/src/org/lwes/db/TokenMgrError.java new file mode 100644 index 0000000..152aff9 --- /dev/null +++ b/src/org/lwes/db/TokenMgrError.java @@ -0,0 +1,133 @@ +/* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 3.0 */ +package org.lwes.db; + +public class TokenMgrError extends Error +{ + /* + * Ordinals for various reasons why an Error of this type can be thrown. + */ + + /** + * Lexical error occured. + */ + static final int LEXICAL_ERROR = 0; + + /** + * An attempt wass made to create a second instance of a static token manager. + */ + static final int STATIC_LEXER_ERROR = 1; + + /** + * Tried to change to an invalid lexical state. + */ + static final int INVALID_LEXICAL_STATE = 2; + + /** + * Detected (and bailed out of) an infinite loop in the token manager. + */ + static final int LOOP_DETECTED = 3; + + /** + * Indicates the reason why the exception is thrown. It will have + * one of the above 4 values. + */ + int errorCode; + + /** + * Replaces unprintable characters by their espaced (or unicode escaped) + * equivalents in the given string + */ + protected static final String addEscapes(String str) { + StringBuffer retval = new StringBuffer(); + char ch; + for (int i = 0; i < str.length(); i++) { + switch (str.charAt(i)) + { + case 0 : + continue; + case '\b': + retval.append("\\b"); + continue; + case '\t': + retval.append("\\t"); + continue; + case '\n': + retval.append("\\n"); + continue; + case '\f': + retval.append("\\f"); + continue; + case '\r': + retval.append("\\r"); + continue; + case '\"': + retval.append("\\\""); + continue; + case '\'': + retval.append("\\\'"); + continue; + case '\\': + retval.append("\\\\"); + continue; + default: + if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { + String s = "0000" + Integer.toString(ch, 16); + retval.append("\\u" + s.substring(s.length() - 4, s.length())); + } else { + retval.append(ch); + } + continue; + } + } + return retval.toString(); + } + + /** + * Returns a detailed message for the Error when it is thrown by the + * token manager to indicate a lexical error. + * Parameters : + * EOFSeen : indicates if EOF caused the lexicl error + * curLexState : lexical state in which this error occured + * errorLine : line number when the error occured + * errorColumn : column number when the error occured + * errorAfter : prefix that was seen before this error occured + * curchar : the offending character + * Note: You can customize the lexical error message by modifying this method. + */ + protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) { + return("Lexical error at line " + + errorLine + ", column " + + errorColumn + ". Encountered: " + + (EOFSeen ? " " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") + + "after : \"" + addEscapes(errorAfter) + "\""); + } + + /** + * You can also modify the body of this method to customize your error messages. + * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not + * of end-users concern, so you can return something like : + * + * "Internal Error : Please file a bug report .... " + * + * from this method for such cases in the release version of your parser. + */ + public String getMessage() { + return super.getMessage(); + } + + /* + * Constructors of various flavors follow. + */ + + public TokenMgrError() { + } + + public TokenMgrError(String message, int reason) { + super(message); + errorCode = reason; + } + + public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) { + this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason); + } +} diff --git a/src/org/lwes/emitter/MulticastEventEmitter.java b/src/org/lwes/emitter/MulticastEventEmitter.java index cbccde0..c8ccccb 100644 --- a/src/org/lwes/emitter/MulticastEventEmitter.java +++ b/src/org/lwes/emitter/MulticastEventEmitter.java @@ -6,7 +6,7 @@ import java.net.InetAddress; import java.net.MulticastSocket; import org.lwes.Event; - +import org.lwes.util.Log; /** * MulticastEventEmitter emits events to multicast groups on the network. @@ -33,7 +33,7 @@ public class MulticastEventEmitter implements EventEmitter { /* the multicast time-to-live */ private int ttl = 31; - /* a lock variable to synchronize heartbeat events */ + /* a lock variable to synchronize events */ private Object lock = new Object(); /** @@ -133,7 +133,7 @@ public class MulticastEventEmitter implements EventEmitter { } catch(IOException ie) { throw ie; } catch(Exception e) { - // TODO: do something here + Log.error("Unable to initialize MulticastEventEmitter", e); } } diff --git a/src/org/lwes/util/Deserializer.java b/src/org/lwes/serializer/Deserializer.java similarity index 88% rename from src/org/lwes/util/Deserializer.java rename to src/org/lwes/serializer/Deserializer.java index 4e77f00..08a5c8b 100644 --- a/src/org/lwes/util/Deserializer.java +++ b/src/org/lwes/serializer/Deserializer.java @@ -1,7 +1,8 @@ -package org.lwes.util; +package org.lwes.serializer; import org.lwes.Event; import org.lwes.util.EncodedString; +import org.lwes.util.Log; import org.lwes.util.NumberCodec; /** @@ -9,11 +10,10 @@ import org.lwes.util.NumberCodec; * of the event system. * * @author Anthony Molinaro + * @author Michael P. Lum */ public class Deserializer { - private static final boolean logDebug = false; - /** * Deserialize a byte out of the byte array bytes * @@ -214,23 +214,20 @@ public class Deserializer try { len = deserializeUINT16(myState,bytes); - if ( logDebug ) - { - System.out.println("datagram bytes : "+ - NumberCodec.byteArrayToHexString(bytes,0,bytes.length)); - System.out.println("string length : "+len); - System.out.println("State : "+myState); - } + Log.debug("Datagram Bytes: " + + NumberCodec.byteArrayToHexString(bytes, 0, bytes.length)); + Log.debug("String Length: " + len); + Log.debug("State: " + myState); aString = EncodedString.bytesToString(bytes,myState.currentIndex(),len, Event.ENCODING_STRINGS[encoding]); myState.incr(len); } catch ( ArrayIndexOutOfBoundsException aioobe ) { - System.err.println("Exception "+aioobe); - System.err.println("datagram bytes : "+ - NumberCodec.byteArrayToHexString(bytes,0,bytes.length)); - System.err.println("string length : "+len); - System.err.println("State : "+myState); + Log.info("Exception: " + aioobe.toString()); + Log.info("Datagram Bytes: " + + NumberCodec.byteArrayToHexString(bytes, 0, bytes.length)); + Log.info("String Length: " + len); + Log.info("State: " + myState); } return aString; @@ -258,23 +255,20 @@ public class Deserializer try { len = (int)deserializeBYTE(myState,bytes); - if ( logDebug ) - { - System.out.println("datagram bytes : "+ - NumberCodec.byteArrayToHexString(bytes,0,bytes.length)); - System.out.println("string length : "+len); - System.out.println("State : "+myState); - } + Log.debug("Datagram Bytes: " + + NumberCodec.byteArrayToHexString(bytes, 0, bytes.length)); + Log.debug("String Length: " + len); + Log.debug("State: " + myState); aString = EncodedString.bytesToString(bytes,myState.currentIndex(),len, Event.ENCODING_STRINGS[encoding]); myState.incr(len); } catch ( ArrayIndexOutOfBoundsException aioobe ) { - System.err.println("Exception "+aioobe); - System.err.println("datagram bytes : "+ - NumberCodec.byteArrayToHexString(bytes,0,bytes.length)); - System.err.println("string length : "+len); - System.err.println("State : "+myState); + Log.info("Exception: " + aioobe.toString()); + Log.info("Datagram Bytes: " + + NumberCodec.byteArrayToHexString(bytes, 0, bytes.length)); + Log.info("String Length: " + len); + Log.info("State: " + myState); } return aString; } diff --git a/src/org/lwes/util/DeserializerState.java b/src/org/lwes/serializer/DeserializerState.java similarity index 93% rename from src/org/lwes/util/DeserializerState.java rename to src/org/lwes/serializer/DeserializerState.java index e0320b7..646f860 100644 --- a/src/org/lwes/util/DeserializerState.java +++ b/src/org/lwes/serializer/DeserializerState.java @@ -1,11 +1,11 @@ -package org.lwes.util; +package org.lwes.serializer; /** * An internal class used by the Deserializer to keep track of its state * * @author Anthony Molinaro */ -public class DeserializerState +public class DeserializerState { private int index; diff --git a/src/org/lwes/util/Serializer.java b/src/org/lwes/serializer/Serializer.java similarity index 97% rename from src/org/lwes/util/Serializer.java rename to src/org/lwes/serializer/Serializer.java index 69cf680..b59ccf0 100644 --- a/src/org/lwes/util/Serializer.java +++ b/src/org/lwes/serializer/Serializer.java @@ -1,4 +1,4 @@ -package org.lwes.util; +package org.lwes.serializer; import java.net.InetAddress; @@ -8,7 +8,7 @@ import org.lwes.util.IPAddress; import org.lwes.util.NumberCodec; /** - * This contains all the low level type serialization used by the + * This contains low level type serialization used by the * rest of the system. * @author Anthony Molinaro */ diff --git a/src/org/lwes/serializer/StringParser.java b/src/org/lwes/serializer/StringParser.java new file mode 100644 index 0000000..f6398cc --- /dev/null +++ b/src/org/lwes/serializer/StringParser.java @@ -0,0 +1,238 @@ +package org.lwes.serializer; + +import java.util.regex.Pattern; + +import org.lwes.EventSystemException; +import org.lwes.TypeID; +import org.lwes.util.IPAddress; +import org.lwes.util.Log; +import org.lwes.util.NumberCodec; + +/** + * This contains low level type serialization used by the rest of the system. + * + * @author Anthony Molinaro + * @author Michael P. Lum + */ +public class StringParser { + public static Object fromStringBYTE(String string) + throws EventSystemException { + Object toReturn = null; + + return toReturn; + } + + public static Object fromStringBOOLEAN(String string) + throws EventSystemException { + Log.trace("Parsing boolean"); + Object toReturn = (Object) Boolean.valueOf(string); + Log.trace("Got '" + toReturn + "'"); + return toReturn; + } + + public static Object fromStringUINT16(String string) + throws EventSystemException { + Object toReturn = null; + + Log.trace("Parsing uint16"); + if (Pattern.matches(TypeID.HEX_SHORT_REGEX, string)) { + if (string.startsWith("0x")) + string = string.substring(2); + + /* pad with zeros since NumberCodec.decodeInt expects a length of 8 */ + string = "0000" + string; + byte[] bytes = NumberCodec.hexStringToByteArray(string); + int aTmpInt = NumberCodec.decodeInt(bytes, 0, bytes.length); + + toReturn = new Integer(aTmpInt); + } else { + try { + toReturn = Integer.valueOf(string); + } catch (NumberFormatException nfe) { + throw new EventSystemException(nfe); + } + int intValue = ((Integer) toReturn).intValue(); + if (intValue < 0 || intValue > 65535) { + throw new EventSystemException("Unsigned Short must be in the " + + "range [0-65535] "); + } + } + Log.trace("received '" + toReturn + "'"); + + return toReturn; + } + + public static Object fromStringINT16(String string) + throws EventSystemException { + Object toReturn = null; + + Log.trace("Parsing int16"); + if (Pattern.matches(TypeID.HEX_SHORT_REGEX, string)) { + if (string.startsWith("0x")) + string = string.substring(2); + + byte[] bytes = NumberCodec.hexStringToByteArray(string); + short aTmpInt = NumberCodec.decodeShort(bytes, 0, bytes.length); + + toReturn = new Short(aTmpInt); + } else { + try { + toReturn = Short.valueOf(string); + } catch (NumberFormatException nfe) { + throw new EventSystemException("Probably not a short, " + + "got exception " + nfe); + } + short shortValue = ((Short) toReturn).shortValue(); + if (shortValue < -32768 || shortValue > 32767) { + throw new EventSystemException("Signed Short must be in the " + + "range [-32768 - 32767] "); + } + } + Log.trace("received '" + toReturn + "'"); + + return toReturn; + } + + public static Object fromStringUINT32(String string) + throws EventSystemException { + Object toReturn = null; + + Log.trace("Parsing uint32"); + if (Pattern.matches(TypeID.HEX_INT_REGEX, string)) { + if (string.startsWith("0x")) + string = string.substring(2); + + /* pad with zeros since NumberCodec.decodeLong expects a length of 8 */ + string = "00000000" + string; + byte[] bytes = NumberCodec.hexStringToByteArray(string); + long aTmpLong = NumberCodec.decodeLong(bytes, 0, bytes.length); + + toReturn = new Long(aTmpLong); + } else { + try { + toReturn = Long.valueOf(string); + } catch (NumberFormatException nfe) { + throw new EventSystemException(nfe); + } + long longValue = ((Long) toReturn).longValue(); + if (longValue < 0 + || longValue > ((long) Integer.MAX_VALUE - ((long) Integer.MIN_VALUE))) { + throw new EventSystemException("Unsigned Int must be in the " + + "range [0-" + + ((long) Integer.MAX_VALUE - (long) Integer.MIN_VALUE) + + "] "); + } + } + Log.trace("received '" + toReturn + "'"); + + return toReturn; + } + + public static Object fromStringINT32(String string) + throws EventSystemException { + Object toReturn = null; + + Log.trace("Parsing int32"); + if (Pattern.matches(TypeID.HEX_INT_REGEX, string)) { + if (string.startsWith("0x")) + string = string.substring(2); + + byte[] bytes = NumberCodec.hexStringToByteArray(string); + int aTmpInt = NumberCodec.decodeInt(bytes, 0, bytes.length); + + toReturn = new Integer(aTmpInt); + } else { + try { + toReturn = Integer.valueOf(string); + } catch (NumberFormatException nfe) { + throw new EventSystemException(nfe); + } + int intValue = ((Integer) toReturn).intValue(); + if (intValue < Integer.MIN_VALUE || intValue > Integer.MAX_VALUE) { + throw new EventSystemException("Signed Short must be in the " + + "range [" + Integer.MIN_VALUE + " - " + + +Integer.MAX_VALUE + "]"); + } + } + Log.trace("received '" + toReturn + "'"); + + return toReturn; + } + + public static Object fromStringUINT64(String string) + throws EventSystemException { + Object toReturn = null; + + Log.trace("Parsing uint64"); + if (Pattern.matches(TypeID.HEX_LONG_REGEX, string)) { + if (string.startsWith("0x")) + string = string.substring(2); + + byte[] bytes = NumberCodec.hexStringToByteArray(string); + long aTmpLong = NumberCodec.decodeLong(bytes, 0, bytes.length); + + toReturn = new Long(aTmpLong); + } else { + try { + toReturn = Long.valueOf(string); + } catch (NumberFormatException nfe) { + throw new EventSystemException("Got Exception " + nfe); + } + } + Log.trace("received '" + toReturn + "'"); + + return toReturn; + } + + public static Object fromStringINT64(String string) + throws EventSystemException { + Object toReturn = null; + + Log.trace("Parsing int64"); + if (Pattern.matches(TypeID.HEX_LONG_REGEX, string)) { + if (string.startsWith("0x")) + string = string.substring(2); + + byte[] bytes = NumberCodec.hexStringToByteArray(string); + long aTmpLong = NumberCodec.decodeLong(bytes, 0, bytes.length); + + toReturn = new Long(aTmpLong); + } else { + try { + toReturn = Long.valueOf(string); + } catch (NumberFormatException nfe) { + throw new EventSystemException(nfe); + } + } + Log.trace("received '" + toReturn + "'"); + + return toReturn; + } + + public static Object fromStringSTRING(String string) + throws EventSystemException { + + Log.trace("Parsing string '" + string + "'"); + return (Object) (string); + } + + public static Object fromStringIPADDR(String string) + throws EventSystemException { + Object toReturn = null; + + Log.trace("Parsing IPAddress"); + + if (Pattern.matches(TypeID.IP_ADDR_REGEX, string)) { + toReturn = new IPAddress(string); + if (((IPAddress) toReturn).toInt() == 0) { + throw new EventSystemException("Possible Bad IP Address " + + string); + } + } else { + throw new EventSystemException("Invalid IP Address"); + } + Log.trace("received '" + toReturn + "'"); + + return toReturn; + } +} diff --git a/src/org/lwes/util/IPAddress.java b/src/org/lwes/util/IPAddress.java index 2ff8808..021d586 100644 --- a/src/org/lwes/util/IPAddress.java +++ b/src/org/lwes/util/IPAddress.java @@ -10,7 +10,7 @@ import java.net.UnknownHostException; */ public class IPAddress implements java.io.Serializable { - /* serializable UID */ + /* serializable UID */ static final long serialVersionUID = -1; /* inet address in network byte order */ diff --git a/src/org/lwes/util/Log.java b/src/org/lwes/util/Log.java new file mode 100644 index 0000000..a815333 --- /dev/null +++ b/src/org/lwes/util/Log.java @@ -0,0 +1,77 @@ +package org.lwes.util; + +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * This is a wrapper class for logging. Instead of embedding log4j or JDK logging + * statements in the code, we have a wrapper around JDK logging so this could be changed + * easily in the future. Also, since we prefer the distribution to be simple and + * standalone, we aren't using Commons Logging so that we don't have to bundle that in + * the distribution. JDK logging could easily be redirected to the destination of the + * library user's choice. + * + * @author Michael P. Lum + */ +public class Log { + private static Logger logger = Logger.getLogger("org.lwes"); + + /** + * Log a trace level message + * @param message the message to be logged + */ + public static void trace(String message) { + logger.log(Level.FINEST, message); + } + + /** + * Log a debug level message + * @param message the message to be logged + */ + public static void debug(String message) { + logger.log(Level.FINER, message); + } + + /** + * Log a info level message + * @param message the message to be logged + */ + public static void info(String message) { + logger.log(Level.FINE, message); + } + + /** + * Log a warning level message + * @param message the message to be logged + */ + public static void warning(String message) { + logger.log(Level.WARNING, message); + } + + /** + * Log an warning level message, with associated Throwable information + * @param message the message to be logged + * @param t the Throwable associated with the log message + */ + public static void warning(String message, Throwable t) { + logger.log(Level.WARNING, message, t); + } + + + /** + * Log an error level message + * @param message the message to be logged + */ + public static void error(String message) { + logger.log(Level.SEVERE, message); + } + + /** + * Log an error level message, with associated Throwable information + * @param message the message to be logged + * @param t the Throwable associated with the log message + */ + public static void error(String message, Throwable t) { + logger.log(Level.SEVERE, message, t); + } +} -- 2.11.4.GIT