/**
   * Determine if the property differs
   *
   * @param propertyName - the property id
   * @param object1 - the object value 1
   * @param object2 - the object value 2
   * @return the difference
   */
  public static PropertyDifference comparePropertyValue(
      String propertyName, Object object1, Object object2) {
    String value1 = "";
    String value2 = "";
    PropertyDifference pd = null;

    // get value 1
    if (object1 == null) {
      value1 = "<null>";
    } else {
      value1 = object1.toString();
    }

    // get value 2
    if (object2 == null) {
      value2 = "<null>";
    } else {
      value2 = object2.toString();
    }

    // not the same
    if (!value1.equals(value2)) {
      pd = new PropertyDifference(propertyName, value1, value2, PropertyDifference.FLAG_CHANGED);
      pd.setObject1(object1);
      pd.setObject2(object2);
      LOG.debug("Property diff: " + propertyName + " [" + value1 + "] [" + value2 + "]");
    } else {
      LOG.debug("Property same: " + propertyName + " [" + value1 + "] [" + value2 + "]");
    }

    return pd;
  }
  /**
   * Determine if the property differs
   *
   * @param method - the property to be compared
   * @param element1 - element 1
   * @param element2 - element 2
   * @param tag - tag
   * @return the difference
   */
  public static PropertyDifference compareProperty(
      Method method, Element element1, Element element2, String tag) {
    LOG.debug("Comparing: " + method.getName());
    PropertyDifference pd = null;
    try {
      Object object1 = method.invoke(element1);
      Object object2 = method.invoke(element2);
      pd = comparePropertyValue(method.getName(), object1, object2);
    } catch (Exception e) {
      pd =
          new PropertyDifference(
              method.getName(),
              ElementComparatorToolbox.TEXT_EXCEPTION,
              ElementComparatorToolbox.TEXT_EXCEPTION,
              PropertyDifference.FLAG_EXCEPTION);
      LOG.debug(
          "Property diff: "
              + method.getName()
              + " ["
              + ElementComparatorToolbox.TEXT_EXCEPTION
              + "] ["
              + ElementComparatorToolbox.TEXT_EXCEPTION
              + "]");
    }

    return pd;
  }
 /**
  * Set the value of an an element
  *
  * @param method - the property to be compared
  * @param element - element to be changed
  * @param value - the value to set
  * @return true if successfully set
  */
 public static boolean setProperty(Method method, Element element, Object value) {
   LOG.debug("Setting: " + method.getName());
   // PropertyDifference pd = null;
   try {
     method.invoke(element, value);
     return true;
   } catch (Exception e) {
     LOG.debug("Set value: " + method.getName() + " failed ");
     return false;
   }
 }
Ejemplo n.º 4
0
 /**
  * Converts a hex string back into a byte array (invalid codes will be skipped).
  *
  * @param sHex hex string
  * @param data the target array
  * @param nSrcOfs from which character in the string the conversion should begin, remember that
  *     (nSrcPos modulo 2) should equals 0 normally
  * @param nDstOfs to store the bytes from which position in the array
  * @param nLen number of bytes to extract
  * @return number of extracted bytes
  */
 public static final int hexStrToBytes(
     String sHex, byte[] data, int nSrcOfs, int nDstOfs, int nLen) {
   int nI, nJ, nStrLen, nAvailBytes, nDstOfsBak;
   byte bActByte;
   boolean blConvertOK;
   // check for correct ranges
   nStrLen = sHex.length();
   nAvailBytes = (nStrLen - nSrcOfs) >> 1;
   if (nAvailBytes < nLen) {
     nLen = nAvailBytes;
   }
   int nOutputCapacity = data.length - nDstOfs;
   if (nLen > nOutputCapacity) {
     nLen = nOutputCapacity;
   }
   // convert now
   nDstOfsBak = nDstOfs;
   for (nI = 0; nI < nLen; nI++) {
     bActByte = 0;
     blConvertOK = true;
     for (nJ = 0; nJ < 2; nJ++) {
       bActByte <<= 4;
       char cActChar = sHex.charAt(nSrcOfs++);
       if ((cActChar >= 'a') && (cActChar <= 'f')) {
         bActByte |= (byte) (cActChar - 'a') + 10;
       } else {
         if ((cActChar >= '0') && (cActChar <= '9')) {
           bActByte |= (byte) (cActChar - '0');
         } else {
           LOG.error("invalid hex:" + cActChar);
           LOG.error("indexI:" + Integer.valueOf(nI).toString());
           LOG.error("indexJ:" + Integer.valueOf(nJ).toString());
           blConvertOK = false;
         }
       }
     }
     if (blConvertOK) {
       data[nDstOfs++] = bActByte;
     }
   }
   return (nDstOfs - nDstOfsBak);
 }