1 2 package com.openedit.page; 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.InputStreamReader ; 6 import java.io.Reader ; 7 import java.io.StringWriter ; 8 import java.text.SimpleDateFormat ; 9 import java.util.ArrayList ; 10 import java.util.Date ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Locale ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.openedit.repository.ContentItem; 18 import org.openedit.repository.RepositoryException; 19 20 import com.openedit.Generator; 21 import com.openedit.OpenEditException; 22 import com.openedit.WebPageRequest; 23 import com.openedit.error.ContentNotAvailableException; 24 import com.openedit.generators.Output; 25 import com.openedit.util.FileUtils; 26 import com.openedit.util.OutputFiller; 27 import com.openedit.util.PathUtilities; 28 import com.openedit.util.strainer.Filter; 29 30 31 public class Page 32 { 33 private static Log log = LogFactory.getLog(Page.class); 34 public static final String BLANK_LAYOUT = "NOLAYOUT"; 35 36 protected String fieldPath; 37 protected PageSettings fieldPageSettings; 38 protected List fieldGenerators; 39 protected ContentItem fieldContentItem; 40 protected Date fieldOriginalyModified; 41 protected boolean fieldOriginalyExistedContentPath; 43 public Page( String inPath, PageSettings inMetaData ) 44 { 45 fieldPath = inPath; 46 setPageSettings(inMetaData ); 47 } 48 51 public Page() 52 { 53 } 54 public Page( Page inPage ) 55 { 56 this( inPage.getPath(), inPage.getPageSettings() ); 57 fieldContentItem = inPage.getContentItem(); 58 } 59 65 public Filter getEditFilter() 66 { 67 return getPageSettings().getEditFilter(); 68 } 69 70 public boolean isBinary() 71 { 72 if ( getMimeType() == null) 73 { 74 return false; } 76 if( getMimeType().startsWith("text") ) 77 { 78 return false; 79 } 80 if( getMimeType().indexOf("xml") > -1 ) 81 { 82 return false; 83 } 84 return true; 85 86 } 87 88 public boolean isFolder() 89 { 90 return getContentItem().isFolder(); 91 } 92 93 public boolean exists() 94 { 95 return getContentItem().exists(); 96 } 97 98 public Date getLastModified() 99 { 100 return getContentItem().lastModified(); 101 } 102 103 107 108 public boolean isCurrent() 109 { 110 if ( getLastModified() == null) 112 { 113 return false; 114 } 115 boolean pageCurrent = getLastModified().equals( getOriginalyModified() ); 116 if( !pageCurrent) 117 { 118 return false; 119 } 120 boolean metaDataCurrent = getPageSettings().isCurrent(); 121 return metaDataCurrent; 122 } 123 130 public String getLastModified(String inDateFormat) 131 { 132 SimpleDateFormat format = new SimpleDateFormat (inDateFormat); 133 134 return format.format(getLastModified()); 135 } 136 137 142 public String getMimeType() 143 { 144 return getPageSettings().getMimeType(); 145 } 146 147 153 public List getPageActions() 154 { 155 List actions = getPageSettings().getPageActions(); 156 if( isHtml() ) 157 { 158 return actions; 159 } 160 List copy = new ArrayList (actions.size()); 161 for (Iterator iter = actions.iterator(); iter.hasNext();) 162 { 163 PageAction action = (PageAction) iter.next(); 164 if ( action.isIncludesAll() ) 165 { 166 copy.add(action); 167 } 168 } 169 return copy; 170 171 } 172 178 public List getPathActions() 179 { 180 List actions = getPageSettings().getPathActions(); 181 if( isHtml() ) 182 { 183 return actions; 184 } 185 List copy = new ArrayList (actions.size()); 186 for (Iterator iter = actions.iterator(); iter.hasNext();) 187 { 188 PageAction action = (PageAction) iter.next(); 189 if ( action.isIncludesAll() ) 190 { 191 copy.add(action); 192 } 193 } 194 return copy; 195 } 196 197 202 public String getPath() 203 { 204 return fieldPath; 205 } 206 207 public String getDirectory() 208 { 209 String path = PathUtilities.extractDirectoryPath(getPath()); 210 return path; 212 } 213 public String getDirectoryRoot() 214 { 215 int slash = getPath().indexOf('/',1); 216 if( slash > 1) 217 { 218 return getPath().substring(0,slash); 219 } 220 return ""; 222 } 223 224 public String getName() 225 { 226 String path = PathUtilities.extractFileName(getPath()); 227 return path; 229 } 230 231 239 public String getProperty(String name) 240 { 241 return getProperty(name, (Locale )null); 242 } 243 public String getProperty(String name, String language) 244 { 245 if( language != null ) 246 { 247 253 PageProperty property = (PageProperty) getPageSettings().getProperty(name); 255 if (property != null) 256 { 257 return property.getValue(language); 258 } 259 else 260 { 261 return null; 262 } 263 } 264 else 265 { 266 return getProperty(name,(Locale )null ); 267 } 268 } 269 283 public String getProperty(String name, Locale language) 284 { 285 debug("Get property [name=" + name + ",locale=" + language + "]"); 286 PageProperty property = (PageProperty) getPageSettings().getProperty(name); 287 288 if (property != null) 289 { 290 return property.getValue(language); 291 } 292 else 293 { 294 return null; 295 } 296 } 297 public void debug( String inMessage ) 298 { 299 log.debug( inMessage ); 300 } 302 303 309 public Filter getViewFilter() 310 { 311 return getPageSettings().getViewFilter(); 312 } 313 314 322 public String get(String name) 323 { 324 return getProperty(name); 326 } 327 328 329 334 public String toString() 335 { 336 return getPath(); 337 } 338 339 public void setPageSettings(PageSettings inSettings) 340 { 341 fieldPageSettings = inSettings; 342 } 343 public PageSettings getPageSettings() 344 { 345 return fieldPageSettings; 346 } 347 348 351 public String getAlternateContentPath() 352 { 353 return getPageSettings().getAlternateContentPath(); 354 } 355 public List fieldGenerators() 356 { 357 return fieldGenerators; 358 } 359 362 public List getGenerator() 363 { 364 if ( fieldGenerators == null) 365 { 366 fieldGenerators = getPageSettings().getGenerators(); 367 } 368 return fieldGenerators; 369 } 370 371 public InputStream getInputStream() throws ContentNotAvailableException 372 { 373 try 374 { 375 return getContentItem().getInputStream(); 376 } 377 catch( RepositoryException e ) 378 { 379 throw new ContentNotAvailableException(e.getMessage(), getPath() ); 380 } 381 } 382 385 public String getLayout() 386 { 387 return getPageSettings().getLayout(); 388 } 389 390 public String getInnerLayout() 391 { 392 PageSettings parent = getPageSettings(); 393 while( parent != null) 394 { 395 String layout = parent.getInnerLayout(); 396 if( layout == null || !layout.equals(getPath())) 397 { 398 return layout; 399 } 400 parent = parent.getParent(); 401 } 402 return null; 403 } 404 405 public boolean hasLayout() 406 { 407 String layout = getLayout(); 408 409 return layout != null && !layout.equals(BLANK_LAYOUT); 410 } 411 public boolean hasInnerLayout() 412 { 413 String il = getInnerLayout(); 414 if ( il == null) 415 { 416 return false; 417 } 418 if ( il.equals(BLANK_LAYOUT)) 419 { 420 return false; 421 } 422 if( il.equalsIgnoreCase(getPath())) 423 { 424 return false; 425 } 426 return true; 427 } 428 429 public ContentItem getContentItem() 430 { 431 return fieldContentItem; 432 } 433 public void setContentItem( ContentItem revision ) 434 { 435 fieldContentItem = revision; 436 if ( revision != null) 437 { 438 fieldOriginalyModified = revision.lastModified(); 439 } 440 } 441 442 public Reader getReader() throws OpenEditException 443 { 444 if ( exists() ) 445 { 446 InputStreamReader in = null; 447 try 448 { 449 in = new InputStreamReader ( getContentItem().getInputStream(), getCharacterEncoding() ); 450 } catch ( Exception ex ) 451 { 452 throw new OpenEditException(ex); 453 } 454 return in; 455 } 456 else 457 { 458 throw new ContentNotAvailableException("No such page " + getPath(),getPath() ); 459 } 460 } 461 protected Date getOriginalyModified() 462 { 463 return fieldOriginalyModified; 464 } 465 468 public String getCharacterEncoding() 469 { 470 String encoding = getPageSettings().getPageCharacterEncoding(); 472 if ( encoding == null) 473 { 474 return "UTF-8"; 475 } 476 return encoding; 477 } 478 479 public String getContent() throws OpenEditException 480 { 481 StringWriter out = new StringWriter (); 482 Reader reader = null; 483 try 484 { 485 reader = getReader(); 486 new OutputFiller().fill( reader, out); 487 } 488 catch (IOException ex) 489 { 490 log.error( ex ); 491 throw new OpenEditException(ex); 492 } 493 finally 494 { 495 FileUtils.safeClose(reader); 496 } 497 return out.toString(); 498 } 499 502 public WebPageRequest generate(WebPageRequest inReq, Output inOut) throws OpenEditException 503 { 504 WebPageRequest req = inReq; 505 if ( inReq.getPage() != this) 506 { 507 req = inReq.copy(this); 508 } 509 if ( !req.hasRedirected() ) 510 { 511 for (Iterator iter = getGenerator().iterator(); iter.hasNext();) 512 { 513 Generator gen= (Generator) iter.next(); 514 if ( gen.canGenerate(inReq)) 515 { 516 gen.generate(req,this,inOut); 517 break; 518 } 519 } 520 } 521 return req; 522 } 523 526 public boolean isHtml() { 527 String mime = getMimeType(); 528 if ( mime != null && mime.endsWith("html")) 529 { 530 return true; 531 } 532 return false; 533 } 534 public boolean isImage() { 535 String mime = getMimeType(); 536 if ( mime != null && mime.startsWith("image")) 537 { 538 return true; 539 } 540 return false; 541 } 542 public boolean isOriginalyExistedContentPath() 543 { 544 return fieldOriginalyExistedContentPath; 545 } 546 public void setOriginalyExistedContentPath(boolean inOriginalContentPathMissing) 547 { 548 fieldOriginalyExistedContentPath = inOriginalContentPathMissing; 549 } 550 public boolean isDraft() 551 { 552 if( getContentItem().getActualPath().indexOf(".draft.") > -1 && exists() ) 553 { 554 return true; 555 } 556 return false; 557 } 558 } 559 | Popular Tags |