KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > html > HTMLLexerFormatter


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.editor.ext.html;
21
22 import javax.swing.text.BadLocationException JavaDoc;
23 import org.netbeans.api.html.lexer.HTMLTokenId;
24 import org.netbeans.api.lexer.Token;
25 import org.netbeans.api.lexer.TokenHierarchy;
26 import org.netbeans.api.lexer.TokenSequence;
27 import org.netbeans.editor.BaseDocument;
28 import org.netbeans.editor.Syntax;
29 import org.netbeans.editor.ext.html.dtd.DTD;
30 import org.netbeans.editor.ext.html.dtd.DTD.Element;
31 import org.netbeans.modules.editor.structure.formatting.TagBasedLexerFormatter;
32
33 /**
34  * A lexer-based formatter for html files.
35  * @author Tomasz.Slota@Sun.COM
36  */

37
38 public class HTMLLexerFormatter extends TagBasedLexerFormatter {
39     private static final String JavaDoc[] UNFORMATTABLE_TAGS = new String JavaDoc[]{"pre", "script", "code"}; //NOI18N
40

41     /** Creates a new instance of HTMLFormater */
42     public HTMLLexerFormatter(Class JavaDoc kitClass) {
43     super(kitClass);
44     }
45     
46     @Override JavaDoc protected boolean acceptSyntax(Syntax syntax) {
47     return (syntax instanceof HTMLSyntax);
48     }
49     
50     @Override JavaDoc protected int getTagEndingAtPosition(TokenHierarchy tokenHierarchy, int position) throws BadLocationException JavaDoc{
51     if (position >= 0) {
52             TokenSequence tokenSequence = tokenHierarchy.tokenSequence();
53             tokenSequence.move(position);
54             tokenSequence.moveNext();
55         Token token = tokenSequence.token();
56                     
57         if (token.id() == HTMLTokenId.TAG_CLOSE_SYMBOL &&
58             !token.text().toString().endsWith("/>")){ //NOI18N
59

60                 while (tokenSequence.movePrevious()){
61                     int tokenOffset = tokenSequence.offset();
62                     
63                     if (isOpeningTag(tokenHierarchy, tokenOffset)
64                             || isClosingTag(tokenHierarchy, tokenOffset)){
65                         return tokenSequence.offset();
66                     }
67                 }
68         }
69     }
70     return -1;
71     }
72     
73     @Override JavaDoc protected int getTagEndOffset(TokenHierarchy tokenHierarchy, int tagStartOffset){
74         TokenSequence tokenSequence = tokenHierarchy.tokenSequence();
75         tokenSequence.move(tagStartOffset);
76         tokenSequence.moveNext();
77         boolean thereAreMoreTokens = true;
78         
79         while (thereAreMoreTokens && tokenSequence.token().id() != HTMLTokenId.TAG_CLOSE_SYMBOL){
80             thereAreMoreTokens &= tokenSequence.moveNext();
81         }
82         
83         return thereAreMoreTokens ? tokenSequence.offset() : -1;
84     }
85     
86     @Override JavaDoc protected boolean isJustBeforeClosingTag(TokenHierarchy tokenHierarchy, int tagTokenOffset) throws BadLocationException JavaDoc{
87         // a workaround for the difference with XML syntax support
88
return super.isJustBeforeClosingTag(tokenHierarchy, tagTokenOffset + "</".length()); //NOI18N
89
}
90     
91     @Override JavaDoc protected boolean isClosingTag(TokenHierarchy tokenHierarchy, int tagTokenOffset){
92         Token token = getTokenAtOffset(tokenHierarchy, tagTokenOffset);
93     return token != null && token.id() == HTMLTokenId.TAG_CLOSE;
94     }
95     
96     @Override JavaDoc protected boolean isOpeningTag(TokenHierarchy tokenHierarchy, int tagTokenOffset){
97         Token token = getTokenAtOffset(tokenHierarchy, tagTokenOffset);
98     return token != null && token.id() == HTMLTokenId.TAG_OPEN;
99     }
100     
101     @Override JavaDoc protected String JavaDoc extractTagName(TokenHierarchy tokenHierarchy, int tagTokenOffset){
102     return getTokenAtOffset(tokenHierarchy, tagTokenOffset).text().toString().trim();
103     }
104     
105     @Override JavaDoc protected boolean areTagNamesEqual(String JavaDoc tagName1, String JavaDoc tagName2){
106     return tagName1.equalsIgnoreCase(tagName2);
107     }
108     
109     @Override JavaDoc protected int getOpeningSymbolOffset(TokenHierarchy tokenHierarchy, int tagTokenOffset){
110     TokenSequence tokenSequence = tokenHierarchy.tokenSequence();
111         tokenSequence.move(tagTokenOffset);
112         boolean thereAreMoreTokens = true;
113     
114     do{
115         thereAreMoreTokens = tokenSequence.movePrevious();
116     }
117     while(thereAreMoreTokens && tokenSequence.token().id() != HTMLTokenId.TAG_OPEN_SYMBOL);
118     
119     if (thereAreMoreTokens){
120         return tokenSequence.offset();
121     }
122     
123     return -1;
124     }
125     
126     @Override JavaDoc protected boolean isClosingTagRequired(BaseDocument doc, String JavaDoc tagName) {
127     HTMLSyntaxSupport htmlsup = (HTMLSyntaxSupport)(doc.getSyntaxSupport().get(HTMLSyntaxSupport.class));
128         DTD dtd = htmlsup.getDTD();
129         
130         if (dtd == null){
131             // Unknown DTD, do not automatically close any tag
132
return false;
133         }
134         
135     Element elem = dtd.getElement(tagName.toUpperCase());
136         
137         if (elem == null){
138             // automatically close unknown tags
139
return true;
140         }
141         
142     return !elem.isEmpty(); // close tag unless it is known to be empty
143
}
144     
145     @Override JavaDoc protected boolean isUnformattableToken(TokenHierarchy tokenHierarchy, int tagTokenOffset) {
146     Token token = getTokenAtOffset(tokenHierarchy, tagTokenOffset);
147         
148     if (token.id() == HTMLTokenId.BLOCK_COMMENT){
149         return true;
150     }
151     
152     return false;
153     }
154     
155     @Override JavaDoc protected boolean isUnformattableTag(String JavaDoc tag) {
156     for(int i = 0; i < UNFORMATTABLE_TAGS.length; i++) {
157         if(tag.equalsIgnoreCase(UNFORMATTABLE_TAGS[i])) {
158         return true;
159         }
160     }
161     
162     return false;
163     }
164 }
165
Popular Tags