/** * Do a recursive find and replace of objects pointed to by this object. * * @since v1.0 * @param objectText Canonical string representation of the portion we want to replace. * @param replacement object we want to replace this portion with. A replacement will occur if a * portion of the structure is found with a match of the encoded text with objectText and with * the same class as replacement. * @param matchSubstring is true if we want to match objectText as a substring of the encoded * target text. (i.e. an object is a candidate for replacement if objectText is a substring of * candidate.encode() && candidate.class.equals(replacement.class) otherwise the match test is * an equality test.) */ public void replace(String objectText, GenericObjectList replacement, boolean matchSubstring) throws IllegalArgumentException { if (objectText == null || replacement == null) { throw new IllegalArgumentException("null argument!"); } Class replacementClass = replacement.getClass(); Class myclass = getClass(); Field[] fields = myclass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; Class fieldType = f.getType(); if (!getClassFromName(SIP_PACKAGE + ".GenericObject").isAssignableFrom(fieldType) && !getClassFromName(SIP_PACKAGE + ".GenericObjectList").isAssignableFrom(fieldType)) { continue; } else if ((f.getModifiers() & Modifier.PRIVATE) == Modifier.PRIVATE) { continue; } try { if (fieldType.equals(replacementClass)) { if (GenericObject.isMySubclass(replacementClass)) { GenericObject obj = (GenericObject) f.get(this); if (!matchSubstring) { if (objectText.compareTo(obj.encode()) == 0) { f.set(this, replacement); } } else { if (obj.encode().indexOf(objectText) >= 0) { f.set(this, replacement); } } } else if (GenericObjectList.isMySubclass(replacementClass)) { GenericObjectList obj = (GenericObjectList) f.get(this); if (!matchSubstring) { if (objectText.compareTo(obj.encode()) == 0) { f.set(this, replacement); } } else { if (obj.encode().indexOf(objectText) >= 0) { f.set(this, replacement); } } } } else if (getClassFromName(SIP_PACKAGE + ".GenericObject").isAssignableFrom(fieldType)) { GenericObject g = (GenericObject) f.get(this); g.replace(objectText, replacement, matchSubstring); } else if (getClassFromName(SIP_PACKAGE + ".GenericObjectList") .isAssignableFrom(fieldType)) { GenericObjectList g = (GenericObjectList) f.get(this); g.replace(objectText, replacement, matchSubstring); } } catch (IllegalAccessException ex) { InternalErrorHandler.handleException(ex); } } }