1 19 package org.netbeans.tests.xml; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyVetoException ; 24 import java.io.*; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.Iterator ; 28 import java.util.Random ; 29 import javax.swing.text.BadLocationException ; 30 import javax.swing.text.StyledDocument ; 31 import org.netbeans.api.java.classpath.ClassPath; 32 import org.netbeans.jellytools.Bundle; 33 import org.netbeans.modules.xml.tax.cookies.TreeDocumentCookie; 34 import org.netbeans.tax.TreeDocument; 35 import org.netbeans.tax.TreeException; 36 import org.netbeans.tax.TreeNode; 37 import org.netbeans.tax.io.XMLStringResult; 38 import org.openide.cookies.EditorCookie; 39 import org.openide.cookies.SaveCookie; 40 import org.openide.filesystems.*; 41 import org.openide.filesystems.FileSystem.AtomicAction; 42 import org.openide.loaders.DataFolder; 43 import org.openide.loaders.DataObject; 44 import org.openide.loaders.DataObjectNotFoundException; 45 import org.openide.loaders.XMLDataObject; 46 import org.openide.modules.ModuleInfo; 47 import org.openide.util.Lookup; 48 import org.openide.util.NbBundle; 49 import org.openide.util.Lookup.Template; 50 51 52 56 public abstract class AbstractTestUtil { 57 protected static boolean DEBUG = true; 58 59 public static final String CATALOG_PACKAGE = "org.netbeans.modules.xml.catalog"; 60 public static final String CORE_PACKAGE = "org.netbeans.modules.xml.core"; 61 public static final String TAX_PACKAGE = "org.netbeans.modules.xml.tax"; 62 public static final String TEXT_PACKAGE = "org.netbeans.modules.xml.text"; 63 public static final String TOOLS_PACKAGE = "org.netbeans.modules.xml.tools"; 64 public static final String TREE_PACKAGE = "org.netbeans.modules.xml.tree"; 65 66 70 73 public static String nodeToString(TreeNode node) { 74 try { 75 return XMLStringResult.toString(node); 76 } catch (TreeException te) { 77 return null; 78 } 79 } 80 81 84 public static String nodeToString(Object node) { 85 return node.toString(); 86 } 87 88 92 99 public static String replaceString(String original, String begin, String end, String replaceTo) { 100 int bi = original.indexOf(begin); 101 int ei = original.indexOf(end, bi) + end.length(); 102 if (bi < 0 || ei < 0) { 103 return original; 104 } else { 105 return original.substring(0, bi) + replaceTo + original.substring(ei, original.length()); 106 } 107 } 108 109 112 public static String removeChar(String str, char ch) { 113 int index = str.indexOf(ch); 114 115 if (index > -1) { 116 StringBuffer sb = new StringBuffer (str).deleteCharAt(str.indexOf(ch)); 117 return new String (sb); 118 } else { 119 return str; 120 } 121 } 122 123 126 public static String joinElements(String [] elements, String delim) { 127 if (elements == null) { 128 return null; 129 } 130 131 String path = elements[0]; 132 for (int i = 1; i < elements.length; i++) { 133 path += (delim + elements[i]); 134 } 135 return path; 136 } 137 138 141 public static final String lastElement(String string, String delim) { 142 int index = string.lastIndexOf(delim); 143 if (index == -1) { 144 return string; 145 } else { 146 return string.substring(index + 1); 147 } 148 } 149 150 154 158 public final String getStringTrimmed(String key) { 159 return Bundle.getStringTrimmed(getBundel(), key); 160 } 161 162 166 public final String getString(String key) { 167 return NbBundle.getMessage(this.getClass(), key); 168 } 169 170 175 public final String getString(String key, Object param) { 176 return NbBundle.getMessage(this.getClass(), key, param); 177 } 178 179 185 public final String getString(String key, Object param1, Object param2) { 186 return NbBundle.getMessage(this.getClass(), key, param1, param2); 187 } 188 189 193 public final char getChar(String key) { 194 return NbBundle.getMessage(this.getClass(), key).charAt(0); 195 } 196 197 private String getBundel() { 198 return this.getClass().getPackage().getName() + ".Bundle"; 199 } 200 201 205 207 public static String dataObjectToString(DataObject dataObject) throws IOException, BadLocationException { 208 EditorCookie editorCookie = (EditorCookie) dataObject.getCookie(EditorCookie.class); 209 210 if (editorCookie != null) { 211 StyledDocument document = editorCookie.openDocument(); 212 if (document != null) { 213 return document.getText(0, document.getLength()); 214 } 215 } 216 return null; 217 } 218 219 221 public static void saveDataObject(DataObject dataObject) throws IOException { 222 SaveCookie cookie = (SaveCookie) dataObject.getCookie(SaveCookie.class); 223 if (cookie == null) throw new IllegalStateException ("Cannot save document without SaveCookie."); 224 cookie.save(); 225 } 226 227 231 234 public static LocalFileSystem mountDirectory(File dir) throws PropertyVetoException , IOException { 235 LocalFileSystem fs = new LocalFileSystem(); 236 fs.setRootDirectory(dir); 237 Repository rep = Repository.getDefault(); 238 FileSystem ffs = rep.findFileSystem(fs.getSystemName()); 239 if (ffs != null) { 240 rep.removeFileSystem(ffs); 241 } 242 rep.addFileSystem(fs); 243 return fs; 244 } 245 246 249 public static TreeDocument openXMLDocument(String aPackage, String name, String ext) throws IOException { 250 DataObject dao = findDataObject(aPackage, name, ext); 251 252 if (dao == null) { 253 throw new IOException(aPackage + "." + name + "." + ext + " data object not found."); 254 } 255 256 XMLDataObject xmlDataObject; 257 if (XMLDataObject.class.isInstance(dao)) { 258 xmlDataObject = (XMLDataObject) dao; 259 } else { 260 throw new IOException(aPackage + "." + name + "." + ext + " data object is not XMLDataObject."); 261 } 262 263 TreeDocumentCookie cookie = (TreeDocumentCookie) xmlDataObject.getCookie(TreeDocumentCookie.class); 264 if (cookie == null) { 265 throw new IOException("Missing TreeDocumentCookie at " + aPackage + "." + name + "." + ext); 266 } 267 268 TreeDocument document = (TreeDocument) cookie.getDocumentRoot(); 269 if (document == null) { 270 throw new IOException("Ivalid XML data object" + aPackage + "." + name + "." + ext); 271 } 272 273 return document; 274 } 275 276 279 public static void deleteFileObject(FileObject fo) throws IOException { 280 DataObject dataObject = DataObject.find(fo); 281 dataObject.getNodeDelegate().destroy(); 282 } 283 284 287 public static DataFolder findFolder(String aPackage) throws Exception { 288 return (DataFolder) findDataObject(aPackage, null, null); 289 } 290 291 294 public static String toAbsolutePath(FileObject fo) { 295 return FileUtil.toFile(fo).getAbsolutePath(); 296 } 297 298 301 public static DataObject findDataObject(String aPackage, String name, String ext) throws DataObjectNotFoundException { 302 FileObject fo = null; 303 fo = Repository.getDefault().find(aPackage, name, ext); 304 if (fo == null) { 305 return null; 306 } else { 307 return DataObject.find(fo); 308 } 309 } 310 311 314 public static FileObject findFileObject(String aPackage, String name, String ext) { 315 return Repository.getDefault().find(aPackage, name, ext); 316 } 317 318 323 public DataObject findData(String name) throws DataObjectNotFoundException { 324 String resName = this.getClass().getPackage().getName(); 326 resName = resName.replace('.', '/'); 327 resName += "/data/" + name; 328 FileObject fo = ClassPath.getClassPath(null, ClassPath.EXECUTE).findResource(resName); 329 if (fo == null) { 330 if (DEBUG) { 331 System.err.println("Cannot find FileObject: " + resName); 332 } 333 return null; 334 } else { 335 return DataObject.find(fo); 336 } 337 } 338 339 343 public static DataObject findDataObject(String name) throws DataObjectNotFoundException { 344 FileObject fo = findFileObject(name); 345 if (fo == null) { 346 if (DEBUG) { 347 System.err.println("Cannot find FileObject: " + name); 348 } 349 return null; 350 } else { 351 return DataObject.find(fo); 352 } 353 } 354 355 359 public static FileObject findFileObject(String name) { 360 FileObject fo = null; 361 if (name.startsWith("nbfs:")) { 362 try { 363 fo = URLMapper.findFileObject(new URL (name)); 364 } catch (MalformedURLException mue) {}; 365 } else { 366 fo = Repository.getDefault().findResource(name); 367 } 368 return fo; 369 } 370 371 374 public static DataObject getTemplate(String tname) throws DataObjectNotFoundException { 375 FileObject fileObject = Repository.getDefault().findResource("Templates/" + tname); 376 if (fileObject == null) { 377 throw new IllegalArgumentException ("Cannot find template: " + tname); 378 } 379 return DataObject.find(fileObject); 380 } 381 382 386 public static DataObject newFromTemplate(String tname, String folder, String name) throws IOException { 387 DataObject dataObject = getTemplate(tname); 388 DataFolder dataFolder = (DataFolder) findDataObject(folder); 389 return dataObject.createFromTemplate(dataFolder, name); 390 } 391 392 396 public static boolean removeDocument(String name) throws IOException { 397 DataObject dataObject = findDataObject(name); 398 if (dataObject != null) { 399 dataObject.delete(); 400 return true; 401 } else { 402 return false; 403 } 404 } 405 406 409 public static DataObject createDataObject(DataFolder folder, final String name, final String extension, final String content) throws IOException { 410 final FileObject targetFolder = folder.getPrimaryFile(); 411 FileSystem filesystem = targetFolder.getFileSystem(); 412 413 final FileObject[] fileObject = new FileObject[1]; 414 AtomicAction fsAction = new AtomicAction() { 415 public void run() throws IOException { 416 FileObject fo = targetFolder.createData(name, extension); 417 FileLock lock = null; 418 try { 419 lock = fo.lock(); 420 OutputStream out = fo.getOutputStream(lock); 421 out = new BufferedOutputStream(out, 999); 422 Writer writer = new OutputStreamWriter(out, "UTF8"); writer.write(content + '\n'); writer.flush(); 425 writer.close(); 426 427 lock.releaseLock(); 429 lock = null; 430 431 fileObject[0] = fo; 432 433 } finally { 434 if (lock != null) lock.releaseLock(); 435 } 436 } 437 }; 438 439 filesystem.runAtomicAction(fsAction); 440 return DataObject.find(fileObject[0]); 441 } 442 443 444 445 449 452 public static void switchModule(String codeName, boolean enable) throws Exception { 453 String statusFile = "Modules/" + codeName.replace('.', '-') + ".xml"; 454 ModuleInfo mi = getModuleInfo(codeName); 455 475 476 if (mi.isEnabled() == enable) { 478 return; 479 } 480 481 DataObject data = findDataObject(statusFile); 482 EditorCookie ec = (EditorCookie) data.getCookie(EditorCookie.class); 483 StyledDocument doc = ec.openDocument(); 484 485 String stag = "<param name=\"enabled\">"; 487 String etag = "</param>"; 488 String enabled = enable ? "true" : "false"; 489 String result; 490 491 String str = doc.getText(0,doc.getLength()); 492 int sindex = str.indexOf(stag); 493 int eindex = str.indexOf(etag, sindex); 494 if (sindex > -1 && eindex > sindex) { 495 result = str.substring(0, sindex + stag.length()) + enabled + str.substring(eindex); 496 } else { 498 return; 501 } 502 503 final Waiter waiter = new Waiter(); 505 final PropertyChangeListener pcl = new PropertyChangeListener () { 506 public void propertyChange(PropertyChangeEvent evt) { 507 if (evt.getPropertyName().equals("enabled")) { 508 waiter.notifyFinished(); 509 } 510 } 511 }; 512 mi.addPropertyChangeListener(pcl); 513 514 doc.remove(0,doc.getLength()); 516 doc.insertString(0,result,null); 517 ec.saveDocument(); 518 519 waiter.waitFinished(); 521 mi.removePropertyChangeListener(pcl); 522 } 523 524 527 public static boolean switchAllXMLModules(boolean enable) throws Exception { 528 boolean result = false; 529 Iterator it = Lookup.getDefault().lookup(new Template(ModuleInfo.class)).allInstances().iterator(); 530 531 while (it.hasNext()) { 532 ModuleInfo mi = (ModuleInfo) it.next(); 533 if (mi.getCodeNameBase().startsWith("org.netbeans.modules.xml.") && (mi.isEnabled() != enable)) { 534 switchModule(mi.getCodeNameBase(), enable); 535 result = true; 536 } 537 } 538 return result; 539 } 540 541 544 public static ModuleInfo getModuleInfo(String codeName) { 545 Iterator it = Lookup.getDefault().lookup(new Template(ModuleInfo.class)).allInstances().iterator(); 546 547 while (it.hasNext()) { 548 ModuleInfo mi = (ModuleInfo) it.next(); 549 if (mi.getCodeNameBase().equals(codeName)) { 551 return mi; 552 } 553 } 554 return null; 555 } 556 557 560 public static boolean isModuleEnabled(String codeName) { 561 ModuleInfo mi = getModuleInfo(codeName); 562 if (mi == null) { 563 throw new IllegalArgumentException ("Invalid codeName: " + codeName); 564 } 565 566 return mi.isEnabled(); 567 } 568 569 protected static Random randomGenerator = new Random (); 570 571 574 public static int randomInt(int n) { 575 return randomGenerator.nextInt(n); 576 } 577 578 582 static class Waiter { 583 private boolean finished = false; 584 585 587 public void start() { 588 finished = false; 589 } 590 591 593 public void waitFinished() { 594 if (!finished) { 595 synchronized (this) { 596 while (!finished) { 597 try { 598 wait(); 599 } catch (InterruptedException ex) { 600 } 601 } 602 } 603 } 604 } 605 606 608 public void notifyFinished() { 609 synchronized (this) { 610 finished = true; 611 notifyAll(); 612 } 613 } 614 } 615 } 616 | Popular Tags |