krysalis-update/src/java/org/krysalis/depot/update/util/io ResolvedFile.java,NONE,1.1 VfsUtils.java,NONE,1.1 HtmlUtils.java,NONE,1.1 FileUtils.java,NONE,1.1
Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:34 +0000
| Newsgroups | gmane.comp.krysalis.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/util/io
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/util/io
Added Files:
ResolvedFile.java VfsUtils.java HtmlUtils.java FileUtils.java
Log Message:
Copied from the apache incubator.
--- NEW FILE: ResolvedFile.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.util.io;
import java.io.File;
import org.krysalis.depot.common.util.SystemUtils;
/**
* The ResolvedFile adds some attributes to the File class. Basically the resolverContext (most cases
* a directory where the fly is) and the originalInput (most cases the file).
*
* @author <a href="http://incubator.apache.org/depot">The Apache Incubator Depot Project</a>
*/
public class ResolvedFile extends File {
private Object m_originalInput = null;
private Object m_resolverContext = null;
private ResolvedFile(Object resolverContext, Object originalInput,
File fullyQualified) {
super(fullyQualified.getAbsolutePath());
m_resolverContext = resolverContext;
m_originalInput = originalInput;
}
//
// Resolve in JVM 'current working directory' context.
//
public static ResolvedFile resolve(Object file) {
return resolve(SystemUtils.getCWD(), file);
}
public static ResolvedFile resolve(Object resolverContext, Object file) {
// Previously resolved within this context...
if ((file instanceof ResolvedFile)
&& (((ResolvedFile) file).m_resolverContext.equals(resolverContext)))
return (ResolvedFile) file;
// Extract a 'path' (local or absolute) from this thing
String path = null;
if (file instanceof File)
path = ((File) file).getPath();
else if (file instanceof String)
path = (String) file;
else {
throw new IllegalArgumentException("Unable to resolve "
+ file.getClass().getName());
}
File pathFile = new File(path);
// Get a fully qualified file
File fullyQualified = null;
if (pathFile.isAbsolute()) {
fullyQualified = pathFile;
} else {
if (resolverContext instanceof File)
if (".".equals(path)) {
fullyQualified = (File) resolverContext;
} else
fullyQualified = new File((File) resolverContext, path);
else if (resolverContext instanceof String)
if (".".equals(path)) {
fullyQualified = new File((String) resolverContext);
} else
fullyQualified = new File(new File(
(String) resolverContext), path);
else {
throw new IllegalArgumentException(
"Unable to resolve using context "
+ resolverContext.getClass().getName());
}
}
return new ResolvedFile(resolverContext, file, fullyQualified);
}
/**
* @return Returns the originalInput.
*/
public Object getOriginalInput() {
return m_originalInput;
}
/**
* @return Returns the resolverContext.
*/
public Object getResolverContext() {
return m_resolverContext;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(m_resolverContext.getClass().toString());
buf.append(" [");
buf.append(m_resolverContext.toString());
buf.append("] : ");
buf.append(m_originalInput.getClass().toString());
buf.append(" [");
buf.append(m_originalInput.toString());
buf.append("] : ");
buf.append(super.toString());
return buf.toString();
}
}
--- NEW FILE: FileUtils.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.krysalis.depot.update.util.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import org.krysalis.depot.common.util.io.IOUtils;
import org.krysalis.depot.update.UpdateRuntimeException;
import org.krysalis.depot.update.util.UpdateConstants;
public class FileUtils {
// for toURI
private static boolean[] isSpecial = new boolean[256];
private static char[] escapedChar1 = new char[256];
private static char[] escapedChar2 = new char[256];
// stolen from FilePathToURI of the Xerces-J team
static {
for (int i = 0; i <= 0x20; i++) {
isSpecial[i] = true;
escapedChar1[i] = Character.forDigit(i >> 4, 16);
escapedChar2[i] = Character.forDigit(i & 0xf, 16);
}
isSpecial[0x7f] = true;
escapedChar1[0x7f] = '7';
escapedChar2[0x7f] = 'F';
char[] escChs = {'<', '>', '#', '%', '"', '{', '}', '|', '\\', '^',
'~', '[', ']', '`'};
int len = escChs.length;
char ch;
for (int i = 0; i < len; i++) {
ch = escChs[i];
isSpecial[ch] = true;
escapedChar1[ch] = Character.forDigit(ch >> 4, 16);
escapedChar2[ch] = Character.forDigit(ch & 0xf, 16);
}
}
/**
* Create a URL for a File path.
*
* @param file
* @return
*/
public static URL getFileURL(File file) {
URL url = null;
String str = null;
try {
str = toURI(file.getAbsolutePath());
url = new URL(str);
} catch (MalformedURLException e) {
//:TODO: Ought never happen, so it is a bug on
// us ..
throw new UpdateRuntimeException("Bogus: " + file + " ? " + str, e);
}
return url;
}
/**
* Create a URI for a String file path
*
* @param path
* @return
*/
public static String toURI(String path) {
return toURI(new File(path));
}
public static String toURI(File path) {
boolean isDir = path.isDirectory();
String pathString = path.getAbsolutePath();
StringBuffer sb = new StringBuffer(UpdateConstants.FILE_PREFIX);
//
// Make and DOS \ into /
//
pathString = pathString.replace('\\', '/');
//
// Escaping..
//
CharacterIterator iter = new StringCharacterIterator(pathString);
for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
if (isSpecial[c]) {
sb.append('%');
sb.append(escapedChar1[c]);
sb.append(escapedChar2[c]);
} else {
sb.append(c);
}
}
//
// Ensure directories end with a /
//
if (isDir && !pathString.endsWith("/")) {
sb.append('/');
}
return sb.toString();
}
/**
* Transfer all the data from one file to file. Does NOT delete the output
* file on exception.
*
* @param inputFile
* @param outputFile
* @throws IOException
*/
public static void transfer(File inputFile, File outputFile)
throws IOException {
FileInputStream input = null;
try {
input = new FileInputStream(inputFile);
FileOutputStream output = null;
try {
output = new FileOutputStream(outputFile);
IOUtils.transfer(input, output);
} finally {
IOUtils.close(output);
}
} finally {
IOUtils.close(input);
}
}
}
--- NEW FILE: VfsUtils.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.util.io;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.commons.vfs.AllFileSelector;
import org.apache.commons.vfs.Capability;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSelector;
import org.apache.commons.vfs.FileSystem;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.FileType;
import org.apache.commons.vfs.impl.DefaultFileSystemManager;
import org.apache.commons.vfs.impl.StandardFileSystemManager;
/**
* @author ajack
*/
/**
*
* @author $Author: chalko $
* @version $Revision: 1.1 $
*
*/
public class VfsUtils {
private static FileSystemManager l_defaultFsm = null;
private static FileSelector l_defaultAllFileSelector = null;
static {
l_defaultAllFileSelector = new AllFileSelector();
}
public static FileSystemManager createFSM(String name, String base)
throws FileSystemException {
DefaultFileSystemManager fsm = new StandardFileSystemManager();
// Avoid CL (except at runtime)
// fsm.setLogger(LogFactory.getLog(name));
fsm.init();
// Set Base Directory for Resolving Relative To...
fsm.setBaseFile(new java.io.File(base));
return fsm;
}
/**
* Create the Default FSM (if it does not exist)
* @return
* @throws FileSystemException
*/
private static FileSystemManager createDefaultFSM()
throws FileSystemException {
if (null == l_defaultFsm) {
l_defaultFsm = createFSM("Depot::FileSystemManager", ".");
}
return l_defaultFsm;
}
/**
* Get (or create) the Default FSM
* @return
* @throws FileSystemException
*/
public static FileSystemManager getDefaultFSM()
throws FileSystemException {
if (l_defaultFsm == null) {
createDefaultFSM();
}
return l_defaultFsm;
}
/**
*
* Resolve a URL within the scope of this FSM
* @param url
* @return
* @throws Exception
*/
public static FileObject resolveUrl(String url) throws Exception {
return getDefaultFSM().resolveFile(url);
}
/**
*
* Convert a File to a FileObject within the scope of this FSM
* @param file
* @return
* @throws Exception
*/
public static FileObject toFileObject(File file) throws Exception {
return getDefaultFSM().toFileObject(file);
}
public static List getChildren(FileObject folder)
throws IOException, MalformedURLException, FileSystemException {
List result = new ArrayList();
if (folder.getFileSystem().hasCapability(Capability.LIST_CHILDREN)) {
if (folder.getType() == FileType.FILE) {
result.add(folder);
}
else {
FileObject array[] = folder.getChildren();
for (int i = 0; i < array.length; ++i)
result.add(array[i]);
}
}
else if ("http".equals(folder.getURL().getProtocol())) {
ArrayList list = new ArrayList();
InputStream urlStream = null;
//Logger.getLogger().debug("Extract HTML Links from VFS");
// try opening the URL
urlStream = null;
try {
urlStream = folder.getContent().getInputStream();
if (null != urlStream) {
String type =
URLConnection.guessContentTypeFromStream(urlStream);
if ((type == null) || !type.startsWith("text/html")) {
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();
// Filter out dynamics... and mail
if ((strLink.length() > 0)
&& (strLink.indexOf("?") == -1)
&& (!strLink.startsWith("mailto:"))) {
//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;
}
//if (added)
// Logger.getLogger().debug(
// "Selected : " + strLink);
}
}
}
}
result = list;
}
}
}
finally {
if (null != urlStream) {
try {
urlStream.close();
}
catch (Exception ce){
}
}
}
}
return result;
}
/**
*
* Unarchive a file (if we can, i.e. JAR or ZIP)using default FSM
*
* @param input
* @return
* @throws FileSystemException
*/
public static FileObject unarchive(FileObject input)
throws FileSystemException {
return unarchive(getDefaultFSM(), input);
}
/**
*
* Unarchive a file (if we can, i.e. JAR or ZIP) using a specified FSM
*
* NOTE: This only returns a referece to the contents, one has to copy
* it to another location to get a real unarchive.
*
* @param input
* @return
* @throws FileSystemException
*/
public static FileObject unarchive(FileSystemManager fsm, FileObject input)
throws FileSystemException {
FileObject temp = null;
FileObject output = null;
String basename = input.getName().getBaseName();
if (FileType.FOLDER != input.getType()) {
if (basename.endsWith(".jar")) {
basename = basename.substring(0, basename.length() - 4);
temp = fsm.createFileSystem("jar", input);
output = temp.resolveFile(basename);
}
else if (basename.endsWith(".zip")) {
basename = basename.substring(0, basename.length() - 4);
temp = fsm.createFileSystem("zip", input);
output = temp.resolveFile(basename);
}
}
/*
* NOTE: This only returns a referece to the contents, one has to copy
* it to another location to get a real unarchive.
*/
if (null == output)
output = input;
return output;
}
/**
*
* Copy to destination from origin, and select all files.
*
* @param destination
* @param origin
* @throws FileSystemException
*/
public static void copyFrom(FileObject destination, FileObject origin)
throws FileSystemException {
VfsUtils.copyFrom(destination, origin, l_defaultAllFileSelector);
}
/**
*
* Copy to destination from origin, and select which files.
*
* @param destination
* @param origin
* @param selector
* @throws FileSystemException
*/
public static void copyFrom(
FileObject destination,
FileObject origin,
FileSelector selector)
throws FileSystemException {
destination.copyFrom(origin, selector);
}
public static void delete(FileObject deletee) throws FileSystemException {
deletee.delete(l_defaultAllFileSelector);
}
//
// :TODO: not working -- gives some odd file names when a file comes from a jar
//
public static File toFile(FileObject fileObject)
throws FileSystemException {
File resultFile = null;
if (null != fileObject) {
FileSystem fileSystem = fileObject.getFileSystem();
resultFile =
fileSystem.replicateFile(fileObject, l_defaultAllFileSelector);
//DepotLogger.g_log.warn("toFile: " + resultFile);
}
return resultFile;
}
public static void main(String args[]) throws Exception {
String url = "http://metamorphosis.apache.org/version/samples/";
if (0 < args.length)
url = args[0];
FileSystemManager fsm = VfsUtils.getDefaultFSM();
FileObject folder = fsm.resolveFile(url);
//if ( !folder.exists() )
// System.out.println("Bad Folder : " + folder);
List kids = VfsUtils.getChildren(folder);
for (Iterator i = kids.iterator(); i.hasNext();) {
FileObject kid = (FileObject) i.next();
System.out.println("Child [" + i + "] -> " + kid);
}
}
}
--- NEW FILE: HtmlUtils.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.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.depot.common.log.Logger;
import org.krysalis.depot.update.util.net.VirtualResourceLocator;
import org.krysalis.depot.update.util.net.select.ChildVrlSelector;
import org.krysalis.depot.update.util.net.select.MatchingVrlSelector;
import org.krysalis.depot.update.util.select.ISelector;
/**
* @author ajack
*/
public class HtmlUtils {
/**
* Extract the child URLs from a content stream
*
* @param parent
* @param contentStream
* @return
* @throws IOException
*/
public static List getChildren(
VirtualResourceLocator parent,
InputStream contentStream)
throws IOException {
// 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);
}
/**
* Extract the child URLs from a content string
*
* @param parent
* @param content
* @return
* @throws IOException
*/
public static List getChildren(
VirtualResourceLocator parent,
String content)
throws IOException {
return getLinks(parent, content, new ChildVrlSelector(parent));
}
/**
* Get all links matching this pattern.
*
* @param parent
* @param content
* @param pattern
* @return
* @throws IOException
*/
public static List getMatching(
VirtualResourceLocator parent,
String content,
String pattern)
throws IOException {
return getLinks(parent, content, new MatchingVrlSelector(pattern));
}
/**
* Get all links matching this selector.
*
* @param parent
* @param content
* @param pattern
* @return
* @throws IOException
*/
public static List getLinks(
VirtualResourceLocator parent,
String content,
ISelector selector)
throws IOException {
List children = new ArrayList();
Logger.getLogger().debug("Extract HTML Links Selector : " + selector);
// Dro pthe case everywhere, to simplify
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;
Logger.getLogger().debug("Link : " + strLink);
VirtualResourceLocator v =
new VirtualResourceLocator(parent, strLink);
try {
if (selector.select(v)) {
//
// Store another child, no dups...
//
if (!children.contains(v)) {
children.add(v);
added = true;
}
if (added)
Logger.getLogger().debug(
"Selected : " + strLink);
}
}
catch (Exception e) {
// :TODO: ...
}
}
}
return children;
}
}
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/