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 TCGroupParser { 42 43 public static final String INSTANCE_DTD_ID_2_0 44 = "-//NetBeans//DTD Top Component in Group Properties 2.0//EN"; 46 private static final boolean DEBUG = Debug.isLoggable(TCGroupParser.class); 47 48 49 private String tc_id; 50 51 52 private FileObject moduleParentFolder; 53 54 55 private FileObject localParentFolder; 56 57 private InternalConfig internalConfig; 58 59 60 private boolean inModuleFolder; 61 62 private boolean inLocalFolder; 63 64 public TCGroupParser(String tc_id) { 65 this.tc_id = tc_id; 66 } 67 68 69 TCGroupConfig load () throws IOException { 70 if (DEBUG) Debug.log(TCGroupParser.class, "load ENTER" + " tcGrp:" + tc_id); 71 TCGroupConfig tcGroupCfg = new TCGroupConfig(); 72 PropertyHandler propertyHandler = new PropertyHandler(); 73 InternalConfig internalCfg = getInternalConfig(); 74 internalCfg.clear(); 75 propertyHandler.readData(tcGroupCfg, internalCfg); 76 if (DEBUG) Debug.log(TCGroupParser.class, "load LEAVE" + " tcGrp:" + tc_id); 77 return tcGroupCfg; 78 } 79 80 81 void save (TCGroupConfig tcGroupCfg) throws IOException { 82 if (DEBUG) Debug.log(TCGroupParser.class, "save ENTER" + " tcGrp:" + tc_id); 83 84 PropertyHandler propertyHandler = new PropertyHandler(); 85 InternalConfig internalCfg = getInternalConfig(); 86 propertyHandler.writeData(tcGroupCfg, internalCfg); 87 if (DEBUG) Debug.log(TCGroupParser.class, "save LEAVE" + " tcGrp:" + tc_id); 88 } 89 90 String getName () { 91 return tc_id; 92 } 93 94 97 InternalConfig getInternalConfig () { 98 if (internalConfig == null) { 99 internalConfig = new InternalConfig(); 100 } 101 return internalConfig; 102 } 103 104 boolean isInModuleFolder () { 105 return inModuleFolder; 106 } 107 108 void setInModuleFolder (boolean inModuleFolder) { 109 this.inModuleFolder = inModuleFolder; 110 } 111 112 boolean isInLocalFolder () { 113 return inLocalFolder; 114 } 115 116 void setInLocalFolder (boolean inLocalFolder) { 117 this.inLocalFolder = inLocalFolder; 118 } 119 120 void setModuleParentFolder (FileObject moduleParentFolder) { 121 this.moduleParentFolder = moduleParentFolder; 122 } 123 124 void setLocalParentFolder (FileObject localParentFolder) { 125 this.localParentFolder = localParentFolder; 126 } 127 128 void log (String s) { 129 Debug.log(TCGroupParser.class, s); 130 } 131 132 private final class PropertyHandler extends DefaultHandler { 133 134 135 private TCGroupConfig tcGroupConfig = null; 136 137 138 private InternalConfig internalConfig = null; 139 140 141 private final Object RW_LOCK = new Object (); 142 143 public PropertyHandler () { 144 } 145 146 private FileObject getConfigFOInput () { 147 FileObject tcGroupConfigFO; 148 if (isInLocalFolder()) { 149 tcGroupConfigFO = localParentFolder.getFileObject 151 (TCGroupParser.this.getName(), PersistenceManager.TCGROUP_EXT); 152 } else if (isInModuleFolder()) { 153 tcGroupConfigFO = moduleParentFolder.getFileObject 155 (TCGroupParser.this.getName(), PersistenceManager.TCGROUP_EXT); 156 } else { 157 tcGroupConfigFO = null; 159 } 160 return tcGroupConfigFO; 162 } 163 164 private FileObject getConfigFOOutput () throws IOException { 165 FileObject tcGroupConfigFO; 166 tcGroupConfigFO = localParentFolder.getFileObject 167 (TCGroupParser.this.getName(), PersistenceManager.TCGROUP_EXT); 168 if (tcGroupConfigFO != null) { 169 return tcGroupConfigFO; 171 } else { 172 StringBuffer buffer = new StringBuffer (); 173 buffer.append(TCGroupParser.this.getName()); 174 buffer.append('.'); 175 buffer.append(PersistenceManager.TCGROUP_EXT); 176 tcGroupConfigFO = FileUtil.createData(localParentFolder, buffer.toString()); 178 180 return tcGroupConfigFO; 181 } 182 } 183 187 void readData (TCGroupConfig tcGroupCfg, InternalConfig internalCfg) 188 throws IOException { 189 tcGroupConfig = tcGroupCfg; 190 internalConfig = internalCfg; 191 192 FileObject cfgFOInput = getConfigFOInput(); 193 if (cfgFOInput == null) { 194 throw new FileNotFoundException("[WinSys] Missing TCGroup configuration file:" + TCGroupParser.this.getName()); 196 } 197 InputStream is = null; 198 try { 199 synchronized (RW_LOCK) { 200 207 is = cfgFOInput.getInputStream(); 209 PersistenceManager.getDefault().getXMLParser(this).parse(new InputSource(is)); 210 } 211 } catch (SAXException exc) { 212 String msg = NbBundle.getMessage(TCGroupParser.class, 214 "EXC_TCGroupParse", cfgFOInput); 215 216 throw (IOException) new IOException(msg).initCause(exc); 217 } finally { 218 try { 219 if (is != null) { 220 is.close(); 221 } 222 } catch (IOException exc) { 223 Logger.getLogger(TCGroupParser.class.getName()).log(Level.WARNING, null, exc); 224 } 225 } 226 227 tcGroupCfg = tcGroupConfig; 228 internalCfg = internalConfig; 229 230 tcGroupConfig = null; 231 internalConfig = null; 232 } 233 234 public void startElement (String nameSpace, String name, String qname, Attributes attrs) 235 throws SAXException { 236 if ("tc-group".equals(qname)) { handleTCGroup(attrs); 238 } else if (internalConfig.specVersion.compareTo(new SpecificationVersion("2.0")) == 0) { if ("module".equals(qname)) { handleModule(attrs); 242 } else if ("tc-id".equals(qname)) { handleTcId(attrs); 244 } else if ("open-close-behavior".equals(qname)) { handleOpenCloseBehavior(attrs); 246 } 247 } else { 248 log("-- TCGroupParser.startElement PARSING OLD"); 249 } 251 } 252 253 public void error(SAXParseException ex) throws SAXException { 254 throw ex; 255 } 256 257 258 private void handleTCGroup (Attributes attrs) { 259 String version = attrs.getValue("version"); if (version != null) { 261 internalConfig.specVersion = new SpecificationVersion(version); 262 } else { 263 PersistenceManager.LOG.log(Level.WARNING, 264 "[WinSys.TCGroupParser.handleTCGroup]" + " Warning: Missing attribute \"version\" of element \"tc-group\"."); internalConfig.specVersion = new SpecificationVersion("2.0"); } 268 } 269 270 271 private void handleModule (Attributes attrs) { 272 String moduleCodeName = attrs.getValue("name"); internalConfig.moduleCodeNameBase = null; 275 internalConfig.moduleCodeNameRelease = null; 276 internalConfig.moduleSpecificationVersion = null; 277 if (moduleCodeName != null) { 278 int i = moduleCodeName.indexOf('/'); 279 if (i != -1) { 280 internalConfig.moduleCodeNameBase = moduleCodeName.substring(0, i); 281 internalConfig.moduleCodeNameRelease = moduleCodeName.substring(i + 1); 282 checkReleaseCode(internalConfig); 283 } else { 284 internalConfig.moduleCodeNameBase = moduleCodeName; 285 } 286 internalConfig.moduleSpecificationVersion = attrs.getValue("spec"); } 288 } 289 290 292 private void checkReleaseCode (InternalConfig internalConfig) { 293 if("null".equals(internalConfig.moduleCodeNameRelease)) { Logger.getLogger(TCGroupParser.class.getName()).log(Level.WARNING, null, 297 new IllegalStateException ("Module release code was saved as null string" + 298 " for module " + 299 internalConfig.moduleCodeNameBase + 300 "! Repairing.")); 301 internalConfig.moduleCodeNameRelease = null; 302 } 303 } 304 305 306 private void handleTcId (Attributes attrs) throws SAXException { 307 String tc_id = attrs.getValue("id"); if (tc_id != null) { 309 tcGroupConfig.tc_id = tc_id; 310 if (!tc_id.equals(TCGroupParser.this.getName())) { 311 PersistenceManager.LOG.log(Level.WARNING, 312 "[WinSys.TCGroupParser.handleTcId]" + " Error: Value of attribute \"id\" of element \"tc-id\"" + " and configuration file name must be the same."); throw new SAXException("Invalid attribute value"); } 317 } else { 318 PersistenceManager.LOG.log(Level.WARNING, 319 "[WinSys.TCGroupParser.handleTcId]" + " Error: Missing required attribute \"id\" of element \"tc-id\"."); throw new SAXException("Missing required attribute"); } 323 } 324 325 326 private void handleOpenCloseBehavior (Attributes attrs) throws SAXException { 327 String open = attrs.getValue("open"); if (open != null) { 329 if ("true".equals(open)) { tcGroupConfig.open = true; 331 } else if ("false".equals(open)) { tcGroupConfig.open = false; 333 } else { 334 PersistenceManager.LOG.log(Level.WARNING, 335 "[WinSys.TCGroupParser.handleOpenCloseBehavior]" + " Warning: Invalid value of attribute \"open\"" + " of element \"open-close-behavior\"."); tcGroupConfig.open = false; 339 } 340 } else { 341 PersistenceManager.LOG.log(Level.WARNING, 342 "[WinSys.TCGroupParser.handleOpenCloseBehavior]" + " Warning: Missing required attribute \"open\"" + " of element \"open-close-behavior\"."); tcGroupConfig.open = false; 346 } 347 348 String close = attrs.getValue("close"); if (close != null) { 350 if ("true".equals(close)) { tcGroupConfig.close = true; 352 } else if ("false".equals(close)) { tcGroupConfig.close = false; 354 } else { 355 PersistenceManager.LOG.log(Level.WARNING, 356 "[WinSys.TCGroupParser.handleOpenCloseBehavior]" + " Warning: Invalid value of attribute \"close\"" + " of element \"open-close-behavior\"."); tcGroupConfig.close = false; 360 } 361 } else { 362 PersistenceManager.LOG.log(Level.WARNING, 363 "[WinSys.TCGroupParser.handleOpenCloseBehavior]" + " Warning: Missing required attribute \"close\"" + " of element \"open-close-behavior\"."); tcGroupConfig.close = false; 367 } 368 369 String wasOpened = attrs.getValue("was-opened"); if (wasOpened != null) { 371 if ("true".equals(wasOpened)) { tcGroupConfig.wasOpened = true; 373 } else if ("false".equals(wasOpened)) { tcGroupConfig.wasOpened = false; 375 } else { 376 PersistenceManager.LOG.log(Level.WARNING, 377 "[WinSys.TCGroupParser.handleOpenCloseBehavior]" + " Warning: Invalid value of attribute \"was-opened\"" + " of element \"open-close-behavior\"."); tcGroupConfig.wasOpened = false; 381 } 382 } else { 383 tcGroupConfig.wasOpened = false; 384 } 385 } 386 387 388 void writeData (TCGroupConfig tcGroupCfg, InternalConfig ic) throws IOException { 389 final StringBuffer buff = fillBuffer(tcGroupCfg, ic); 390 synchronized (RW_LOCK) { 391 FileObject cfgFOOutput = getConfigFOOutput(); 392 FileLock lock = null; 393 OutputStream os = null; 394 OutputStreamWriter osw = null; 395 try { 396 lock = cfgFOOutput.lock(); 397 os = cfgFOOutput.getOutputStream(lock); 398 osw = new OutputStreamWriter(os, "UTF-8"); osw.write(buff.toString()); 400 } finally { 403 try { 404 if (osw != null) { 405 osw.close(); 406 } 407 } catch (IOException exc) { 408 Logger.getLogger(TCGroupParser.class.getName()).log(Level.WARNING, null, exc); 409 } 410 if (lock != null) { 411 lock.releaseLock(); 412 } 413 } 414 } 415 } 416 417 419 private StringBuffer fillBuffer (TCGroupConfig tcGroupCfg, InternalConfig ic) throws IOException { 420 StringBuffer buff = new StringBuffer (800); 421 String curValue = null; 422 buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"). 427 append("<tc-group version=\"2.0\">\n"); 429 appendModule(ic, buff); 430 appendTcId(tcGroupCfg, buff); 431 appendOpenCloseBehavior(tcGroupCfg, buff); 432 433 buff.append("</tc-group>\n"); return buff; 435 } 436 437 private void appendModule (InternalConfig ic, StringBuffer buff) { 438 if (ic == null) { 439 return; 440 } 441 if (ic.moduleCodeNameBase != null) { 442 buff.append(" <module name=\""); buff.append(ic.moduleCodeNameBase); 444 if (ic.moduleCodeNameRelease != null) { 445 buff.append("/").append(ic.moduleCodeNameRelease); } 447 if (ic.moduleSpecificationVersion != null) { 448 buff.append("\" spec=\""); buff.append(ic.moduleSpecificationVersion); 450 } 451 buff.append("\" />\n"); } 453 } 454 455 private void appendTcId (TCGroupConfig tcGroupCfg, StringBuffer buff) { 456 buff.append(" <tc-id id=\"").append( 457 PersistenceManager.escapeTcId4XmlContent(tcGroupCfg.tc_id)). 458 append("\"/>\n"); } 460 461 private void appendOpenCloseBehavior (TCGroupConfig tcGroupCfg, StringBuffer buff) { 462 buff.append(" <open-close-behavior open=\"").append(tcGroupCfg.open). append("\" close=\"").append(tcGroupCfg.close). append("\" was-opened=\"").append(tcGroupCfg.wasOpened).append("\"/>\n"); } 466 467 } 468 469 } 470 | Popular Tags |