1 19 20 package org.netbeans.api.languages; 21 22 import java.io.InputStream ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 import org.openide.ErrorManager; 29 import org.openide.util.Lookup; 30 import org.openide.xml.XMLUtil; 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.XMLReader ; 35 import org.xml.sax.helpers.DefaultHandler ; 36 37 38 43 public class LibrarySupport { 44 45 50 public static LibrarySupport create (String resourceName) { 51 return new LibrarySupport (resourceName); 52 } 53 54 private String resourceName; 55 56 private LibrarySupport (String resourceName) { 57 this.resourceName = resourceName; 58 } 59 60 61 private Map <String ,List <String >> keys = new HashMap <String ,List <String >> (); 62 63 70 public List <String > getItems (String context) { 71 List <String > k = keys.get (context); 72 if (k == null) { 73 Map <String ,Map <String ,String >> m = getItems ().get (context); 74 if (m == null) return null; 75 k = new ArrayList <String > (m.keySet ()); 76 Collections.<String >sort (k); 77 k = Collections.<String >unmodifiableList (k); 78 keys.put (context, k); 79 } 80 return k; 81 } 82 83 90 public String getProperty (String context, String item, String propertyName) { 91 Map <String ,Map <String ,String >> m = getItems ().get (context); 92 if (m == null) return null; 93 Map <String ,String > m1 = m.get (item); 94 if (m1 == null) return null; 95 return m1.get (propertyName); 96 } 97 98 99 101 private Map <String ,Map <String ,Map <String ,String >>> items; 102 103 private Map <String ,Map <String ,Map <String ,String >>> getItems () { 104 if (items == null) 105 try { 106 XMLReader reader = XMLUtil.createXMLReader (); 107 Handler handler = new Handler (); 108 reader.setEntityResolver (handler); 109 reader.setContentHandler (handler); 110 ClassLoader loader = (ClassLoader ) Lookup.getDefault (). 111 lookup (ClassLoader .class); 112 InputStream is = loader.getResourceAsStream (resourceName); 113 try { 114 reader.parse (new InputSource (is)); 115 } finally { 116 is.close (); 117 } 118 items = handler.result; 119 } catch (Exception ex) { 120 ErrorManager.getDefault ().notify (ex); 121 items = Collections.<String ,Map <String ,Map <String ,String >>> emptyMap (); 122 } 123 return items; 124 } 125 126 static class Handler extends DefaultHandler { 127 128 Map <String ,Map <String ,Map <String ,String >>> result = new HashMap <String ,Map <String ,Map <String ,String >>> (); 129 130 public void startElement ( 131 String uri, 132 String localName, 133 String name, 134 Attributes attributes 135 ) throws SAXException { 136 try { 137 if (name.equals ("node")) { 138 String contexts = attributes.getValue ("context"); 139 String key = attributes.getValue ("key"); 140 Map <String ,String > properties = null; 141 if (attributes.getLength () > 2) { 142 properties = new HashMap <String ,String > (); 143 int i, k = attributes.getLength (); 144 for (i = 0; i < k; i++) { 145 String propertyName = attributes.getQName (i); 146 if ("context".equals (propertyName)) continue; 147 if ("key".equals (propertyName)) continue; 148 properties.put (propertyName, attributes.getValue (i)); 149 } 150 } 151 while (true) { 152 int i = contexts.indexOf (','); 153 String context = i >= 0 ? 154 contexts.substring (0, i).trim () : contexts; 155 Map <String ,Map <String ,String >> c = result.get (context); 156 if (c == null) { 157 c = new HashMap <String ,Map <String ,String >> (); 158 result.put (context, c); 159 } 160 if (c.containsKey (key)) 161 throw new IllegalArgumentException ("Key " + context + "-" + key + " already exists!"); 162 c.put (key, properties); 163 if (i < 0) break; 164 contexts = contexts.substring (i + 1); 165 } 166 } 167 } catch (Exception ex) { 168 ErrorManager.getDefault ().notify (ex); 169 } 170 } 171 172 public InputSource resolveEntity (String pubid, String sysid) { 173 return new InputSource ( 174 new java.io.ByteArrayInputStream (new byte [0]) 175 ); 176 } 177 } 178 } 179 | Popular Tags |