/*******************************************************************************
 * Copyright 2014 Centre for Development of Advanced Computing(C-DAC), Pune
 * 
 * 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 in.cdac.medinfo.csnotk.csnolib.util;

/**
 * Provides semantic tag details .
 *
 */
public class SemanticTagBuilder{
	private static StringBuilder output=new StringBuilder();
	private static char[] arr = null;
	private static int i = 0;
	private static char c;
	private static String semanticTag=null;
	/**
	 * Function to retrieve the Semantic Tag/hierarchy information from the 
	 * FSN of the concept.
	 * @param fsn
	 * 			 FSN or Fully Specified Name.
	 * @return the Semantic Tag/hierarchy {@link String} of the concept.
	 */
	public static String buildSemanticTagFromString(String fsn){		
		if(null == fsn)
			return null;
		if(output==null)
			output=new StringBuilder();
		else
			if(output.length()>0)
				output.delete(0,output.length());
		arr = fsn.toCharArray();
		i = arr.length - 2;
		c = arr[i];	
		while (c != '(' && i  != 0){
			output.append(c);
			c = arr[--i];
		}	
		
		semanticTag=output.reverse().toString().toLowerCase();
		if(semanticTag.contains(")"))
			if(semanticTag.indexOf(")")==semanticTag.length()-1)
				semanticTag=semanticTag.substring(0,semanticTag.length()-1);		
		return semanticTag;
	}
}
