KickJava   Java API By Example, From Geeks To Geeks.

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


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.editor.BaseDocument;
24 import org.netbeans.editor.Syntax;
25 import org.netbeans.editor.TokenItem;
26 import org.netbeans.editor.ext.ExtSyntaxSupport;
27 import org.netbeans.editor.ext.html.dtd.DTD;
28 import org.netbeans.editor.ext.html.dtd.DTD.Element;
29 import org.netbeans.modules.editor.structure.formatting.TagBasedFormatter;
30
31 /**
32  * Formatter for html files.
33  * @author Tomasz.Slota@Sun.COM
34  */

35
36 public class HTMLFormatter extends TagBasedFormatter {
37     private static final String JavaDoc[] UNFORMATTABLE_TAGS = new String JavaDoc[]{"pre", "script", "code"}; //NOI18N
38

39     /** Creates a new instance of HTMLFormater */
40     public HTMLFormatter(Class JavaDoc kitClass) {
41     super(kitClass);
42     }
43     
44     @Override JavaDoc protected boolean acceptSyntax(Syntax syntax) {
45     return (syntax instanceof HTMLSyntax);
46     }
47     
48     @Override JavaDoc protected ExtSyntaxSupport getSyntaxSupport(BaseDocument doc){
49     return (HTMLSyntaxSupport)(doc.getSyntaxSupport().get(HTMLSyntaxSupport.class));
50     }
51     
52     @Override JavaDoc protected TokenItem getTagTokenEndingAtPosition(BaseDocument doc, int position) throws BadLocationException JavaDoc{
53     if (position >= 0) {
54         HTMLSyntaxSupport sup = (HTMLSyntaxSupport)getSyntaxSupport(doc);
55         TokenItem token = sup.getTokenChain(position, position + 1);
56         
57         if (token.getTokenID() == HTMLTokenContext.TAG_CLOSE_SYMBOL &&
58             !token.getImage().endsWith("/>")){ //NOI18N
59

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