1 19 20 package org.openide.filesystems; 21 22 import java.beans.PropertyVetoException ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.OutputStream ; 30 import java.lang.ref.Reference ; 31 import java.lang.ref.WeakReference ; 32 import java.net.MalformedURLException ; 33 import java.net.URI ; 34 import java.net.URL ; 35 import java.net.URLConnection ; 36 import java.util.ArrayList ; 37 import java.util.Arrays ; 38 import java.util.Collection ; 39 import java.util.Collections ; 40 import java.util.Date ; 41 import java.util.Enumeration ; 42 import java.util.HashMap ; 43 import java.util.HashSet ; 44 import java.util.Iterator ; 45 import java.util.Map ; 46 import java.util.Set ; 47 import java.util.Stack ; 48 import org.openide.util.Enumerations; 49 import org.openide.util.NbBundle; 50 import org.openide.xml.XMLUtil; 51 import org.xml.sax.Attributes ; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXException ; 54 import org.xml.sax.SAXParseException ; 55 import org.xml.sax.XMLReader ; 56 import org.xml.sax.helpers.DefaultHandler ; 57 58 129 public final class XMLFileSystem extends AbstractFileSystem { 130 static final long serialVersionUID = 28974107313702326L; 131 132 private static final Map <String , String > DTD_MAP = new HashMap <String , String >(); 136 137 static { 138 DTD_MAP.put("-//NetBeans//DTD Filesystem 1.0//EN", "org/openide/filesystems/filesystem.dtd"); DTD_MAP.put("-//NetBeans//DTD Filesystem 1.1//EN", "org/openide/filesystems/filesystem1_1.dtd"); } 141 142 143 private URL [] urlsToXml = new URL [] { }; 144 private transient FileObjRef<? extends FileObject> rootRef; 145 146 147 public XMLFileSystem() { 148 Impl impl = new Impl(this); 149 this.list = impl; 150 this.info = impl; 151 this.change = impl; 152 this.attr = impl; 153 } 154 155 159 public XMLFileSystem(String uri) throws SAXException { 160 this(); 161 162 if (uri == null) { 163 throw new NullPointerException ("Null uri"); } 165 166 try { 167 setXmlUrl(new URL (uri)); 168 } catch (Exception e) { 169 throw (SAXException ) ExternalUtil.copyAnnotation(new SAXException (e.getMessage()), e); 170 } 171 } 172 173 177 public XMLFileSystem(URL url) throws SAXException { 178 this(); 179 180 if (url == null) { 181 throw new NullPointerException ("Null url"); } 183 184 try { 185 setXmlUrl(url); 186 } catch (Exception e) { 187 throw (SAXException ) ExternalUtil.copyAnnotation(new SAXException (e.getMessage()), e); 188 } 189 } 190 191 196 @Deprecated 197 public XMLFileSystem(FileSystemCapability cap) { 198 this(); 199 setCapability(cap); 200 } 201 202 207 public URL getXmlUrl() { 208 return (urlsToXml.length > 0) ? urlsToXml[0] : null; 209 } 210 211 217 public synchronized void setXmlUrl(URL url) throws IOException , PropertyVetoException { 218 setXmlUrl(url, false); 219 } 220 221 228 public void setXmlUrl(URL url, boolean validate) throws IOException , PropertyVetoException { 229 try { 230 beginAtomicAction(); 231 232 synchronized (this) { 233 setXmlUrls(new URL [] { url }, validate); 234 } 235 } finally { 236 finishAtomicAction(); 237 } 238 } 239 240 245 @Deprecated 246 public URL [] getXmlUrls() { 247 return urlsToXml; 248 } 249 250 258 public void setXmlUrls(URL [] urls) throws IOException , PropertyVetoException { 259 try { 260 beginAtomicAction(); 261 262 synchronized (this) { 263 setXmlUrls(urls, false); 264 } 265 } finally { 266 finishAtomicAction(); 267 } 268 } 269 270 @SuppressWarnings ("deprecation") private void _setSystemName(String s) throws PropertyVetoException { 272 setSystemName(s); 273 } 274 275 private synchronized void setXmlUrls(URL [] urls, boolean validate) 276 throws IOException , PropertyVetoException { 277 if ((urls == null) || Arrays.asList(urls).contains(null)) { 278 throw new NullPointerException ("Null URL list or member"); } 280 281 ResourceElem rootElem; 282 String oldDisplayName = getDisplayName(); 283 284 if (urls.length == 0) { 285 urlsToXml = new URL [] { }; 286 refreshChildrenInAtomicAction((AbstractFolder) getRoot(), rootElem = new ResourceElem(true, urls, null)); rootElem = null; 288 289 return; 290 } 291 292 Handler handler = new Handler (DTD_MAP, rootElem = new ResourceElem(true, urls, null), validate); 294 URL [] origUrls = urlsToXml; 295 urlsToXml = new URL [urls.length]; 296 297 try { 298 _setSystemName("XML_" + urls[0].toExternalForm().replace('/','-')); } catch (PropertyVetoException pvx) { 300 urlsToXml = origUrls; 301 rootElem = null; 302 throw pvx; 303 } 304 305 URL act = null; 306 307 try { 308 XMLReader xp = XMLUtil.createXMLReader(validate, false); 309 xp.setEntityResolver(handler); 310 xp.setContentHandler(handler); 311 xp.setErrorHandler(handler); 312 313 for (int index = 0; index < urls.length; index++) { 314 act = urls[index]; 315 urlsToXml[index] = act; 316 handler.urlContext = act; 317 318 String systemId = act.toExternalForm(); 319 320 xp.parse(systemId); 321 } 322 323 refreshChildrenInAtomicAction((AbstractFolder) getRoot(), rootElem); 324 } catch (IOException iox) { 325 urlsToXml = origUrls; 326 throw iox; 327 } catch (Exception e) { 328 throw (IOException ) new IOException (act + ": " + e.toString()).initCause(e); } finally { 330 rootElem = null; 331 } 332 333 firePropertyChange(PROP_DISPLAY_NAME, oldDisplayName, getDisplayName()); 334 } 335 336 339 boolean isLastModifiedCacheEnabled() { 340 return false; 341 } 342 343 348 private boolean isFolder(String name) { 349 Reference ref = findReference(name); 350 351 if ((ref != null) && (ref instanceof FileObjRef)) { 352 return ((FileObjRef) ref).isFolder(); 353 } 354 355 return false; 356 } 357 358 365 private InputStream getInputStream(String name) throws java.io.FileNotFoundException { 366 Reference ref = findReference(name); 367 368 if ((ref != null) && (ref instanceof FileObjRef)) { 369 return (((FileObjRef) ref).getInputStream(name)); 370 } 371 372 throw new FileNotFoundException (NbBundle.getMessage(XMLFileSystem.class, "EXC_CanntRead", name)); } 374 375 382 URL getURL(String name) throws java.io.FileNotFoundException { 383 Reference ref = findReference(name); 384 385 if ((ref != null) && (ref instanceof FileObjRef)) { 386 return ((FileObjRef) ref).createAbsoluteUrl(name); 387 } 388 389 throw new FileNotFoundException (NbBundle.getMessage(XMLFileSystem.class, "EXC_CanntRead", name)); } 391 392 393 private long getSize(String name) { 394 Reference ref = findReference(name); 395 396 if ((ref != null) && (ref instanceof FileObjRef)) { 397 return ((FileObjRef) ref).getSize(name); 398 } 399 400 return 0; 401 } 402 403 404 private java.util.Date lastModified(String name) { 405 Reference ref = findReference(name); 406 407 if ((ref != null) && (ref instanceof FileObjRef)) { 408 return ((FileObjRef) ref).lastModified(name); 409 } 410 411 412 return new Date (0); 413 } 414 415 418 public String getDisplayName() { 419 if ((urlsToXml.length == 0) || (urlsToXml[0] == null) || (urlsToXml[0].toExternalForm().length() == 0)) { 420 return NbBundle.getMessage(XMLFileSystem.class, "XML_NotValidXMLFileSystem"); } 422 423 return "XML:" + urlsToXml[0].toExternalForm().trim(); } 425 426 429 public boolean isReadOnly() { 430 return true; 431 } 432 433 435 private void readObject(ObjectInputStream ois) throws IOException , ClassNotFoundException { 436 ObjectInputStream.GetField fields = ois.readFields(); 438 URL [] urls = (URL []) fields.get("urlsToXml", null); 440 if (urls == null) { 441 urls = new URL [1]; 442 urls[0] = (URL ) fields.get("uriId", null); } 444 445 try { 446 if (urlsToXml.length != 1) { 447 setXmlUrls(urlsToXml); 448 } else { 449 setXmlUrl(urlsToXml[0]); 450 } 451 } catch (PropertyVetoException ex) { 452 IOException x = new IOException (ex.getMessage()); 453 ExternalUtil.copyAnnotation(x, ex); 454 throw x; 455 } 456 } 457 458 461 public void addNotify() { 462 } 463 464 468 public void removeNotify() { 469 } 470 471 protected <T extends FileObject> Reference <T> createReference(T fo) { 472 return new FileObjRef<T>(fo); 473 } 474 475 private void refreshChildrenInAtomicAction(AbstractFolder fo, ResourceElem resElem) { 476 try { 477 beginAtomicAction(); 478 Collection <AbstractFolder> oldChildren = new HashSet <AbstractFolder>(Collections.list(fo.existingSubFiles(true))); 479 480 refreshChildren(fo, resElem); 481 482 Collection <AbstractFolder> newChildren = Collections.list(fo.existingSubFiles(true)); 483 oldChildren.removeAll(newChildren); 484 for (Iterator <AbstractFolder> it = oldChildren.iterator(); it.hasNext();) { 485 AbstractFileObject invalid = (AbstractFileObject)it.next(); 486 if (invalid.validFlag) { 487 invalid.validFlag = false; 488 invalid.fileDeleted0(new FileEvent(invalid)); 489 } 490 } 491 } finally { 492 finishAtomicAction(); 493 } 494 } 495 496 497 private void refreshChildren(AbstractFolder fo, ResourceElem resElem) { 498 if (fo.isRoot()) { 499 initializeReference(rootRef = new FileObjRef<AbstractFolder>(fo), resElem); 500 } 501 502 java.util.List <String > nameList = resElem.getChildren(); 503 String [] names = new String [nameList.size()]; 504 ResourceElem[] children = new ResourceElem[names.length]; 505 506 nameList.toArray(names); 507 508 for (int i = 0; i < names.length; i++) 509 children[i] = resElem.getChild(names[i]); 510 511 fo.refresh(null, null, true, true, names); 512 513 for (int i = 0; i < children.length; i++) { 514 AbstractFolder fo2 = (AbstractFolder) fo.getFileObject(names[i]); 515 @SuppressWarnings ("unchecked") 516 FileObjRef currentRef = (FileObjRef) findReference(fo2.getPath()); 517 int diff = initializeReference(currentRef, children[i]); 518 fo2.lastModified(); 519 520 if (fo2.isFolder()) { 521 refreshChildren(fo2, children[i]); 522 } else { 523 if ((diff & 0x01) != 0) { 524 fo2.fileChanged0(new FileEvent(fo2)); 525 } else { 526 if ((diff & 0x02) != 0) { 527 fo2.fileAttributeChanged0(new FileAttributeEvent(fo2, null, null, null)); 528 } 529 } 530 } 531 } 532 } 533 534 539 private int initializeReference(FileObjRef currentRef, ResourceElem resElem) { 540 if (!currentRef.isInitialized()) { 541 currentRef.initialize(resElem); 542 return 0x00; 543 } else { 544 boolean attrDiff = currentRef.attacheAttrs(resElem.getAttr(false)); 545 currentRef.setUrlContext(resElem.getUrlContext()); 546 547 boolean diff = false; 548 if (resElem.getContent() != null) { 549 diff = !(currentRef.content instanceof byte[]) || !java.util.Arrays.equals((byte[])currentRef.content, resElem.getContent()); 550 currentRef.content = resElem.getContent(); 551 } else if (resElem.getURI() != null) { 552 diff = !resElem.getURI().equals(currentRef.content); 553 currentRef.content = resElem.getURI(); 554 } 555 556 return (diff ? 0x01 : 0x00) + (attrDiff ? 0x02 : 0x00); 557 } 558 } 559 560 561 private static class ResourceElem { 562 private java.util.List <ResourceElem> children; 563 private java.util.List <String > names; 564 private byte[] content; 565 private java.util.List <URL > urlContext = new ArrayList <URL >(); 566 private XMLMapAttr foAttrs; 567 private boolean isFolder; 568 private String uri; 569 570 public ResourceElem(boolean isFolder, URL [] urlContext, String uri) { 571 this.isFolder = isFolder; 572 this.uri = uri; 573 this.urlContext.addAll(Arrays.asList(urlContext)); 574 575 if (isFolder) { 576 children = new ArrayList <ResourceElem>(); 577 names = new ArrayList <String >(); 578 } 579 } 580 581 public ResourceElem(boolean isFolder, URL urlContext, String uri) { 582 this.isFolder = isFolder; 583 this.uri = uri; 584 this.urlContext.add(urlContext); 585 586 if (isFolder) { 587 children = new ArrayList <ResourceElem>(); 588 names = new ArrayList <String >(); 589 } 590 } 591 592 ResourceElem addChild(String name, ResourceElem child) { 593 if (!isFolder) { 594 throw new IllegalArgumentException ("not a folder"); } 596 597 ResourceElem retVal = child; 598 int idx = names.indexOf(name); 599 600 if (idx == -1) { 601 names.add(name); 602 children.add(child); 603 } else { 604 retVal = children.get(idx); 605 } 606 607 return retVal; 608 } 609 610 java.util.List <String > getChildren() { 611 return names; 612 } 613 614 ResourceElem getChild(String name) { 615 return children.get(names.indexOf(name)); 616 } 617 618 XMLMapAttr getAttr(boolean create) { 619 if (create && (foAttrs == null)) { 620 foAttrs = new XMLMapAttr(); 621 } 622 623 return foAttrs; 624 } 625 626 byte[] getContent() { 627 return content; 628 } 629 630 URL [] getUrlContext() { 631 URL [] retVal = new URL [urlContext.size()]; 632 urlContext.toArray(retVal); 633 634 return retVal; 635 } 636 637 String getURI() { 638 return uri; 639 } 640 641 void setContent(byte[] content) { 642 if (this.content == null) { 643 byte[] alloc = new byte[content.length]; 644 System.arraycopy(content, 0, alloc, 0, content.length); 645 this.content = alloc; 646 } 647 } 648 649 boolean isFolder() { 650 return isFolder; 651 } 652 } 653 654 656 659 public static class Impl extends Object implements AbstractFileSystem.List, AbstractFileSystem.Info, 660 AbstractFileSystem.Change, AbstractFileSystem.Attr { 661 662 static final long serialVersionUID = -67233358102597232L; 663 664 665 private XMLFileSystem fs; 666 667 670 public Impl(XMLFileSystem fs) { 671 this.fs = fs; 672 } 673 674 680 public String [] children(String name) { 681 FileObject fo2name; 682 683 if ((fo2name = fs.findResource(name)) == null) { 684 return new String [] { }; 685 } 686 687 synchronized (fo2name) { 688 return ((AbstractFolder) fo2name).getChildrenArray(); 689 } 690 } 691 692 696 701 public void createFolder(String name) throws java.io.IOException { 702 throw new IOException (); 703 } 704 705 710 public void createData(String name) throws IOException { 711 throw new IOException (); 712 } 713 714 719 public void rename(String oldName, String newName) 720 throws IOException { 721 throw new IOException (); 722 } 723 724 730 public void delete(String name) throws IOException { 731 throw new IOException (); 732 } 733 734 736 742 public java.util.Date lastModified(String name) { 743 return fs.lastModified(name); 744 } 745 746 751 public boolean folder(String name) { 752 return fs.isFolder(name); 753 } 754 755 760 public boolean readOnly(String name) { 761 return true; 762 } 763 764 773 public String mimeType(String name) { 774 return null; 775 } 776 777 784 public long size(String name) { 785 if (fs.isFolder(name)) { 786 return 0; 787 } 788 789 return fs.getSize(name); 790 } 791 792 799 public InputStream inputStream(String name) throws java.io.FileNotFoundException { 800 InputStream is = fs.getInputStream(name); 801 802 if (is == null) { 803 throw new java.io.FileNotFoundException (name); 804 } 805 806 return is; 807 } 808 809 816 public OutputStream outputStream(String name) throws java.io.IOException { 817 throw new IOException (); 818 } 819 820 826 public void lock(String name) throws IOException { 827 FSException.io("EXC_CannotLock", name, fs.getDisplayName(), name); } 829 830 835 public void unlock(String name) { 836 } 837 838 843 public void markUnimportant(String name) { 844 } 845 846 851 public Object readAttribute(String name, String attrName) { 852 @SuppressWarnings ("unchecked") 853 FileObjRef ref = (FileObjRef) fs.findReference(name); 854 855 if ((ref == null) && (name.length() == 0) && (fs.rootRef != null)) { 856 ref = fs.rootRef; 857 } 858 859 if (ref == null) { 860 return null; 861 } 862 863 return ref.readAttribute(attrName); 864 } 865 866 872 public void writeAttribute(String name, String attrName, Object value) 873 throws IOException { 874 throw new IOException (); 875 } 876 877 881 public Enumeration <String > attributes(String name) { 882 @SuppressWarnings ("unchecked") 883 FileObjRef<? extends FileObject> ref = (FileObjRef) fs.findReference(name); 884 885 if ((ref == null) && (name.length() == 0) && (fs.rootRef != null)) { 886 ref = fs.rootRef; 887 } 888 889 if (ref == null) { 890 return Enumerations.empty(); 891 } 892 893 return ref.attributes(); 894 } 895 896 900 public void renameAttributes(String oldName, String newName) { 901 } 902 903 907 public void deleteAttributes(String name) { 908 } 909 } 910 911 914 private static class FileObjRef<T extends FileObject> extends WeakReference <T> { 915 private T fo; 916 private Object content; 917 private XMLMapAttr foAttrs; 918 byte isFolder = -1; 919 Object urlContext = null; 920 921 public FileObjRef(T fo) { 922 super(fo); 923 this.fo = fo; 924 } 925 926 public boolean isInitialized() { 927 return (isFolder != -1); 928 } 929 930 public void initialize(ResourceElem res) { 931 content = res.getContent(); 932 933 XMLMapAttr tmp = res.getAttr(false); 934 935 if ((tmp != null) && !tmp.isEmpty()) { 936 foAttrs = tmp; 937 } 938 939 isFolder = (byte) (res.isFolder() ? 1 : 0); 940 941 if (content == null) { 942 content = res.getURI(); 943 } 944 945 setUrlContext(res.getUrlContext()); 946 } 947 948 public boolean isFolder() { 949 return (isFolder == 1); 950 } 951 952 953 public boolean attacheAttrs(XMLMapAttr attrs) { 954 if ((attrs == null) || attrs.isEmpty()) { 955 return false; 956 } 957 958 if (foAttrs == null) { 959 foAttrs = new XMLMapAttr(); 960 } 961 962 Iterator it = attrs.entrySet().iterator(); 963 boolean ch = false; 964 while (it.hasNext()) { 965 Map.Entry attrEntry = (Map.Entry ) it.next(); 966 Object prev = foAttrs.put(attrEntry.getKey(), attrEntry.getValue()); 967 968 ch |= (prev == null && attrEntry.getValue() != null) || !prev.equals(attrEntry.getValue()); 969 } 970 971 return ch; 972 } 973 974 public void setUrlContext(URL [] ctx) { 975 if (ctx.length > 0) { 976 if (ctx.length > 1) { 977 urlContext = ctx; 978 } else { 979 urlContext = ctx[0]; 980 } 981 } 982 } 983 984 public Enumeration <String > attributes() { 985 if (foAttrs == null) { 986 return Enumerations.empty(); 987 } else { 988 Set <String > s = new HashSet <String >(foAttrs.keySet()); 989 990 return Collections.enumeration(s); 991 } 992 } 993 994 private URL [] getLayers() { 995 if (urlContext == null) { 996 return null; 997 } 998 999 if (urlContext instanceof URL []) { 1000 return (URL []) urlContext; 1001 } 1002 1003 return new URL [] { (URL ) urlContext }; 1004 } 1005 1006 public Object readAttribute(String attrName) { 1007 if (attrName.equals("layers")) { 1009 return getLayers(); 1010 } 1011 1012 if (foAttrs == null) { 1013 return null; 1014 } 1015 1016 FileObject topFO = MultiFileObject.attrAskedFileObject.get(); 1017 FileObject f = (topFO == null) ? fo : topFO; 1018 1019 MultiFileObject.attrAskedFileObject.set(null); 1020 1021 try { 1022 Object [] objs = new Object [] { f, attrName }; 1023 1024 return foAttrs.get(attrName, objs); 1025 } finally { 1026 MultiFileObject.attrAskedFileObject.set(topFO); 1027 } 1028 } 1029 1030 1037 public InputStream getInputStream(String name) 1038 throws java.io.FileNotFoundException { 1039 InputStream is = null; 1040 1041 if (content == null) { 1042 return new ByteArrayInputStream (new byte[] { }); 1043 } 1044 1045 if (content instanceof String ) { 1046 URL absURL = createAbsoluteUrl(name); 1047 1048 try { 1049 is = absURL.openStream(); 1050 } catch (IOException iox) { 1051 FileNotFoundException x = new FileNotFoundException (name); 1052 ExternalUtil.copyAnnotation(x, iox); 1053 throw x; 1054 } 1055 } 1056 1057 if (content instanceof byte[]) { 1058 is = new ByteArrayInputStream ((byte[]) content); 1059 } 1060 1061 if (is == null) { 1062 throw new FileNotFoundException (name); 1063 } 1064 1065 return is; 1066 } 1067 1068 private URL createAbsoluteUrl(String name) throws java.io.FileNotFoundException { 1069 if (!(content instanceof String )) { 1070 return null; 1071 } 1072 1073 String uri = (String ) content; 1074 1075 try { 1076 URL [] uc = getLayers(); 1077 URL retVal = ((uc == null) || (uc.length == 0)) ? new URL (uri) : new URL (uc[0], uri); 1078 1079 return retVal; 1080 } catch (IOException ex) { 1082 FileNotFoundException x = new FileNotFoundException (name); 1083 ExternalUtil.copyAnnotation(x, ex); 1084 throw x; 1085 } 1086 } 1087 1088 public long getSize(String name) { 1089 if (content == null) { 1090 return 0; 1091 } 1092 1093 if (content instanceof byte[]) { 1094 return ((byte[]) content).length; 1095 } 1096 1097 if (content instanceof String ) { 1098 try { 1099 URL absURL = createAbsoluteUrl(name); 1100 URLConnection urlConnection = absURL.openConnection(); 1101 1102 try { 1103 return urlConnection.getContentLength(); 1104 } finally { 1105 urlConnection.getInputStream().close(); 1106 } 1107 } catch (IOException iex) { 1108 } 1109 } 1110 1111 return 0; 1112 } 1113 1114 public Date lastModified(String name) { 1115 URL url = null; 1116 Date retval = null; 1117 1118 if ((content == null) || !(content instanceof String )) { 1119 URL [] all = getLayers(); 1120 url = (all != null && all.length > 0) ? all[0] : null; 1121 } else { 1122 try { 1123 url = createAbsoluteUrl(name); 1124 } catch (IOException iex) { 1125 url = null; 1126 } 1127 } 1128 1129 if (url != null) { 1130 String protocol = url.getProtocol(); 1131 if ("jar".equals(protocol)) { URL tmp = FileUtil.getArchiveFile(url); 1133 url = (tmp != null) ? tmp : url; 1134 } 1135 1136 if ("file".equals(protocol)) { File f = new File (URI.create(url.toExternalForm())); 1138 retval = (f.exists()) ? new Date (f.lastModified()) : null; 1139 } else { 1140 retval = timeFromDateHeaderField(url); 1141 } 1142 } 1143 1144 if (retval == null) { 1145 retval = new Date (0); 1146 } 1147 1148 return retval; 1149 } 1150 1151 1152 private static String getLocalResource(URL url) { 1153 if (url == null) { 1154 return null; 1155 } 1156 1157 if (url.getProtocol().equals("jar")) { 1159 URL testURL = null; 1160 1161 try { 1162 testURL = new URL (url.getFile()); 1163 1164 if (testURL.getProtocol().equals("file")) { 1166 return testURL.getFile(); 1167 } 1168 } catch (MalformedURLException mfx) { 1169 return null; 1170 } 1171 } 1172 1173 if (url.getProtocol().equals("file")) { 1175 return url.getFile(); 1176 } 1177 1178 return null; 1179 } 1180 1181 1182 private static File getFileFromResourceString(String localResource) { 1183 if (localResource == null) { 1184 return null; 1185 } 1186 1187 int idx = localResource.indexOf('!'); 1188 String fileName = (idx != -1) ? localResource.substring(0, idx) : localResource; 1189 File f = new File (fileName); 1190 1191 if (f.exists()) { 1192 return f; 1193 } 1194 1195 return null; 1196 } 1197 1198 private java.util.Date timeFromDateHeaderField(URL url) { 1199 URLConnection urlConn; 1200 1201 try { 1202 urlConn = url.openConnection(); 1203 1204 try { 1205 return new Date (urlConn.getLastModified()); 1206 } finally { 1207 urlConn.getInputStream().close(); 1208 } 1209 } catch (IOException ie) { 1210 return new java.util.Date (0); 1211 } 1212 } 1213 1214 private java.util.Date timeFromDateHeaderField(URL [] urls) { 1215 return (urls.length > 0) ? timeFromDateHeaderField(urls[0]) : new java.util.Date (0); 1216 } 1217 } 1218 1219 1221 static class Handler extends DefaultHandler { 1222 private static final int FOLDER_CODE = "folder".hashCode(); private static final int FILE_CODE = "file".hashCode(); private static final int ATTR_CODE = "attr".hashCode(); private ResourceElem rootElem; 1226 private boolean validate = false; 1227 Stack <ResourceElem> resElemStack = new Stack <ResourceElem>(); 1228 Stack <String > elementStack = new Stack <String >(); 1229 URL urlContext; 1230 private Map dtdMap; 1231 private ResourceElem topRE; 1232 private StringBuffer pcdata = new StringBuffer (); 1233 1234 Handler(Map dtdMap, ResourceElem rootElem, boolean validate) { 1235 this.dtdMap = dtdMap; 1236 this.rootElem = rootElem; 1237 this.validate = validate; 1238 } 1239 1240 public void error(SAXParseException exception) 1241 throws SAXException { 1242 throw exception; 1243 } 1244 1245 public void warning(SAXParseException exception) 1246 throws SAXException { 1247 throw exception; 1248 } 1249 1250 public void fatalError(SAXParseException exception) 1251 throws SAXException { 1252 throw exception; 1253 } 1254 1255 public void startElement(String xmluri, String lname, String name, Attributes amap) 1256 throws SAXException { 1257 int controlCode = name.hashCode(); 1258 1259 elementStack.push(name); 1260 1261 String foName = amap.getValue("name"); 1263 if (controlCode == FOLDER_CODE) { 1264 if (foName == null) { 1265 throw new SAXException (NbBundle.getMessage(XMLFileSystem.class, "XML_MisssingAttr")); } 1267 1268 ResourceElem newRes = new ResourceElem(true, urlContext, null); 1269 1270 topRE = topRE.addChild(foName, newRes); 1271 resElemStack.push(topRE); 1272 1273 return; 1274 } 1275 1276 if (controlCode == FILE_CODE) { 1277 if (foName == null) { 1278 throw new SAXException (NbBundle.getMessage(XMLFileSystem.class, "XML_MisssingAttr")); } 1280 1281 foName = foName.intern(); 1282 1283 String uri = null; 1284 1285 if (amap.getLength() > 1) { 1286 uri = amap.getValue("url"); } 1288 1289 ResourceElem newRes = new ResourceElem(false, urlContext, uri); 1290 1291 topRE = topRE.addChild(foName, newRes); 1292 resElemStack.push(topRE); 1293 1294 pcdata.setLength(0); 1295 1296 return; 1297 } 1298 1299 if (controlCode == ATTR_CODE) { 1300 if (foName == null) { 1301 throw new SAXException (NbBundle.getMessage(XMLFileSystem.class, "XML_MisssingAttr")); } 1303 1304 int len = amap.getLength(); 1305 1306 for (int i = 0; i < len; i++) { 1307 String key = amap.getQName(i); 1308 String value = amap.getValue(i); 1309 1310 if (XMLMapAttr.Attr.isValid(key) != -1) { 1311 XMLMapAttr.Attr attr = XMLMapAttr.createAttributeAndDecode(key, value); 1312 XMLMapAttr attrMap = topRE.getAttr(true); 1313 Object retVal = attrMap.put(foName, attr); 1314 1315 if (retVal != null) { 1316 attrMap.put(foName, retVal); 1317 } 1318 } 1319 } 1320 1321 return; 1322 } 1323 } 1324 1325 public void endElement(String uri, String lname, String name) throws SAXException { 1326 if ((elementStack.peek().hashCode() == FILE_CODE) && !topRE.isFolder()) { 1327 String string = pcdata.toString().trim(); 1328 1329 if (string.length() > 0) { 1330 topRE.setContent(string.getBytes()); 1331 } 1332 1333 pcdata.setLength(0); 1334 } 1335 1336 int controlCode = name.hashCode(); 1337 1338 elementStack.pop(); 1339 1340 if ((controlCode == FOLDER_CODE) || (controlCode == FILE_CODE)) { 1341 resElemStack.pop(); 1342 topRE = resElemStack.peek(); 1343 1344 return; 1345 } 1346 } 1347 1348 public void characters(char[] ch, int start, int length) 1349 throws SAXException { 1350 if (elementStack.peek().hashCode() != FILE_CODE) { 1351 return; 1352 } 1353 1354 if (topRE.isFolder()) { 1355 return; 1356 } 1357 1358 pcdata.append(new String (ch, start, length)); 1359 } 1360 1361 public InputSource resolveEntity(String pid, String sid) 1362 throws SAXException { 1363 String publicURL = (String ) dtdMap.get(pid); 1364 1365 if (publicURL != null) { 1366 if (validate) { 1367 publicURL = getClass().getClassLoader().getResource(publicURL).toExternalForm(); 1368 1369 return new InputSource (publicURL); 1370 } else { 1371 return new InputSource (new ByteArrayInputStream (new byte[0])); 1372 } 1373 } 1374 1375 return new InputSource (sid); 1376 } 1377 1378 public void startDocument() throws SAXException { 1379 super.startDocument(); 1380 resElemStack = new Stack <ResourceElem>(); 1381 resElemStack.push(rootElem); 1382 topRE = rootElem; 1383 1384 elementStack = new Stack <String >(); 1385 elementStack.push("<root>"); } 1387 1388 public void endDocument() throws SAXException { 1389 super.endDocument(); 1390 resElemStack.pop(); 1391 elementStack.pop(); 1392 } 1393 } 1394} 1395 | Popular Tags |