FileDocCategorySizeDatePackage
XUXmlWriter.javaAPI DocAzureus 3.0.3.47368Mon Jul 02 14:00:28 BST 2007org.gudy.azureus2.core3.xml.util

XUXmlWriter

public class XUXmlWriter extends Object
author
parg

Fields Summary
private static final int
INDENT_AMOUNT
private String
current_indent_string
private PrintWriter
writer
private boolean
generic_simple
Constructors Summary
protected XUXmlWriter()

	
	
	
	
		resetIndent();
	
protected XUXmlWriter(OutputStream _output_stream)

		setOutputStream( _output_stream );
		
		resetIndent();
	
Methods Summary
protected voidcloseOutputStream()

		if ( writer != null ){
								
			writer.flush();
					
			writer.close();
			
			writer	= null;
		}
	
protected java.lang.StringencodeBytes(byte[] bytes)

		String data = ByteFormatter.nicePrint( bytes, true );
			
		return( data );

		/*
		try{
		
			return( URLEncoder.encode(new String( bytes, Constants.DEFAULT_ENCODING ), Constants.DEFAULT_ENCODING));
			
		}catch( UnsupportedEncodingException e ){

			throw( new TOTorrentException( 	"TOTorrentXMLSerialiser: unsupported encoding for '" + new String(bytes) + "'",
										TOTorrentException.RT_UNSUPPORTED_ENCODING));
		}
		*/
	
public static java.lang.StringescapeXML(java.lang.String str)

		if ( str == null ){
			
			return( "" );
			
		}
		str = str.replaceAll( "&", "&" );
		str = str.replaceAll( ">", ">" );
		str = str.replaceAll( "<", "<" );
		str = str.replaceAll( "\"", """ );
		str = str.replaceAll( "--", "--" );
		
		return( str );
	
protected voidexdent()

		if ( current_indent_string.length() >= INDENT_AMOUNT ){
		
			current_indent_string = current_indent_string.substring(0,current_indent_string.length()-INDENT_AMOUNT);
		}else{
			
			current_indent_string	= "";
		}
	
protected voidflushOutputStream()

		if ( writer != null ){
								
			writer.flush();					
		}
	
protected java.lang.StringgetUTF(byte[] bytes)

		try{
			return( new String(bytes,Constants.DEFAULT_ENCODING));
			
		}catch( UnsupportedEncodingException e ){
			
			Debug.printStackTrace( e );
			
			return( "" );
		}
	
protected voidindent()

		for (int i=0;i<INDENT_AMOUNT;i++){
		
			current_indent_string += " ";
		}
	
protected voidresetIndent()

		current_indent_string	= "";
	
protected voidsetGenericSimple(boolean simple)

		generic_simple	= simple;
	
protected voidsetOutputStream(java.io.OutputStream _output_stream)

		try{
		
			writer	= new PrintWriter( new OutputStreamWriter(_output_stream , Constants.DEFAULT_ENCODING ));

		}catch( UnsupportedEncodingException e ){
			
			Debug.printStackTrace( e );
			
			writer = new PrintWriter( _output_stream );
		}
	
protected voidsetOutputWriter(java.io.Writer _writer)

		if ( _writer instanceof PrintWriter ){
			
			writer = (PrintWriter)_writer;
			
		}else{
			
			writer = new PrintWriter( _writer );
		}
	
public static java.lang.StringunescapeXML(java.lang.String str)

		if ( str == null ){
			
			return( "" );
			
		}
		str = str.replaceAll( ">", ">" );
		str = str.replaceAll( "<", "<" );
		str = str.replaceAll( """, "\"" );
		str = str.replaceAll( "--", "--" );
		str = str.replaceAll( "&", "&" );

		return( str );
	
protected voidwriteGeneric(java.lang.Object obj)

		if ( obj instanceof Map ){
			
			writeGeneric((Map)obj);
			
		}else if( obj instanceof List ){
			
			writeGeneric((List)obj);
			
		}else if ( obj instanceof byte[] ){
		
			writeGeneric((byte[])obj);
			
		}else{
			
			writeGeneric((Long)obj);
		}
	
protected voidwriteGeneric(java.util.Map map)

		writeLineRaw( "<MAP>" );
		
		try{
			indent();
			
			Iterator it = map.keySet().iterator();
			
			while(it.hasNext()){
				
				String	key = (String)it.next();
				
				writeGenericMapEntry( key, map.get( key ));
			}
		}finally{
			
			exdent();
		}	

		writeLineRaw( "</MAP>" );
	
protected voidwriteGeneric(java.util.List list)

		writeLineRaw( "<LIST>" );
		
		try{
			indent();
			
			for (int i=0;i<list.size();i++){
				
				writeGeneric( list.get(i));
			}
		}finally{
			
			exdent();
		}
		
		writeLineRaw( "</LIST>" );
	
protected voidwriteGeneric(byte[] bytes)

		if ( generic_simple ){
			
			try{
				writeLineRaw( escapeXML( new String(bytes, "UTF-8" )));
				
			}catch( Throwable e ){
				
				e.printStackTrace();
			}
		}else{
			
			writeTag( "BYTES", encodeBytes( bytes ));
		}
	
protected voidwriteGeneric(java.lang.Long l)

		if ( generic_simple ){
			
			writeLineRaw( l.toString());
		
		}else{
			writeTag( "LONG", ""+l );
		}
	
protected voidwriteGenericMapEntry(java.lang.String name, java.lang.Object value)

		if ( generic_simple ){
			
			name = name.replace(' ", '_" ).toUpperCase();
			
			writeLineRaw( "<" + name + ">" );
			
			try{
				indent();
				
				writeGeneric( value );
			}finally{
				
				exdent();
			}
			
			writeLineRaw( "</" + name + ">" );
			
		}else{
			writeLineRaw( "<KEY name=\"" + escapeXML( name ) + "\">");
			
			try{
				indent();
				
				writeGeneric( value );
			}finally{
				
				exdent();
			}
			
			writeLineRaw( "</KEY>");
		}
	
protected voidwriteLineEscaped(java.lang.String str)

		writer.println( current_indent_string + escapeXML(str));
	
protected voidwriteLineRaw(java.lang.String str)

		writer.println( current_indent_string + str );
	
protected voidwriteLocalisableTag(java.lang.String tag, byte[] content)

		boolean	use_bytes = true;
		
		String	utf_string = null;
		
		try{
			utf_string = new String(content,Constants.DEFAULT_ENCODING);
			
			if ( Arrays.equals(
					content,
					utf_string.getBytes( Constants.DEFAULT_ENCODING))){

				use_bytes = false;					
			}
		}catch( UnsupportedEncodingException e ){
		}
		
		writeLineRaw( "<" + tag + " encoding=\""+(use_bytes?"bytes":"utf8") + "\">" + 
					(use_bytes?encodeBytes( content ):escapeXML(utf_string)) + "</" + tag + ">" );	
	
protected voidwriteTag(java.lang.String tag, byte[] content)

		writeLineRaw( "<" + tag + ">" + encodeBytes( content ) + "</" + tag + ">" );	
	
protected voidwriteTag(java.lang.String tag, java.lang.String content)

		writeLineRaw( "<" + tag + ">" + escapeXML( content ) + "</" + tag + ">" );	
	
protected voidwriteTag(java.lang.String tag, long content)

		writeLineRaw( "<" + tag + ">" + content + "</" + tag + ">" );	
	
protected voidwriteTag(java.lang.String tag, boolean content)

		writeLineRaw( "<" + tag + ">" + (content?"YES":"NO") + "</" + tag + ">" );