1 4 package com.openedit.page; 5 6 import java.io.InputStreamReader ; 7 import java.io.Reader ; 8 import java.util.ArrayList ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Locale ; 13 import java.util.Map ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.openedit.repository.ContentItem; 18 19 import com.openedit.OpenEditException; 20 import com.openedit.config.Configuration; 21 import com.openedit.util.strainer.Filter; 22 23 28 public class PageSettings 29 { 30 private static Log log = LogFactory.getLog(PageSettings.class); 31 32 protected ContentItem fieldXConf; 34 protected Configuration fieldUserDefinedData; 35 protected long fieldModifiedTime; 36 37 protected String fieldLayout; 38 protected String fieldInnerLayout; 39 protected List fieldGenerators; 40 protected Map fieldProperties; 41 protected List fieldPageActions; 42 protected List fieldPathActions; 43 protected String fieldAlternateContentPath; 44 protected Filter fieldEditFilter; 45 protected Filter fieldViewFilter; 46 protected String fieldPageCharacterEncoding; 47 protected boolean fieldModified = false; 48 protected String fieldMimeType; 49 protected PageSettings fieldFallBack; 50 protected PageSettings fieldParent; 51 protected String fieldDefaultLanguage; 52 53 public String getAlternateContentPath() 54 { 55 PageSettings parent = this; 56 PageSettings fallbackparent = getFallback(); 57 while ( parent != null) 58 { 59 if( parent.fieldAlternateContentPath != null) { 61 return parent.fieldAlternateContentPath; 62 } 63 if ( fallbackparent != null) { 65 if( fallbackparent.fieldAlternateContentPath != null) 66 { 67 return fallbackparent.fieldAlternateContentPath; 68 } 69 fallbackparent = fallbackparent.getParent(); } 71 parent = parent.getParent(); 72 } 73 return null; 74 } 75 public void setAlternateContentPath( String alternateContentPath ) 76 { 77 fieldAlternateContentPath = alternateContentPath; 78 } 79 public String getPath() 80 { 81 return getXConf().getPath(); 82 } 83 public String toString() 84 { 85 if ( fieldXConf != null) 86 { 87 return getXConf().getPath(); 88 } 89 return super.toString(); 90 91 } 92 public List getGenerators() 93 { 94 List finalList = new ArrayList (4); 96 97 PageSettings parent = this; 98 PageSettings fallbackparent = getFallback(); 99 while ( parent != null) 100 { 101 if( parent.fieldGenerators != null) 102 { 103 finalList.addAll(parent.fieldGenerators); 104 } 105 if ( fallbackparent != null) { 107 if( fallbackparent.fieldGenerators != null) 108 { 109 finalList.addAll(fallbackparent.fieldGenerators); 110 } 111 fallbackparent = fallbackparent.getParent(); 112 } 113 parent = parent.getParent(); 114 } 115 return finalList; 116 } 117 public void setGenerators( List generators ) 118 { 119 fieldGenerators = generators; 120 } 121 public String getLayout() 122 { 123 PageSettings parent = this; 124 PageSettings fallbackparent = getFallback(); 125 while ( parent != null) 126 { 127 if( parent.fieldLayout != null) { 129 return parent.fieldLayout; 130 } 131 if ( fallbackparent != null) { 133 if( fallbackparent.fieldLayout != null) 134 { 135 return fallbackparent.fieldLayout; 136 } 137 fallbackparent = fallbackparent.getParent(); } 139 parent = parent.getParent(); 140 } 141 return null; 142 } 143 public String getInnerLayout() 144 { 145 PageSettings parent = this; 146 PageSettings fallbackparent = getFallback(); 147 while ( parent != null) 148 { 149 if( parent.fieldInnerLayout != null) { 151 return parent.fieldInnerLayout; 152 } 153 if ( fallbackparent != null) { 155 if( fallbackparent.fieldInnerLayout != null) 156 { 157 return fallbackparent.fieldInnerLayout; 158 } 159 fallbackparent = fallbackparent.getParent(); } 161 parent = parent.getParent(); 162 } 163 return null; 164 } 165 protected void debug( String inMessage ) 166 { 167 log.debug( inMessage ); 168 } 170 171 public void setLayout( String layout ) 172 { 173 fieldLayout = layout; 174 } 175 public void setInnerLayout( String innerLayout ) 176 { 177 fieldInnerLayout = innerLayout; 178 } 179 public List getPageActions() 180 { 181 List finalList = new ArrayList (); 183 184 PageSettings parent = this; 185 PageSettings fallbackparent = getFallback(); 186 while ( parent != null) 187 { 188 if( parent.fieldPageActions != null) 189 { 190 finalList.addAll(0,parent.fieldPageActions); 191 } 192 if ( fallbackparent != null) { 194 if( fallbackparent.fieldPageActions != null) 195 { 196 finalList.addAll(0,fallbackparent.fieldPageActions); 197 } 198 fallbackparent = fallbackparent.getParent(); 199 } 200 parent = parent.getParent(); 201 } 202 return finalList; 203 204 } 205 public void setPageActions( List pageActions ) 206 { 207 fieldPageActions = pageActions; 208 } 209 public List getPathActions() 210 { 211 List finalList = new ArrayList (4); 213 214 PageSettings parent = this; 215 PageSettings fallbackparent = getFallback(); 216 while ( parent != null) 217 { 218 if( parent.fieldPathActions != null) 219 { 220 finalList.addAll(0,parent.fieldPathActions); 221 } 222 if ( fallbackparent != null) { 224 if( fallbackparent.fieldPathActions != null) 226 { 227 finalList.addAll(0,fallbackparent.fieldPathActions); 228 } 229 fallbackparent = fallbackparent.getParent(); 230 } 231 parent = parent.getParent(); 232 } 233 return finalList; } 235 236 protected int getDepth() 237 { 238 int i = 0; 239 PageSettings settings = this; 240 while( settings != null) 241 { 242 i++; 243 settings = settings.getParent(); 244 } 245 return i; 246 } 247 public Map getProperties() 248 { 249 if ( fieldProperties == null ) 250 { 251 fieldProperties = new HashMap (); 252 } 253 return fieldProperties; 254 } 255 public List getAllPropertyKeysWithoutPrefix(String inPrefix) 256 { 257 List all = new ArrayList (); 258 PageSettings parent = this; 259 PageSettings fallbackparent = getFallback(); 260 while ( parent != null) 261 { 262 if( parent.fieldProperties != null) 263 { 264 for (Iterator iter = parent.fieldProperties.keySet().iterator(); iter.hasNext();) 265 { 266 String key = (String ) iter.next(); 267 if( key.startsWith(inPrefix)) 268 { 269 key = key.substring(inPrefix.length()); 270 all.add(key); 271 } 272 } 273 } 274 if ( fallbackparent != null) { 276 if( fallbackparent.fieldProperties != null) 277 { 278 for (Iterator iter = fallbackparent.fieldProperties.keySet().iterator(); iter.hasNext();) 279 { 280 String key = (String ) iter.next(); 281 if( key.startsWith(inPrefix)) 282 { 283 key = key.substring(inPrefix.length()); 284 all.add(key); 285 } 286 } 287 } 288 fallbackparent = fallbackparent.getParent(); 289 } 290 parent = parent.getParent(); 291 } 292 return all; 293 294 } 295 296 public PageProperty getProperty(String inKey) 297 { 298 PageSettings parent = this; 299 PageSettings fallbackparent = getFallback(); 300 while ( parent != null) 301 { 302 if( parent.fieldProperties != null) 303 { 304 PageProperty val = parent.getFieldProperty(inKey); 305 if ( val != null) 306 { 307 return val; 308 } 309 } 310 if ( fallbackparent != null) { 312 if( fallbackparent.fieldProperties != null) 313 { 314 PageProperty val = (PageProperty)fallbackparent.getFieldProperty(inKey); 315 if ( val != null) 316 { 317 return val; 318 } 319 } 320 fallbackparent = fallbackparent.getParent(); 321 } 322 parent = parent.getParent(); 323 } 324 return null; 325 326 } 327 public PageProperty getFieldProperty(String inKey ) 328 { 329 PageProperty val = (PageProperty)getProperties().get(inKey); 330 if (val != null ) 331 { 332 val.setPath(getXConf().getPath()); 333 } 334 return val; 335 } 336 public void setProperties( Map properties ) 337 { 338 fieldProperties = properties; 339 } 340 public void putProperty(PageProperty inProperty) 341 { 342 getProperties().put( inProperty.getName(), inProperty); 343 } 344 345 public Filter getEditFilter() 346 { 347 PageSettings parent = this; 348 PageSettings fallbackparent = getFallback(); 349 while ( parent != null) 350 { 351 if( parent.fieldEditFilter != null) 352 { 353 return parent.fieldEditFilter; 354 } 355 if ( fallbackparent != null) 356 { 357 if( fallbackparent.fieldEditFilter != null) 358 { 359 return fallbackparent.fieldEditFilter; 360 } 361 fallbackparent = fallbackparent.getParent(); 362 } 363 parent = parent.getParent(); 364 } 365 return null; 366 } 367 public void setEditFilter( Filter editFilter ) 368 { 369 fieldEditFilter = editFilter; 370 } 371 public Filter getViewFilter() 372 { 373 PageSettings parent = this; 374 PageSettings fallbackparent = getFallback(); 375 while ( parent != null) 376 { 377 if( parent.fieldViewFilter != null) 378 { 379 return parent.fieldViewFilter; 380 } 381 if ( fallbackparent != null) { 383 if( fallbackparent.fieldViewFilter != null) 384 { 385 return fallbackparent.fieldViewFilter; 386 } 387 fallbackparent = fallbackparent.getParent(); 388 } 389 parent = parent.getParent(); 390 } 391 return null; 392 } 393 public void setViewFilter( Filter viewFilter ) 394 { 395 fieldViewFilter = viewFilter; 396 } 397 public PageSettings getFallback() 398 { 399 return fieldFallBack; 400 } 401 404 public void setFallBack(PageSettings inParent) 405 { 406 fieldFallBack = inParent; 407 408 } 409 410 public boolean isCurrent() 411 { 412 PageSettings parent = this; 413 PageSettings fallbackparent = getFallback(); 414 while ( parent != null) 415 { 416 if( !parent.fieldIsCurrent() ) { 418 return false; 419 } 420 if ( fallbackparent != null) 421 { 422 if( !fallbackparent.fieldIsCurrent() ) 423 { 424 return false; 425 } 426 fallbackparent = fallbackparent.getParent(); } 428 parent = parent.getParent(); 429 } 430 return true; 431 } 432 public boolean fieldIsCurrent() 433 { 434 if ( getXConf().lastModified() == null) 435 { 436 return false; 437 } 438 boolean self = getXConf().lastModified().getTime() == getModifiedTime(); 439 return self; 440 } 441 public boolean exists() 442 { 443 return getXConf().exists(); 444 } 445 public ContentItem getXConf() 446 { 447 return fieldXConf; 448 } 449 public void setXConf(ContentItem inConf) 450 { 451 fieldXConf = inConf; 452 if ( inConf != null && inConf.lastModified() != null) 453 { 454 setModifiedTime( inConf.lastModified().getTime() ); 455 } 456 else 457 { 458 setModifiedTime( -1 ); } 460 } 461 464 public Reader getReader() throws OpenEditException 465 { 466 try 467 { 468 String enc = getPageCharacterEncoding(); 469 if ( enc != null) 470 { 471 return new InputStreamReader (getXConf().getInputStream(), enc ); 472 } 473 return new InputStreamReader (getXConf().getInputStream(), "UTF-8"); 474 475 } 476 catch ( Exception ex ) 477 { 478 throw new OpenEditException(ex); 479 } 480 } 481 public long getModifiedTime() 482 { 483 return fieldModifiedTime; 484 } 485 public void setModifiedTime(long inLastModifiedTime) 486 { 487 fieldModifiedTime = inLastModifiedTime; 488 } 489 492 public void setPageCharacterEncoding(String inString) 493 { 494 fieldPageCharacterEncoding = inString; 495 } 496 public String getPageCharacterEncoding() 497 { 498 PageSettings parent = this; 499 PageSettings fallbackparent = getFallback(); 500 while ( parent != null) 501 { 502 if( parent.fieldPageCharacterEncoding != null) { 504 return parent.fieldPageCharacterEncoding; 505 } 506 if ( fallbackparent != null) { 508 if( fallbackparent.fieldPageCharacterEncoding != null) 509 { 510 return fallbackparent.fieldPageCharacterEncoding; 511 } 512 fallbackparent = fallbackparent.getParent(); } 514 parent = parent.getParent(); 515 } 516 return null; 517 518 } 519 public Configuration getUserDefinedData() 520 { 521 return fieldUserDefinedData; 522 } 523 public void setUserDefinedData(Configuration inUserDefinedData) 524 { 525 fieldUserDefinedData = inUserDefinedData; 526 } 527 531 public Configuration getUserDefined(String inString) 532 { 533 if ( getUserDefinedData() != null) 534 { 535 Configuration config = getUserDefinedData().getChild(inString); 536 if ( config != null ) 537 { 538 return config; 539 } 540 } 541 return null; 542 } 543 546 public void setPathActions(List inList) 547 { 548 fieldPathActions = inList; 549 } 550 554 public String getPropertyValue(String inString, Locale inLocale) 555 { 556 PageProperty prop = getProperty(inString); 557 if ( prop != null) 558 { 559 return prop.getValue(inLocale); 560 } 561 return null; 562 } 563 564 567 public String getMimeType() { 568 return fieldMimeType; 569 } 570 573 public void setMimeType(String inMimeType) { 574 fieldMimeType = inMimeType; 575 } 576 577 public PageSettings getParent() 578 { 579 return fieldParent; 580 } 581 public void setParent(PageSettings inParent) 582 { 583 fieldParent = inParent; 584 } 585 public String getFieldAlternativeContentPath() 586 { 587 return fieldAlternateContentPath; 588 } 589 public List getFieldGenerator() 590 { 591 return fieldGenerators; 592 } 593 public String getFieldInnerLayout() 594 { 595 return fieldInnerLayout; 596 } 597 public String getFieldLayout() 598 { 599 return fieldLayout; 600 } 601 public String getFieldPageCharacterEncoding() 602 { 603 return fieldPageCharacterEncoding; 604 } 605 public String getDefaultLanguage() 606 { 607 if( fieldDefaultLanguage == null) 608 { 609 fieldDefaultLanguage = getPropertyValue("defaultlanguage", null); 610 if( fieldDefaultLanguage == null) 611 { 612 fieldDefaultLanguage = ""; 613 } 614 } 615 return fieldDefaultLanguage; 616 } 617 618 public String getPropertyValueFixed( String inKey ) 619 { 620 String val = getPropertyValue(inKey, null); 621 return replaceProperty(val); 622 } 623 public String replaceProperty(String inValue) 624 { 625 if( inValue == null) 626 { 627 return inValue; 628 } 629 int start = inValue.indexOf("${"); 630 if( start != -1) 631 { 632 int end = inValue.indexOf("}",start); 633 if( end != -1) 634 { 635 String key = inValue.substring(start+2,end); 636 Object variable = getProperty(key); if( variable != null) 638 { 639 inValue = inValue.substring(0,start) + variable + inValue.substring(end+1); 640 } 641 } 642 } 643 644 return inValue; 645 } 646 647 648 } 649 | Popular Tags |