FileDocCategorySizeDatePackage
HTMLChunkImpl.javaAPI DocAzureus 3.0.3.44896Thu Feb 09 19:43:12 GMT 2006org.gudy.azureus2.core3.html.impl

HTMLChunkImpl

public class HTMLChunkImpl extends Object
author
parg

Fields Summary
String
content
Constructors Summary
protected HTMLChunkImpl()

	
protected HTMLChunkImpl(String _content)

		content	= _content;
	
Methods Summary
public java.lang.StringgetContent()

		return( content );
	
public java.lang.String[]getLinks()

		int	pos	= 0;

		List	res = new ArrayList();
		
		while(true){
			
			int	p1 = content.indexOf( "<", pos );
			
			if ( p1 == -1 ){
				
				break;
			}
			
			p1++;
			
			int	p2 = content.indexOf( ">", p1 );
			
			if ( p2 == -1 ){
				
				break;
			}
			
			pos	= p2;
			
			String	tag 	= content.substring( p1, p2 ).trim();
			
			String	lc_tag 	= tag.toLowerCase();
						
			if ( lc_tag.startsWith("a " )){
				
				int	hr_start = lc_tag.indexOf( "href");
				
				if ( hr_start == -1 ){
					
					continue;
				}
				
				hr_start = lc_tag.indexOf("=", hr_start);
				
				if ( hr_start == -1 ){
					
					continue;
				}
				
				hr_start += 1;
				
				while( 	hr_start < lc_tag.length() &&
						Character.isWhitespace(lc_tag.charAt(hr_start))){
					
					hr_start++;
				}
				
				int hr_end = hr_start;
				
				while(	hr_end < lc_tag.length() &&
						!Character.isWhitespace(lc_tag.charAt(hr_end))){
										
					hr_end++;
				}
				
				String	href = tag.substring(hr_start, hr_end ).trim();
				
				if ( href.startsWith("\"")){
					
					href = href.substring(1,href.length()-1);
				}
				
				res.add( href );
			}
		}
		
		String[]	res_array = new String[res.size()];
		
		res.toArray( res_array );
		
		return( res_array );
	
public HTMLTable[]getTables()

		String[]	tables = getTagPairContent( "table" );
		
		HTMLTable[]	res = new HTMLTable[tables.length];
		
		for (int i=0;i<tables.length;i++){
			
			res[i] = new HTMLTableImpl( tables[i] );
		}
		
		return( res );
	
public java.lang.String[]getTagPairContent(java.lang.String tag_name)

		tag_name = tag_name.toLowerCase();
		
		String	lc_content = content.toLowerCase();
		
		int	pos	= 0;

		List	res = new ArrayList();
		
		int	level 		= 0;
		int	start_pos	= -1;
		
		while(true){
			
			int	start_tag_start = lc_content.indexOf( "<" + tag_name,  pos );
			int end_tag_start	= lc_content.indexOf( "</" + tag_name, pos );
			
			if ( level == 0 ){
				
				if ( start_tag_start == -1 ){
					
					break;
				}
				
				start_pos = start_tag_start;
				
				level	= 1;
				
				pos		= start_pos+1;
				
			}else{
				
				if ( end_tag_start == -1 ){
					
					break;
				}
				
				if ( start_tag_start == -1 || end_tag_start < start_tag_start ){
					
					if ( level == 1 ){
						
						String	tag_contents = content.substring( start_pos + tag_name.length() + 1, end_tag_start );
						
						res.add( tag_contents );
						
						// System.out.println( "got tag:" + tag_contents );						
					}
					
					level--;
					
					pos	= end_tag_start + 1;
					
				}else{
					
					if ( start_tag_start == -1 ){
						
						break;
					}
					
					level++;
					
					pos = start_tag_start+1;
				}
			}
		}
		
		String[]	res_array = new String[res.size()];
		
		res.toArray( res_array );
		
		return( res_array );
	
public java.lang.String[]getTags(java.lang.String tag_name)
this just returns the tags themselves.

param
tag
return

		tag_name = tag_name.toLowerCase();
							
		String	lc_content = content.toLowerCase();
							
		int	pos	= 0;

		List	res = new ArrayList();
														
		while(true){
			
			int	p1 = lc_content.indexOf( "<" + tag_name,  pos );
			
			if ( p1 == -1 ){
				
				break;
			}
			
			int	p2 = lc_content.indexOf( ">", p1 );
			
			if ( p2 == -1 ){
				
				break;
			}

			res.add( content.substring( p1+1, p2 ));
			
			pos	= p2+1;
		}
		
		String[]	x = new String[res.size()];
		
		res.toArray( x );
		
		return( x );
	
protected voidsetContent(java.lang.String str)

		content	= str;