KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > formatter > XmlDocumentFormattingStrategy


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 John-Mason P. Shackelford and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * John-Mason P. Shackelford - initial API and implementation
10  * IBM Corporation - bug 52076
11  *******************************************************************************/

12
13 package org.eclipse.ant.internal.ui.editor.formatter;
14
15 import java.util.LinkedList JavaDoc;
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     /** Documents to be formatted by this strategy */
27     private final LinkedList JavaDoc fDocuments= new LinkedList JavaDoc();
28     
29     /** access to the preferences store * */
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     /* (non-Javadoc)
45      * @see org.eclipse.jface.text.formatter.IFormattingStrategyExtension#format()
46      */

47     public void format() {
48  
49         super.format();
50         final IDocument document= (IDocument)fDocuments.removeFirst();
51         if (document != null) {
52             // TODO allow formatting of regions, not just the entire document; bug 75611
53
String JavaDoc 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 JavaDoc formattedText = formatter.format(documentText, this.prefs);
60             if (formattedText != null && !formattedText.equals(documentText)) {
61                 document.set(formattedText);
62             }
63         }
64      }
65      
66      /*
67      * @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#formatterStarts(org.eclipse.jface.text.formatter.IFormattingContext)
68      */

69     public void formatterStarts(final IFormattingContext context) {
70         super.formatterStarts(context);
71         
72         fDocuments.addLast(context.getProperty(FormattingContextProperties.CONTEXT_MEDIUM));
73     }
74
75     /*
76      * @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#formatterStops()
77      */

78     public void formatterStops() {
79         super.formatterStops();
80
81         fDocuments.clear();
82     }
83 }
Popular Tags