1 19 20 package org.openide.filesystems; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.Externalizable ; 25 import java.io.File ; 26 import java.io.FileNotFoundException ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.ObjectInput ; 30 import java.io.ObjectInputStream ; 31 import java.io.ObjectOutput ; 32 import java.io.OutputStream ; 33 import java.io.OutputStreamWriter ; 34 import java.io.PrintWriter ; 35 import java.io.PushbackInputStream ; 36 import java.lang.ref.Reference ; 37 import java.lang.ref.SoftReference ; 38 import java.util.Collections ; 39 import java.util.Enumeration ; 40 import java.util.HashMap ; 41 import java.util.HashSet ; 42 import java.util.Iterator ; 43 import java.util.Locale ; 44 import java.util.Map ; 45 import java.util.TreeSet ; 46 import javax.xml.parsers.FactoryConfigurationError ; 47 import javax.xml.parsers.ParserConfigurationException ; 48 import org.openide.util.Enumerations; 49 import org.openide.util.NbBundle; 50 import org.openide.util.Utilities; 51 import org.openide.util.io.NbMarshalledObject; 52 import org.openide.xml.XMLUtil; 53 import org.xml.sax.Attributes ; 54 import org.xml.sax.InputSource ; 55 import org.xml.sax.SAXException ; 56 import org.xml.sax.SAXParseException ; 57 import org.xml.sax.XMLReader ; 58 import org.xml.sax.helpers.DefaultHandler ; 59 60 89 @SuppressWarnings ("unchecked") 90 public class DefaultAttributes extends Object implements AbstractFileSystem.Attr, AbstractFileSystem.List { 91 static final long serialVersionUID = -5801291358293736478L; 92 93 96 @Deprecated 97 public final static String ATTR_NAME = "filesystem"; 99 102 @Deprecated 103 public final static String ATTR_EXT = "attributes"; 105 108 @Deprecated 109 public final static String ATTR_NAME_EXT = ATTR_NAME + '.' + ATTR_EXT; 110 private final static String ATTR_NAME_EXT_XML = System.getProperty( 111 "org.openide.filesystems.DefaultAttributes.ATTR_NAME_EXT_XML", ".nbattrs" 112 ); 114 121 private final static String READONLY_ATTRIBUTES = "readOnlyAttrs"; 123 private static final String PUBLIC_ID = "-//NetBeans//DTD DefaultAttributes 1.0//EN"; private static final String DTD_PATH = "org/openide/filesystems/attributes.dtd"; 129 130 private AbstractFileSystem.Info info; 131 132 133 private AbstractFileSystem.Change change; 134 135 136 private AbstractFileSystem.List list; 137 138 139 private String fileName; 140 141 145 private transient Map cache; 146 147 152 public DefaultAttributes( 153 AbstractFileSystem.Info info, AbstractFileSystem.Change change, AbstractFileSystem.List list 154 ) { 155 this.info = info; 156 this.change = change; 157 this.list = list; 158 fileName = ATTR_NAME_EXT_XML; 159 } 160 161 169 protected DefaultAttributes( 170 AbstractFileSystem.Info info, AbstractFileSystem.Change change, AbstractFileSystem.List list, String fileName 171 ) { 172 this(info, change, list); 173 this.fileName = fileName; 174 } 175 176 179 private void readObject(ObjectInputStream ois) throws ClassNotFoundException , IOException { 180 ObjectInputStream.GetField fields = ois.readFields(); 181 182 Object o1 = AbstractFileSystem.readImpl("change", fields); Object o2 = AbstractFileSystem.readImpl("info", fields); Object o3 = AbstractFileSystem.readImpl("list", fields); 186 change = (AbstractFileSystem.Change) o1; 187 info = (AbstractFileSystem.Info) o2; 188 list = (AbstractFileSystem.List) o3; 189 } 190 191 200 public String [] children(String f) { 201 String [] arr = list.children(f); 202 int lookUpIndex = 0; 203 204 if (arr == null) { 205 return null; 206 } 207 208 int size = arr.length; 209 210 if (size == 1) { 211 if ((Utilities.getOperatingSystem() == Utilities.OS_VMS) && (arr[0] != null) && (f != null)) { 216 if (arr[0].equalsIgnoreCase("_nbattrs.")) { 217 try { 218 deleteFile(f + "/" + arr[0]); } catch (IOException ioe) { 220 } 221 222 arr[0] = getFileName(); 223 } 224 } 225 226 if ((getFileName().equals(arr[0]) || ATTR_NAME_EXT_XML.equals(arr[0]) || ATTR_NAME_EXT.equals(arr[0]))) { 227 try { 228 deleteFile(f + "/" + arr[0]); } catch (IOException iox) { 230 } 231 232 return new String [] { }; 233 } 234 } 235 236 for (int i = 0; i < size; i++) { 237 if ((Utilities.getOperatingSystem() == Utilities.OS_VMS) && (arr[i] != null) && (f != null)) { 242 if (arr[i].equalsIgnoreCase("_nbattrs.")) { 243 try { 244 File fp = new File (f + "/" + ".nbattrs"); 245 246 if (!fp.exists()) { 247 cache = null; 248 copyVMSAttrFile(f); 249 } 250 } catch (IOException ioe) { 251 } 252 253 arr[i] = getFileName(); 254 } 255 } 256 257 String safeNbAttrsCopy = getFileName() + "~"; 259 if ( 260 getFileName().equals(arr[i]) || ATTR_NAME_EXT.equals(arr[i]) || ATTR_NAME_EXT_XML.equals(arr[i]) || 261 safeNbAttrsCopy.equals(arr[i]) 262 ) { 263 arr[i] = null; 265 266 if (++lookUpIndex >= 2) { 268 break; 269 } 270 } 271 } 272 273 return arr; 274 } 275 276 284 private void copyVMSAttrFile(String f) throws IOException { 285 InputStream is = null; 286 OutputStream os = null; 287 288 try { 289 change.createData(f + "/" + getFileName()); 290 is = info.inputStream(f + "/" + "_nbattrs."); 291 os = info.outputStream(f + "/" + getFileName()); 292 293 byte[] buf = new byte[256]; 294 int readi; 295 296 while ((readi = is.read(buf, 0, 256)) >= 0x0) { 297 os.write(buf, 0, readi); 298 } 299 300 is.close(); 301 302 is = null; 304 } catch (IOException ie) { 305 } finally { 306 if (is != null) { 307 is.close(); 308 } 309 310 if (os != null) { 311 os.close(); 312 } 313 } 314 } 315 316 329 334 public Object readAttribute(String name, String attrName) { 335 Table t; 336 String [] arr = new String [2]; 337 split(name, arr); 338 339 340 if (attrName.equals(READONLY_ATTRIBUTES)) { 341 return info.readOnly(arr[0]) ? Boolean.TRUE : Boolean.FALSE; 342 } 343 344 synchronized (this) { 345 t = loadTable(arr[0]); 348 } 349 350 return t.getAttr(arr[1], attrName); 360 } 361 362 368 public void writeAttribute(String name, String attrName, Object value) 369 throws IOException { 370 int objType; 373 374 String [] arr = new String [2]; 375 split(name, arr); 376 377 for (;;) { 378 int version; 379 Table t; 380 381 synchronized (this) { 382 t = loadTable(arr[0]); 383 version = t.version; 384 } 385 386 Object prev = t.getAttr(arr[1], attrName); 388 389 if (prev == value ) { 390 return; 391 } 392 393 synchronized (this) { 394 Table t2 = loadTable(arr[0]); 395 396 if ((t == t2) && (version == t2.version)) { 397 if (value == null) { 401 t.setAttr(arr[1], attrName, null); } else { 403 if ( 404 (objType = XMLMapAttr.Attr.distinguishObject(value)) == XMLMapAttr.Attr.isValid( 405 "SERIALVALUE" 406 ) 407 ) { t.setAttr(arr[1], attrName, value); } else { 410 t.setAttr(arr[1], attrName, XMLMapAttr.createAttribute(objType, value.toString())); 411 } 412 } 413 414 saveTable(arr[0], t); 415 416 return; 418 } 419 } 420 421 } 423 } 424 425 429 public synchronized Enumeration <String > attributes(String name) { 430 String [] arr = new String [2]; 431 split(name, arr); 432 433 Table t = loadTable(arr[0]); 434 435 return t.attrs(arr[1]); 436 } 437 438 443 public synchronized void renameAttributes(String oldName, String newName) { 444 try { 445 String [] arr = new String [2]; 446 split(oldName, arr); 447 448 Table t = loadTable(arr[0]); 449 Map v = (Map ) t.remove(arr[1]); 450 451 if (v == null) { 453 return; 455 } 456 457 split(newName, arr); 458 459 Iterator it = v.entrySet().iterator(); 461 462 while (it.hasNext()) { 463 Map.Entry pair = (Map.Entry ) it.next(); 464 465 if (FileUtil.transientAttributes.contains(pair.getKey())) { 466 it.remove(); 467 } 468 } 469 t.put(arr[1], v); 470 471 saveTable(arr[0], t); 473 } catch (IOException e) { 474 ExternalUtil.exception(e); 475 } 476 } 477 478 482 public synchronized void deleteAttributes(String name) { 483 try { 484 String [] arr = new String [2]; 485 split(name, arr); 486 487 Table t = loadTable(arr[0]); 488 489 if (t.remove(arr[1]) != null) { 490 saveTable(arr[0], t); 492 } 493 } catch (IOException e) { 494 ExternalUtil.exception(e); 495 } 496 } 497 498 500 private Map getCache() { 501 if (cache == null) { 502 cache = new HashMap (31); 503 } 504 505 return cache; 506 } 507 508 512 private static void split(String name, String [] arr) { 513 int i = name.lastIndexOf('/'); 514 515 if (i == -1) { 516 arr[0] = ""; arr[1] = name; 518 519 return; 520 } 521 522 arr[0] = name.substring(0, i); 524 525 if (++i == name.length()) { 527 arr[1] = ""; } else { 529 arr[1] = name.substring(i); 531 } 532 } 533 534 538 private void saveTable(String name, Table map) throws IOException { 539 String fullName = ((name.length() == 0) ? "" : (name + '/')) + getFileName(); 541 542 String safeName = fullName + "~"; 544 if (info.folder(fullName)) { 545 if (map.size() == 0) { 546 return; 548 } 549 550 change.createData(fullName); 552 } else { 553 if (map.size() == 0) { 554 deleteFile(fullName); 555 556 return; 557 } 558 } 559 560 PrintWriter pw = null; 561 IOException ioexc = null; 562 563 try { 564 pw = new PrintWriter (new OutputStreamWriter (new BufferedOutputStream (info.outputStream(safeName)), "UTF8")); map.writeToXML(pw); 566 pw.flush(); 567 } catch (IOException iex) { 568 ioexc = iex; 569 } finally { 570 if (pw != null) { 571 pw.close(); 572 } 573 574 if (ioexc != null) { 575 deleteFile(safeName); 576 throw ioexc; 577 } else { 578 try { 579 deleteFile(fullName); 580 } catch (IOException iex2) { 581 584 } 585 586 this.change.rename(safeName, fullName); 587 } 588 } 589 } 590 591 595 private Table loadTable(String name) { 597 Reference r = (Reference ) getCache().get(name); 598 599 if (r != null) { 600 Table m = (Table) r.get(); 601 602 if (m != null) { 603 return m; 604 } 605 } 606 607 Table t = load(name); 609 t.attach(name, this); 610 611 getCache().put(name, new SoftReference (t)); 612 613 return t; 614 } 615 616 618 private Table load(String name) { 619 String [] acceptNames = { 620 ((name.length() == 0) ? "" : (name + '/')) + getFileName(), ((name.length() == 0) ? "" : (name + '/')) + ATTR_NAME_EXT 622 }; 624 for (int i = 0; i < acceptNames.length; i++) { 625 if (info.size(acceptNames[i]) > 0L) { 626 try { 627 InputStream fis = info.inputStream(acceptNames[i]); 628 629 try { 630 return loadTable(fis, acceptNames[i]); 631 } finally { 632 try { 633 fis.close(); 634 } catch (IOException e) { 635 } 637 } 638 } catch (FileNotFoundException ex) { 639 ExternalUtil.exception(ex); 640 } 641 } 642 } 643 644 return new Table(); 645 } 646 647 652 static Table loadTable(InputStream is, String folderName) { 653 Table retTable = new Table(); 654 PushbackInputStream pbStream = null; 655 boolean isSerialized = false; 656 657 try { 658 if (folderName.endsWith(ATTR_NAME_EXT)) { 659 pbStream = new PushbackInputStream (is, 4); isSerialized = isSerialized(pbStream); 661 } 662 663 if (isSerialized && (pbStream != null)) { 664 BufferedInputStream fis = new BufferedInputStream (pbStream); 665 ObjectInputStream ois = new org.openide.util.io.NbObjectInputStream(fis); 666 Object o = ois.readObject(); 667 668 if (o instanceof Table) { 669 return (Table) o; 670 } 671 } else { 672 BufferedInputStream bis = (pbStream != null) ? new BufferedInputStream (pbStream) 673 : new BufferedInputStream (is); 674 retTable.readFromXML(bis, false); 675 676 return retTable; 677 } 678 } catch (Exception e) { 679 IOException summaryEx = new IOException ( 681 NbBundle.getMessage(DefaultAttributes.class, "EXC_DefAttrReadErr") + ": " + folderName 682 ); 683 ExternalUtil.copyAnnotation(summaryEx, e); 684 ExternalUtil.exception(summaryEx); 685 } 686 687 return new Table(); 689 } 690 691 695 static private final boolean isSerialized(PushbackInputStream pbStream) 696 throws IOException { 697 int[] serialPattern = { '\u00AC', '\u00ED', '\u0000', '\u0005' }; byte[] checkedArray = new byte[serialPattern.length]; 699 int unsignedConv = 0; 700 701 pbStream.read(checkedArray, 0, checkedArray.length); 702 pbStream.unread(checkedArray); 703 704 for (int i = 0; i < checkedArray.length; i++) { 705 unsignedConv = (checkedArray[i] < 0) ? (checkedArray[i] + 256) : checkedArray[i]; 706 707 if (serialPattern[i] != unsignedConv) { 708 return false; 709 } 710 } 711 712 return true; 713 } 714 715 716 synchronized void removeTable(String name) { 717 getCache().remove(name); 718 } 719 720 724 728 static boolean acceptName(String name) { 729 return (name.endsWith(ATTR_NAME_EXT) || name.endsWith(ATTR_NAME_EXT_XML)); 730 } 731 732 private String getFileName() { 733 if (fileName == null) { 734 fileName = ATTR_NAME_EXT_XML; 735 } 736 737 return fileName; 738 } 739 740 private void deleteFile(String name) throws IOException { 741 OutputStream os = null; 742 try { 743 this.info.lock(name); 744 os = this.info.outputStream(name); 746 os.close(); os = null; 747 this.change.delete(name); 748 } finally { 749 if (os != null) { 750 os.close(); 751 } 752 this.info.unlock(name); 753 } 754 } 755 756 757 760 final static class Table extends HashMap implements Externalizable { 761 static final long serialVersionUID = 2353458763249746934L; 762 763 764 private transient String name; 765 766 767 private transient DefaultAttributes attrs; 768 769 770 private transient int version = 0; 771 772 773 public Table() { 774 super(11); 775 } 776 777 778 public void attach(String name, DefaultAttributes attrs) { 779 this.name = name; 780 this.attrs = attrs; 781 } 782 783 785 protected void finalize() { 786 attrs.removeTable(name); 788 } 789 790 795 public Object getAttr(String fileName, String attrName) { 796 XMLMapAttr m = (XMLMapAttr) get(fileName); 797 798 if (m != null) { 799 Object o = null; 800 801 try { 802 o = m.getAttribute(attrName); 803 } catch (Exception e) { 804 ExternalUtil.annotate(e, "fileName = " + fileName); ExternalUtil.exception(e); 806 } 807 808 if (o == null) { 809 return null; 810 } 811 812 if (!(o instanceof NbMarshalledObject)) { 813 return o; 814 } 815 816 NbMarshalledObject mo = (NbMarshalledObject) o; 817 818 try { 819 return (mo == null) ? null : mo.get(); 820 } catch (IOException e) { 821 ExternalUtil.log("Cannot load attribute " + attrName + " from " + fileName); ExternalUtil.exception(e); 823 } catch (ClassNotFoundException e) { 824 ExternalUtil.log("Cannot load attribute " + attrName + " from " + fileName); ExternalUtil.exception(e); 826 } 827 } 828 829 return null; 830 } 831 832 834 final void setMarshalledAttr(String fileName, String attrName, NbMarshalledObject obj) { 835 setAttr(fileName, attrName, obj); 836 } 837 838 845 final void setAttr(String fileName, String attrName, Object obj) { 846 XMLMapAttr m = (XMLMapAttr) get(fileName); 847 848 if (m == null) { 849 m = new XMLMapAttr(); put(fileName, m); 851 } 852 853 m.put(attrName, obj, false); 854 855 if ((obj == null) && (m.size() == 1)) { 856 remove(fileName); 857 } 858 859 version++; 861 } 862 863 865 public Enumeration <String > attrs(String fileName) { 866 Map m = (Map ) get(fileName); 867 868 if (m == null) { 869 return Enumerations.empty(); 870 } else { 871 HashSet s = new HashSet (m.keySet()); 872 873 return Collections.enumeration(s); 874 } 875 } 876 877 881 private ElementHandler parseFirstLevel() { 882 ElementHandler elemService = new ElementHandler() { 883 private final String [] ELM_KEYS = { "ATTRIBUTES" }; private final String [] MANDAT_ATTR_KEYS = { "VERSION" }; 886 public void internalStartElement(String elemName, HashMap mapMandatory, HashMap mapAllowed) 887 throws SAXException { 888 } 890 891 protected String [] getKeys() { 892 return ELM_KEYS; 893 } 894 895 protected String [] getMandatoryAttrs() { 896 return MANDAT_ATTR_KEYS; 897 } 898 }; 899 900 return elemService; 901 } 902 903 908 private ElementHandler parseSecondLevel(final StringBuffer fileName) { 909 ElementHandler elemService = new ElementHandler() { 910 private final String [] ELM_KEYS = { "FILEOBJECT" }; private final String [] MANDAT_ATTR_KEYS = { "NAME" }; 913 public void internalStartElement(String elemName, HashMap mapMandatory, HashMap mapAllowed) 914 throws SAXException { 915 String temp; 916 fileName.delete(0, fileName.length()); 917 temp = (String ) mapMandatory.get("NAME"); 919 if (temp == null) { 920 temp = (String ) mapMandatory.get("name"); } 922 923 if (temp != null) { 924 fileName.append(temp); 925 } 926 } 927 928 public void endElement(String elementName) 929 throws SAXException { 930 } 931 932 protected String [] getKeys() { 933 return ELM_KEYS; 934 } 935 936 protected String [] getMandatoryAttrs() { 937 return MANDAT_ATTR_KEYS; 938 } 939 }; 940 941 return elemService; 942 } 943 944 949 private ElementHandler parseThirdLevel(final StringBuffer fileName) { 950 ElementHandler elemService = new ElementHandler() { 951 private final String [] ELM_KEYS = { "ATTR" }; private final String [] MANDAT_ATTR_KEYS = { "NAME" }; 954 public void internalStartElement(String elemName, HashMap mapMandatory, HashMap mapAllowed) 955 throws SAXException { 956 String attrName; 957 958 if (mapAllowed.isEmpty()) { 959 return; 960 } 961 962 attrName = (String ) mapMandatory.get("NAME"); 964 if (attrName == null) { 965 attrName = (String ) mapMandatory.get("name"); } 967 968 if (attrName == null) { 969 return; 970 } 971 972 Iterator it = mapAllowed.entrySet().iterator(); 973 974 while (it.hasNext()) { 975 Map.Entry pair = (Map.Entry ) it.next(); 976 977 if (XMLMapAttr.Attr.isValid((String ) pair.getKey()) != -1) { 978 XMLMapAttr.Attr attr = XMLMapAttr.createAttributeAndDecode( 979 (String ) pair.getKey(), (String ) pair.getValue() 980 ); 981 setAttr(fileName.toString(), attrName, attr); 982 } 983 } 984 } 985 986 protected String [] getKeys() { 987 return ELM_KEYS; 988 } 989 990 protected String [] getMandatoryAttrs() { 991 return MANDAT_ATTR_KEYS; 992 } 993 994 protected String [] getAllowedAttrs() { 995 return XMLMapAttr.Attr.getAttrTypes(); 996 } 997 }; 999 1000 return elemService; 1001 } 1002 1003 1006 public void writeToXML(PrintWriter pw) { 1007 Iterator it = new TreeSet (keySet()).iterator(); 1009 XMLMapAttr.writeHeading(pw); 1010 1011 while (it.hasNext()) { 1012 String file = (String ) it.next(); 1013 XMLMapAttr attr = (XMLMapAttr) get(file); 1014 1015 if ((attr != null) && !attr.isEmpty()) { 1016 attr.write(pw, file, " "); } 1018 } 1019 1020 XMLMapAttr.writeEnding(pw); 1021 } 1022 1023 1029 public void readFromXML(InputStream is, boolean validate) 1030 throws SAXException { 1031 StringBuffer fileName = new StringBuffer (); 1032 ElementHandler[] elmKeyService = { parseFirstLevel(), parseSecondLevel(fileName), parseThirdLevel(fileName) }; String dtd = getClass().getClassLoader().getResource(DTD_PATH).toExternalForm(); 1034 InnerParser parser = new InnerParser(PUBLIC_ID, dtd, elmKeyService); 1035 1036 try { 1037 parser.parseXML(is, validate); 1038 } catch (Exception ioe) { 1039 throw (SAXException ) ExternalUtil.copyAnnotation( 1040 new SAXException (NbBundle.getMessage(DefaultAttributes.class, "EXC_DefAttrReadErr")), ioe 1041 ); 1042 } catch (FactoryConfigurationError fce) { 1043 throw (SAXException ) ExternalUtil.copyAnnotation( 1045 new SAXException (NbBundle.getMessage(DefaultAttributes.class, "EXC_DefAttrReadErr")), fce 1046 ); 1047 } 1048 } 1049 1050 1053 public void writeExternal(ObjectOutput oo) throws IOException { 1054 Iterator it = keySet().iterator(); 1056 1057 while (it.hasNext()) { 1058 String file = (String ) it.next(); 1059 Map attr = (Map ) get(file); 1060 1061 if ((attr != null) && !attr.isEmpty()) { 1062 oo.writeObject(file); 1063 1064 Iterator entries = attr.entrySet().iterator(); 1065 1066 while (entries.hasNext()) { 1067 Map.Entry entry = (Map.Entry ) entries.next(); 1068 String key = (String ) entry.getKey(); 1069 Object value = entry.getValue(); 1070 1071 if ((key != null) && (value != null)) { 1072 oo.writeObject(key); 1073 oo.writeObject(value); 1074 } 1075 } 1076 1077 oo.writeObject(null); 1078 } 1079 } 1080 1081 oo.writeObject(null); 1082 } 1083 1084 1086 public void readExternal(ObjectInput oi) throws IOException , ClassNotFoundException { 1087 for (;;) { 1088 String file = (String ) oi.readObject(); 1089 1090 if (file == null) { 1091 break; 1092 } 1093 1094 for (;;) { 1095 String attr = (String ) oi.readObject(); 1096 1097 if (attr == null) { 1098 break; 1099 } 1100 1101 Object o = oi.readObject(); 1102 1103 if (o instanceof java.rmi.MarshalledObject ) { 1105 o = ((java.rmi.MarshalledObject ) o).get(); 1106 o = new NbMarshalledObject(o); 1107 } 1108 1109 if (o instanceof NbMarshalledObject) { 1111 setAttr(file, attr, o); 1112 } 1113 } 1114 } 1115 } 1116 } 1117 1118 1129 static abstract class ElementHandler { 1130 private static final String [] EMPTY = { }; 1131 private int mandatAttrCount; 1132 1133 public void startElement(String elemName, Attributes attrs) 1134 throws SAXException { 1135 HashMap mapAllowed = new HashMap (); 1136 HashMap mapMandatory = new HashMap (); 1137 1138 if (checkAttributes(attrs, mapMandatory, mapAllowed) == false) { 1139 throw new SAXException ( 1140 NbBundle.getMessage(DefaultAttributes.class, "XML_InaccurateParam") + ": " + elemName 1141 ); } 1143 1144 internalStartElement(elemName, mapMandatory, mapAllowed); 1145 } 1146 1147 1153 protected void internalStartElement(String elemName, HashMap mapMandatory, HashMap mapAllowed) 1154 throws SAXException { 1155 } 1156 1157 1163 protected void characters(char[] ch, int start, int length) 1164 throws SAXException { 1165 } 1166 1167 1171 protected void endElement(String elemName) throws SAXException { 1172 } 1173 1174 1176 protected String [] getKeys() { 1177 return EMPTY; 1178 } 1179 1180 1182 protected String [] getMandatoryAttrs() { 1183 return getKeys(); 1184 } 1185 1186 1188 protected String [] getAllowedAttrs() { 1189 return EMPTY; 1190 } 1191 1192 private int isMyTag(String name) { 1193 return isInArray(name, getKeys()); 1194 } 1195 1196 private int isAllowedAttr(String name) { 1197 return isInArray(name, getAllowedAttrs()); 1198 } 1199 1200 private boolean isMandatOK() { 1201 return (mandatAttrCount == getMandatoryAttrs().length); 1202 } 1203 1204 private int isMandatoryAttr(String name) { 1205 int retValue = isInArray(name, getMandatoryAttrs()); 1206 1207 if (retValue != -1) { 1208 mandatAttrCount++; 1209 } 1210 1211 return retValue; 1212 } 1213 1214 private int isInArray(String name, String [] arr) { 1215 if ((arr == null) || (name == null)) { 1216 return -1; 1217 } 1218 1219 String correctStr = name.trim(); 1220 1221 for (int i = 0; i < arr.length; i++) { 1222 if (correctStr.equalsIgnoreCase(arr[i]) == true) { 1223 return i; 1224 } 1225 } 1226 1227 return -1; 1228 } 1229 1230 private boolean checkAttributes(Attributes attrList, HashMap mapMandatory, HashMap mapAllowed) { 1231 String temp; 1232 mandatAttrCount = 0; 1233 1234 if (attrList == null) { 1235 return false; 1236 } 1237 1238 for (int i = 0; i < attrList.getLength(); i++) { 1239 if (isMandatoryAttr(attrList.getQName(i)) != -1) { 1240 temp = attrList.getQName(i).toUpperCase(Locale.ENGLISH); 1241 mapMandatory.put(temp, attrList.getValue(i)); 1242 1243 continue; 1244 } 1245 1246 if (isAllowedAttr(attrList.getQName(i)) != -1) { 1247 temp = attrList.getQName(i).toUpperCase(Locale.ENGLISH); 1248 mapAllowed.put(temp, attrList.getValue(i)); 1249 1250 continue; 1251 } 1252 } 1253 1254 return isMandatOK(); 1255 } 1256 } 1257 1258 1260 static class InnerParser extends DefaultHandler { 1261 private ElementHandler[] elmKeyService; private String tagInProcess = ""; private String publicId; 1264 private String publicURL; 1265 1266 InnerParser(String publicId, String publicURL, ElementHandler[] elmKeyService) { 1267 this.elmKeyService = elmKeyService; 1268 this.publicId = publicId; 1269 this.publicURL = publicURL; 1270 } 1271 1272 1278 public void parseXML(String uri, boolean validate) 1279 throws IOException , SAXException , ParserConfigurationException , FactoryConfigurationError { 1280 XMLReader parser = getParser(validate); 1281 parser.parse(uri); 1282 } 1283 1284 1290 public void parseXML(InputStream is, boolean validate) 1291 throws IOException , SAXException , ParserConfigurationException , FactoryConfigurationError { 1292 InputSource iSource = new InputSource (is); 1293 XMLReader parser = getParser(validate); 1294 parser.parse(iSource); 1295 } 1296 1297 private XMLReader getParser(boolean validate) 1298 throws SAXException , ParserConfigurationException , FactoryConfigurationError { 1299 XMLReader parser = XMLUtil.createXMLReader(validate); 1300 1301 parser.setEntityResolver(this); 1303 parser.setContentHandler(this); 1304 parser.setErrorHandler(this); 1305 1306 return parser; 1307 } 1308 1309 public void error(SAXParseException exception) 1310 throws SAXException { 1311 throw exception; 1312 } 1313 1314 public void warning(SAXParseException exception) 1315 throws SAXException { 1316 throw exception; 1317 } 1318 1319 public void fatalError(SAXParseException exception) 1320 throws SAXException { 1321 throw exception; 1322 } 1323 1324 public void startElement(String uri, String lname, String name, Attributes attrs) 1325 throws SAXException { 1326 tagInProcess = name = name.trim(); 1327 1328 for (int i = 0; i < elmKeyService.length; i++) { 1329 if (elmKeyService[i].isMyTag(name) != -1) { 1330 elmKeyService[i].startElement(name, attrs); 1331 1332 return; 1333 } 1334 } 1335 1336 throw new SAXException (NbBundle.getMessage(DefaultAttributes.class, "XML_UnknownElement") + " " + name); } 1338 1339 public void endElement(String uri, String lname, String name) throws SAXException { 1340 for (int i = 0; i < elmKeyService.length; i++) { 1341 if (elmKeyService[i].isMyTag(name.trim()) != -1) { 1342 elmKeyService[i].endElement(name.trim()); 1343 1344 return; 1345 } 1346 } 1347 1348 throw new SAXException (NbBundle.getMessage(DefaultAttributes.class, "XML_UnknownElement") + " " + name); } 1350 1351 public void characters(char[] ch, int start, int length) 1352 throws SAXException { 1353 for (int i = 0; i < elmKeyService.length; i++) { 1354 if (elmKeyService[i].isMyTag(tagInProcess) != -1) { 1355 elmKeyService[i].characters(ch, start, length); 1356 1357 return; 1358 } 1359 } 1360 1361 throw new SAXException ( 1362 NbBundle.getMessage(DefaultAttributes.class, "XML_UnknownElement") + " " + tagInProcess 1363 ); } 1365 1366 public InputSource resolveEntity(java.lang.String pid, java.lang.String sid) 1367 throws SAXException { 1368 if ((pid != null) && pid.equals(publicId)) { 1369 return new InputSource (publicURL); 1370 } 1371 1372 return new InputSource (sid); 1373 } 1374 } 1375} 1376 | Popular Tags |