1 package net.sourceforge.phpeclipse.phpeditor.html;
3 import java.io.StringWriter;
4 import java.util.HashSet;
7 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
8 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
9 import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.jface.text.source.SourceViewer;
15 * Modified from the XMLFormatter. Some tags in HTML do not indent.
17 * @fixme The HTML parser cannot recognize < tag> as tag due to the extra space.
21 public class HTMLFormatter implements IHTMLConstants {
23 ////////////////////////////////////////////////////////////////////////
25 private static final String NAME = "HTMLFormatter";
26 private static final boolean TRACE = false;
27 // private static boolean VERBOSE = false;
29 private static Set fBLOCK_TAG_SET;
30 private static Set fSTART_TAG_SET;
32 fBLOCK_TAG_SET = new HashSet();
33 for (int i = 0; i < BLOCK_TAGS.length; ++i)
34 fBLOCK_TAG_SET.add(BLOCK_TAGS[i]);
36 fSTART_TAG_SET = new HashSet();
37 for (int i = 0; i < START_TAGS.length; ++i)
38 fSTART_TAG_SET.add(START_TAGS[i]);
41 ////////////////////////////////////////////////////////////////////////
43 private PHPSourceViewerConfiguration fConfig;
44 private SourceViewer fViewer;
46 private PHPEditor fEditor;
47 private String fFilename;
49 ////////////////////////////////////////////////////////////////////////
52 * Constructor for XMLFormatter.
54 public HTMLFormatter(PHPSourceViewerConfiguration cf, SourceViewer viewer) {
58 fEditor = fConfig.getEditor();
61 ////////////////////////////////////////////////////////////////////////
64 * Formats the String <code>sourceString</code>,
65 * and returns a string containing the formatted version.
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.
87 StringWriter ret = new StringWriter(2048);
89 if (PHPeclipsePlugin.DEBUG) {
90 System.err.println(NAME + ".format(): inputname=" + inputname);
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");
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());