001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.poifs.filesystem;
019:
020: import java.io.File;
021:
022: /**
023: * Class POIFSDocumentPath
024: *
025: * @author Marc Johnson (mjohnson at apache dot org)
026: * @version %I%, %G%
027: */
028:
029: public class POIFSDocumentPath {
030: private String[] components;
031: private int hashcode = 0;
032:
033: /**
034: * constructor for the path of a document that is not in the root
035: * of the POIFSFileSystem
036: *
037: * @param components the Strings making up the path to a document.
038: * The Strings must be ordered as they appear in
039: * the directory hierarchy of the the document
040: * -- the first string must be the name of a
041: * directory in the root of the POIFSFileSystem,
042: * and every Nth (for N > 1) string thereafter
043: * must be the name of a directory in the
044: * directory identified by the (N-1)th string.
045: * <p>
046: * If the components parameter is null or has
047: * zero length, the POIFSDocumentPath is
048: * appropriate for a document that is in the
049: * root of a POIFSFileSystem
050: *
051: * @exception IllegalArgumentException if any of the elements in
052: * the components parameter
053: * are null or have zero
054: * length
055: */
056:
057: public POIFSDocumentPath(final String[] components)
058: throws IllegalArgumentException {
059: if (components == null) {
060: this .components = new String[0];
061: } else {
062: this .components = new String[components.length];
063: for (int j = 0; j < components.length; j++) {
064: if ((components[j] == null)
065: || (components[j].length() == 0)) {
066: throw new IllegalArgumentException(
067: "components cannot contain null or empty strings");
068: }
069: this .components[j] = components[j];
070: }
071: }
072: }
073:
074: /**
075: * simple constructor for the path of a document that is in the
076: * root of the POIFSFileSystem. The constructor that takes an
077: * array of Strings can also be used to create such a
078: * POIFSDocumentPath by passing it a null or empty String array
079: */
080:
081: public POIFSDocumentPath() {
082: this .components = new String[0];
083: }
084:
085: /**
086: * constructor that adds additional subdirectories to an existing
087: * path
088: *
089: * @param path the existing path
090: * @param components the additional subdirectory names to be added
091: *
092: * @exception IllegalArgumentException if any of the Strings in
093: * components is null or zero
094: * length
095: */
096:
097: public POIFSDocumentPath(final POIFSDocumentPath path,
098: final String[] components) throws IllegalArgumentException {
099: if (components == null) {
100: this .components = new String[path.components.length];
101: } else {
102: this .components = new String[path.components.length
103: + components.length];
104: }
105: for (int j = 0; j < path.components.length; j++) {
106: this .components[j] = path.components[j];
107: }
108: if (components != null) {
109: for (int j = 0; j < components.length; j++) {
110: if ((components[j] == null)
111: || (components[j].length() == 0)) {
112: throw new IllegalArgumentException(
113: "components cannot contain null or empty strings");
114: }
115: this .components[j + path.components.length] = components[j];
116: }
117: }
118: }
119:
120: /**
121: * equality. Two POIFSDocumentPath instances are equal if they
122: * have the same number of component Strings, and if each
123: * component String is equal to its coresponding component String
124: *
125: * @param o the object we're checking equality for
126: *
127: * @return true if the object is equal to this object
128: */
129:
130: public boolean equals(final Object o) {
131: boolean rval = false;
132:
133: if ((o != null) && (o.getClass() == this .getClass())) {
134: if (this == o) {
135: rval = true;
136: } else {
137: POIFSDocumentPath path = (POIFSDocumentPath) o;
138:
139: if (path.components.length == this .components.length) {
140: rval = true;
141: for (int j = 0; j < this .components.length; j++) {
142: if (!path.components[j]
143: .equals(this .components[j])) {
144: rval = false;
145: break;
146: }
147: }
148: }
149: }
150: }
151: return rval;
152: }
153:
154: /**
155: * calculate and return the hashcode
156: *
157: * @return hashcode
158: */
159:
160: public int hashCode() {
161: if (hashcode == 0) {
162: for (int j = 0; j < components.length; j++) {
163: hashcode += components[j].hashCode();
164: }
165: }
166: return hashcode;
167: }
168:
169: /**
170: * @return the number of components
171: */
172:
173: public int length() {
174: return components.length;
175: }
176:
177: /**
178: * get the specified component
179: *
180: * @param n which component (0 ... length() - 1)
181: *
182: * @return the nth component;
183: *
184: * @exception ArrayIndexOutOfBoundsException if n < 0 or n >=
185: * length()
186: */
187:
188: public String getComponent(int n)
189: throws ArrayIndexOutOfBoundsException {
190: return components[n];
191: }
192:
193: /**
194: * <p>Returns the path's parent or <code>null</code> if this path
195: * is the root path.</p>
196: *
197: * @since 2002-01-24
198: * @return path of parent, or null if this path is the root path
199: */
200:
201: public POIFSDocumentPath getParent() {
202: final int length = components.length - 1;
203:
204: if (length < 0) {
205: return null;
206: }
207: POIFSDocumentPath parent = new POIFSDocumentPath(null);
208:
209: parent.components = new String[length];
210: System.arraycopy(components, 0, parent.components, 0, length);
211: return parent;
212: }
213:
214: /**
215: * <p>Returns a string representation of the path. Components are
216: * separated by the platform-specific file separator.</p>
217: *
218: * @return string representation
219: *
220: * @since 2002-01-24
221: */
222:
223: public String toString() {
224: final StringBuffer b = new StringBuffer();
225: final int l = length();
226:
227: b.append(File.separatorChar);
228: for (int i = 0; i < l; i++) {
229: b.append(getComponent(i));
230: if (i < l - 1) {
231: b.append(File.separatorChar);
232: }
233: }
234: return b.toString();
235: }
236: } // end public class POIFSDocumentPath
|