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