fcdb34a971e79b5c32eabdd558a438b0d33a3ffd
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / html / HTMLFormatter.java
1  package net.sourceforge.phpeclipse.phpeditor.html;
2
3 import java.io.PrintWriter;
4 import java.io.StringReader;
5 import java.io.StringWriter;
6 import java.util.HashSet;
7 import java.util.Set;
8
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
10 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
11 import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.jface.text.source.SourceViewer;
15
16 /**
17  * Modified from the XMLFormatter.  Some tags in HTML do not indent.
18  * 
19  * @fixme The HTML parser cannot recognize < tag> as tag due to the extra space.
20  * 
21  * @author chrisl
22  */
23 public class HTMLFormatter implements IHTMLConstants {
24
25         ////////////////////////////////////////////////////////////////////////
26
27         private static final String NAME = "HTMLFormatter";
28         private static final boolean TRACE = false;
29 //      private static boolean VERBOSE = false;
30
31         private static Set fBLOCK_TAG_SET;
32         private static Set fSTART_TAG_SET;
33         static {
34                 fBLOCK_TAG_SET = new HashSet();
35                 for (int i = 0; i < BLOCK_TAGS.length; ++i)
36                         fBLOCK_TAG_SET.add(BLOCK_TAGS[i]);
37                 //
38                 fSTART_TAG_SET = new HashSet();
39                 for (int i = 0; i < START_TAGS.length; ++i)
40                         fSTART_TAG_SET.add(START_TAGS[i]);
41         }
42
43         ////////////////////////////////////////////////////////////////////////
44
45         private PHPSourceViewerConfiguration fConfig;
46         private SourceViewer fViewer;
47         //
48         private PHPEditor fEditor;
49         private String fFilename;
50
51         ////////////////////////////////////////////////////////////////////////
52
53         /**
54          * Constructor for XMLFormatter.
55          */
56         public HTMLFormatter(PHPSourceViewerConfiguration cf, SourceViewer viewer) {
57                 fConfig=cf;
58                 fViewer = viewer;
59                 //
60                 fEditor = fConfig.getEditor();
61         }
62
63         ////////////////////////////////////////////////////////////////////////
64
65         /** 
66          * Formats the String <code>sourceString</code>,
67          * and returns a string containing the formatted version.
68          * 
69          * @param string the string to format
70          * @param indentationLevel the initial indentation level, used 
71          *      to shift left/right the entire source fragment. An initial indentation
72          *      level of zero has no effect.
73          * @param positions an array of positions to map. These are
74          *      character-based source positions inside the original source,
75          *     for which corresponding positions in the formatted source will
76          *     be computed (so as to relocate elements associated with the original
77          *     source). It updates the positions array with updated positions.
78          *     If set to <code>null</code>, then no positions are mapped.
79          * @param lineSeparator the line separator to use in formatted source,
80          *     if set to <code>null</code>, then the platform default one will be used.
81          * @return the formatted output string.
82          */
83         public String format(
84                 String string,
85                 int indentationLevel,
86                 int[] positions,
87                 String lineSeparator,
88                 String inputname) {
89                 StringWriter ret = new StringWriter(2048);
90                 try {
91                         if (PHPeclipsePlugin.DEBUG) {
92                                 System.err.println(NAME + ".format(): inputname=" + inputname);
93                         }
94 //                      TidyHTMLParser parser = new TidyHTMLParser(new TidyConfiguration(fEditor, fViewer));
95 //                      parser.setCompactFormat(
96 //                              ((Boolean) fViewer.getData(IConstants.KEY_COMPACT_FORMAT)).booleanValue());
97 //                      parser.parse(new StringReader(string), inputname, new PrintWriter(ret));
98 //                      if (parser.getParseErrors() != 0) {
99 //        PHPeclipsePlugin.log(IStatus.INFO, "Parse error");
100 //                              return string;
101 //                      }
102                         return ret.toString();
103                 } catch (Exception e) {
104                 //      PHPeclipsePlugin.error("Unknown parse error: "+e.getMessage(), null, fEditor.getEditorInput(), fViewer);
105       PHPeclipsePlugin.log(IStatus.ERROR, e.getMessage());
106                         return string;
107                 }
108         }
109
110 }