The array returned by getElementsByTagName() function is LIVE!
What I mean by LIVE is that as you traverse the array and invoke changes to the DOM, the items in the array itself can change automatically! Here's an example:
var inputEls = divEl.getElementsByTagName('input');
for (var j=0; j < inputEls.length; j++) {
alert('inputEls length='+inputEls.length);
var inputEl = inputEls[j];
inputEl.parentNode.removeChild(inputEl);
}
What you'll actually see is that the length of the array automatically decreases with each iteration because I am removing those elements from the DOM.
To work around this issue I had to traverse the array backwards:
for(var i=inputEls.length-1;i>=0; i--) {
}
Tips and experience about developing websites with various technologies
Wednesday, August 5, 2009
Tuesday, August 4, 2009
Java TreeSet missing elements
If you notice that some elements may be missing after having added them to a TreeSet then it is probably because your implementation of Comparator or Comparable is incorrect.
This is the key note to remember:
"If two elements are different but compareTo returns 0, then TreeSet will consider them equal, and the newly added element replaces the old one".
This is the key note to remember:
"If two elements are different but compareTo returns 0, then TreeSet will consider them equal, and the newly added element replaces the old one".
Wednesday, July 22, 2009
HibernateSystemException: attempted to assign id from null one-to-one property
I had a bidirectional relationship in my one-to-one mapping, however I had forgotten to set the objects in my Java code to reference each other. Once I had done that, the problem was resolved.
SpermAnalysis sa = new SpermAnalysis();
cryoSession.getControlMouse().setSpermAnalysis(sa);
// Bidirectional relationship
sa.setMouse(cryoSession.getControlMouse());
SpermAnalysis sa = new SpermAnalysis();
cryoSession.getControlMouse().setSpermAnalysis(sa);
// Bidirectional relationship
sa.setMouse(cryoSession.getControlMouse());
Determine Mime type from bytes
If you have an image file stored in the database but don't have the extension of the file, then you may not know what the mime type should be.
There is a handy Java library that will inspect the bytes to determine the Mime type. The library can be found here:
MimeUtil
It's fairly easy to use. An example code is shown below:
There is a handy Java library that will inspect the bytes to determine the Mime type. The library can be found here:
MimeUtil
It's fairly easy to use. An example code is shown below:
byte[] bytes;
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MimeMagicMimeDetector");
MimeUtil.getMimeTypes(bytes);
MimeUtil.unregisterMimeDetector("eu.medsea.mimeutil.detector.MimeMagicMimeDetector");
Tuesday, April 14, 2009
Java Bean validation using reflection
public static boolean validateBeanFields(Object obj) {
if (obj == null)
return false;
Class objClass = obj.getClass();
Method[] methods = objClass.getMethods();
for (Method aMethod : methods) {
String methodName = aMethod.getName();
if (methodName.startsWith("get")) {
try {
Object retObj = aMethod.invoke(obj, null);
if (retObj == null)
return false;
else if (retObj instanceof Enum) // skip enum values
continue;
else if (retObj instanceof String) {
if (((String)retObj).length() == 0)
return false;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
return true;
}
Subscribe to:
Posts (Atom)