123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /*
- Copyright (c) 2013 James Ahlborn
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
-
- package com.healthmarketscience.jackcess.impl;
-
- import java.nio.ByteBuffer;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.Map;
-
- import org.apache.commons.lang.SystemUtils;
- import org.apache.commons.lang.builder.StandardToStringStyle;
- import org.apache.commons.lang.builder.ToStringBuilder;
-
- /**
- * Custom ToStringStyle for use with ToStringBuilder.
- *
- * @author James Ahlborn
- */
- public class CustomToStringStyle extends StandardToStringStyle
- {
- private static final long serialVersionUID = 0L;
-
- private static final String ML_FIELD_SEP = SystemUtils.LINE_SEPARATOR + " ";
- private static final String IMPL_SUFFIX = "Impl";
- private static final int MAX_BYTE_DETAIL_LEN = 20;
-
- public static final CustomToStringStyle INSTANCE = new CustomToStringStyle() {
- private static final long serialVersionUID = 0L;
- {
- setContentStart("[");
- setFieldSeparator(ML_FIELD_SEP);
- setFieldSeparatorAtStart(true);
- setFieldNameValueSeparator(": ");
- setArraySeparator("," + ML_FIELD_SEP);
- setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
- setUseShortClassName(true);
- }
- };
-
- public static final CustomToStringStyle VALUE_INSTANCE = new CustomToStringStyle() {
- private static final long serialVersionUID = 0L;
- {
- setUseShortClassName(true);
- setUseIdentityHashCode(false);
- }
- };
-
- private CustomToStringStyle() {
- }
-
- public static ToStringBuilder builder(Object obj) {
- return new ToStringBuilder(obj, INSTANCE);
- }
-
- public static ToStringBuilder valueBuilder(Object obj) {
- return new ToStringBuilder(obj, VALUE_INSTANCE);
- }
-
- @Override
- protected void appendClassName(StringBuffer buffer, Object obj) {
- if(obj instanceof String) {
- // the caller gave an "explicit" class name
- buffer.append(obj);
- } else {
- super.appendClassName(buffer, obj);
- }
- }
-
- @Override
- protected String getShortClassName(Class clss) {
- String shortName = super.getShortClassName(clss);
- if(shortName.endsWith(IMPL_SUFFIX)) {
- shortName = shortName.substring(0,
- shortName.length() - IMPL_SUFFIX.length());
- }
- int idx = shortName.lastIndexOf('.');
- if(idx >= 0) {
- shortName = shortName.substring(idx + 1);
- }
- return shortName;
- }
-
- @Override
- protected void appendDetail(StringBuffer buffer, String fieldName,
- Object value) {
- if(value instanceof ByteBuffer) {
- appendDetail(buffer, (ByteBuffer)value);
- } else {
- buffer.append(indent(value));
- }
- }
-
- @Override
- protected void appendDetail(StringBuffer buffer, String fieldName,
- Collection value) {
- buffer.append("[");
-
- // gather contents of list in a new StringBuffer
- StringBuffer sb = new StringBuffer();
- Iterator<?> iter = value.iterator();
- if(iter.hasNext()) {
- if(isFieldSeparatorAtStart()) {
- appendFieldSeparator(sb);
- }
- appendValueDetail(sb, fieldName, iter.next());
- }
- while(iter.hasNext()) {
- sb.append(getArraySeparator());
- appendValueDetail(sb, fieldName, iter.next());
- }
-
- // indent entire list contents another level
- buffer.append(indent(sb));
-
- if(isFieldSeparatorAtStart()) {
- appendFieldSeparator(buffer);
- }
- buffer.append("]");
- }
-
-
- @Override
- protected void appendDetail(StringBuffer buffer, String fieldName,
- Map value) {
- buffer.append("{");
-
- // gather contents of map in a new StringBuffer
- StringBuffer sb = new StringBuffer();
- @SuppressWarnings("unchecked")
- Iterator<Map.Entry<?,?>> iter = value.entrySet().iterator();
- if(iter.hasNext()) {
- if(isFieldSeparatorAtStart()) {
- appendFieldSeparator(sb);
- }
- Map.Entry<?,?> e = iter.next();
- sb.append(e.getKey()).append("=");
- appendValueDetail(sb, fieldName, e.getValue());
- }
- while(iter.hasNext()) {
- sb.append(getArraySeparator());
- Map.Entry<?,?> e = iter.next();
- sb.append(e.getKey()).append("=");
- appendValueDetail(sb, fieldName, e.getValue());
- }
-
- // indent entire map contents another level
- buffer.append(indent(sb));
-
- if(isFieldSeparatorAtStart()) {
- appendFieldSeparator(buffer);
- }
- buffer.append("}");
- }
-
- @Override
- protected void appendDetail(StringBuffer buffer, String fieldName,
- byte[] array) {
- appendDetail(buffer, PageChannel.wrap(array));
- }
-
- private void appendValueDetail(StringBuffer buffer, String fieldName,
- Object value) {
- if (value == null) {
- appendNullText(buffer, fieldName);
- } else {
- appendInternal(buffer, fieldName, value, true);
- }
- }
-
- private static void appendDetail(StringBuffer buffer, ByteBuffer bb) {
- int len = bb.remaining();
- buffer.append("(").append(len).append(") ");
- buffer.append(ByteUtil.toHexString(bb, bb.position(),
- Math.min(len, MAX_BYTE_DETAIL_LEN)));
- if(len > MAX_BYTE_DETAIL_LEN) {
- buffer.append(" ...");
- }
- }
-
- private static String indent(Object obj) {
- return ((obj != null) ? obj.toString().replaceAll(
- SystemUtils.LINE_SEPARATOR, ML_FIELD_SEP) : null);
- }
- }
|