ruper2/src/java/core/org/krysalis/ruper2/util/io HtmlUtils.java,NONE,1.1 VfsUtils.java,1.1,1.2

[email protected]
Newsgroups gmane.comp.krysalis.metamorphosis.cvs
Message-ID <[email protected]>
Update of /cvsroot/metamorphosis/ruper2/src/java/core/org/krysalis/ruper2/util/io
In directory sc8-pr-cvs1:/tmp/cvs-serv16855/src/java/core/org/krysalis/ruper2/util/io

Modified Files:
	VfsUtils.java 
Added Files:
	HtmlUtils.java 
Log Message:
1) Monitoring (so we can sniff out failures in testing, to improve)
2) First step in removing VFS...

--- NEW FILE: HtmlUtils.java ---
/*
 * Created on Oct 9, 2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.krysalis.ruper2.util.io;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.krysalis.ruper2.util.net.VirtualResourceLocator;

/**
 * @author ajack
 */
public class HtmlUtils {

	public static List getChildren(
		VirtualResourceLocator parent,
		InputStream contentStream)
		throws IOException {
		List children = new ArrayList();

		// search the input stream for links
		// first, read in the entire URL
		byte b[] = new byte[1000];
		int numRead = contentStream.read(b);
		String content = new String(b, 0, numRead);
		while (numRead != -1) {
			numRead = contentStream.read(b);
			if (numRead != -1) {
				String newContent = new String(b, 0, numRead);
				content += newContent;
			}
		}

		return getChildren(parent, content);
	}

	public static List getChildren(
		VirtualResourceLocator parent,
		String content)
		throws IOException {
		List children = new ArrayList();

		String lowerCaseContent = content.toLowerCase();
		int index = 0;
		while ((index = lowerCaseContent.indexOf("<a", index)) != -1) {
			if ((index = lowerCaseContent.indexOf("href", index)) == -1) {
				break;
			}
			if ((index = lowerCaseContent.indexOf("=", index)) == -1) {
				break;
			}
			index++;
			String remaining = content.substring(index);
			StringTokenizer st = new StringTokenizer(remaining, "\t\n\r\">#");
			String strLink = st.nextToken();

			if ((strLink.length() > 0)
				&& (strLink.indexOf("?") == -1)
				&& (!"/".equals(strLink))) {
				boolean added = false;

				//System.out.println("Link          : " + strLink);

				VirtualResourceLocator v =
					new VirtualResourceLocator(parent, strLink);

				//System.out.println("Possible Child: " + v);

				//
				// Check that the parent of the child is this
				//
				if (v.getParent().equals(parent)) {

					//
					// Store another child, no dups...
					//
					if (!children.contains(v)) {
						children.add(v);
						added = true;
						//System.out.println("Accepted Child: " + v);
					}
				}
			}
		}

		return children;
	}
}

Index: VfsUtils.java
===================================================================
RCS file: /cvsroot/metamorphosis/ruper2/src/java/core/org/krysalis/ruper2/util/io/VfsUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** VfsUtils.java	29 Sep 2003 18:09:24 -0000	1.1
--- VfsUtils.java	9 Oct 2003 20:30:26 -0000	1.2
***************
*** 177,252 ****
  			ArrayList list = new ArrayList();
  			InputStream urlStream = null;
  			// try opening the URL
