1 19 20 package org.netbeans.core.windows.persistence; 21 22 import java.util.logging.Level ; 23 import org.netbeans.core.windows.Debug; 24 import org.openide.filesystems.FileLock; 25 import org.openide.filesystems.FileObject; 26 import org.openide.filesystems.FileUtil; 27 import org.openide.modules.SpecificationVersion; 28 import org.openide.util.NbBundle; 29 import org.xml.sax.*; 30 import org.xml.sax.helpers.DefaultHandler ; 31 32 import java.io.*; 33 import java.util.logging.Logger ; 34 35 40 41 class TCRefParser { 42 43 public static final String INSTANCE_DTD_ID_1_0 44 = "-//NetBeans//DTD Top Component in Mode Properties 1.0//EN"; public static final String INSTANCE_DTD_ID_2_0 46 = "-//NetBeans//DTD Top Component in Mode Properties 2.0//EN"; public static final String INSTANCE_DTD_ID_2_1 48 = "-//NetBeans//DTD Top Component in Mode Properties 2.1//EN"; public static final String INSTANCE_DTD_ID_2_2 50 = "-//NetBeans//DTD Top Component in Mode Properties 2.2//EN"; 52 private static final boolean DEBUG = Debug.isLoggable(TCRefParser.class); 53 54 55 private String tc_id; 56 57 58 private FileObject moduleParentFolder; 59 60 61 private FileObject localParentFolder; 62 63 private InternalConfig internalConfig; 64 65 66 private boolean inModuleFolder; 67 68 private boolean inLocalFolder; 69 70 public TCRefParser (String tc_id) { 71 this.tc_id = tc_id; 72 } 73 74 75 TCRefConfig load () throws IOException { 76 if (DEBUG) Debug.log(TCRefParser.class, "load ENTER" + " tcRef:" + tc_id); 77 TCRefConfig tcRefCfg = new TCRefConfig(); 78 PropertyHandler propertyHandler = new PropertyHandler(); 79 InternalConfig internalCfg = getInternalConfig(); 80 internalCfg.clear(); 81 propertyHandler.readData(tcRefCfg, internalCfg); 82 if (DEBUG) Debug.log(TCRefParser.class, "load LEAVE" + " tcRef:" + tc_id); 83 return tcRefCfg; 84 } 85 86 87 void save (TCRefConfig tcRefCfg) throws IOException { 88 if (DEBUG) Debug.log(TCRefParser.class, "save ENTER" + " tcRef:" + tc_id); 89 PropertyHandler propertyHandler = new PropertyHandler(); 90 InternalConfig internalCfg = getInternalConfig(); 91 propertyHandler.writeData(tcRefCfg, internalCfg); 92 if (DEBUG) Debug.log(TCRefParser.class, "save LEAVE" + " tcRef:" + tc_id); 93 } 94 95 String getName () { 96 return tc_id; 97 } 98 99 102 InternalConfig getInternalConfig () { 103 if (internalConfig == null) { 104 internalConfig = new InternalConfig(); 105 } 106 return internalConfig; 107 } 108 109 113 void setInternalConfig (InternalConfig internalCfg) { 114 internalConfig = internalCfg; 115 } 116 117 boolean isInModuleFolder () { 118 return inModuleFolder; 119 } 120 121 void setInModuleFolder (boolean inModuleFolder) { 122 this.inModuleFolder = inModuleFolder; 123 } 124 125 boolean isInLocalFolder () { 126 return inLocalFolder; 127 } 128 129 void setInLocalFolder (boolean inLocalFolder) { 130 this.inLocalFolder = inLocalFolder; 131 } 132 133 void setModuleParentFolder (FileObject moduleParentFolder) { 134 this.moduleParentFolder = moduleParentFolder; 135 } 136 137 void setLocalParentFolder (FileObject localParentFolder) { 138 this.localParentFolder = localParentFolder; 139 } 140 141 void log (String s) { 142 Debug.log(TCRefParser.class, s); 143 } 144 145 146 private final class PropertyHandler extends DefaultHandler { 147 148 149 private TCRefConfig tcRefConfig = null; 150 151 152 private InternalConfig internalConfig = null; 153 154 155 private final Object RW_LOCK = new Object (); 156 157 public PropertyHandler () { 158 } 159 160 private FileObject getConfigFOInput () { 161 FileObject tcRefConfigFO; 162 if (isInLocalFolder()) { 163 tcRefConfigFO = localParentFolder.getFileObject 165 (TCRefParser.this.getName(), PersistenceManager.TCREF_EXT); 166 } else if (isInModuleFolder()) { 167 tcRefConfigFO = moduleParentFolder.getFileObject 169 (TCRefParser.this.getName(), PersistenceManager.TCREF_EXT); 170 } else { 171 tcRefConfigFO = null; 173 } 174 return tcRefConfigFO; 176 } 177 178 private FileObject getConfigFOOutput () throws IOException { 179 FileObject tcRefConfigFO; 180 tcRefConfigFO = localParentFolder.getFileObject 181 (TCRefParser.this.getName(), PersistenceManager.TCREF_EXT); 182 if (tcRefConfigFO != null) { 183 return tcRefConfigFO; 185 } else { 186 StringBuffer buffer = new StringBuffer (); 187 buffer.append(TCRefParser.this.getName()); 188 buffer.append('.'); 189 buffer.append(PersistenceManager.TCREF_EXT); 190 tcRefConfigFO = FileUtil.createData(localParentFolder, buffer.toString()); 192 return tcRefConfigFO; 194 } 195 } 196 200 void readData (TCRefConfig tcRefCfg, InternalConfig internalCfg) 201 throws IOException { 202 tcRefConfig = tcRefCfg; 203 internalConfig = internalCfg; 204 205 FileObject cfgFOInput = getConfigFOInput(); 206 if (cfgFOInput == null) { 207 throw new FileNotFoundException("[WinSys] Missing TCRef configuration file:" + TCRefParser.this.getName()); 209 } 210 InputStream is = null; 211 try { 212 synchronized (RW_LOCK) { 213 220 is = cfgFOInput.getInputStream(); 222 PersistenceManager.getDefault().getXMLParser(this).parse(new InputSource(is)); 223 } 224 } catch (SAXException exc) { 225 String msg = NbBundle.getMessage(TCRefParser.class, 227 "EXC_TCRefParse", cfgFOInput); 228 229 throw (IOException) new IOException(msg).initCause(exc); 230 } finally { 231 try { 232 if (is != null) { 233 is.close(); 234 } 235 } catch (IOException exc) { 236 Logger.getLogger(TCRefParser.class.getName()).log(Level.WARNING, null, exc); 237 } 238 } 239 240 tcRefCfg = tcRefConfig; 241 internalCfg = internalConfig; 242 243 tcRefConfig = null; 244 internalConfig = null; 245 } 246 247 public void startElement (String nameSpace, String name, String qname, Attributes attrs) 248 throws SAXException { 249 if ("tc-ref".equals(qname)) { handleTCRef(attrs); 251 } else if (internalConfig.specVersion.compareTo(new SpecificationVersion("2.0")) >= 0) { if ("module".equals(qname)) { handleModule(attrs); 255 } else if ("tc-id".equals(qname)) { handleTcId(attrs); 257 } else if ("state".equals(qname)) { handleState(attrs); 259 } else if ("previousMode".equals(qname)) { handlePreviousMode(attrs); 261 } else if ("docking-status".equals(qname)) { handleDockingStatus(attrs); 263 } else if ("slide-in-status".equals(qname)) { handleSlideInStatus(attrs); 265 } 266 } else { 267 log("-- TCRefParser.startElement PARSING OLD"); 268 } 270 } 271 272 public void error(SAXParseException ex) throws SAXException { 273 throw ex; 274 } 275 276 277 private void handleTCRef (Attributes attrs) { 278 String version = attrs.getValue("version"); if (version != null) { 280 internalConfig.specVersion = new SpecificationVersion(version); 281 } else { 282 PersistenceManager.LOG.log(Level.WARNING, 283 "[WinSys.TCRefParser.handleTCRef]" + " Warning: Missing attribute \"version\" of element \"tc-ref\"."); internalConfig.specVersion = new SpecificationVersion("2.0"); } 287 if (internalConfig.specVersion.compareTo(new SpecificationVersion("2.0")) < 0) { String tc_id = attrs.getValue("id"); if (tc_id != null) { 292 } else { 294 PersistenceManager.LOG.log(Level.WARNING, 295 "[WinSys.TCRefParser.handleTCRef]" + " Warning: Missing attribute \"id\" of element \"tc-ref\"."); } 298 } 299 } 300 301 302 private void handleModule (Attributes attrs) { 303 String moduleCodeName = attrs.getValue("name"); internalConfig.moduleCodeNameBase = null; 306 internalConfig.moduleCodeNameRelease = null; 307 internalConfig.moduleSpecificationVersion = null; 308 if (moduleCodeName != null) { 309 int i = moduleCodeName.indexOf('/'); 310 if (i != -1) { 311 internalConfig.moduleCodeNameBase = moduleCodeName.substring(0, i); 312 internalConfig.moduleCodeNameRelease = moduleCodeName.substring(i + 1); 313 checkReleaseCode(internalConfig); 314 } else { 315 internalConfig.moduleCodeNameBase = moduleCodeName; 316 } 317 internalConfig.moduleSpecificationVersion = attrs.getValue("spec"); } 319 } 320 321 323 private void checkReleaseCode (InternalConfig internalConfig) { 324 if("null".equals(internalConfig.moduleCodeNameRelease)) { Logger.getLogger(TCRefParser.class.getName()).log(Level.WARNING, null, 328 new IllegalStateException ("Module release code was saved as null string" + 329 " for module " + 330 internalConfig.moduleCodeNameBase + 331 "! Repairing.")); 332 internalConfig.moduleCodeNameRelease = null; 333 } 334 } 335 336 337 private void handleTcId (Attributes attrs) throws SAXException { 338 String tc_id = attrs.getValue("id"); if (tc_id != null) { 340 tcRefConfig.tc_id = tc_id; 341 if (!tc_id.equals(TCRefParser.this.getName())) { 342 PersistenceManager.LOG.log(Level.WARNING, 343 "[WinSys.TCRefParser.handleTcId]" + " Error: Value of attribute \"id\" of element \"tc-id\"" + " and configuration file name must be the same."); throw new SAXException("Invalid attribute value"); } 348 } else { 349 PersistenceManager.LOG.log(Level.WARNING, 350 "[WinSys.TCRefParser.handleTcId]" + " Error: Missing required attribute \"id\" of element \"tc-id\"."); throw new SAXException("Missing required attribute"); } 354 } 355 356 private void handleState (Attributes attrs) throws SAXException { 357 String opened = attrs.getValue("opened"); if (opened != null) { 359 if ("true".equals(opened)) { tcRefConfig.opened = true; 361 } else if ("false".equals(opened)) { tcRefConfig.opened = false; 363 } else { 364 PersistenceManager.LOG.log(Level.WARNING, 365 "[WinSys.TCRefParser.handleState]" + " Warning: Invalid value of attribute \"opened\"" + " of element \"state\"."); tcRefConfig.opened = false; 369 } 370 } else { 371 PersistenceManager.LOG.log(Level.WARNING, 372 "[WinSys.TCRefParser.handleState]" + " Warning: Missing required attribute \"opened\"" + " of element \"state\"."); tcRefConfig.opened = false; 376 } 377 } 378 379 private void handlePreviousMode (Attributes attrs) throws SAXException { 380 String name = attrs.getValue("name"); if (name != null) { 382 tcRefConfig.previousMode = name; 383 } else { 384 PersistenceManager.LOG.log(Level.WARNING, 385 "[WinSys.TCRefParser.handlePreviousMode]" + " Warning: Missing required attribute \"name\"" + " of element \"previousMode\"."); tcRefConfig.previousMode = null; 389 } 390 391 String index = attrs.getValue("index"); if (index != null) { 393 try { 394 tcRefConfig.previousIndex = Integer.parseInt( index ); 395 } catch( NumberFormatException nfE ) { 396 PersistenceManager.LOG.log(Level.WARNING, 397 "[WinSys.TCRefParser.handlePreviousMode]" + " Warning: Invalid value of attribute \"index\"" + " of element \"previousMode\"."); tcRefConfig.previousIndex = -1; 401 } 402 } 403 } 404 405 private void handleDockingStatus (Attributes attrs) throws SAXException { 406 String status = attrs.getValue("maximized-mode"); if (status != null) { 408 if ("docked".equals(status)) { tcRefConfig.dockedInMaximizedMode = true; 410 } else if ("slided".equals(status)) { tcRefConfig.dockedInMaximizedMode = false; 412 } else { 413 PersistenceManager.LOG.log(Level.WARNING, 414 "[WinSys.TCRefParser.handleDockingStatus]" + " Warning: Invalid value of attribute \"maximized-mode\"" + " of element \"docking-status\"."); tcRefConfig.dockedInMaximizedMode = false; 418 } 419 } 420 status = attrs.getValue("default-mode"); if (status != null) { 422 if ("docked".equals(status)) { tcRefConfig.dockedInDefaultMode = true; 424 } else if ("slided".equals(status)) { tcRefConfig.dockedInDefaultMode = false; 426 } else { 427 PersistenceManager.LOG.log(Level.WARNING, 428 "[WinSys.TCRefParser.handleDockingStatus]" + " Warning: Invalid value of attribute \"default-mode\"" + " of element \"docking-status\"."); tcRefConfig.dockedInDefaultMode = true; 432 } 433 } 434 } 435 436 private void handleSlideInStatus (Attributes attrs) throws SAXException { 437 String status = attrs.getValue("maximized"); if (status != null) { 439 if ("true".equals(status)) { tcRefConfig.slidedInMaximized = true; 441 } else if ("false".equals(status)) { tcRefConfig.slidedInMaximized = false; 443 } else { 444 PersistenceManager.LOG.log(Level.WARNING, 445 "[WinSys.TCRefParser.handleSlideInStatus]" + " Warning: Invalid value of attribute \"maximized\"" + " of element \"slide-in-status\"."); tcRefConfig.slidedInMaximized = false; 449 } 450 } 451 } 452 453 454 void writeData (TCRefConfig tcRefCfg, InternalConfig ic) throws IOException { 455 final StringBuffer buff = fillBuffer(tcRefCfg, ic); 456 synchronized (RW_LOCK) { 457 FileObject cfgFOOutput = getConfigFOOutput(); 458 FileLock lock = null; 459 OutputStream os = null; 460 OutputStreamWriter osw = null; 461 try { 462 lock = cfgFOOutput.lock(); 463 os = cfgFOOutput.getOutputStream(lock); 464 osw = new OutputStreamWriter(os, "UTF-8"); osw.write(buff.toString()); 466 } finally { 469 try { 470 if (osw != null) { 471 osw.close(); 472 } 473 } catch (IOException exc) { 474 Logger.getLogger(TCRefParser.class.getName()).log(Level.WARNING, null, exc); 475 } 476 if (lock != null) { 477 lock.releaseLock(); 478 } 479 } 480 } 481 } 482 483 485 private StringBuffer fillBuffer (TCRefConfig tcRefCfg, InternalConfig ic) throws IOException { 486 StringBuffer buff = new StringBuffer (800); 487 String curValue = null; 488 buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"); 493 buff.append("<tc-ref version=\"2.2\">\n"); 495 appendModule(ic, buff); 496 appendTcId(tcRefCfg, buff); 497 appendState(tcRefCfg, buff); 498 if (tcRefCfg.previousMode != null) { 499 appendPreviousMode(tcRefCfg, buff); 500 } 501 appendDockingStatus( tcRefCfg, buff ); 502 appendSlideInStatus( tcRefCfg, buff ); 503 504 buff.append("</tc-ref>\n"); return buff; 506 } 507 508 private void appendModule (InternalConfig ic, StringBuffer buff) { 509 if (ic == null) { 510 return; 511 } 512 if (ic.moduleCodeNameBase != null) { 513 buff.append(" <module"); buff.append(" name=\""); buff.append(ic.moduleCodeNameBase); 516 if (ic.moduleCodeNameRelease != null) { 517 buff.append("/" + ic.moduleCodeNameRelease); } 519 if (ic.moduleSpecificationVersion != null) { 520 buff.append("\" spec=\""); buff.append(ic.moduleSpecificationVersion); 522 } 523 buff.append("\" />\n"); } 525 } 526 527 private void appendTcId (TCRefConfig tcRefCfg, StringBuffer buff) { 528 buff.append(" <tc-id"); buff.append(" id=\""); 531 buff.append(PersistenceManager.escapeTcId4XmlContent(tcRefCfg.tc_id)); 532 buff.append("\""); buff.append(" />\n"); } 535 536 private void appendState (TCRefConfig tcRefCfg, StringBuffer buff) { 537 buff.append(" <state"); buff.append(" opened=\""); if (tcRefCfg.opened) { 540 buff.append("true"); } else { 542 buff.append("false"); } 544 buff.append("\""); buff.append(" />\n"); } 547 548 private void appendDockingStatus (TCRefConfig tcRefCfg, StringBuffer buff) { 549 if( tcRefCfg.dockedInMaximizedMode || !tcRefCfg.dockedInDefaultMode ) { 550 buff.append(" <docking-status"); if( tcRefCfg.dockedInMaximizedMode ) 552 buff.append(" maximized-mode=\"docked\""); if( !tcRefCfg.dockedInDefaultMode ) 554 buff.append(" default-mode=\"slided\""); buff.append(" />\n"); } 557 } 558 559 private void appendSlideInStatus (TCRefConfig tcRefCfg, StringBuffer buff) { 560 if( tcRefCfg.slidedInMaximized ) { 561 buff.append(" <slide-in-status maximized=\"true\" />\n"); } 563 } 564 565 private void appendPreviousMode(TCRefConfig tcRefCfg, StringBuffer buff) { 566 buff.append(" <previousMode name=\""); buff.append(tcRefCfg.previousMode).append("\" "); 568 if( tcRefCfg.previousIndex >= 0 ) 569 buff.append( " index=\"" ).append( tcRefCfg.previousIndex ).append( "\" " ); 570 buff.append(" />\n"); } 572 573 } 574 575 } 576 | Popular Tags |