1 12 13 package org.eclipse.ant.internal.ui.editor.formatter; 14 15 import java.util.LinkedList ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.jface.text.IDocument; 19 import org.eclipse.jface.text.TextUtilities; 20 import org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy; 21 import org.eclipse.jface.text.formatter.FormattingContextProperties; 22 import org.eclipse.jface.text.formatter.IFormattingContext; 23 24 public class XmlDocumentFormattingStrategy extends ContextBasedFormattingStrategy { 25 26 27 private final LinkedList fDocuments= new LinkedList (); 28 29 30 private FormattingPreferences prefs; 31 32 private int indent= -1; 33 34 public XmlDocumentFormattingStrategy() { 35 this.prefs = new FormattingPreferences(); 36 } 37 38 public XmlDocumentFormattingStrategy(FormattingPreferences prefs, int indent) { 39 Assert.isNotNull(prefs); 40 this.prefs = prefs; 41 this.indent= indent; 42 } 43 44 47 public void format() { 48 49 super.format(); 50 final IDocument document= (IDocument)fDocuments.removeFirst(); 51 if (document != null) { 52 String documentText = document.get(); 54 XmlDocumentFormatter formatter = new XmlDocumentFormatter(); 55 if (indent != -1) { 56 formatter.setInitialIndent(indent); 57 } 58 formatter.setDefaultLineDelimiter(TextUtilities.getDefaultLineDelimiter(document)); 59 String formattedText = formatter.format(documentText, this.prefs); 60 if (formattedText != null && !formattedText.equals(documentText)) { 61 document.set(formattedText); 62 } 63 } 64 } 65 66 69 public void formatterStarts(final IFormattingContext context) { 70 super.formatterStarts(context); 71 72 fDocuments.addLast(context.getProperty(FormattingContextProperties.CONTEXT_MEDIUM)); 73 } 74 75 78 public void formatterStops() { 79 super.formatterStops(); 80 81 fDocuments.clear(); 82 } 83 } | Popular Tags |