Started with PHP/HTML Formatter menu => doesn't work right now
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / html / HTMLFormatter.java
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/html/HTMLFormatter.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/html/HTMLFormatter.java
new file mode 100644 (file)
index 0000000..fcdb34a
--- /dev/null
@@ -0,0 +1,110 @@
+ package net.sourceforge.phpeclipse.phpeditor.html;
+
+import java.io.PrintWriter;
+import java.io.StringReader;
+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 <code>sourceString</code>,
+        * 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 <code>null</code>, then no positions are mapped.
+        * @param lineSeparator the line separator to use in formatted source,
+        *     if set to <code>null</code>, 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;
+               }
+       }
+
+}