1 2 17 18 19 package org.apache.poi.poifs.filesystem; 20 21 import java.io.File ; 22 23 29 30 public class POIFSDocumentPath 31 { 32 private String [] components; 33 private int hashcode = 0; 34 35 58 59 public POIFSDocumentPath(final String [] components) 60 throws IllegalArgumentException 61 { 62 if (components == null) 63 { 64 this.components = new String [ 0 ]; 65 } 66 else 67 { 68 this.components = new String [ 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 ( 75 "components cannot contain null or empty strings"); 76 } 77 this.components[ j ] = components[ j ]; 78 } 79 } 80 } 81 82 88 89 public POIFSDocumentPath() 90 { 91 this.components = new String [ 0 ]; 92 } 93 94 105 106 public POIFSDocumentPath(final POIFSDocumentPath path, 107 final String [] components) 108 throws IllegalArgumentException 109 { 110 if (components == null) 111 { 112 this.components = new String [ path.components.length ]; 113 } 114 else 115 { 116 this.components = 117 new String [ 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 ( 131 "components cannot contain null or empty strings"); 132 } 133 this.components[ j + path.components.length ] = 134 components[ j ]; 135 } 136 } 137 } 138 139 148 149 public boolean equals(final Object 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 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 202 203 public int length() 204 { 205 return components.length; 206 } 207 208 218 219 public String getComponent(int n) 220 throws ArrayIndexOutOfBoundsException 221 { 222 return components[ n ]; 223 } 224 225 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 [ length ]; 244 System.arraycopy(components, 0, parent.components, 0, length); 245 return parent; 246 } 247 248 256 257 public String toString() 258 { 259 final StringBuffer b = new StringBuffer (); 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 } 275 | Popular Tags |