FileDocCategorySizeDatePackage
LocaleUtil.javaAPI DocAzureus 3.0.3.46801Tue May 30 23:09:48 BST 2006org.gudy.azureus2.core3.internat

LocaleUtil

public class LocaleUtil extends Object
Copyright (C) 2004, 2005, 2006 Aelitis SAS, All rights Reserved This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details ( see the LICENSE file ). You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA AELITIS, SAS au capital de 46,603.30 euros, 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.

Fields Summary
private static final String
systemEncoding
private static final String[]
manual_charset
protected static final String[]
generalCharsets
private static LocaleUtil
singleton
private LocaleUtilDecoder[]
all_decoders
private LocaleUtilDecoder[]
general_decoders
private LocaleUtilDecoder
system_decoder
private LocaleUtilDecoder
fallback_decoder
Constructors Summary
private LocaleUtil()

	List	decoders 		= new ArrayList();
  	List	decoder_names	= new ArrayList();
  	
	for (int i = 0; i < manual_charset.length; i++) {
	   try {
		 String	name = manual_charset[i];
		 
		 CharsetDecoder decoder = Charset.forName(name).newDecoder();
		 
		 if ( decoder != null ){
		 	
			 LocaleUtilDecoder	lu_decoder =  new LocaleUtilDecoderReal(decoders.size(),decoder);
			 
			 decoder_names.add( lu_decoder.getName());
			
			 if ( i == 0 ){
			 	
			 	system_decoder = lu_decoder;
			 }
			 
			 decoders.add( lu_decoder );
			 
		 }else if ( i == 0 ){
		 	
		 	Debug.out( "System decoder failed to be found!!!!" );
		 }
		 
	   }catch (Exception ignore) {
	   }
	 }

	general_decoders = new LocaleUtilDecoder[generalCharsets.length];
	
	for (int i=0;i<general_decoders.length;i++){
		
		int	gi = decoder_names.indexOf( generalCharsets[i]);
		
		if ( gi != -1 ){
		
			general_decoders[i] = (LocaleUtilDecoder)decoders.get(gi);
		}
	}

	boolean show_all = COConfigurationManager.getBooleanParameter("File.Decoder.ShowAll" );

	if ( show_all ){
		
		Map m = Charset.availableCharsets();
	  	
		Iterator it = m.keySet().iterator();
	
		while(it.hasNext()){
	  		
			String	charset_name = (String)it.next();
	  		
			if ( !decoder_names.contains( charset_name)){
	  		
				try {
				  CharsetDecoder decoder = Charset.forName(charset_name).newDecoder();
				 
				  if ( decoder != null ){
				  	
				  	LocaleUtilDecoder	lu_decoder = new LocaleUtilDecoderReal(decoders.size(),decoder);
				  
				  	decoders.add( lu_decoder);
				  
				  	decoder_names.add( lu_decoder.getName());
				  }
				 
				} catch (Exception ignore) {
				}
			}
		}
	}
    
	fallback_decoder = new LocaleUtilDecoderFallback(decoders.size());
	
	decoders.add( fallback_decoder );

	all_decoders	= new LocaleUtilDecoder[ decoders.size()];
	
	decoders.toArray( all_decoders); 
  
Methods Summary
protected java.util.ListgetCandidateDecoders(byte[] array)
Determine which decoders are candidates for handling the supplied type of string

param
array String in a byte array
return
list of possibly valid decoders. LocaleUtilDecoder

  	LocaleUtilDecoderCandidate[] 	candidates = getCandidates( array );
  	
  	List	decoders = new ArrayList();
  	
  	for (int i=0;i<candidates.length;i++){
  	
  		LocaleUtilDecoder	d = candidates[i].getDecoder();
  		
  		if (d != null)
  			decoders.add(d);
  	}
  	
  	return decoders;
  
protected LocaleUtilDecoderCandidate[]getCandidates(byte[] array)
Determine which locales are candidates for handling the supplied type of string

param
array String in an byte array
return
list of candidates. Valid candidates have getDecoder() non-null

	LocaleUtilDecoderCandidate[] candidates = new LocaleUtilDecoderCandidate[all_decoders.length];
    
	boolean show_less_likely_conversions = COConfigurationManager.getBooleanParameter("File.Decoder.ShowLax" );

	for (int i = 0; i < all_decoders.length; i++){
    	
	  candidates[i] = new LocaleUtilDecoderCandidate(i);
      
	  try{
			LocaleUtilDecoder decoder = all_decoders[i];
      	      	
			String str = decoder.tryDecode( array, show_less_likely_conversions );

			if ( str != null ){
				
				candidates[i].setDetails( decoder, str );
			}
	  } catch (Exception ignore) {
      	
	  }
	}
    
	/*
	System.out.println( "getCandidates: = " + candidates.length );
	
	for (int i=0;i<candidates.length;i++){
		
		LocaleUtilDecoderCandidate	cand = candidates[i];
		
		if ( cand != null ){
		
			String	value = cand.getValue();
			
			if ( value != null ){
			
				System.out.println( cand.getDecoder().getName() + "/" + (value==null?-1:value.length()) + "/" + value );
			}
		}  
	}
	*/
	
	return candidates;
  
protected java.util.ListgetCandidatesAsList(byte[] array)

param
array
return
List of LocaleUtilDecoderCandidate

		LocaleUtilDecoderCandidate[] candidates = getCandidates(array);

		List candidatesList = new ArrayList();

		for (int i = 0; i < candidates.length; i++) {
			if (candidates[i].getDecoder() != null)
				candidatesList.add(candidates[i]);
		}

		return candidatesList;
	
public LocaleUtilDecoder[]getDecoders()

  	return( all_decoders );
  
public LocaleUtilDecodergetFallBackDecoder()

  	return fallback_decoder;
  
public LocaleUtilDecoder[]getGeneralDecoders()

	   	return( general_decoders );
	
public static org.gudy.azureus2.core3.internat.LocaleUtilgetSingleton()

  
     
   
   
   	return( singleton );
   
public LocaleUtilDecodergetSystemDecoder()

  	return( system_decoder );
  
public java.lang.StringgetSystemEncoding()

  	return( systemEncoding );