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.util.List;
021: import java.util.Iterator;
022: import java.util.ArrayList;
023:
024: /** A virutal file in a virtual file system. JPublish provides a virtual
025: file system as a means for navigating through content hierarchies.
026:
027: @author Anthony Eden
028: @since 1.3
029: */
030:
031: public class VFSFile {
032:
033: private String name;
034: private VFSFile parent;
035: private List children;
036: private boolean directoryFlag;
037: private VFSProvider provider;
038:
039: public VFSFile(String name, VFSFile parent, boolean directoryFlag) {
040: this .name = name;
041: this .parent = parent;
042: this .directoryFlag = directoryFlag;
043: this .children = new ArrayList();
044:
045: if (parent != null) {
046: this .provider = parent.getProvider();
047: }
048: }
049:
050: /** Construct a new VFSFile with the given name and parent.
051:
052: @param name The file name
053: @param parent The file parent
054: @param directoryFlag Set to true if this virtual file is a directory
055: */
056:
057: public VFSFile(String name, VFSFile parent, boolean directoryFlag,
058: VFSProvider provider) {
059: this .name = name;
060: this .parent = parent;
061: this .directoryFlag = directoryFlag;
062: this .children = new ArrayList();
063: this .provider = provider;
064: }
065:
066: /** Get the file name.
067:
068: @return The file name
069: */
070:
071: public String getName() {
072: return name;
073: }
074:
075: /** Get the file's parent directory. This method may return null if the
076: file is the root.
077:
078: @return The virtual file's parent directory
079: */
080:
081: public VFSFile getParent() {
082: return parent;
083: }
084:
085: /** Get a List of all children.
086:
087: @return All children
088: */
089:
090: public List getChildren() {
091: return children;
092: }
093:
094: /** Return true if this virutal file represents a directory.
095:
096: @return True if this virtual file is a directory
097: */
098:
099: public boolean isDirectory() {
100: return directoryFlag;
101: }
102:
103: /** Get the Virtual File System provider.
104:
105: @return The VFSProvider
106: */
107:
108: public VFSProvider getProvider() {
109: return provider;
110: }
111:
112: /** Return a String representation of the file which is the full path
113: for the file.
114:
115: @return The path String
116: */
117:
118: public String toPathString() {
119: StringBuffer buffer = new StringBuffer();
120:
121: VFSFile currentParent = parent;
122:
123: if (currentParent == null) {
124: return "/";
125: }
126:
127: while (currentParent != null) {
128: if (currentParent.getParent() != null) {
129: buffer.insert(0, "/");
130: buffer.insert(0, currentParent.getName());
131: }
132:
133: currentParent = currentParent.getParent();
134: }
135:
136: // insert the root
137: buffer.insert(0, "/");
138:
139: // append the file name
140: buffer.append(getName());
141:
142: if (isDirectory()) {
143: buffer.append("/");
144: }
145:
146: return buffer.toString();
147: }
148:
149: /** Get an Iterator which provides a means for iterating through
150: each path part in the file's path.
151:
152: @return The path Iterator
153: */
154:
155: public Iterator getPathIterator() {
156: VFSFile currentDirectory = null;
157: ArrayList path = new ArrayList();
158: if (isDirectory()) {
159: currentDirectory = this ;
160: } else {
161: currentDirectory = getParent();
162: }
163:
164: while (currentDirectory != null) {
165: path.add(0, currentDirectory);
166: currentDirectory = currentDirectory.getParent();
167: }
168:
169: return path.iterator();
170: }
171:
172: /** Return a String representation of the virtual file.
173:
174: @return A String
175: */
176:
177: public String toString() {
178: return toPathString();
179: }
180:
181: }
|