/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.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://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */
package com.sun.jini.config;

import java.net.UnknownHostException;

/**
 * A set of static convenience methods for use in configuration files.
 * This class cannot be instantiated.
 *
 * @author Sun Microsystems, Inc.
 * @since 2.0
 * @see net.jini.config.ConfigurationFile
 */
public class ConfigUtil {
    /** This class cannot be instantiated. */
    private ConfigUtil() {
	throw new AssertionError(
            "com.sun.jini.config.ConfigUtil cannot be instantiated");
    }
      
    /**
     * Concatenate the strings resulting from calling {@link
     * java.lang.String#valueOf(Object)} on each element of
     * an array of objects. Passing a zero length array will result in
     * the empty string being returned.
     * @param objects the array of objects to be processed.
     * @return the concatenation of the return values from
     *         calling <code>String.valueOf</code> on each element of 
     *         <code>objects</code>.  
     * @throws NullPointerException if <code>objects</code> 
     *         is <code>null</code>.
     */
    public static String concat(Object[] objects) {
	if (objects.length == 0)
	    return "";
	
	final StringBuffer buf = new StringBuffer(String.valueOf(objects[0]));
	for (int i=1; i<objects.length; i++) 
	    buf.append(objects[i]);

	return buf.toString();
    }

    /**
     * Return the local hostname.
     * @return the local hostname.
     * @throws UnknownHostException if no IP address for the local
     *         host could be found.
     */
    public static String getHostName() throws UnknownHostException {
	 return java.net.InetAddress.getLocalHost().getCanonicalHostName();
    }

    /**
     * Returns a <code>String</code> whose characters, if parsed by a
     * {@link net.jini.config.ConfigurationFile}, would yield a
     * <code>String</code> equivalent to the passed argument.  This is
     * done by replacing CR and LF with their escape codes, quoting
     * '\' and '"' with '\', and enclosing the entire sequence in
     * double quotes. Additionally the tab, form feed, and backspace 
     * characters will be converted to their escape codes and other control
     * characters (besides CR and LF) to octal escapes for better readability
     * if the string is printed for debugging purposes.
     * 
     * @param string the string to turn into a string literal
     * @return a <code>String</code> that if parsed as sequence of 
     *         of characters by <code>ConfigurationFile</code> would 
     *         yield a <code>String</code> equivalent to <code>string</code>
     * @throws NullPointerException if <code>string</code> is 
     * <code>null</code> 
     */
    public static String stringLiteral(String string) {	
	final StringBuffer sb = new StringBuffer(string.length() + 2);
	sb.append('"');

	final char[] ca = string.toCharArray();
	for (int i = 0; i < ca.length; i++) {
	    final char c = ca[i];
	    if (c == '\\' || c == '"')
		sb.append("\\").append(c);
	    else if (c == '\n')
		sb.append("\\n");
	    else if (c == '\r')
		sb.append("\\r");
	    else if (c == '\t')
		sb.append("\\t");
	    else if (c == '\f')
		sb.append("\\f");
	    else if (c == '\b')
		sb.append("\\b");
	    else if (c < 0x20)
		sb.append("\\").append(Integer.toOctalString(c));
	    else 
		sb.append(c);
	}

	return sb.append('"').toString();
    }
}
