KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > indent > XMLFormatter


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 package org.netbeans.modules.xml.text.indent;
20
21 import java.util.regex.Pattern JavaDoc;
22 import javax.swing.text.BadLocationException JavaDoc;
23
24 import org.netbeans.editor.Syntax;
25 import org.netbeans.editor.TokenItem;
26 import org.netbeans.editor.ext.AbstractFormatLayer;
27 import org.netbeans.editor.ext.ExtSyntaxSupport;
28 import org.netbeans.editor.ext.FormatTokenPosition;
29 import org.netbeans.editor.ext.FormatSupport;
30 import org.netbeans.editor.ext.FormatWriter;
31 import org.netbeans.editor.BaseDocument;
32 import org.netbeans.modules.editor.structure.formatting.TagBasedFormatter;
33 import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport;
34 import org.netbeans.modules.xml.text.syntax.XMLTokenIDs;
35
36 import org.netbeans.modules.xml.text.syntax.javacc.lib.JJEditorSyntax;
37
38 /**
39  * @author Tomasz.Slota@Sun.COM
40  */

41 public class XMLFormatter extends TagBasedFormatter {
42     
43     private static final String JavaDoc TAG_OPENING_PREFIX = "<"; //NOI18N
44
private static final String JavaDoc TAG_CLOSING_PREFIX = "</"; //NOI18N
45
private static final String JavaDoc TAG_CLOSED_SUFFIX = "/>"; //NOI18N
46
private static final String JavaDoc TAG_CLOSING_SUFFIX = "/>"; //NOI18N
47

48     //at least one character
49
private static final Pattern JavaDoc VALID_TAG_NAME = Pattern.compile("[\\w+|-]*"); // NOI18N
50

51     private static final int WORKUNITS_MAX = 100;
52     
53     public XMLFormatter(Class JavaDoc kitClass) {
54         super(kitClass);
55     }
56     
57     public FormatSupport createFormatSupport(FormatWriter fw) {
58         return new XMLFormatSupport(fw);
59     }
60     
61     protected boolean acceptSyntax(Syntax syntax) {
62         return (syntax instanceof JJEditorSyntax);
63     }
64     
65     protected void initFormatLayers() {
66         addFormatLayer(new StripEndWhitespaceLayer());
67     }
68     
69     protected String JavaDoc extractTagName(TokenItem tknTag){
70         
71         String JavaDoc tagImage = tknTag.getImage();
72         int startIndex = -1;
73         
74         if (isOpeningTag(tknTag)){
75             startIndex = TAG_OPENING_PREFIX.length();
76         } else if (isClosingTag(tknTag)){
77             startIndex = TAG_CLOSING_PREFIX.length();
78         }
79         
80         if (startIndex >= 0){
81             String JavaDoc tagName = tagImage.substring(startIndex);
82             return tagName;
83         }
84         
85         return null;
86     }
87     
88     @Override JavaDoc protected boolean isOpeningTag(TokenItem token){
89         return token != null
90                 && token.getTokenID() == XMLTokenIDs.TAG
91                 && token.getImage().startsWith(TAG_OPENING_PREFIX)
92                 && !token.getImage().startsWith(TAG_CLOSING_PREFIX);
93     }
94     
95     @Override JavaDoc protected boolean isClosingTag(TokenItem token){
96         return token != null
97                 && token.getTokenID() == XMLTokenIDs.TAG
98                 && token.getImage().startsWith(TAG_CLOSING_PREFIX);
99     }
100     
101     @Override JavaDoc protected int getTagEndOffset(TokenItem token){
102         TokenItem t = token.getNext();
103         
104         while (t != null && t.getTokenID() != XMLTokenIDs.TAG){
105             t = t.getNext();
106         }
107         
108         return t == null ? -1 : t.getOffset();
109     }
110     
111     @Override JavaDoc protected ExtSyntaxSupport getSyntaxSupport(BaseDocument doc){
112         return (XMLSyntaxSupport)(doc.getSyntaxSupport().get(XMLSyntaxSupport.class));
113     }
114     
115     @Override JavaDoc protected boolean areTagNamesEqual(String JavaDoc tagName1, String JavaDoc tagName2){
116         return tagName1.equals(tagName2);
117     }
118     
119     @Override JavaDoc protected boolean isClosingTagRequired(BaseDocument doc, String JavaDoc tagName){
120         return true;
121     }
122     
123     @Override JavaDoc protected int getOpeningSymbolOffset(TokenItem tknTag){
124         return tknTag.getOffset();
125     }
126     
127     @Override JavaDoc protected TokenItem getTagTokenEndingAtPosition(BaseDocument doc, int position) throws BadLocationException JavaDoc{
128         if (position >= 0) {
129             ExtSyntaxSupport sup = getSyntaxSupport(doc);
130             TokenItem token = sup.getTokenChain(position, position + 1);
131             
132             if (token.getTokenID() == XMLTokenIDs.TAG &&
133                     token.getImage().equals(">")){ //NOI18N
134
do {
135                     token = token.getPrevious();
136                 }
137                 while (token != null && !isOpeningTag(token) && !isClosingTag(token));
138                 
139                 return token;
140             }
141         }
142         return null;
143     }
144     
145     @Override JavaDoc protected boolean isUnformattableToken(TokenItem token) {
146         
147         if (token.getTokenID() == XMLTokenIDs.BLOCK_COMMENT
148                 || token.getTokenID() == XMLTokenIDs.CDATA_SECTION){
149             return true;
150         }
151         
152         return false;
153     }
154     
155     @Override JavaDoc protected boolean isUnformattableTag(String JavaDoc tag) {
156         return false;
157     }
158     
159     public class StripEndWhitespaceLayer extends AbstractFormatLayer {
160         
161         public StripEndWhitespaceLayer() {
162             super("xml-strip-whitespace-at-line-end-layer"); // NOI18N
163
}
164         
165         protected FormatSupport createFormatSupport(FormatWriter fw) {
166             return new XMLFormatSupport(fw);
167         }
168         
169         public void format(FormatWriter fw) {
170             XMLFormatSupport xfs = (XMLFormatSupport)createFormatSupport(fw);
171             
172             FormatTokenPosition pos = xfs.getFormatStartPosition();
173             
174             if ( (xfs.isLineStart(pos) == false) ||
175                     xfs.isIndentOnly() ) { // don't do anything
176

177             } else { // remove end-line whitespace
178
while (pos.getToken() != null) {
179                     pos = xfs.removeLineEndWhitespace(pos);
180                     if (pos.getToken() != null) {
181                         pos = xfs.getNextPosition(pos);
182                     }
183                 }
184             }
185         }
186     }
187 }
188
Popular Tags