1 16 package org.apache.cocoon.bean; 17 18 import java.util.TreeMap ; 19 20 import org.apache.cocoon.Constants; 21 import org.apache.cocoon.util.MIMEUtils; 22 import org.apache.cocoon.util.NetUtils; 23 import org.apache.cocoon.ProcessingException; 24 25 34 public class Target { 35 private static final String APPEND_TYPE = "append"; 37 private static final String REPLACE_TYPE = "replace"; 38 private static final String INSERT_TYPE = "insert"; 39 40 private final String type; 41 private final String root; 42 private final String sourceURI; 43 private final String destURI; 44 private final String deparameterizedSourceURI; 45 private final TreeMap parameters; 46 47 private String parentURI = null; 48 private String originalURI = null; 49 private String mimeType = null; 50 private String defaultFilename = Constants.INDEX_URI; 51 private String finalDestinationURI = null; 52 private String extension = null; 53 54 private boolean followLinks; 55 private boolean confirmExtension; 56 private String logger; 57 58 private transient int _hashCode; 59 private transient String _toString; 60 61 public Target(String type, 62 String root, 63 String sourceURI, 64 String destURI) 65 throws IllegalArgumentException { 66 this.type = type; 67 this.root = root; 68 69 if (destURI == null || destURI.length() == 0) { 70 throw new IllegalArgumentException ("You must specify a destination directory when defining a target"); 71 } 72 if (!destURI.endsWith("/")) { 73 destURI += "/"; 74 } 75 this.destURI = destURI; 76 77 this.parameters = new TreeMap (); 78 79 sourceURI = NetUtils.normalize(root + sourceURI); 81 this.deparameterizedSourceURI = NetUtils.deparameterize(sourceURI, this.parameters); 82 this.sourceURI = NetUtils.parameterize(this.deparameterizedSourceURI, this.parameters); 83 } 84 85 public Target(String type, String sourceURI, String destURI) 86 throws IllegalArgumentException { 87 this(type, "", sourceURI, destURI); 88 } 89 90 public Target(String sourceURI, String destURI) 91 throws IllegalArgumentException { 92 this(APPEND_TYPE, "", sourceURI, destURI); 93 } 94 95 public Target getDerivedTarget(String originalLinkURI) 96 throws IllegalArgumentException { 97 98 String linkURI = originalLinkURI; 99 if (linkURI.startsWith("?")) { 101 linkURI = this.getPageURI() + linkURI; 102 } 103 linkURI = 104 NetUtils.normalize(NetUtils.absolutize(this.getPath(), linkURI)); 105 106 if (!linkURI.startsWith(this.root)) { 108 return null; 109 } 110 linkURI = linkURI.substring(root.length()); 111 112 Target target = new Target(this.type, this.root, linkURI, this.destURI); 113 target.setOriginalURI(originalLinkURI); 114 target.setParentURI(this.sourceURI); 115 target.setConfirmExtension(this.confirmExtension); 116 target.setFollowLinks(this.followLinks); 117 target.setDefaultFilename(this.defaultFilename); 118 target.setLogger(this.logger); 119 return target; 120 } 121 122 128 public void setOriginalURI(String uri) { 129 this.originalURI = uri; 130 } 131 132 136 public void setParentURI(String uri) { 137 this.parentURI = uri; 138 } 139 140 153 public void setMimeType(String mimeType) { 154 this.mimeType = mimeType; 155 this.finalDestinationURI = null; 156 } 157 158 164 public void setExtraExtension(String extension) { 165 this.extension = extension; 166 this.finalDestinationURI = null; 167 } 168 180 public void setDefaultFilename(String filename) { 181 this.defaultFilename = filename; 182 } 183 184 189 public String getPageURI() { 190 String pageURI = this.getSourceURI(); 191 if (pageURI.indexOf("/") != -1) { 192 pageURI = pageURI.substring(pageURI.lastIndexOf("/") + 1); 193 if (pageURI.length() == 0) { 194 pageURI = "./"; 195 } 196 } 197 return pageURI; 198 } 199 200 204 public String getPath() { 205 return NetUtils.getPath(this.getSourceURI()); 206 } 207 208 211 public String getExtension() { 212 return NetUtils.getExtension(this.getSourceURI()); 213 } 214 215 220 public String getParentURI() { 221 return this.parentURI; 222 } 223 224 233 public String getDestinationURI() 234 throws ProcessingException { 235 236 if (this.finalDestinationURI == null) { 237 238 String actualSourceURI = this.sourceURI; 239 if (!actualSourceURI.startsWith(root)) { 240 throw new ProcessingException( 241 "Derived target does not share same root: " 242 + actualSourceURI); 243 } 244 actualSourceURI = actualSourceURI.substring(root.length()); 245 actualSourceURI = mangle(actualSourceURI); 246 247 String destinationURI; 248 if (APPEND_TYPE.equals(this.type)) { 249 destinationURI = destURI + actualSourceURI; 250 } else if (REPLACE_TYPE.equals(this.type)) { 251 destinationURI = destURI; 252 } else if (INSERT_TYPE.equals(this.type)) { 253 int starPos = destURI.indexOf("*"); 254 if (starPos == -1) { 255 throw new ProcessingException("Missing * in replace mapper uri"); 256 } else if (starPos == destURI.length() - 1) { 257 destinationURI = destURI.substring(0, starPos) + actualSourceURI; 258 } else { 259 destinationURI = destURI.substring(0, starPos) 260 + actualSourceURI 261 + destURI.substring(starPos + 1); 262 } 263 } else { 264 throw new ProcessingException( 265 "Unknown mapper type: " + this.type); 266 } 267 if (mimeType != null) { 268 final String ext = NetUtils.getExtension(destinationURI); 269 final String defaultExt = MIMEUtils.getDefaultExtension(mimeType); 270 if (defaultExt != null) { 271 if ((ext == null) || (!ext.equals(defaultExt))) { 272 destinationURI += defaultExt; 273 } 274 } 275 } 276 if (this.extension != null) { 277 destinationURI += this.extension; 278 } 279 this.finalDestinationURI = destinationURI; 280 } 281 return this.finalDestinationURI; 282 } 283 284 289 public String getTranslatedURI(String path) 290 throws ProcessingException { 291 292 String actualSourceURI = this.sourceURI; 293 if (!actualSourceURI.startsWith(root)) { 294 return actualSourceURI; 295 } 296 actualSourceURI = mangle(actualSourceURI); 297 298 if (mimeType != null) { 299 final String ext = NetUtils.getExtension(actualSourceURI); 300 final String defaultExt = MIMEUtils.getDefaultExtension(mimeType); 301 if (defaultExt != null) { 302 if ((ext == null) || (!ext.equals(defaultExt))) { 303 actualSourceURI += defaultExt; 304 } 305 } 306 } 307 return NetUtils.relativize(path, actualSourceURI); 308 } 309 310 315 public String getAuthlessDestURI() throws ProcessingException { 316 return NetUtils.removeAuthorisation(this.getDestinationURI()); 317 } 318 319 323 public String getOriginalSourceURI() { 324 return this.originalURI; 325 } 326 327 337 public String getSourceURI() { 338 return this.sourceURI; 339 } 340 341 347 public String getDeparameterizedSourceURI() { 348 return this.deparameterizedSourceURI; 349 } 350 351 356 public TreeMap getParameters() { 357 return this.parameters; 358 } 359 360 366 private String mangle(String uri) { 367 if (uri.length()==0 || uri.charAt(uri.length() - 1) == '/') { 368 uri += defaultFilename; 369 } 370 uri = uri.replace('"', '\''); 371 uri = uri.replace('?', '_'); 372 uri = uri.replace(':', '_'); 373 374 return uri; 375 } 376 377 public boolean equals(Object o) { 378 return (o instanceof Target) && o.toString().equals(toString()); 379 } 380 381 public int hashCode() { 382 if (_hashCode == 0) { 383 return _hashCode = toString().hashCode(); 384 } 385 return _hashCode; 386 } 387 388 public String toString() { 389 if (_toString == null) { 390 return _toString = 391 "<" 392 + type 393 + "|" 394 + root 395 + "|" 396 + sourceURI 397 + "|" 398 + destURI 399 + ">"; 400 } 401 return _toString; 402 } 403 406 public boolean confirmExtensions() { 407 return confirmExtension; 408 } 409 410 public boolean followLinks() { 411 return followLinks; 412 } 413 414 public String getLogger() { 415 return logger; 416 } 417 418 public void setConfirmExtension(boolean b) { 419 confirmExtension = b; 420 } 421 422 public void setFollowLinks(boolean b) { 423 followLinks = b; 424 } 425 426 public void setLogger(String string) { 427 logger = string; 428 } 429 } 430 | Popular Tags |