All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jvnet.hyperjaxb3.lang.util.ToStringUtils Maven / Gradle / Ivy

The newest version!
package org.jvnet.hyperjaxb3.lang.util;

import java.util.Collection;
import java.util.Iterator;

import javax.xml.bind.JAXBElement;

import org.jvnet.hyperjaxb3.lang.ToString;

public class ToStringUtils {

  public static void elementToIndentedString(JAXBElement element, String indent, StringBuffer buffer) {
    final String localPart = element.getName().getLocalPart();
    buffer.append(localPart).append('(');
    final Object value = element.getValue();
    if (value != null) {
      if (value instanceof ToString) {
        ((ToString) value).toIndentedString(indent, buffer);
      }
      else {
        buffer.append(value);
      }
    }

    buffer.append(')');

  }

  public static String toString(Object object) {
    final StringBuffer sb = new StringBuffer();
    toIndentedString(object, "", sb);
    return sb.toString();
  }

  public static void toIndentedString(Object object, String indent, StringBuffer buffer) {

    if (object == null) {
      buffer.append("null");
    }
    else if (object instanceof JAXBElement) {
      elementToIndentedString((JAXBElement) object, indent, buffer);
    }
    else if (object instanceof ToString) {
      ((ToString) object).toIndentedString(indent, buffer);
    }
    else {
      buffer.append(object);
    }

  }

  public static void collectionToIndentedString(Collection list, String indent, StringBuffer buffer) {

    buffer.append('[');
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
      final Object element = iterator.next();
      toIndentedString(element, indent, buffer);
      if (iterator.hasNext()) {
        buffer.append(",\n").append(indent);
      }
    }
    buffer.append(']');
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy