1 package net.sourceforge.phpeclipse.phpeditor.html;
3 import java.io.PrintWriter;
4 import java.io.StringReader;
5 import java.io.StringWriter;
6 import java.util.HashSet;
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
10 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
11 import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.jface.text.source.SourceViewer;
17 * Modified from the XMLFormatter. Some tags in HTML do not indent.
19 * @fixme The HTML parser cannot recognize < tag> as tag due to the extra space.
23 public class HTMLFormatter implements IHTMLConstants {
25 ////////////////////////////////////////////////////////////////////////
27 private static final String NAME = "HTMLFormatter";
28 private static final boolean TRACE = false;
29 // private static boolean VERBOSE = false;
31 private static Set fBLOCK_TAG_SET;
32 private static Set fSTART_TAG_SET;
34 fBLOCK_TAG_SET = new HashSet();
35 for (int i = 0; i < BLOCK_TAGS.length; ++i)
36 fBLOCK_TAG_SET.add(BLOCK_TAGS[i]);
38 fSTART_TAG_SET = new HashSet();
39 for (int i = 0; i < START_TAGS.length; ++i)
40 fSTART_TAG_SET.add(START_TAGS[i]);
43 ////////////////////////////////////////////////////////////////////////
45 private PHPSourceViewerConfiguration fConfig;
46 private SourceViewer fViewer;
48 private PHPEditor fEditor;
49 private String fFilename;
51 ////////////////////////////////////////////////////////////////////////
54 * Constructor for XMLFormatter.
56 public HTMLFormatter(PHPSourceViewerConfiguration cf, SourceViewer viewer) {
60 fEditor = fConfig.getEditor();
63 ////////////////////////////////////////////////////////////////////////
66 * Formats the String <code>sourceString</code>,
67 * and returns a string containing the formatted version.
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.
89 StringWriter ret = new StringWriter(2048);
91 if (PHPeclipsePlugin.DEBUG) {
92 System.err.println(NAME + ".format(): inputname=" + inputname);
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");
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());