KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > lexer > InputAttributes


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.lexer;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * Supplementary information about particular input
27  * that may be used to influence the lexer's operation.
28  * <br/>
29  * For example there may be a version of the language
30  * to be used when lexing the input. The following code
31  * will interpret the "assert" as an identifier:
32  * <pre>
33  * InputAttributes attrs = new InputAttributes();
34  * attrs.setValue(JavaTokenId.language(), "version", "1.3", true);
35  * TokenHierarchy.create("assert", false, JavaTokenId.language(), null, attrs);
36  * </pre>
37  *
38  * <p>
39  * The properties are attached to a concrete language path only
40  * or they may be applied globally to all of the occurrences
41  * of the given path as a sub-path of the target path.
42  * <br/>
43  * See the "global" argument of
44  * {@link #setValue(Language,Object,Object,boolean)}.
45  * </p>
46  *
47  * <p>
48  * This class may safely be operated by multiple threads.
49  * </p>
50  *
51  * @author Miloslav Metelka
52  * @version 1.00
53  */

54
55 public final class InputAttributes {
56     
57     private final Map JavaDoc<LanguagePath,LPAttrs> lp2attrs;
58     
59     public InputAttributes() {
60         lp2attrs = new HashMap JavaDoc<LanguagePath,LPAttrs>();
61     }
62
63     /**
64      * Get value for the given key for the particular language path.
65      * <br/>
66      * If (for the given key) there was an explicit value set
67      * directly for the given language path then it will be returned.
68      * <br/>
69      * If not and there was a global value set for one of the sub-paths
70      * (from the largest to the smallest) then it will be returned.
71      *
72      * @param languagePath non-null language path.
73      * @param attributeKey non-null key of the attribute.
74      * @return value of the key for the given language path (or a global
75      * value for one of the subpaths).
76      */

77     public Object JavaDoc getValue(LanguagePath languagePath, Object JavaDoc attributeKey) {
78         checkAttributeKeyNotNull(attributeKey);
79         synchronized (lp2attrs) {
80             LPAttrs attrs = lp2attrs.get(languagePath);
81             Object JavaDoc value = null;
82             if (attrs != null) {
83                 value = attrs.getSpecific(attributeKey);
84                 if (value == null) { // try global value
85
value = attrs.getGlobal(attributeKey);
86                 }
87             }
88             // Try global values for subpaths
89
while (value == null && languagePath.size() > 1) {
90                 languagePath = languagePath.subPath(1);
91                 attrs = lp2attrs.get(languagePath);
92                 if (attrs != null) {
93                     value = attrs.getGlobal(attributeKey);
94                 }
95             }
96             return value;
97         }
98     }
99     
100     /**
101      * Assign a new value to a property for the language path constructed
102      * from the given language.
103      *
104      * @see #setValue(LanguagePath, Object, Object, boolean)
105      */

106     public void setValue(Language<? extends TokenId> language,
107     Object JavaDoc attributeKey, Object JavaDoc attributeValue, boolean global) {
108         setValue(LanguagePath.get(language), attributeKey, attributeValue, global);
109     }
110
111     /**
112      * Assign a new value to a property for the given language path.
113      *
114      * @param languagePath non-null language path.
115      * @param attributeKey non-null key of the attribute.
116      * @param attributeValue value of the key for the given language path.
117      * @param global if set to true then the value will be used not only for the given
118      * language path but also as a default value (if the value is not overwritten explicitly)
119      * for all the cases where the given path is embedded into the target language path.
120      * <br/>
121      * The following code
122      * <pre>
123      * attrs.setValue(LanguagePath.get(JavaTokenId.language()),
124      * "version", Integer.valueOf(5), true);
125      * </pre>
126      * sets the version 5 (it means java 1.5) to all the java code snipets
127      * regardless of where they are embedded in the token hierarchy.
128      */

129     public void setValue(LanguagePath languagePath,
130     Object JavaDoc attributeKey, Object JavaDoc attributeValue, boolean global) {
131         checkAttributeKeyNotNull(attributeKey);
132         synchronized (lp2attrs) {
133             LPAttrs attrs = lp2attrs.get(languagePath);
134             if (attrs == null) {
135                 attrs = new LPAttrs();
136                 lp2attrs.put(languagePath, attrs);
137             }
138             if (global) {
139                 attrs.putGlobal(attributeKey, attributeValue);
140             } else {
141                 attrs.putSpecific(attributeKey, attributeValue);
142             }
143         }
144     }
145     
146     private void checkAttributeKeyNotNull(Object JavaDoc attributeKey) {
147         if (attributeKey == null) {
148             throw new IllegalArgumentException JavaDoc("attributeKey cannot be null");
149         }
150     }
151     
152     private static final class LPAttrs {
153         
154         private Map JavaDoc<Object JavaDoc,Object JavaDoc> specifics;
155         
156         private Map JavaDoc<Object JavaDoc,Object JavaDoc> globals;
157         
158         public Object JavaDoc getSpecific(Object JavaDoc key) {
159             return (specifics != null) ? specifics.get(key) : null;
160         }
161         
162         public Object JavaDoc getGlobal(Object JavaDoc key) {
163             return (globals != null) ? globals.get(key) : null;
164         }
165         
166         public void putSpecific(Object JavaDoc key, Object JavaDoc value) {
167             if (specifics == null) {
168                 specifics = new HashMap JavaDoc<Object JavaDoc,Object JavaDoc>(4);
169             }
170             specifics.put(key, value);
171         }
172         
173         public void putGlobal(Object JavaDoc key, Object JavaDoc value) {
174             if (globals == null) {
175                 globals = new HashMap JavaDoc<Object JavaDoc,Object JavaDoc>(4);
176             }
177             globals.put(key, value);
178         }
179         
180     }
181     
182 }
183
Popular Tags