! 			urlStream = folder.getContent().getInputStream();
! 			
! 			if (null != urlStream) {
! 				String type =
! 					URLConnection.guessContentTypeFromStream(urlStream);
  
! 				if ((type == null) || (type.compareTo("text/html") != 0)) {
! 					result.add(folder);
! 				}
! 				else {
! 					// search the input stream for links
! 					// first, read in the entire URL
! 					byte b[] = new byte[1000];
! 					int numRead = urlStream.read(b);
! 					String content = new String(b, 0, numRead);
! 					while (numRead != -1) {
! 						numRead = urlStream.read(b);
! 						if (numRead != -1) {
! 							String newContent = new String(b, 0, numRead);
! 							content += newContent;
! 						}
! 					}
! 					urlStream.close();
  
! 					String lowerCaseContent = content.toLowerCase();
! 					int index = 0;
! 					while ((index = lowerCaseContent.indexOf("<a", index))
! 						!= -1) {
! 						if ((index = lowerCaseContent.indexOf("href", index))
! 							== -1) {
! 							break;
! 						}
! 						if ((index = lowerCaseContent.indexOf("=", index))
! 							== -1) {
! 							break;
  						}
- 						index++;
- 						String remaining = content.substring(index);
- 						StringTokenizer st =
- 							new StringTokenizer(remaining, "\t\n\r\">#");
- 						String strLink = st.nextToken();
  
! 						if ((strLink.length() > 0)
! 							&& (strLink.indexOf("?") == -1)) {
! 							boolean added = false;
! 							FileObject f = folder.resolveFile(strLink);
  
! 							//
! 							// Check not ".."
! 							//
! 							if (!f.equals(folder.getParent())) {
  
  								//
! 								// Check that the parent of the child is this
  								//
! 								if (f.getParent().equals(folder)) {
  
  									//
! 									// Store another child, no dups...
  									//
! 									if (!list.contains(f)) {
! 										list.add(f);
! 										added = true;
  									}
  								}
- 							}
  
  						}
- 					}
  
! 					result = list;
  				}
  			}
--- 177,267 ----
  			ArrayList list = new ArrayList();
  			InputStream urlStream = null;
+ 
  			// try opening the URL
! 			urlStream = null;
  
! 			try {
! 				urlStream = folder.getContent().getInputStream();
  
! 				if (null != urlStream) {
! 					String type =
! 						URLConnection.guessContentTypeFromStream(urlStream);
! 
! 					if ((type == null) || (type.compareTo("text/html") != 0)) {
! 						result.add(folder);
! 					}
! 					else {
! 						// search the input stream for links
! 						// first, read in the entire URL
! 						byte b[] = new byte[1000];
! 						int numRead = urlStream.read(b);
! 						String content = new String(b, 0, numRead);
! 						while (numRead != -1) {
! 							numRead = urlStream.read(b);
! 							if (numRead != -1) {
! 								String newContent = new String(b, 0, numRead);
! 								content += newContent;
! 							}
  						}
  
! 						String lowerCaseContent = content.toLowerCase();
! 						int index = 0;
! 						while ((index = lowerCaseContent.indexOf("<a", index))
! 							!= -1) {
! 							if ((index =
! 								lowerCaseContent.indexOf("href", index))
! 								== -1) {
! 								break;
! 							}
! 							if ((index = lowerCaseContent.indexOf("=", index))
! 								== -1) {
! 								break;
! 							}
! 							index++;
! 							String remaining = content.substring(index);
! 							StringTokenizer st =
! 								new StringTokenizer(remaining, "\t\n\r\">#");
! 							String strLink = st.nextToken();
  
! 							if ((strLink.length() > 0)
! 								&& (strLink.indexOf("?") == -1)) {
! 								boolean added = false;
! 								FileObject f = folder.resolveFile(strLink);
  
  								//
! 								// Check not ".."
  								//
! 								if (!f.equals(folder.getParent())) {
  
  									//
! 									// Check that the parent of the child is this
  									//
! 									if (f.getParent().equals(folder)) {
! 
! 										//
! 										// Store another child, no dups...
! 										//
! 										if (!list.contains(f)) {
! 											list.add(f);
! 											added = true;
! 										}
  									}
  								}
  
+ 							}
  						}
  
! 						result = list;
! 					}
! 				}
! 			}
! 			finally {
! 				if (null != urlStream) {
! 					try {
! 						urlStream.close();
! 					}
! 					catch (Exception ce){
! 						
! 					}
  				}
  			}




-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.