1 19 20 package org.netbeans.modules.xml.retriever.catalog; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.UnsupportedEncodingException ; 31 import java.lang.management.ManagementFactory ; 32 import java.net.MalformedURLException ; 33 import java.net.Proxy ; 34 import java.net.URI ; 35 import java.net.URISyntaxException ; 36 import java.net.URL ; 37 import java.net.URLConnection ; 38 import java.net.UnknownHostException ; 39 import java.util.ArrayList ; 40 import java.util.Collections ; 41 import java.util.HashMap ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Map ; 45 import java.util.Map.Entry; 46 import java.util.Set ; 47 import java.util.StringTokenizer ; 48 import java.util.logging.Level ; 49 import java.util.logging.Logger ; 50 import javax.swing.text.Document ; 51 import javax.xml.namespace.NamespaceContext ; 52 import javax.xml.xpath.XPath ; 53 import javax.xml.xpath.XPathConstants ; 54 import javax.xml.xpath.XPathFactory ; 55 import org.netbeans.api.project.FileOwnerQuery; 56 import org.netbeans.api.project.Project; 57 import org.netbeans.api.project.ProjectUtils; 58 import org.netbeans.api.project.SourceGroup; 59 import org.netbeans.editor.BaseDocument; 60 import org.netbeans.modules.xml.retriever.Retriever; 61 import org.netbeans.modules.xml.retriever.RetrieverImpl; 62 import org.netbeans.modules.xml.retriever.XMLCatalogProvider; 63 import org.netbeans.modules.xml.retriever.catalog.impl.*; 64 import org.netbeans.modules.xml.xam.ModelSource; 65 import org.netbeans.modules.xml.xam.locator.CatalogModel; 66 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 67 import org.netbeans.modules.xml.xam.locator.CatalogModelFactory; 68 import org.netbeans.spi.project.CacheDirectoryProvider; 69 import org.netbeans.spi.xml.cookies.DataObjectAdapters; 70 import org.openide.cookies.EditorCookie; 71 import org.openide.filesystems.FileObject; 72 import org.openide.filesystems.FileUtil; 73 import org.openide.loaders.DataObject; 74 import org.openide.loaders.DataObjectNotFoundException; 75 import org.openide.util.Lookup; 76 import org.openide.util.UserQuestionException; 77 import org.openide.util.lookup.Lookups; 78 import org.w3c.dom.Node ; 79 import org.w3c.dom.NodeList ; 80 import org.xml.sax.InputSource ; 81 82 86 public class Utilities { 87 public static final String NO_NAME_SPACE = "NO_NAME_SPACE"; private static final Logger logger = getLogger(); 89 90 public static URL appendURL(URL prefixURL, String suffixStr){ 91 String str = prefixURL.toString(); 92 String newurl = null; 93 if(str.endsWith("/")) 94 newurl = str+suffixStr; 95 else 96 newurl = str+"/"+suffixStr; 97 try { 98 return new URL (newurl); 99 } catch (MalformedURLException ex) { 100 } 101 return null; 102 } 103 104 public static boolean localResourceExists(URL localURL){ 105 File locaFile = null; 106 try { 107 locaFile = new File (localURL.toURI()); 108 return locaFile.exists(); 109 } catch (URISyntaxException ex) { 110 } 111 return false; 112 113 } 114 115 public static File toFile(URL url){ 116 URI uri; 117 try { 118 uri = url.toURI(); 119 } catch (URISyntaxException ex) { 120 return null; 121 } 122 return new File (uri); 123 } 124 125 public static void deleteRecursively(File aDir){ 126 if(aDir.isDirectory()){ 127 File [] children = aDir.listFiles(); 129 for(File file: children) 130 deleteRecursively(file); 131 aDir.delete(); 133 }else{ 134 aDir.delete(); 137 } 138 } 139 140 public static String normalizeURI(String uriref) { 141 StringBuilder newRef = new StringBuilder (); 142 byte[] bytes; 143 144 if (uriref == null) { 145 return null; 146 } 147 148 try { 149 bytes = uriref.getBytes("UTF-8"); 150 } catch (UnsupportedEncodingException uee) { 151 return uriref; 153 } 154 155 for (int count = 0; count < bytes.length; count++) { 156 int ch = bytes[count] & 0xFF; 157 158 if ((ch <= 0x20) || (ch > 0x7F) || (ch == 0x22) || (ch == 0x3C) || (ch == 0x3E) || (ch == 0x5C) || (ch == 0x5E) || (ch == 0x60) || (ch == 0x7B) || (ch == 0x7C) || (ch == 0x7D) || (ch == 0x7F)) { 170 newRef.append(encodedByte(ch)); 171 } else { 172 newRef.append((char) bytes[count]); 173 } 174 } 175 176 return newRef.toString(); 177 } 178 179 public static String encodedByte(int b) { 180 String hex = Integer.toHexString(b).toUpperCase(); 181 if (hex.length() < 2) { 182 return "%0" + hex; 183 } else { 184 return "%" + hex; 185 } 186 } 187 188 193 public static String relativize(URI master, URI slave) { 194 String masterStr = master.toString(); 195 String slaveStr = slave.toString(); 196 StringTokenizer masterTok = new StringTokenizer (masterStr, "/"); 197 StringTokenizer slaveTok = new StringTokenizer (slaveStr, "/"); 198 String masterLast = null; 199 String slaveLast = null; 200 int iteration = -1; 201 while(true){ 202 iteration++; 203 if(masterTok.hasMoreTokens() && slaveTok.hasMoreTokens()){ 204 masterLast = masterTok.nextToken(); 205 slaveLast = slaveTok.nextToken(); 206 if(masterLast.equals(slaveLast)) 207 continue; 208 else 209 break; 210 } 211 break; 212 } 213 if(iteration < 2) 215 return slave.toString(); 216 int dirCount = masterTok.countTokens(); 218 String pathPrefix = ""; 220 StringBuffer pathPrefixBuff = new StringBuffer (""); 221 for(int i=0; i<dirCount;i++){ 222 pathPrefixBuff.append("../"); 223 } 224 pathPrefix = pathPrefixBuff.toString(); 225 StringBuilder slaveResult = masterLast.equals(slaveLast)? 227 new StringBuilder ():new StringBuilder (slaveLast); 228 while(slaveTok.hasMoreTokens()) { 229 if(slaveResult.length() != 0) 230 slaveResult.append('/'); 231 slaveResult.append(slaveTok.nextToken()); 232 } 233 return pathPrefix + slaveResult.toString(); 235 } 236 237 254 public static Logger getLogger(){ 255 return Logger.getLogger(Utilities.class.getName()); 256 } 257 258 public static List <FileObject> getFilesOfNSInProj(Project prj, DocumentTypesEnum docType, String nameSpace, List <String > sourceGroupTypeList){ 259 List <FileObject> result = new ArrayList <FileObject>(); 260 Map <FileObject, String > fobj2nsMap = getFiles2NSMappingInProj(prj, docType, sourceGroupTypeList); 261 Set <FileObject> fileList = fobj2nsMap.keySet(); 262 for(FileObject fobj: fileList){ 263 if(Thread.currentThread().isInterrupted()) 264 break; 266 if(fobj2nsMap.get(fobj).equals(nameSpace)) 267 result.add(fobj); 268 } 269 return result; 270 } 271 272 public static List <FileObject> getFilesOfNoNSInProj(Project prj, DocumentTypesEnum docType, List <String > sourceGroupTypeList){ 273 return getFilesOfNSInProj(prj, docType, NO_NAME_SPACE, sourceGroupTypeList); 274 } 275 276 277 public static Map <FileObject, String > getFiles2NSMappingInProj(Project prj, DocumentTypesEnum docType, List <String > sourceGroupTypeList){ 278 Map <FileObject, String > result = new HashMap <FileObject, String >(); 279 List <FileObject> rootList = getAllSourceRoots(prj, sourceGroupTypeList); 280 for(FileObject fob : rootList){ 281 result.putAll(getFiles2NSMappingInProj(FileUtil.toFile(fob), docType)); 282 } 283 return result; 284 } 285 286 private static List <FileObject> getAllSourceRoots(Project prj, List <String > sourceGroupTypeList){ 287 List <FileObject> result = new ArrayList <FileObject>(); 288 for(String type: sourceGroupTypeList){ 289 SourceGroup[] srcGrps = ProjectUtils.getSources(prj).getSourceGroups(type); 290 if(srcGrps != null){ 291 for(SourceGroup srcGrp : srcGrps) 292 result.add(srcGrp.getRootFolder()); 293 } 294 } 295 return result; 296 } 297 298 public static Map <FileObject, String > getFiles2NSMappingInProj(File rootFile, DocumentTypesEnum docType){ 299 List <File > fileList = getFilesWithExtension(rootFile, docType.toString(), new ArrayList <File >()); 300 Map <FileObject, String > result = new HashMap <FileObject, String >(); 301 String xpathQuery = null; 302 if(docType == docType.schema) 303 xpathQuery = "//xsd:schema/@targetNamespace"; 304 else 305 xpathQuery = "//wsdl:definitions/@targetNamespace"; 306 307 for(File file: fileList){ 308 if(Thread.currentThread().isInterrupted()) 309 break; 311 List <String > targetNSList = null; 312 try { 313 targetNSList = runXPathQuery(file, xpathQuery); 314 String targetNS = null; 315 FileObject fobj = FileUtil.toFileObject(file); 316 if(targetNSList.size() > 0){ 317 targetNS = targetNSList.get(0); 319 } else{ 320 targetNS = NO_NAME_SPACE; 321 } 322 if((docType == docType.wsdl) && (targetNS == NO_NAME_SPACE)) 323 continue; 325 result.put(fobj, targetNS); 326 } catch (Exception ex) { 327 } 330 } 331 return result; 332 } 333 334 335 336 public static List <File > getFilesWithExtension(File startFile, String fileExtension, List <File > curList) { 337 if(Thread.currentThread().isInterrupted()) 338 return curList; 340 if(curList == null) 341 curList = new ArrayList <File >(); 342 if(startFile.isFile()){ 343 int index = startFile.getName().lastIndexOf("."); 344 if(index != -1){ 345 String extn = startFile.getName().substring(index+1); 346 if((extn != null) && (extn.equalsIgnoreCase(fileExtension))) 347 curList.add(startFile); 348 } 349 } 350 if(startFile.isDirectory()){ 351 File [] children = startFile.listFiles(); 352 if(children != null){ 353 for(File child: children){ 354 getFilesWithExtension(child, fileExtension, curList); 355 } 356 } 357 } 358 return curList; 359 } 360 361 public static List <String > runXPathQuery(File parsedFile, String xpathExpr) throws Exception { 362 List <String > result = new ArrayList <String >(); 363 XPath xpath = XPathFactory.newInstance().newXPath(); 364 xpath.setNamespaceContext(getNamespaceContext()); 365 366 InputSource inputSource = new InputSource (new FileInputStream (parsedFile)); 367 NodeList nodes = (NodeList ) xpath.evaluate(xpathExpr, inputSource, XPathConstants.NODESET); 368 if((nodes != null) && (nodes.getLength() > 0)){ 369 for(int i=0; i<nodes.getLength();i++){ 370 Node node = nodes.item(i); 371 result.add(node.getNodeValue()); 372 } 373 } 374 return result; 375 } 376 377 private static Map <String , String > namespaces = new HashMap <String ,String >(); 378 private static Map <String , String > prefixes = new HashMap <String ,String >(); 379 380 private static NamespaceContext getNamespaceContext() { 381 namespaces.put("xsd","http://www.w3.org/2001/XMLSchema"); 383 prefixes.put("http://www.w3.org/2001/XMLSchema", "xsd"); 384 385 namespaces.put("wsdl", "http://schemas.xmlsoap.org/wsdl/"); 387 prefixes.put("http://schemas.xmlsoap.org/wsdl/", "wsdl"); 388 return new HashNamespaceResolver(namespaces, prefixes); 389 } 390 391 public static int countPushdownFolders(URI master, URI slave) { 392 String masterStr = master.toString(); 393 String slaveStr = slave.toString(); 394 StringTokenizer masterTok = new StringTokenizer (masterStr, "/"); 395 StringTokenizer slaveTok = new StringTokenizer (slaveStr, "/"); 396 String masterLast = null; 397 String slaveLast = null; 398 while(true){ 399 if(masterTok.hasMoreTokens() && slaveTok.hasMoreTokens()){ 400 masterLast = masterTok.nextToken(); 401 slaveLast = slaveTok.nextToken(); 402 if(masterLast.equals(slaveLast)) 403 continue; 404 else 405 break; 406 } 407 break; 408 } 409 return slaveTok.countTokens()+1; 411 } 412 413 public static File downloadURLAndSave(URL downloadURL, File saveFile) throws IOException { 414 return downloadURLUsingProxyAndSave(downloadURL, null, saveFile); 415 } 416 417 418 public static File downloadURLUsingProxyAndSave(URL downloadURL, Proxy proxy, File saveFile) throws IOException { 419 IOException expn = null; 420 URLConnection ucn = null; 421 422 if(Thread.currentThread().isInterrupted()) 423 return null; 424 if(proxy != null) 425 ucn = downloadURL.openConnection(proxy); 426 else 427 ucn = downloadURL.openConnection(); 428 429 if(Thread.currentThread().isInterrupted()) 430 return null; 431 ucn.connect(); 432 433 int fileLen = ucn.getContentLength(); 434 byte buffer[] = new byte[1024]; 435 BufferedInputStream bis = new BufferedInputStream (ucn.getInputStream()); 436 saveFile.getParentFile().mkdirs(); 437 BufferedOutputStream bos = null; 438 try { 439 bos = new BufferedOutputStream (new FileOutputStream (saveFile)); 440 } catch (FileNotFoundException ex) { 441 bis.close(); 442 throw ex; 443 } 444 int curLen = 0; 445 while( curLen < fileLen){ 446 try { 447 if(Thread.currentThread().isInterrupted()) 448 break; 449 Thread.sleep(100); 450 } catch (InterruptedException ex) {} 451 try{ 452 int readLen = bis.available(); 453 int len = bis.read(buffer, 0, (readLen>buffer.length)?buffer.length:readLen); 454 bos.write(buffer, 0, len); 455 curLen += len; 456 }catch (IOException e){ 457 expn = e; 458 break; 459 } 460 } 461 try { 462 bis.close(); 463 } catch (IOException ex) { 464 } 466 try { 467 bos.close(); 468 } catch (IOException ex) { 469 } 471 if(expn != null) 472 throw expn; 473 return saveFile; 474 } 475 476 public static InputStream getInputStreamOfURL(URL downloadURL, Proxy proxy) throws IOException { 477 478 URLConnection ucn = null; 479 480 if(Thread.currentThread().isInterrupted()) 481 return null; 482 if(proxy != null) 483 ucn = downloadURL.openConnection(proxy); 484 else 485 ucn = downloadURL.openConnection(); 486 487 if(Thread.currentThread().isInterrupted()) 488 return null; 489 ucn.connect(); 490 491 return ucn.getInputStream(); 492 493 } 494 495 public static Document getDocument(FileObject modelSourceFileObject){ 496 Document result = null; 497 try { 498 DataObject dObject = DataObject.find(modelSourceFileObject); 499 EditorCookie ec = (EditorCookie)dObject.getCookie(EditorCookie.class); 500 Document doc = ec.openDocument(); 501 if(doc instanceof BaseDocument) 502 return doc; 503 504 505 result = new org.netbeans.editor.BaseDocument( 506 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 507 String str = doc.getText(0, doc.getLength()); 508 result.insertString(0,str,null); 509 510 } catch (Exception dObjEx) { 511 return null; 512 } 513 return result; 514 } 515 516 private static Document _getDocument(DataObject modelSourceDataObject) 517 throws IOException { 518 Document result = null; 519 if (modelSourceDataObject != null && modelSourceDataObject.isValid()) { 520 EditorCookie ec = (EditorCookie) 521 modelSourceDataObject.getCookie(EditorCookie.class); 522 assert ec != null : "Data object "+modelSourceDataObject.getPrimaryFile().getPath()+" has no editor cookies."; 523 Document doc = null; 524 try { 525 doc = ec.openDocument(); 526 } catch (UserQuestionException uce) { 527 uce.confirmed(); 530 doc = ec.openDocument(); 531 } 532 assert(doc instanceof BaseDocument); 533 result = doc; 534 } 535 return result; 536 } 537 538 542 protected static Document _getDocument(FileObject modelSourceFileObject) 543 throws DataObjectNotFoundException, IOException { 544 DataObject dObject = DataObject.find(modelSourceFileObject); 545 return _getDocument(dObject); 546 } 547 548 private static CatalogWriteModel testCatalogModel = null; 549 public static CatalogWriteModel getTestCatalogWriteModel() throws IOException { 550 if(testCatalogModel == null){ 551 CatalogWriteModel cm = new CatalogWriteModelImpl(new File (System.getProperty("java.io.tmpdir"))); 552 File file = FileUtil.toFile(cm.getCatalogFileObject()); 553 file.deleteOnExit(); 554 return cm; 555 } 556 return testCatalogModel; 557 } 558 559 560 public static FileObject getProjectCatalogFileObject(Project prj) throws IOException { 561 if(prj == null) 562 return null; 563 564 FileObject result = null; 565 FileObject myProjectRootFileObject = prj.getProjectDirectory(); 566 567 XMLCatalogProvider catProv = (XMLCatalogProvider) prj.getLookup(). 569 lookup(XMLCatalogProvider.class); 570 if(catProv != null){ 571 URI caturi = catProv.getProjectWideCatalog(); 572 if(caturi != null){ 573 caturi = FileUtil.toFile(myProjectRootFileObject).toURI().resolve(caturi); 574 File catFile = new File (caturi); 575 if(!catFile.isFile()){ 576 catFile.createNewFile(); 577 } 578 result = FileUtil.toFileObject(FileUtil.normalizeFile(catFile)); 579 } 580 } 581 582 if(result == null){ 583 String fileName = CatalogWriteModel.PUBLIC_CATALOG_FILE_NAME+CatalogWriteModel.CATALOG_FILE_EXTENSION; 584 result = myProjectRootFileObject.getFileObject(fileName); 585 if(result == null){ 586 result = myProjectRootFileObject.createData(fileName); 587 } 588 } 589 return result; 590 } 591 592 public static FileObject getFileObject(ModelSource ms){ 593 return (FileObject) ms.getLookup().lookup(FileObject.class); 594 } 595 596 public static CatalogModel getCatalogModel(ModelSource ms) throws CatalogModelException{ 597 return CatalogModelFactory.getDefault().getCatalogModel(ms); 598 } 599 600 public static ModelSource getModelSource(FileObject bindingHandlerFO, boolean editable){ 601 try { 602 return createModelSource(bindingHandlerFO, editable); 603 } catch (CatalogModelException ex) { 604 return null; 605 } 606 } 607 608 613 public static ModelSource createModelSource(FileObject thisFileObj, 614 boolean editable) throws CatalogModelException{ 615 assert thisFileObj != null : "Null file object."; 616 final CatalogModel catalogModel = createCatalogModel(thisFileObj); 617 final DataObject dobj; 618 try { 619 dobj = DataObject.find(thisFileObj); 620 } catch (DataObjectNotFoundException ex) { 621 throw new CatalogModelException(ex); 622 } 623 Lookup proxyLookup = Lookups.proxy( 624 new Lookup.Provider() { 625 public Lookup getLookup() { 626 Document document = null; 627 try { 628 document = _getDocument(dobj); 629 if (document != null) { 630 return Lookups.fixed(new Object [] { 631 dobj.getPrimaryFile(), 632 document, 633 dobj, 634 DataObjectAdapters.source(dobj), 635 catalogModel 636 }); 637 } else { 638 return Lookups.fixed(new Object [] { 639 dobj.getPrimaryFile(), 640 dobj, 641 catalogModel 642 }); 643 } 644 } catch (IOException ioe) { 645 logger.log(Level.SEVERE, ioe.getMessage()); 646 return Lookups.fixed(new Object [] { 647 dobj, 648 catalogModel 649 }); 650 } 651 } 652 } 653 ); 654 655 return new ModelSource(proxyLookup, editable); 656 } 657 658 659 664 704 705 706 707 708 public static CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{ 709 return new CatalogModelFactoryImpl().getCatalogModel(fo); 710 } 711 712 public static void printMemoryUsage(String str){ 713 long init = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getInit(); 714 long cur = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); 715 long max = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax(); 716 System.out.printf("%s:\n@@@@@@MEMORY: %d/%d/%d\n",str, (init/(1024*1024)), (cur/(1024*1024)), (max/(1024*1024))); 717 } 718 719 public static final String DEFAULT_PRIVATE_CATALOG_URI_STR = "private/cache/retriever/catalog.xml"; 720 public static final String DEFAULT_PRIVATE_CAHCE_URI_STR = "private/cache/retriever"; 721 722 public static final String PRIVATE_CATALOG_URI_STR = "retriever/catalog.xml"; 723 public static final String PRIVATE_CAHCE_URI_STR = "retriever"; 724 725 726 727 public static boolean retrieveAndCache(URI locationURI, FileObject sourceFileObject) { 728 URI privateCatalogURI = null; 729 URI privateCacheURI = null; 730 731 Project prj = FileOwnerQuery.getOwner(sourceFileObject); 732 if(prj == null) 733 return false; 734 735 FileObject prjrtfo = prj.getProjectDirectory(); 736 File prjrt = FileUtil.toFile(prjrtfo); 737 if(prjrt == null) 738 return false; 739 740 CacheDirectoryProvider cdp = (CacheDirectoryProvider) prj.getLookup(). 742 lookup(CacheDirectoryProvider.class); 743 String catalogstr = DEFAULT_PRIVATE_CATALOG_URI_STR; 744 String cachestr = DEFAULT_PRIVATE_CAHCE_URI_STR; 745 try{ 746 if( (cdp != null) && (cdp.getCacheDirectory() != null) ){ 747 URI prjrturi = prjrt.toURI(); 748 URI cpduri = FileUtil.toFile(cdp.getCacheDirectory()).toURI(); 749 String cachedirstr = Utilities.relativize(prjrturi, cpduri); 750 catalogstr = cachedirstr+"/"+PRIVATE_CATALOG_URI_STR; 751 cachestr = cachedirstr+"/"+PRIVATE_CAHCE_URI_STR; 752 } 753 privateCatalogURI = new URI (catalogstr); 754 privateCacheURI = new URI (cachestr); 755 }catch(Exception e){ 756 return false; 757 } 758 759 URI cacheURI = prjrt.toURI().resolve(privateCacheURI); 761 File cacheFile = new File (cacheURI); 762 if(!cacheFile.isDirectory()) 763 cacheFile.mkdirs(); 764 FileObject cacheFO = FileUtil.toFileObject(FileUtil.normalizeFile(cacheFile)); 765 if(cacheFO == null) 766 return false; 767 Retriever ret = Retriever.getDefault(); 768 FileObject result; 769 try { 770 ((RetrieverImpl) ret).startNewThread = true; 771 result = ret.retrieveResource(cacheFO, privateCatalogURI, locationURI); 772 } catch (UnknownHostException ex) { 773 result = null; 774 } catch (IOException ex) { 775 result = null; 776 } catch (URISyntaxException ex) { 777 result = null; 778 } 779 780 782 783 XMLCatalogProvider catProv = (XMLCatalogProvider) prj.getLookup(). 785 lookup(XMLCatalogProvider.class); 786 FileObject publicCatFO = null; 787 FileObject peerCatFO = null; 788 if(catProv != null){ 789 790 URI publicCatURI = catProv.getProjectWideCatalog(); 792 if(publicCatURI != null){ 793 URI pubcatURI = prjrt.toURI().resolve(publicCatURI); 794 if(pubcatURI != null){ 795 File pubcatFile = new File (pubcatURI); 796 if(!pubcatFile.isFile()) 797 try { 798 pubcatFile.createNewFile(); 799 } catch (IOException ex) { 800 } 801 publicCatFO = FileUtil.toFileObject(FileUtil. 802 normalizeFile(pubcatFile)); 803 } 804 } 805 806 URI peerCatURI = catProv.getCatalog(sourceFileObject); 808 if(peerCatURI != null){ 809 URI peercatURI = prjrt.toURI().resolve(peerCatURI); 810 if(peercatURI != null){ 811 File peercatFile = new File (peercatURI); 812 if(!peercatFile.isFile()) 813 try { 814 peercatFile.createNewFile(); 815 } catch (IOException ex) { 816 } 817 peerCatFO = FileUtil.toFileObject(FileUtil. 818 normalizeFile(peercatFile)); 819 } 820 } 821 } 822 URI cacheCatFullURI = FileUtil.toFile(prjrtfo).toURI().resolve(privateCatalogURI); 825 CatalogWriteModel catWriter = null; 826 try { 827 if(publicCatFO == null){ 828 catWriter = CatalogWriteModelFactory.getInstance(). 830 getCatalogWriteModelForProject(sourceFileObject); 831 } else{ 832 catWriter = CatalogWriteModelFactory.getInstance(). 833 getCatalogWriteModelForCatalogFile(publicCatFO); 834 } 835 } catch (CatalogModelException ex) {} 836 if(catWriter == null){ 837 return true; 839 } 840 try { 841 catWriter.addNextCatalog(cacheCatFullURI, true); 842 } catch (IOException ex) { 843 } 844 845 if(publicCatFO != peerCatFO){ 847 catWriter = null; 849 try { 850 if(peerCatFO == null){ 851 catWriter = CatalogWriteModelFactory.getInstance(). 853 getCatalogWriteModelForProject(sourceFileObject); 854 } else{ 855 catWriter = CatalogWriteModelFactory.getInstance(). 856 getCatalogWriteModelForCatalogFile(peerCatFO); 857 } 858 } catch (CatalogModelException ex) {} 859 if(catWriter == null){ 860 return true; 862 } 863 try { 864 catWriter.addNextCatalog(cacheCatFullURI, true); 865 } catch (IOException ex) { 866 } 867 } 868 return true; 869 } 870 871 public enum DocumentTypesEnum { 872 schema, 873 wsdl; 874 875 public String toString(){ 876 if(name().equals("schema")) 877 return "xsd"; 878 else 879 return name(); 880 } 881 } 882 883 884 public static final class HashNamespaceResolver implements NamespaceContext { 885 private Map <String , String > prefixes; private Map <String , String > namespaces; 888 public HashNamespaceResolver(Map <String ,String > nsTable) { 889 namespaces = nsTable; 890 prefixes = new HashMap <String ,String >(); 891 for (Entry<String ,String > e : namespaces.entrySet()) { 892 prefixes.put(e.getValue(), e.getKey()); 893 } 894 } 895 896 public HashNamespaceResolver(Map <String ,String > namespaces, Map <String ,String > prefixes) { 897 this.namespaces = namespaces; 898 this.prefixes = prefixes; 899 } 900 901 public Iterator getPrefixes(String namespaceURI) { 902 return Collections.singletonList(getPrefix(namespaceURI)).iterator(); 903 } 904 905 public String getPrefix(String namespaceURI) { 906 return prefixes.get(namespaceURI); 907 } 908 909 public String getNamespaceURI(String prefix) { 910 return namespaces.get(prefix); 911 } 912 } 913 } 914 | Popular Tags |