1 17 18 19 20 package org.apache.lenya.cms.publication; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Date ; 25 26 29 public class DefaultDocument implements Document { 30 31 private String id; 32 private Publication publication; 33 private DublinCore dublincore; 34 35 41 public DefaultDocument(Publication publication, String id) { 42 43 if (id == null) { 44 throw new IllegalArgumentException ("The document ID must not be null!"); 45 } 46 if (!id.startsWith("/")) { 47 throw new IllegalArgumentException ("The document ID must start with a slash!"); 48 } 49 this.id = id; 50 51 assert(publication != null) && !"".equals(publication.getId()); 52 this.publication = publication; 53 this.dublincore = new DublinCoreProxy(this); 54 } 55 56 64 protected DefaultDocument(Publication publication, String id, String area) { 65 if (id == null) { 66 throw new IllegalArgumentException ("The document ID must not be null!"); 67 } 68 if (!id.startsWith("/")) { 69 throw new IllegalArgumentException ("The document ID must start with a slash!"); 70 } 71 this.id = id; 72 73 assert(publication != null) && !"".equals(publication.getId()); 74 this.publication = publication; 75 76 setArea(area); 77 setLanguage(publication.getDefaultLanguage()); 78 79 this.dublincore = new DublinCoreProxy(this); 80 81 } 82 83 91 protected DefaultDocument(Publication publication, String id, String area, String language) { 92 if (id == null) { 93 throw new IllegalArgumentException ("The document ID must not be null!"); 94 } 95 if (!id.startsWith("/")) { 96 throw new IllegalArgumentException ("The document ID must start with a slash!"); 97 } 98 this.id = id; 99 100 assert(publication != null) && !"".equals(publication.getId()); 101 this.publication = publication; 102 this.language = language; 103 setArea(area); 104 105 this.dublincore = new DublinCoreProxy(this); 106 107 } 108 109 112 public String getId() { 113 return id; 114 } 115 116 119 public String getName() { 120 String [] ids = id.split("/"); 121 String nodeId = ids[ids.length - 1]; 122 123 return nodeId; 124 } 125 126 130 public String getNodeId() { 131 return getName(); 132 } 133 134 137 public Publication getPublication() { 138 return publication; 139 } 140 141 144 public Date getLastModified() { 145 return new Date (getFile().lastModified()); 146 } 147 148 151 public DublinCore getDublinCore() { 152 return dublincore; 153 } 154 155 159 public File getFile() { 160 return getPublication().getPathMapper().getFile( 161 getPublication(), 162 getArea(), 163 getId(), 164 getLanguage()); 165 } 166 167 private String language = ""; 168 169 172 public String getLanguage() { 173 return language; 174 } 175 176 179 public String [] getLanguages() throws DocumentException { 180 ArrayList languages = new ArrayList (); 181 SiteTree sitetree; 182 try { 183 sitetree = getPublication().getTree(getArea()); 184 if (sitetree != null) { 185 SiteTreeNode node = sitetree.getNode(getId()); 186 if (node != null) { 187 Label[] labels = node.getLabels(); 188 for (int i = 0; i < labels.length; i++) { 189 languages.add(labels[i].getLanguage()); 190 } 191 } 192 } else { 193 languages.add(getLanguage()); 194 } 195 } catch (SiteTreeException e) { 196 throw new DocumentException(e); 197 } 198 199 return (String []) languages.toArray(new String [languages.size()]); 200 } 201 202 206 public void setLanguage(String language) { 207 assert language != null; 208 this.language = language; 209 } 210 211 214 public String getLabel() throws DocumentException { 215 String label = ""; 216 try { 217 SiteTree siteTree = getPublication().getTree(getArea()); 218 if (siteTree != null) { 219 label = siteTree.getNode(getId()).getLabel(getLanguage()).getLabel(); 220 } 221 } catch (SiteTreeException e) { 222 throw new DocumentException(e); 223 } 224 return label; 225 } 226 227 private String area; 228 229 232 public String getArea() { 233 return area; 234 } 235 236 239 public String getCompleteURL() { 240 return "/" + getPublication().getId() + "/" + getArea() + getDocumentURL(); 241 } 242 243 246 public String getCompleteInfoURL() { 247 return "/" 248 + getPublication().getId() 249 + "/" 250 + Publication.INFO_AREA_PREFIX 251 + getArea() 252 + getDocumentURL(); 253 } 254 255 258 public String getCompleteURLWithoutLanguage() { 259 String extensionSuffix = "".equals(getExtension()) ? "" : ("." + getExtension()); 260 261 return "/" + getPublication().getId() + "/" + getArea() + getId() + extensionSuffix; 262 } 263 264 268 protected void setArea(String area) { 269 if (!AbstractPublication.isValidArea(area)) { 270 throw new IllegalArgumentException ("The area [" + area + "] is not valid!"); 271 } 272 this.area = area; 273 } 274 275 private String extension = "html"; 276 277 280 public String getExtension() { 281 return extension; 282 } 283 284 288 protected void setExtension(String extension) { 289 assert extension != null; 290 this.extension = extension; 291 } 292 293 private String documentURL; 294 295 299 public void setDocumentURL(String url) { 300 assert url != null; 301 this.documentURL = url; 302 } 303 304 307 public String getDocumentURL() { 308 return documentURL; 309 } 310 311 314 public boolean exists() throws DocumentException { 315 boolean exists; 316 try { 317 SiteTree sitetree = getPublication().getTree(getArea()); 318 if (sitetree != null) { 319 SiteTreeNode node = sitetree.getNode(getId()); 320 exists = (node != null) && (node.getLabel(getLanguage()) != null); 321 } else { 322 exists = getFile().exists(); 323 } 324 } catch (SiteTreeException e) { 325 throw new DocumentException(e); 326 } 327 return exists; 328 } 329 330 333 public boolean existsInAnyLanguage() throws DocumentException { 334 boolean exists = false; 335 try { 336 SiteTree sitetree = getPublication().getTree(getArea()); 337 if (sitetree != null) { 338 SiteTreeNode node = sitetree.getNode(getId()); 339 exists = node != null; 340 } else { 341 exists = getFile().exists(); 342 } 343 } catch (SiteTreeException e) { 344 throw new DocumentException(e); 345 } 346 return exists; 347 } 348 349 352 public boolean equals(Object object) { 353 boolean equals = false; 354 if (getClass().isInstance(object)) { 355 Document document = (Document) object; 356 equals = 357 getPublication().equals(document.getPublication()) 358 && getId().equals(document.getId()) 359 && getArea().equals(document.getArea()) 360 && getLanguage().equals(document.getLanguage()); 361 } 362 return equals; 363 364 } 365 366 369 public int hashCode() { 370 371 String key = 372 getPublication().getId() 373 + ":" 374 + getPublication().getServletContext() 375 + ":" 376 + getArea() 377 + ":" 378 + getId() 379 + ":" 380 + getLanguage(); 381 382 return key.hashCode(); 383 } 384 385 388 public String toString() { 389 return getPublication().getId() + ":" + getArea() + ":" + getId() + ":" + getLanguage(); 390 } 391 392 } 393 | Popular Tags |