Java XML Transform Usage convertDisplay(Object object)

Here you can find the source of convertDisplay(Object object)

Description

convert Display

License

Apache License

Declaration

private static String convertDisplay(Object object) 

Method Source Code


//package com.java2s;
/*//from w ww . j  av  a 2  s .com
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

import java.util.Arrays;
import java.io.InputStream;
import java.io.ByteArrayInputStream;

import java.io.StringWriter;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;

import javax.xml.transform.stream.StreamResult;

public class Main {
    public static final int MAX_MSG_DISPLAY_SIZE = 1500;

    private static String convertDisplay(Object object) {
        try {
            String result;
            if (object instanceof ByteArrayInputStream) {
                InputStream is = (InputStream) object;
                byte[] data = new byte[is.available()];
                is.mark(0);
                is.read(data);
                is.reset();
                // Heuristic to check if this is a string
                if (isBinary(data)) {
                    result = Arrays.toString(data);
                } else {
                    result = new String(data);
                }
            } else if (object instanceof DOMSource) {
                StringWriter buffer = new StringWriter();
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.transform((DOMSource) object, new StreamResult(buffer));
                result = buffer.toString();
            } else if (object != null) {
                result = object.toString();
            } else {
                result = "<null>";
            }
            // trim if too long
            if (result.length() > MAX_MSG_DISPLAY_SIZE) {
                result = result.substring(0, MAX_MSG_DISPLAY_SIZE) + "...";
            }
            return result;
        } catch (Throwable t) {
            return "Error display value (" + t.toString() + ")";
        }
    }

    private static boolean isBinary(byte[] data) {
        if (data.length == 0) {
            return true;
        }
        double prob_bin = 0;
        for (int i = 0; i < data.length; i++) {
            int j = (int) data[i];
            if (j < 32 || j > 127) {
                prob_bin++;
            }
        }
        double pb = prob_bin / data.length;
        return pb > 0.5;
    }
}

Related

  1. castResultToSource(StreamResult result)
  2. closeSource(final Source source)
  3. compareSource(Source src, Source target)
  4. compareStreamSource(StreamSource src1, StreamSource src2)
  5. convert(final Element element)
  6. convertMessageToInputStream(Source src)
  7. createAndGetFactory(Collection schemaSources)
  8. createDefaultPropertiesForXML(boolean omitXMLDeclaration)
  9. createSafeReader(InputStream inputStream)