//
// CIMOMEscape.java
//
// The contents of this file are subject to the SNIA Public License Version 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://www.snia.org/resources/openSource.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 Original Code is CIMOMEscape.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s):
//
// This source uses the Solaris WBEM APIs version 2.2. 
// Synchronization will be maintained with the Solaris WBEM APIS,
// which are being standardized through the Java Community Process.
//
// $Id: CIMOMEscape.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: CIMOMEscape.java,v $
// Revision 1.1  2003/07/17 18:32:55  kumpf
// HP-BAPU: PEP-64 CIM Client and CIM Listener Java Interface
//
// Revision 1.1.1.1  2001/10/31 18:15:20  karl
// import of SNIA 0_7
//
// Revision 1.2  2001/01/07 16:48:01  davidsi
// - Changed escaping mechanism (again!) so that only single escapes
//   are added.
//
// Revision 1.1  2000/11/06 18:31:40  davidsi
// - Initial version.
//
//
package org.snia.wbemcmd.cimom;


//
public class CIMOMEscape  {
   private static int bufSize=0;
    private static char[] newValue;
    private static Integer syncPoint=new Integer(0);

   /**
    * Escapes quotes and '/'
    * 
    * @param inString
    *   The string to escape
    * @return
    *   The escaped string
    */	
    public static String escape(String inString) {
	int i;
	char[] values;
	int out=0;
	boolean noconv=true;
				 
	if (inString==null)
	    return(null);
	
	synchronized(syncPoint) {
	    values=inString.toCharArray();
	    if (values.length*5+1>bufSize) {
		bufSize=5*values.length+1;
		newValue=new char[bufSize];
	    }

	    for(i=0;i<values.length;i++) {
		char ch=values[i];
	    
		if (ch=='\\') {
		    newValue[out++]='\\';
		    newValue[out++]=ch;
		    noconv=false;
		} else if (ch=='\"') {
		    newValue[out++]='\\';
		    newValue[out++]='\"';
		    noconv=false;
		} else
		    newValue[out++]=ch;
	    }
	    //
	    // If no conversion, then don't build the new string
	    //
	    if (noconv)
		return(inString);
	
	    newValue[out++]='\0';
		return(new String(newValue, 0, out-1));
	}
    }		
	
   /**
    * Unescapes quotes and '/'
    * 
    * @param inString
    *   The string in an escaped form
    * @return
    *   The string with escape sequences removed
    */
    public static String unEscape(String inString) {
	int i;
	char[] values;
	int out=0;
	boolean noconv=true;
				 
	if (inString==null)
	    return(null);
	
	synchronized(syncPoint) {
	    values=inString.toCharArray();
	    if (values.length*5+1>bufSize) {
		bufSize=5*values.length+1;
		newValue=new char[bufSize];
	    }
	    for(i=0;i<values.length;i++) {
		char ch=values[i];
	    
		if (ch=='\\') {
		    if (i+1 < values.length) {
			i++;
			newValue[out++]=values[i];
		    }
		    noconv=false;
		} else
		    newValue[out++]=values[i];
	    }
	    //
	    // If no conversion, then don't build the new string
	    //
	    if (noconv)
		return(inString);
	
	    newValue[out++]='\0';
	    return(new String(newValue, 0, out-1));
	}
    }
}
