KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > languages > LibrarySupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.languages;
21
22 import java.io.InputStream JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import org.openide.ErrorManager;
29 import org.openide.util.Lookup;
30 import org.openide.xml.XMLUtil;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34 import org.xml.sax.XMLReader JavaDoc;
35 import org.xml.sax.helpers.DefaultHandler JavaDoc;
36
37
38 /**
39  * Support for definition of libraries. Used for code completion.
40  *
41  * @author Jan Jancura
42  */

43 public class LibrarySupport {
44
45     /**
46      * Crates new Instance of LibrarySupport and reads library definition from give resource file.
47      *
48      * @param resourceName a name of resource file
49      */

50     public static LibrarySupport create (String JavaDoc resourceName) {
51         return new LibrarySupport (resourceName);
52     }
53     
54     private String JavaDoc resourceName;
55     
56     private LibrarySupport (String JavaDoc resourceName) {
57         this.resourceName = resourceName;
58     }
59     
60     
61     private Map JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> keys = new HashMap JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> ();
62     
63     /**
64      * Returns list of items for given context (e.g. list of static methods
65      * for fiven class name).
66      *
67      * @param context
68      * @return list of items for given context
69      */

70     public List JavaDoc<String JavaDoc> getItems (String JavaDoc context) {
71         List JavaDoc<String JavaDoc> k = keys.get (context);
72         if (k == null) {
73             Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>> m = getItems ().get (context);
74             if (m == null) return null;
75             k = new ArrayList JavaDoc<String JavaDoc> (m.keySet ());
76             Collections.<String JavaDoc>sort (k);
77             k = Collections.<String JavaDoc>unmodifiableList (k);
78             keys.put (context, k);
79         }
80         return k;
81     }
82     
83     /**
84      * Returns property for given item, context and property name.
85      *
86      * @param context a context
87      * @param item an item
88      * @param propertyName a name of property
89      */

90     public String JavaDoc getProperty (String JavaDoc context, String JavaDoc item, String JavaDoc propertyName) {
91         Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>> m = getItems ().get (context);
92         if (m == null) return null;
93         Map JavaDoc<String JavaDoc,String JavaDoc> m1 = m.get (item);
94         if (m1 == null) return null;
95         return m1.get (propertyName);
96     }
97     
98     
99     // generics support methods ................................................
100

101     private Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>>> items;
102     
103     private Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>>> getItems () {
104         if (items == null)
105             try {
106                 XMLReader JavaDoc reader = XMLUtil.createXMLReader ();
107                 Handler JavaDoc handler = new Handler JavaDoc ();
108                 reader.setEntityResolver (handler);
109                 reader.setContentHandler (handler);
110                 ClassLoader JavaDoc loader = (ClassLoader JavaDoc) Lookup.getDefault ().
111                     lookup (ClassLoader JavaDoc.class);
112                 InputStream JavaDoc is = loader.getResourceAsStream (resourceName);
113                 try {
114                     reader.parse (new InputSource JavaDoc (is));
115                 } finally {
116                     is.close ();
117                 }
118                 items = handler.result;
119             } catch (Exception JavaDoc ex) {
120                 ErrorManager.getDefault ().notify (ex);
121                 items = Collections.<String JavaDoc,Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>>> emptyMap ();
122             }
123         return items;
124     }
125     
126     static class Handler extends DefaultHandler JavaDoc {
127         
128         Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>>> result = new HashMap JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>>> ();
129         
130         public void startElement (
131             String JavaDoc uri,
132             String JavaDoc localName,
133             String JavaDoc name,
134             Attributes JavaDoc attributes
135         ) throws SAXException JavaDoc {
136             try {
137                 if (name.equals ("node")) {
138                     String JavaDoc contexts = attributes.getValue ("context");
139                     String JavaDoc key = attributes.getValue ("key");
140                     Map JavaDoc<String JavaDoc,String JavaDoc> properties = null;
141                     if (attributes.getLength () > 2) {
142                         properties = new HashMap JavaDoc<String JavaDoc,String JavaDoc> ();
143                         int i, k = attributes.getLength ();
144                         for (i = 0; i < k; i++) {
145                             String JavaDoc 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 JavaDoc context = i >= 0 ?
154                             contexts.substring (0, i).trim () : contexts;
155                         Map JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>> c = result.get (context);
156                         if (c == null) {
157                             c = new HashMap JavaDoc<String JavaDoc,Map JavaDoc<String JavaDoc,String JavaDoc>> ();
158                             result.put (context, c);
159                         }
160                         if (c.containsKey (key))
161                             throw new IllegalArgumentException JavaDoc ("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 JavaDoc ex) {
168                 ErrorManager.getDefault ().notify (ex);
169             }
170         }
171         
172         public InputSource JavaDoc resolveEntity (String JavaDoc pubid, String JavaDoc sysid) {
173             return new InputSource JavaDoc (
174                 new java.io.ByteArrayInputStream JavaDoc (new byte [0])
175             );
176         }
177     }
178 }
179
Popular Tags