-- $Id$ library Java; -- ====================================================================== -- generic helpers begin -- ====================================================================== helper context String def: firstToUpper() : String = self.substring(1, 1).toUpper() + self.substring(2, self.size()); helper context String def : startsWith(begin : String) : Boolean = if (self.size() < begin.size()) then false else self.substring(1, begin.size()) = begin endif; helper context String def : endsWith(end : String) : Boolean = if (self.size() < end.size()) then false else self.substring(self.size() - end.size() + 1, self.size()) = end endif; helper context String def : isSequenceSet() : Boolean = self = 'SequenceSet'; helper context String def : isSet() : Boolean = self = 'Set' or self.isSequenceSet(); helper context String def : isSequence() : Boolean = self = 'Sequence' or self.isSequenceSet(); helper context String def : isCollection() : Boolean = self = 'Collection' or self = 'Bag' or self.isSequence() or self.isSet(); helper context String def : s() : String = if self.isCollection() then 's' else '' endif; helper context String def : javaSetter() : String = 'this.' + self + ' = ' + self + ';'; helper context String def : javaDualSetter(otherEnd : String) : String = 'if (this.' + self + ' != ' + self + ') {\n' + ' this.' + self + ' = ' + self + ';\n' + ' ' + self + '.set' + otherEnd + '(this);\n' + '}'; helper context String def : javaAddRemoveSetter(otherEnd : String) : String = if otherEnd <> '' then 'if (this.' + self + ' != ' + self + ') {\n' + ' if (this.' + self + ' != null) this.' + self + '.remove' + otherEnd + '(this);\n' + ' this.' + self + ' = ' + self + ';\n' + ' if (' + self + ' != null) ' + self + '.add' + otherEnd + '(this);\n' + '}' else 'this.' + self + ' = ' + self + ';' endif; helper context String def : javaGetter() : String = 'return ' + self + ';'; helper context String def : javaGetAt(type : String) : String = 'try {\n' + ' return (' + type + ') ' + self + '.elementAt(index);\n' + '} catch (ArrayIndexOutOfBoundsException e) {\n' + ' return null;\n' + '}'; helper context String def : java2GetAt(type : String) : String = 'try {\n' + ' return (' + type + ') ' + self + '.get(index);\n' + '} catch (IndexOutOfBoundsException e) {\n' + ' return null;\n' + '}'; helper context String def : javaAdder() : String = 'this.' + self + '.addElement(' + self + ');'; helper context String def : java2Adder() : String = 'this.' + self + '.add(' + self + ');'; helper context String def : javaInsert() : String = 'try {\n' + ' this.' + self + '.insertElementAt(' + self + ', index);' + '} catch (ArrayIndexOutOfBoundsException e) {\n' + ' ' + self.javaAdder() + '\n' + '};'; helper context String def : java2Insert() : String = 'try {\n' + ' this.' + self + '.add(index, ' + self + ');\n' + '} catch (IndexOutOfBoundsException e) {\n' + ' ' + self.java2Adder() + '\n' + '};'; helper context String def : javaNotContainsCheck(body : String) : String = 'if (! this.' + self + '.contains(' + self + ')) {\n' + body + '\n}'; helper context String def : javaContainsCheck(body : String) : String = 'if (this.' + self + '.contains(' + self + ')) {\n' + body + '\n}'; helper context String def : javaOtherEndSetter(otherEnd : String) : String = self + '.set' + otherEnd + '(this);'; helper context String def : javaOtherEndAdder(otherEnd : String) : String = self + '.add' + otherEnd + '(this);'; helper context String def : javaOtherEndResetter(otherEnd : String) : String = self + '.set' + otherEnd + '(null);'; helper context String def : javaOtherEndRemover(otherEnd : String) : String = self + '.remove' + otherEnd + '(this);'; helper context String def : javaSetterAdder(otherEnd : String) : String = self.javaNotContainsCheck( ' ' + self.javaAdder() + '\n' + ' ' + self.javaOtherEndSetter(otherEnd)); helper context String def : java2SetterAdder(otherEnd : String) : String = self.javaNotContainsCheck( ' ' + self.java2Adder() + '\n' + ' ' + self.javaOtherEndSetter(otherEnd)); helper context String def : javaSetterInsert(otherEnd : String) : String = self.javaNotContainsCheck( ' ' + self.javaInsert() + '\n' + ' ' + self.javaOtherEndSetter(otherEnd)); helper context String def : java2SetterInsert(otherEnd : String) : String = self.javaNotContainsCheck( ' ' + self.java2Insert() + '\n' + ' ' + self.javaOtherEndSetter(otherEnd)); helper context String def : javaDualAdder(otherEnd : String) : String = self.javaNotContainsCheck( ' ' + self.javaAdder() + '\n' + ' ' + self.javaOtherEndAdder(otherEnd)); helper context String def : java2DualAdder(otherEnd : String) : String = self.javaNotContainsCheck( ' ' + self.java2Adder() + '\n' + ' ' + self.javaOtherEndAdder(otherEnd)); helper context String def : javaDualInsert(otherEnd : String) : String = self.javaNotContainsCheck( ' ' + self.javaInsert() + '\n' + ' ' + self.javaOtherEndAdder(otherEnd)); helper context String def : java2DualInsert(otherEnd : String) : String = self.javaNotContainsCheck( ' ' + self.java2Insert() + '\n' + ' ' + self.javaOtherEndAdder(otherEnd)); helper context String def : javaRemover() : String = 'this.' + self + '.removeElement(' + self + ');'; helper context String def : java2Remover() : String = 'this.' + self + '.remove(' + self + ');'; helper context String def : javaSetterRemover(otherEnd : String) : String = self.javaContainsCheck( ' ' + self.javaRemover() + '\n' + ' ' + self.javaOtherEndResetter(otherEnd)); helper context String def : java2SetterRemover(otherEnd : String) : String = self.javaContainsCheck( ' ' + self.java2Remover() + '\n' + ' ' + self.javaOtherEndResetter(otherEnd)); helper context String def : javaDualRemover(otherEnd : String) : String = self.javaContainsCheck( ' ' + self.javaRemover() + '\n' + ' ' + self.javaOtherEndRemover(otherEnd)); helper context String def : java2DualRemover(otherEnd : String) : String = self.javaContainsCheck( ' ' + self.java2Remover() + '\n' + ' ' + self.javaOtherEndRemover(otherEnd)); helper context String def : javaInstance() : String = if self.isCollection() then 'new java.util.Vector()' else 'null' endif; helper context String def : java2Instance() : String = if self.isSet() then 'new java.util.TreeSet()' else if self.isCollection() then 'new java.util.ArrayList()' else 'null' endif endif; helper context String def : removeOcl() : String = if self.startsWith('ocl.') then self.substring(5, self.size()) else self endif; helper context String def : javaBoxedType() : String = if self = 'OclAny' then 'Object' else if self = 'Real' then 'Double' else self endif endif; helper context String def : javaBoxing(type : String) : String = if type = 'Boolean' then 'new Boolean(' + self + ')' else if type = 'Integer' then 'new Integer(' + self + ')' else if type = 'Real' then 'new Double(' + self + ')' else self endif endif endif; helper context String def : javaUnboxing(type : String) : String = if type = 'Boolean' then '(' + self + ').booleanValue()' else if type = 'Integer' then '(' + self + ').intValue()' else if type = 'Real' then '(' + self + ').doubleValue()' else self endif endif endif; helper context String def : javaTypedValue(type : String) : String = '(' + type.removeOcl().javaBoxedType() + ') ' + self; helper context String def : javaUnboxValue(type : String) : String = self.javaTypedValue(type).javaUnboxing(type.removeOcl()); helper context String def : javaNotifyObservers() : String = 'for (int i = 0; i < observers.size(); i++) {\n' + ' ((observer.Observer) observers.elementAt(i)).update(' + self + ');\n' + '}'; helper context String def : notifyStanza(type : String) : String = if self = self.javaBoxing(type.removeOcl()) then ' notifyObservers("' + self.firstToUpper() + '", ' + self + ');\n' else ' notifyObservers("' + self.firstToUpper() + '", ' + self.javaBoxing(type.removeOcl()) + ');\n' endif; helper context String def : javaNotifyStanza(type : String) : String = ' setChanged();\n' + ' java.util.Hashtable e = new java.util.Hashtable();\n' + if self = self.javaBoxing(type.removeOcl()) then ' e.put("name", "' + self.firstToUpper() + '");\n' + ' e.put("class", ' + type.removeOcl().javaBoxedType() + '.class);\n' + ' if (' + self + ' != null) {\n' + ' e.put("value", ' + self + ');\n' + ' }\n' else ' e.put("name", "' + self.firstToUpper() + '");\n' + ' e.put("class", ' + type.removeOcl().javaBoxedType() + '.class);\n' + ' e.put("value", ' + self.javaBoxing(type.removeOcl()) + ');\n' endif + ' notifyObservers(e);\n'; helper context String def : javaUpdater() : String = 'if (' + self + ' instanceof java.util.Hashtable) {\n' + ' java.util.Hashtable e = (java.util.Hashtable) ' + self + ';\n' + ' String mName = "on" + ((String) e.get("name")) + "Change";\n' + ' Class[] parmTypes = { (Class) e.get("class") };\n' + ' try {\n' + ' java.lang.reflect.Method m = getClass().getDeclaredMethod(mName, parmTypes);\n' + ' Object[] args = { e.get("value") };\n' + ' m.invoke(this, args);\n' + ' } catch (NoSuchMethodException nex) {\n' + ' // no handler\n' + ' } catch (Exception ex) {\n' + ' // wrong handler configuration\n' + ' ex.printStackTrace();\n' + ' }\n' + '}'; helper context String def : javaSubscribeStanza() : String = 'if (' + self + ' != null) ' + self + '.addObserver(this);\n'; helper context String def : javaUnsubscribeStanza() : String = 'if (this.' + self + ' != null) this.' + self + '.deleteObserver(this);\n'; helper context String def : javaRemoveUnsubscribeStanza() : String = 'if (' + self + ' != null) ' + self + '.deleteObserver(this);\n'; helper context String def : javaSetterSubscribe(body : String) : String = '// Begin subscribe stanza\n' + self.javaUnsubscribeStanza() + '// Begin original body\n' + body + '\n// End original body\n' + self.javaSubscribeStanza() + '// End subscribe stanza'; helper context String def : javaAdderSubscribe(body : String) : String = '// Begin subscribe stanza\n' + '// Begin original body\n' + body + '\n// End original body\n' + self.javaSubscribeStanza() + '// End subscribe stanza'; helper context String def : javaRemoverSubscribe(body : String) : String = '// Begin subscribe stanza\n' + self.javaRemoveUnsubscribeStanza() + '// Begin original body\n' + body + '\n// End original body\n' + '// End subscribe stanza'; helper context String def : javaSingletonBody() : String = 'if (instance == null) {\n' + ' instance = new ' + self + '();\n' + '}\n' + 'return instance;'; helper context Boolean def : javaAppletInitBody() : String = if self then 'instance = this;\n' + 'setLayout(new java.awt.BorderLayout());' else 'setLayout(new java.awt.BorderLayout());' endif; helper context Boolean def : javaMIDletInitBody() : String = if self then 'instance = this;' else '' endif; -- ====================================================================== -- generic helpers end -- ======================================================================