1 4 package com.openedit.page; 5 6 import java.io.IOException ; 7 import java.io.StringReader ; 8 import java.io.StringWriter ; 9 import java.util.ArrayList ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 import org.dom4j.DocumentHelper; 14 import org.dom4j.Element; 15 import org.dom4j.io.OutputFormat; 16 import org.dom4j.io.XMLWriter; 17 18 import com.openedit.OpenEditException; 19 import com.openedit.OpenEditRuntimeException; 20 import com.openedit.config.Configuration; 21 import com.openedit.config.XMLConfiguration; 22 import com.openedit.util.XmlUtil; 23 24 27 public class XconfConfiguration extends XMLConfiguration 28 { 29 30 public XconfConfiguration() 31 { 32 33 } 34 35 public String getViewRequirementsXml() 36 { 37 XMLConfiguration conf = (XMLConfiguration)getViewRequirements(); 38 if( conf == null) 39 { 40 return null; 41 } 42 return asInnerXml("view-requirements"); 43 } 44 public String getEditRequirementsXml() 45 { 46 XMLConfiguration conf = (XMLConfiguration)getEditRequirements(); 47 if( conf == null) 48 { 49 return null; 50 } 51 return asInnerXml("edit-requirements"); 52 } 53 54 private String asInnerXml(String inName) 55 { 56 Element root = DocumentHelper.createElement(inName); 57 XMLConfiguration conf = (XMLConfiguration)getChild(inName); 58 conf.appendXml(conf,root); 59 StringWriter text = new StringWriter (); 60 OutputFormat format = OutputFormat.createPrettyPrint(); 61 format.setEncoding("UTF-8"); 62 XMLWriter out = new XMLWriter(text, format); 63 try 64 { 65 out.write((Element)root.elementIterator().next()); 66 } 67 catch (IOException ex) 68 { 69 throw new OpenEditRuntimeException(ex); 70 } 71 String xml = text.toString(); 72 return xml; 73 74 } 75 76 77 public Configuration getViewRequirements() 78 { 79 return getChild( "view-requirements" ); 80 } 81 82 public Configuration getEditRequirements() 83 { 84 return getChild( "edit-requirements" ); 85 } 86 87 public String getContentFile() 88 { 89 return getChildValue( "contentfile" ); 90 } 91 public String getOverrideDirectory() 92 { 93 return getChildValue( "overridedirectory" ); 94 } 95 96 public List getGenerators() 97 { 98 return getChildren("generator"); 99 } 100 101 public Configuration getLayout() 102 { 103 return getChild("layout"); 104 } 105 106 public Configuration getInnerLayout() 107 { 108 return getChild("inner-layout"); 109 } 110 public Configuration getProperty(String inId) 111 { 112 Configuration config = selectSingleNode("//property[@name='" + inId + "']"); 113 return config; 114 } 115 116 public List getProperties() 117 { 118 return getChildren("property"); 119 } 120 121 public List getPageActions() 122 { 123 return getChildren( "page-action" ); 124 } 125 public List getPathActions() 126 { 127 return getChildren( "path-action" ); 128 } 129 protected String fieldWorkingDir; 131 protected String fieldPagePath; 132 public static final String VIEW_REQ = "view-requirements"; 133 public static final String WRITE_REQ = "edit-requirements"; 134 protected Configuration fieldConfig; 135 protected List fieldStandardSettings; 136 137 138 public void setConfig(Configuration inConfig) 139 { 140 fieldConfig = inConfig; 141 } 142 public String get(String inKey) 143 { 144 Configuration child = getChild(inKey); 145 if (child != null) 146 { 147 return child.getValue(); 148 } 149 return null; 150 } 151 public String getGeneratorChildValue(String inChildName) 152 { 153 Configuration prop = selectSingleNode("//generator/" + inChildName); 155 if (prop != null) 156 { 157 return prop.getValue(); 158 } 159 return null; 160 } 161 162 public boolean checkedText(String inName, String inText) 171 { 172 for (Iterator iter = getChildIterator(inName); iter.hasNext();) 173 { 174 Configuration element = (Configuration) iter.next(); 175 if (inText.equalsIgnoreCase(element.getValue())) 176 { 177 return true; 178 } 179 } 180 return false; 181 } 182 public String propertyValue( String inKey ) 183 { 184 String val = null; 185 for (Iterator iter = getChildIterator("property"); iter.hasNext();) 186 { 187 Configuration element = (Configuration) iter.next(); 188 if (inKey.equalsIgnoreCase(element.getAttribute("name"))) 189 { 190 val = element.getValue(); 191 if( val == null ) 192 { 193 for (Iterator iterator = element.getChildIterator("value"); iterator.hasNext();) 195 { 196 Configuration valconf = (Configuration) iterator.next(); 197 val = valconf.getValue(); 198 break; 199 } 200 } 201 } 202 } 203 return val; 204 205 } 206 public boolean isTrueProperty(String inKey) 207 { 208 String value = propertyValue(inKey); 209 if ( value != null ) 210 { 211 return value.equalsIgnoreCase("true"); 212 } 213 return false; 214 } 215 public boolean isEmptyProperty(String inKey) 216 { 217 String value = propertyValue(inKey); 218 if ( value != null ) 219 { 220 return false; 221 } 222 return true; 223 } 224 public boolean isFalseProperty(String inKey) 225 { 226 String value = propertyValue(inKey); 227 if ( value != null ) 228 { 229 return value.equalsIgnoreCase("false"); 230 } 231 return false; 232 } 233 public boolean isBlankLayout() 234 { 235 Configuration layout = getChild("layout"); 236 if ( layout != null ) 237 { 238 if( layout.getValue() == null || 239 layout.getValue().length() == 0 || 240 Page.BLANK_LAYOUT.equals(layout.getValue()) ) 241 { 242 return true; 243 } 244 } 245 return false; 246 } 247 public boolean isBlankInnerLayout() 248 { 249 Configuration layout = getChild("inner-layout"); 250 if ( layout != null ) 251 { 252 if( layout.getValue() == null || 253 layout.getValue().length() == 0 || 254 Page.BLANK_LAYOUT.equals(layout.getValue()) ) 255 { 256 return true; 257 } 258 } 259 return false; 260 } 261 266 public String listPageActions() 267 { 268 return outerXml("page-action"); 269 } 270 271 public List getAllProperties() 272 { 273 List props = new ArrayList (); 274 for (Iterator iter = getChildIterator("property"); iter.hasNext();) 275 { 276 Configuration element = (Configuration) iter.next(); 277 props.add(element.getAttribute("name")); 278 } 279 props.remove("title"); 280 props.remove("keywords"); 281 props.remove("description"); 282 if( props.size() > 0) 283 { 284 props.add(0,"description"); 285 } 286 else 287 { 288 props.add("description"); 289 } 290 props.add(0,"keywords"); 291 props.add(0,"title"); 292 return props; 293 } 294 public List getUserProperties() 295 { 296 List props = new ArrayList (); 297 for (Iterator iter = getAllProperties().iterator(); iter.hasNext();) 298 { 299 String name = (String ) iter.next(); 300 if ( !getStandardSettings().contains(name) ) 301 { 302 props.add(name); 303 } 304 305 } 306 return props; 307 } 308 312 public void removeProperty(String name) 313 { 314 Configuration prop = (Configuration) selectSingleNode("//property[@name='" + name + "']"); 316 if (prop != null) 317 { 318 prop.getParent().removeChild(prop); 319 } 320 } 321 326 public void saveProperty(String name, String value, String inLocale) 327 { 328 if (value == null) 331 { 332 removeProperty(name); 333 } 334 else 335 { 336 Configuration prop = 337 (Configuration) selectSingleNode("//property[@name='" + name + "']"); 338 if( inLocale != null && inLocale.length() == 0) 339 { 340 inLocale = null; 341 } 342 if (prop == null) 343 { 344 prop = addChild("property"); 345 prop.setAttribute("name", name); 346 if( inLocale == null ) 347 { 348 prop.setValue(value); 349 return; 350 } 351 } 352 boolean existing = false; 353 String defaultval = prop.getValue(); 354 if( defaultval != null) 356 { 357 Configuration inline = prop.addChild("value"); 358 inline.setValue(defaultval); 359 prop.setValue(null); 360 } 361 362 for (Iterator iter = prop.getChildIterator("value"); iter.hasNext();) 363 { 364 Configuration valueconfig = (Configuration ) iter.next(); 365 String vlocale = valueconfig.getAttribute("locale"); 366 if( inLocale == null && vlocale == null) 367 { 368 valueconfig.setValue(value); 369 existing = true; 370 break; 371 } 372 if( vlocale != null && vlocale.equals(inLocale)) 373 { 374 valueconfig.setValue(value); 375 existing = true; 376 break; 377 } 378 } 379 if( !existing) 380 { 381 Configuration valueconf = prop.addChild("value"); 383 valueconf.setAttribute("locale", inLocale); 384 valueconf.setValue(value); 385 } 386 } 387 } 388 392 private Configuration selectSingleNode(String xpath) 393 { 394 if ( xpath == null) 395 { 396 return null; 397 } 398 if( xpath.indexOf('[') == -1) 401 { 402 String [] path = xpath.split("\\/"); 405 Configuration parent = this; for (int i = 0; i < path.length; i++) 407 { 408 if ( path[i].trim().length() == 0) 409 { 410 continue; 411 } 412 Configuration child = parent.getChild(path[i]); 413 if ( child == null) 414 { 415 return parent; 416 } 417 parent = child; 418 } 419 } 420 else 421 { 422 String child = xpath.substring(2,xpath.indexOf('[')); 423 String attrib = xpath.substring(xpath.indexOf('@') + 1,xpath.indexOf('=')); 424 String value = xpath.substring(xpath.indexOf('\'') + 1,xpath.lastIndexOf('\'')); 425 for (Iterator iter = getChildren(child).iterator(); iter.hasNext();) 426 { 427 Configuration element = (Configuration) iter.next(); 428 if ( value.equals( element.getAttribute(attrib))) 429 { 430 return element; 431 } 432 } 433 } 434 return null; 435 } 436 437 public boolean isMissing(String inConfiguration) 438 { 439 Configuration elem = getChild(inConfiguration); 440 if (elem == null ) 441 { 442 return true; 443 } 444 else 445 { 446 return false; 447 } 448 } 449 public void saveConfiguration(String inName, String inText) 450 { 451 452 if ( inText != null && inText.trim().length() == 0) 453 { 454 inText = null; 455 } 456 457 Configuration prop = getChild(inName); 458 if (prop != null) 459 { 460 if ( inText != null) 461 { 462 prop.setValue(inText); 463 } 464 else 465 { 466 removeChild(prop); 467 } 468 } 469 else if ( inText != null) 470 { 471 addChild(inName).setValue(inText); 472 } 473 } 474 475 479 public void removeConfigurations(String inName) 480 { 481 for (Iterator iter = getChildren(inName).iterator(); iter.hasNext();) 482 { 483 Configuration elem = (Configuration) iter.next(); 484 elem.getParent().removeChild(elem); 485 } 486 } 487 488 protected String outerXml(String inField) 489 { 490 StringWriter wri = new StringWriter (); 491 for (Iterator iter = getChildIterator(inField); iter.hasNext();) 492 { 493 XMLConfiguration read = (XMLConfiguration) iter.next(); 494 wri.write(read.toXml("UTF-8")); 495 } 496 return wri.toString(); 497 } 498 499 protected String innerXml(String inField) 500 { 501 Configuration read = getChild(inField); 502 if (read != null) 503 { 504 StringWriter wri = new StringWriter (); 505 506 for (Iterator iter = read.getChildren().iterator(); iter.hasNext();) 507 { 508 XMLConfiguration child = (XMLConfiguration) iter.next(); 509 wri.write(child.toXml("UTF-8")); 510 } 511 return wri.toString(); 512 } 513 return null; 514 } 515 public String getWritePermissions() 516 { 517 return innerXml(WRITE_REQ); 518 } 519 522 public void setReadPermissions(String inXML) throws OpenEditException 523 { 524 resetValueWithXml(VIEW_REQ, inXML); 525 } 526 protected void resetValueWithXml(String inField, String inXML) throws OpenEditException 527 { 528 try 529 { 530 Configuration read = getChild(inField); 531 if (read != null) 532 { 533 removeChild(read); 534 } 535 if (inXML != null && inXML.length() > 0) 537 { 538 appendXml("<" + inField + ">\n" + inXML + "\n</" + inField + ">"); 539 } 540 } 541 catch (Exception ex) 542 { 543 throw new OpenEditException(ex); 544 } 545 } 546 public void setAllActions(String inXML) throws OpenEditException 547 { 548 removeConfigurations("page-action"); 549 appendXml(inXML); 550 } 551 552 555 public void removeScriptAction(String inFilename) 556 { 557 for (Iterator iter = getChildIterator("page-action"); iter.hasNext();) 559 { 560 Configuration element = (Configuration) iter.next(); 561 if ("Script.run".equals(element.getAttribute("name"))) 562 { 563 for (Iterator iterator = element.getChildIterator("property"); iterator.hasNext();) 564 { 565 Configuration prop = (Configuration) iterator.next(); 566 if ("code".equals(prop.getAttribute("name")) 567 && inFilename.equals(prop.getValue())) 568 { 569 removeChild(element); 570 } 571 } 572 } 573 } 574 575 } 576 577 public void appendScriptAction(String inCode) throws OpenEditException 578 { 579 removeScriptAction(inCode); 580 Configuration top = addChild("page-action"); 581 top.setAttribute("name", "Script.run"); 582 Configuration prop = top.addChild("property"); 583 prop.setAttribute("name", "code"); 584 prop.setValue(inCode); 585 } 586 protected void appendXml(String inXML) throws OpenEditException 587 { 588 if (inXML != null && inXML.length() > 0) 589 { 590 StringReader in = new StringReader ("<tmp>" + inXML + "</tmp>"); 591 XMLConfiguration tmpconfig = new XMLConfiguration(); 592 Element child = new XmlUtil().getXml(in, "UTF-8"); 593 tmpconfig.populate(child); 594 for (Iterator iter = tmpconfig.getChildren().iterator(); iter.hasNext();) 595 { 596 Configuration element = (Configuration) iter.next(); 597 addChild(element); 598 } 599 } 600 } 601 602 public void setWritePermissions(String inXML) throws OpenEditException 603 { 604 resetValueWithXml(WRITE_REQ, inXML); 605 } 606 public boolean isEmpty() 607 { 608 return 609 getChildren() == null 610 || getChildren().size() == 0; 611 } 612 613 614 615 public void setContentFile(String inContentFile) 616 { 617 saveConfiguration("contentfile",inContentFile); 618 } 619 620 623 public void removeAllProperties() 624 { 625 List props = new ArrayList (); 626 for (Iterator iter = getChildIterator("property"); iter.hasNext();) 627 { 628 Configuration element = (Configuration) iter.next(); 629 props.add(element ); 630 } 631 for (Iterator iter = props.iterator(); iter.hasNext();) 632 { 633 Configuration element = (Configuration) iter.next(); 634 removeChild(element); 635 636 } 637 638 } 639 640 public List getStandardSettings() 641 { 642 if ( fieldStandardSettings == null) 643 { fieldStandardSettings = new ArrayList (); 645 fieldStandardSettings.add("editable"); 649 fieldStandardSettings.add("showToolbar"); 650 fieldStandardSettings.add("saveasxhtml"); 651 fieldStandardSettings.add("contentfile"); 652 fieldStandardSettings.add("encoding"); 653 fieldStandardSettings.add("mimetype"); 654 fieldStandardSettings.add("overridedirectory"); 655 fieldStandardSettings.add("virtual"); 656 } 657 return fieldStandardSettings; 658 } 659 public void setStandardSettings(List inStandardSettings) 660 { 661 fieldStandardSettings = inStandardSettings; 662 } 663 664 667 public void removeElements(String inString) 668 { 669 Configuration config = getChild(inString); 670 while(config != null ) 671 { 672 removeChild(config); 673 config = getChild(inString); 674 } 675 } 676 677 } 678 | Popular Tags |