001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017:
018: package org.jpublish.util.vfs;
019:
020: import java.io.FileNotFoundException;
021: import java.util.List;
022: import java.util.Vector;
023: import java.util.Iterator;
024: import java.util.Hashtable;
025: import java.util.StringTokenizer;
026:
027: /** Utilities which can be used with the JPublish Virtual File System.
028:
029: @author Anthony Eden
030: @since 1.3
031: */
032:
033: public class VFSUtilities {
034:
035: private VFSUtilities() {
036: // no op
037: }
038:
039: public static VFSFile findRoot(VFSFile file) {
040: VFSFile parent = file.getParent();
041: while (parent != null) {
042: file = parent;
043: parent = file.getParent();
044: }
045: return file;
046: }
047:
048: public static VFSFile pathToFile(String path, VFSFile root)
049: throws FileNotFoundException {
050: if (path.equals("/")) {
051: return root;
052: }
053:
054: VFSFile parent = root;
055:
056: StringTokenizer tk = new StringTokenizer(path, "\\/");
057: while (tk.hasMoreTokens()) {
058: String pathPart = tk.nextToken();
059: Iterator children = parent.getChildren().iterator();
060: while (children.hasNext()) {
061: VFSFile file = (VFSFile) children.next();
062: if (file.getName().equals(pathPart)) {
063: if (tk.hasMoreTokens()) {
064: parent = file;
065: } else {
066: return file;
067: }
068: }
069: }
070: }
071:
072: if (path.endsWith("/")) {
073: return parent;
074: } else {
075: throw new FileNotFoundException("Cannot find the file: "
076: + path);
077: }
078: }
079:
080: public static Hashtable vfsToHash(VFSFile file) {
081: //log.info("Converting VFS to Hashtable");
082: //log.info("VFS Root: " + file);
083:
084: Hashtable fileHash = new Hashtable();
085: fileHash.put("name", file.getName());
086: fileHash.put("isDirectory", new Boolean(file.isDirectory()));
087:
088: Vector childVector = new Vector();
089: Iterator children = file.getChildren().iterator();
090: while (children.hasNext()) {
091: childVector
092: .addElement(vfsToHash((VFSFile) children.next()));
093: }
094: fileHash.put("children", childVector);
095:
096: //log.info("Returning file as hashtable");
097:
098: return fileHash;
099: }
100:
101: public static VFSFile hashToVFS(Hashtable hash) {
102: return hashToVFS(hash, null);
103: }
104:
105: protected static VFSFile hashToVFS(Hashtable hash, VFSFile parent) {
106: //log.info("hashToVFS()");
107: String name = (String) hash.get("name");
108: boolean isDirectory = ((Boolean) hash.get("isDirectory"))
109: .booleanValue();
110:
111: //log.debug("Creating VFSFile");
112: VFSFile file = new VFSFile(name, parent, isDirectory);
113:
114: List children = file.getChildren();
115: Iterator iter = ((Vector) hash.get("children")).iterator();
116: while (iter.hasNext()) {
117: VFSFile child = hashToVFS((Hashtable) iter.next(), file);
118: children.add(child);
119: }
120:
121: //log.info("Returning file: " + file);
122: return file;
123:
124: }
125:
126: }
|