KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > lexer > LanguageEmbedding


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.spi.lexer;
21
22 import org.netbeans.api.lexer.Language;
23 import org.netbeans.api.lexer.TokenId;
24 import org.netbeans.lib.lexer.LanguageOperation;
25 import org.netbeans.lib.lexer.LexerUtilsConstants;
26
27 /**
28  * Description of a particular language embedding including
29  * starting and ending skipped regions of a token containing this embedding
30  * and a definition of an embedded language hierarchy.
31  *
32  * @author Miloslav Metelka
33  * @version 1.00
34  */

35
36 public final class LanguageEmbedding<T extends TokenId> {
37     
38     /**
39      * Create language embedding that does not join embedded sections.
40      *
41      * @see #create(Language, int, int, boolean)
42      */

43     public static <T extends TokenId> LanguageEmbedding<T> create(
44     Language<T> language, int startSkipLength, int endSkipLength) {
45         return create(language, startSkipLength, endSkipLength, false);
46     }
47
48     /**
49      * Construct new language embedding for the given parameters
50      * or get an existing cached one.
51      *
52      * @param language non-null language.
53      * @param startSkipLength &gt;=0 number of characters in an initial part of the token
54      * for which the language embedding is defined that should be excluded
55      * from the embedded section. The excluded characters will not be lexed
56      * and there will be no tokens created for them.
57      * @param endSkipLength &gt;=0 number of characters at the end of the token
58      * for which the language embedding is defined that should be excluded
59      * from the embedded section. The excluded characters will not be lexed
60      * and there will be no tokens created for them.
61      * @param joinSections whether sections with this embedding should be joined
62      * across the input source or whether they should stay separate.
63      * <br/>
64      * For example for HTML sections embedded in JSP this flag should be true:
65      * <pre>
66      * &lt;!-- HTML comment start
67      * &lt;% System.out.println("Hello"); %&gt;
68             still in HTML comment --&lt;
69      * </pre>
70      * <br/>
71      * Only the embedded sections with the same language path can be joined.
72      */

73     public static <T extends TokenId> LanguageEmbedding<T> create(
74     Language<T> language, int startSkipLength, int endSkipLength, boolean joinSections) {
75         if (language == null) {
76             throw new IllegalArgumentException JavaDoc("language may not be null"); // NOI18N
77
}
78         if (startSkipLength < 0) {
79             throw new IllegalArgumentException JavaDoc("startSkipLength=" + startSkipLength + " < 0");
80         }
81         if (endSkipLength < 0) {
82             throw new IllegalArgumentException JavaDoc("endSkipLength=" + endSkipLength + " < 0");
83         }
84
85         LanguageOperation<T> op = LexerUtilsConstants.languageOperation(language);
86         return op.getEmbedding(startSkipLength, endSkipLength, joinSections);
87     }
88     
89     private final Language<T> language;
90     
91     private final int startSkipLength;
92     
93     private final int endSkipLength;
94     
95     private final boolean joinSections;
96     
97     /**
98      * Package-private constructor used by lexer spi package accessor.
99      */

100     LanguageEmbedding(Language<T> language,
101     int startSkipLength, int endSkipLength, boolean joinSections) {
102         assert (language != null) : "Embedded language may not be null."; // NOI18N
103
assert (startSkipLength >= 0 && endSkipLength >= 0);
104         this.language = language;
105         this.startSkipLength = startSkipLength;
106         this.endSkipLength = endSkipLength;
107         this.joinSections = joinSections;
108     }
109     
110     /**
111      * Get the embedded language.
112      *
113      * @return non-null embedded language.
114      */

115     public Language<T> language() {
116         return language;
117     }
118
119     /**
120      * Get length of the initial part of the token (for which the embedding
121      * is being created) that should be skipped
122      * so it will be excluded from lexing and no tokens will be created for it.
123      *
124      * @return &gt;=0 number of characters in an initial part of the token
125      * (for which the language embedding is defined) that should be excluded
126      * from the embedded section. The excluded characters will not be lexed
127      * and there will be no tokens created for them.
128      */

129     public int startSkipLength() {
130         return startSkipLength;
131     }
132     
133     /**
134      * Get length of the ending part of the token (for which the embedding
135      * is being created) that should be skipped
136      * so it will be excluded from lexing and no tokens will be created for it.
137      *
138      * @return &gt;=0 number of characters at the end of the token
139      * (for which the language embedding is defined) that should be excluded
140      * from the embedded section. The excluded characters will not be lexed
141      * and there will be no tokens created for them.
142      */

143     public int endSkipLength() {
144         return endSkipLength;
145     }
146
147     /**
148      * Whether sections with this embedding should be joined with the other
149      * sections with this embedding at the same level.
150      *
151      * @return joinSections whether sections with this embedding should be joined
152      * across the input source or whether they should stay separate.
153      */

154     public boolean joinSections() {
155         return joinSections;
156     }
157     
158     public String JavaDoc toString() {
159         return "language: " + language() + ", skip[" + startSkipLength() // NOI18N
160
+ ", " + endSkipLength + "]"; // NOI18N
161
}
162     
163 }
164
Popular Tags