package net.sourceforge.phpeclipse.phpeditor.html; import java.io.StringWriter; import java.util.HashSet; import java.util.Set; import net.sourceforge.phpeclipse.PHPeclipsePlugin; import net.sourceforge.phpeclipse.phpeditor.PHPEditor; import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration; import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.text.source.SourceViewer; /** * Modified from the XMLFormatter. Some tags in HTML do not indent. * * @fixme The HTML parser cannot recognize < tag> as tag due to the extra space. * * @author chrisl */ public class HTMLFormatter implements IHTMLConstants { //////////////////////////////////////////////////////////////////////// private static final String NAME = "HTMLFormatter"; private static final boolean TRACE = false; // private static boolean VERBOSE = false; private static Set fBLOCK_TAG_SET; private static Set fSTART_TAG_SET; static { fBLOCK_TAG_SET = new HashSet(); for (int i = 0; i < BLOCK_TAGS.length; ++i) fBLOCK_TAG_SET.add(BLOCK_TAGS[i]); // fSTART_TAG_SET = new HashSet(); for (int i = 0; i < START_TAGS.length; ++i) fSTART_TAG_SET.add(START_TAGS[i]); } //////////////////////////////////////////////////////////////////////// private PHPSourceViewerConfiguration fConfig; private SourceViewer fViewer; // private PHPEditor fEditor; private String fFilename; //////////////////////////////////////////////////////////////////////// /** * Constructor for XMLFormatter. */ public HTMLFormatter(PHPSourceViewerConfiguration cf, SourceViewer viewer) { fConfig=cf; fViewer = viewer; // fEditor = fConfig.getEditor(); } //////////////////////////////////////////////////////////////////////// /** * Formats the String sourceString, * and returns a string containing the formatted version. * * @param string the string to format * @param indentationLevel the initial indentation level, used * to shift left/right the entire source fragment. An initial indentation * level of zero has no effect. * @param positions an array of positions to map. These are * character-based source positions inside the original source, * for which corresponding positions in the formatted source will * be computed (so as to relocate elements associated with the original * source). It updates the positions array with updated positions. * If set to null, then no positions are mapped. * @param lineSeparator the line separator to use in formatted source, * if set to null, then the platform default one will be used. * @return the formatted output string. */ public String format( String string, int indentationLevel, int[] positions, String lineSeparator, String inputname) { StringWriter ret = new StringWriter(2048); try { if (PHPeclipsePlugin.DEBUG) { System.err.println(NAME + ".format(): inputname=" + inputname); } // TidyHTMLParser parser = new TidyHTMLParser(new TidyConfiguration(fEditor, fViewer)); // parser.setCompactFormat( // ((Boolean) fViewer.getData(IConstants.KEY_COMPACT_FORMAT)).booleanValue()); // parser.parse(new StringReader(string), inputname, new PrintWriter(ret)); // if (parser.getParseErrors() != 0) { // PHPeclipsePlugin.log(IStatus.INFO, "Parse error"); // return string; // } return ret.toString(); } catch (Exception e) { // PHPeclipsePlugin.error("Unknown parse error: "+e.getMessage(), null, fEditor.getEditorInput(), fViewer); PHPeclipsePlugin.log(IStatus.ERROR, e.getMessage()); return string; } } }