KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
22 import javax.swing.text.Document JavaDoc;
23 import javax.swing.text.BadLocationException JavaDoc;
24
25 import org.netbeans.editor.Syntax;
26 import org.netbeans.editor.TokenID;
27 import org.netbeans.editor.TokenItem;
28 import org.netbeans.editor.TokenItem;
29 import org.netbeans.editor.ext.AbstractFormatLayer;
30 import org.netbeans.editor.ext.FormatTokenPosition;
31 import org.netbeans.editor.ext.ExtFormatter;
32 import org.netbeans.editor.ext.FormatLayer;
33 import org.netbeans.editor.ext.FormatSupport;
34 import org.netbeans.editor.ext.ExtFormatSupport;
35 import org.netbeans.editor.ext.FormatWriter;
36 import org.netbeans.editor.Utilities;
37 import org.netbeans.editor.BaseDocument;
38
39 import org.netbeans.modules.xml.text.syntax.javacc.lib.JJEditorSyntax;
40
41 /**
42  * @author Libor Kramolis
43  * @version 0.1
44  */

45 public class DTDFormatter extends ExtFormatter {
46
47     //
48
// init
49
//
50

51     /** */
52     public DTDFormatter (Class JavaDoc kitClass) {
53         super (kitClass);
54     }
55
56
57     //
58
// ExtFormatter
59
//
60

61     /**
62      */

63     public FormatSupport createFormatSupport (FormatWriter fw) {
64         return new XMLFormatSupport (fw);
65     }
66
67     /**
68      */

69     protected boolean acceptSyntax (Syntax syntax) {
70         return (syntax instanceof JJEditorSyntax);
71     }
72
73     /**
74      */

75     protected void initFormatLayers() {
76         addFormatLayer (new StripEndWhitespaceLayer());
77     }
78
79    /** Inserts new line at given position and indents the new line with
80     * spaces.
81     *
82     * @param doc the document to work on
83     * @param offset the offset of a character on the line
84     * @return new offset to place cursor to
85     */

86     public int indentNewLine (Document JavaDoc doc, int offset) {
87         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n+ XMLFormatter::indentNewLine: doc = " + doc); // NOI18N
88
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ ::indentNewLine: offset = " + offset); // NOI18N
89

90         if (doc instanceof BaseDocument) {
91             BaseDocument bdoc = (BaseDocument)doc;
92
93             bdoc.atomicLock();
94             try {
95                 bdoc.insertString (offset, "\n", null); // NOI18N
96
offset++;
97
98                 int fullLine = Utilities.getFirstNonWhiteBwd (bdoc, offset - 1);
99                 int indent = Utilities.getRowIndent (bdoc, fullLine);
100                 
101                 if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ ::indentNewLine: fullLine = " + fullLine); // NOI18N
102
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ ::indentNewLine: indent = " + indent); // NOI18N
103

104                 if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ ::indentNewLine: offset = " + offset); // NOI18N
105
// if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ ::indentNewLine: sb = '" + sb.toString() + "'"); // NOI18N
106

107                 String JavaDoc indentation = getIndentString(bdoc, indent);
108                 bdoc.insertString (offset, indentation, null);
109                 offset += indentation.length();
110
111                 if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+ ::indentNewLine: offset = " + offset); // NOI18N
112
} catch (BadLocationException JavaDoc e) {
113                 if (Boolean.getBoolean ("netbeans.debug.exceptions")) { // NOI18N
114
e.printStackTrace();
115                 }
116             } finally {
117                 bdoc.atomicUnlock();
118             }
119
120             return offset;
121         }
122
123         return super.indentNewLine (doc, offset);
124     }
125
126     
127     //
128
// class StripEndWhitespaceLayer
129
//
130

131     /**
132      *
133      */

134     public class StripEndWhitespaceLayer extends AbstractFormatLayer {
135        
136         //
137
// init
138
//
139

140         /** */
141         public StripEndWhitespaceLayer() {
142             super("xml-strip-whitespace-at-line-end-layer"); // NOI18N
143
}
144         
145         //
146
// AbstractFormatLayer
147
//
148

149         /**
150          */

151         protected FormatSupport createFormatSupport (FormatWriter fw) {
152             return new XMLFormatSupport (fw);
153         }
154         
155         /**
156          */

157         public void format (FormatWriter fw) {
158             XMLFormatSupport xfs = (XMLFormatSupport)createFormatSupport (fw);
159             
160             FormatTokenPosition pos = xfs.getFormatStartPosition();
161             
162             if ( (xfs.isLineStart (pos) == false) ||
163                  xfs.isIndentOnly() ) { // don't do anything
164

165             } else { // remove end-line whitespace
166
while (pos.getToken() != null) {
167                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("XMLFormatSupport::StripEndWhitespaceLayer::format: position = " + pos); // NOI18N
168

169                     pos = xfs.removeLineEndWhitespace (pos);
170                     if (pos.getToken() != null) {
171                         pos = xfs.getNextPosition (pos);
172                     }
173                 }
174             }
175         }
176
177     } // end: class StripEndWhitespaceLayer
178

179 }
180
Popular Tags