1 19 package org.netbeans.modules.xml.text.indent; 20 21 import java.io.IOException ; 22 import javax.swing.text.Document ; 23 import javax.swing.text.BadLocationException ; 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 45 public class DTDFormatter extends ExtFormatter { 46 47 51 52 public DTDFormatter (Class kitClass) { 53 super (kitClass); 54 } 55 56 57 61 63 public FormatSupport createFormatSupport (FormatWriter fw) { 64 return new XMLFormatSupport (fw); 65 } 66 67 69 protected boolean acceptSyntax (Syntax syntax) { 70 return (syntax instanceof JJEditorSyntax); 71 } 72 73 75 protected void initFormatLayers() { 76 addFormatLayer (new StripEndWhitespaceLayer()); 77 } 78 79 86 public int indentNewLine (Document doc, int offset) { 87 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\n+ XMLFormatter::indentNewLine: doc = " + doc); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("+ ::indentNewLine: offset = " + offset); 90 if (doc instanceof BaseDocument) { 91 BaseDocument bdoc = (BaseDocument)doc; 92 93 bdoc.atomicLock(); 94 try { 95 bdoc.insertString (offset, "\n", null); offset++; 97 98 int fullLine = Utilities.getFirstNonWhiteBwd (bdoc, offset - 1); 99 int indent = Utilities.getRowIndent (bdoc, fullLine); 100 101 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("+ ::indentNewLine: fullLine = " + fullLine); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("+ ::indentNewLine: indent = " + indent); 104 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("+ ::indentNewLine: offset = " + offset); 107 String indentation = getIndentString(bdoc, indent); 108 bdoc.insertString (offset, indentation, null); 109 offset += indentation.length(); 110 111 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("+ ::indentNewLine: offset = " + offset); } catch (BadLocationException e) { 113 if (Boolean.getBoolean ("netbeans.debug.exceptions")) { 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 131 134 public class StripEndWhitespaceLayer extends AbstractFormatLayer { 135 136 140 141 public StripEndWhitespaceLayer() { 142 super("xml-strip-whitespace-at-line-end-layer"); } 144 145 149 151 protected FormatSupport createFormatSupport (FormatWriter fw) { 152 return new XMLFormatSupport (fw); 153 } 154 155 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() ) { 165 } else { while (pos.getToken() != null) { 167 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("XMLFormatSupport::StripEndWhitespaceLayer::format: position = " + pos); 169 pos = xfs.removeLineEndWhitespace (pos); 170 if (pos.getToken() != null) { 171 pos = xfs.getNextPosition (pos); 172 } 173 } 174 } 175 } 176 177 } 179 } 180 | Popular Tags |