Java XML String Transform toString(Collection c, String separator)

Here you can find the source of toString(Collection c, String separator)

Description

Converts collection to a separated string.

License

Open Source License

Parameter

Parameter Description
c collection
separator string to separate items

Return

collection items separated with given separator

Declaration

public static String toString(Collection<String> c, String separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2013 Richard Hoefter and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /*from w ww.  j a  v a  2 s  .  co m*/
 * Contributors:
 *     Richard Hoefter (richard.hoefter@web.de) - initial API and implementation, bug 95300, bug 95297, bug 128104, bug 201180, bug 288830 
 *     IBM Corporation - NLS'ing and incorporating into Eclipse. 
 *                     - Bug 177833 Class created from combination of all utility classes of contribution 
 *                     - Bug 267459 Java project with an external jar file from C:\ on the build path throws a NPE during the Ant Buildfile generation.
 *                     - bug fixing
 *******************************************************************************/

import java.io.StringWriter;

import java.util.Collection;

import java.util.Iterator;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class Main {
    /**
     * Convert document to formatted XML string.
     */
    public static String toString(Document doc)
            throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
        // NOTE: There are different transformer implementations in the wild,
        // which are configured differently
        // regarding the indent size:
        // Java 1.4: org.apache.xalan.transformer.TransformerIdentityImpl
        // Java 1.5:
        // com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl

        StringWriter writer = new StringWriter();
        Source source = new DOMSource(doc);
        Result result = new StreamResult(writer);
        TransformerFactory factory = TransformerFactory.newInstance();
        boolean indentFallback = false;
        try {
            // indent using TransformerImpl
            factory.setAttribute("indent-number", "4"); //$NON-NLS-1$ //$NON-NLS-2$
        } catch (IllegalArgumentException e) {
            // option not supported, set indent size below
            indentFallback = true;
        }
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        if (indentFallback) {
            // indent using TransformerIdentityImpl
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        transformer.transform(source, result);
        return writer.toString();
    }

    /**
     * Converts collection to a separated string.
     * 
     * @param c
     *            collection
     * @param separator
     *            string to separate items
     * @return collection items separated with given separator
     */
    public static String toString(Collection<String> c, String separator) {
        StringBuffer b = new StringBuffer();
        for (Iterator<String> iter = c.iterator(); iter.hasNext();) {
            b.append(iter.next());
            b.append(separator);
        }
        if (c.size() > 0) {
            b.delete(b.length() - separator.length(), b.length());
        }
        return b.toString();
    }
}

Related

  1. toHexString(byte[] array)
  2. toHTML(String xml)
  3. toJson(String xml)
  4. toLCCNDisplay(String packedLCCN)
  5. tomcatPort(String fileName)
  6. toString(Source input)
  7. write(Node node, OutputStream out, String... props)
  8. writeElement(Element element, String fileName)
  9. writeNodeSubtreeXMLString(final Node node, final Writer writer)