package com.javamark.book.html;

/*
 * Author: Administrator
 * Created: Tuesday, April 16, 2002 10:02:54 PM
 * Modified: Tuesday, April 16, 2002 10:02:54 PM
 */

import java.io.*;

public class PageBreaker
{
	private final String TEST_FILE = "E:\\development\\projects\\bookclean\\test\\book1\\ch01\\0001-0005.html";
	
	private StringBuffer htmlContent = null;
	
	public static void main( String [] args ) throws IOException
	{
		PageBreaker pb = new PageBreaker();
		
		pb.parse();
	}
	
	private void parse()
	{
		htmlContent = new StringBuffer(readObject(TEST_FILE));
		
		split();
	}
	
	private void split()
	{
		String content = htmlContent.toString();
		
		int startTagPos = 0;	
		int endTagPos = 0;
		int findPos = 0;
		boolean inTag = false;
		
		int partID = 0;
		
		while(startTagPos != -1)
		{
			// find the start of a tag
			startTagPos = content.indexOf("<",findPos);
			
			if(startTagPos != -1)
			{
				if(startTagPos > endTagPos +1 )
				{
					// this is the data is exposed here
					System.out.println(partID+ "" + content.substring(endTagPos+1,startTagPos));
					partID++;
					//System.out.println();
				}
				
				if(inTag == false)
				{
					inTag = true;
				}
				
				// ok in the tag
				if(inTag)
				{
					// find the end of the tag
					endTagPos = content.indexOf(">",startTagPos);
					
					if(endTagPos != -1)
					{
						// end of the tag found
						inTag = false;
						// this is the tags are exposed here
						printTag(partID+ "" + content.substring(startTagPos,endTagPos+1));
						partID++;
						findPos = endTagPos;
					}
				}
				
				
				
				//System.out.println(findPos);
				
				
			}
		}
	}
	
	private void printTag(String tag)
	{
		System.out.println(tag);
	}
	
	private String readObject(String objectid)
	{
		int len = 0;
		String objectString = "";
		try
		{
			len = (int)(new File(objectid)).length();
			
			FileInputStream fis = new FileInputStream(objectid);
			
			byte buf[] = new byte[len];
			
			fis.read(buf);
			
			fis.close();
			
			int cnt = 0;
			
			for(int i = 0; i < len; i++)
				if(buf[i] == 10)
					cnt++;
			
			objectString = new String(buf, 0, len);
		}
		catch(IOException e)
		{
			System.err.println("file error " + e);
		}
		return objectString;
	}
}

