KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > filesystem > POIFSDocumentPath


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.poifs.filesystem;
20
21 import java.io.File JavaDoc;
22
23 /**
24  * Class POIFSDocumentPath
25  *
26  * @author Marc Johnson (mjohnson at apache dot org)
27  * @version %I%, %G%
28  */

29
30 public class POIFSDocumentPath
31 {
32     private String JavaDoc[] components;
33     private int hashcode = 0;
34
35     /**
36      * constructor for the path of a document that is not in the root
37      * of the POIFSFileSystem
38      *
39      * @param components the Strings making up the path to a document.
40      * The Strings must be ordered as they appear in
41      * the directory hierarchy of the the document
42      * -- the first string must be the name of a
43      * directory in the root of the POIFSFileSystem,
44      * and every Nth (for N > 1) string thereafter
45      * must be the name of a directory in the
46      * directory identified by the (N-1)th string.
47      * <p>
48      * If the components parameter is null or has
49      * zero length, the POIFSDocumentPath is
50      * appropriate for a document that is in the
51      * root of a POIFSFileSystem
52      *
53      * @exception IllegalArgumentException if any of the elements in
54      * the components parameter
55      * are null or have zero
56      * length
57      */

58
59     public POIFSDocumentPath(final String JavaDoc [] components)
60         throws IllegalArgumentException JavaDoc
61     {
62         if (components == null)
63         {
64             this.components = new String JavaDoc[ 0 ];
65         }
66         else
67         {
68             this.components = new String JavaDoc[ components.length ];
69             for (int j = 0; j < components.length; j++)
70             {
71                 if ((components[ j ] == null)
72                         || (components[ j ].length() == 0))
73                 {
74                     throw new IllegalArgumentException JavaDoc(
75                         "components cannot contain null or empty strings");
76                 }
77                 this.components[ j ] = components[ j ];
78             }
79         }
80     }
81
82     /**
83      * simple constructor for the path of a document that is in the
84      * root of the POIFSFileSystem. The constructor that takes an
85      * array of Strings can also be used to create such a
86      * POIFSDocumentPath by passing it a null or empty String array
87      */

88
89     public POIFSDocumentPath()
90     {
91         this.components = new String JavaDoc[ 0 ];
92     }
93
94     /**
95      * constructor that adds additional subdirectories to an existing
96      * path
97      *
98      * @param path the existing path
99      * @param components the additional subdirectory names to be added
100      *
101      * @exception IllegalArgumentException if any of the Strings in
102      * components is null or zero
103      * length
104      */

105
106     public POIFSDocumentPath(final POIFSDocumentPath path,
107                              final String JavaDoc [] components)
108         throws IllegalArgumentException JavaDoc
109     {
110         if (components == null)
111         {
112             this.components = new String JavaDoc[ path.components.length ];
113         }
114         else
115         {
116             this.components =
117                 new String JavaDoc[ path.components.length + components.length ];
118         }
119         for (int j = 0; j < path.components.length; j++)
120         {
121             this.components[ j ] = path.components[ j ];
122         }
123         if (components != null)
124         {
125             for (int j = 0; j < components.length; j++)
126             {
127                 if ((components[ j ] == null)
128                         || (components[ j ].length() == 0))
129                 {
130                     throw new IllegalArgumentException JavaDoc(
131                         "components cannot contain null or empty strings");
132                 }
133                 this.components[ j + path.components.length ] =
134                     components[ j ];
135             }
136         }
137     }
138
139     /**
140      * equality. Two POIFSDocumentPath instances are equal if they
141      * have the same number of component Strings, and if each
142      * component String is equal to its coresponding component String
143      *
144      * @param o the object we're checking equality for
145      *
146      * @return true if the object is equal to this object
147      */

148
149     public boolean equals(final Object JavaDoc o)
150     {
151         boolean rval = false;
152
153         if ((o != null) && (o.getClass() == this.getClass()))
154         {
155             if (this == o)
156             {
157                 rval = true;
158             }
159             else
160             {
161                 POIFSDocumentPath path = ( POIFSDocumentPath ) o;
162
163                 if (path.components.length == this.components.length)
164                 {
165                     rval = true;
166                     for (int j = 0; j < this.components.length; j++)
167                     {
168                         if (!path.components[ j ]
169                                 .equals(this.components[ j ]))
170                         {
171                             rval = false;
172                             break;
173                         }
174                     }
175                 }
176             }
177         }
178         return rval;
179     }
180
181     /**
182      * calculate and return the hashcode
183      *
184      * @return hashcode
185      */

186
187     public int hashCode()
188     {
189         if (hashcode == 0)
190         {
191             for (int j = 0; j < components.length; j++)
192             {
193                 hashcode += components[ j ].hashCode();
194             }
195         }
196         return hashcode;
197     }
198
199     /**
200      * @return the number of components
201      */

202
203     public int length()
204     {
205         return components.length;
206     }
207
208     /**
209      * get the specified component
210      *
211      * @param n which component (0 ... length() - 1)
212      *
213      * @return the nth component;
214      *
215      * @exception ArrayIndexOutOfBoundsException if n < 0 or n >=
216      * length()
217      */

218
219     public String JavaDoc getComponent(int n)
220         throws ArrayIndexOutOfBoundsException JavaDoc
221     {
222         return components[ n ];
223     }
224
225     /**
226      * <p>Returns the path's parent or <code>null</code> if this path
227      * is the root path.</p>
228      *
229      * @since 2002-01-24
230      * @return path of parent, or null if this path is the root path
231      */

232
233     public POIFSDocumentPath getParent()
234     {
235         final int length = components.length - 1;
236
237         if (length < 0)
238         {
239             return null;
240         }
241         POIFSDocumentPath parent = new POIFSDocumentPath(null);
242
243         parent.components = new String JavaDoc[ length ];
244         System.arraycopy(components, 0, parent.components, 0, length);
245         return parent;
246     }
247
248     /**
249      * <p>Returns a string representation of the path. Components are
250      * separated by the platform-specific file separator.</p>
251      *
252      * @return string representation
253      *
254      * @since 2002-01-24
255      */

256
257     public String JavaDoc toString()
258     {
259         final StringBuffer JavaDoc b = new StringBuffer JavaDoc();
260         final int l = length();
261
262         b.append(File.separatorChar);
263         for (int i = 0; i < l; i++)
264         {
265             b.append(getComponent(i));
266             if (i < l - 1)
267             {
268                 b.append(File.separatorChar);
269             }
270         }
271         return b.toString();
272     }
273 } // end public class POIFSDocumentPath
274

275
Popular Tags