1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.jtidy / src / net / sourceforge / phpdt / tidy / JtidyPlugin.java
1 package net.sourceforge.phpdt.tidy;
2
3 import java.io.File;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6
7 import net.sourceforge.phpdt.tidy.preferences.IPreferenceConstants;
8 import net.sourceforge.phpdt.tidy.w3c.Configuration;
9 import net.sourceforge.phpdt.tidy.w3c.Tidy;
10
11 import org.eclipse.core.resources.IResource;
12 import org.eclipse.core.resources.IWorkspace;
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.core.runtime.IPluginDescriptor;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.ui.plugin.AbstractUIPlugin;
19 import org.osgi.framework.BundleContext;
20
21 /**
22  * The main plugin class to be used in the desktop.
23  */
24 public class JtidyPlugin extends AbstractUIPlugin implements IPreferenceConstants {
25   //The shared instance.
26   private static JtidyPlugin fPlugin;
27   //Resource bundle.
28   private ResourceBundle fResourceBundle;
29
30   private Tidy fTidy;
31   private boolean fUseConfigurationFile;
32   public static String MARKER_NAME = "net.sourceforge.phpdt.tidy.MarkerName";
33
34   /**
35    * The constructor.
36    */
37   public JtidyPlugin(IPluginDescriptor descriptor) {
38     super(descriptor);
39     fPlugin = this;
40     try {
41       fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpdt.tidy.JtidyPluginResources");
42     } catch (MissingResourceException x) {
43       fResourceBundle = null;
44     }
45     initTidy();
46   }
47
48   /* (non-Javadoc)
49    * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
50    */
51   protected void initializeDefaultPreferences(IPreferenceStore store) {
52     store.setDefault(GENERAL_CONFIG_FILE, ""); //$NON-NLS-1$
53     store.setDefault(GENERAL_USE_CONFIG_FILE, "false"); //$NON-NLS-1$
54
55     store.setDefault(GENERAL_TIDY_MARK, "true"); //$NON-NLS-1$
56     store.setDefault(GENERAL_SHOW_WARNINGS, "true"); //$NON-NLS-1$
57     store.setDefault(GENERAL_QUIET, "false"); //$NON-NLS-1$
58     store.setDefault(GENERAL_EMACS, "false"); //$NON-NLS-1$
59     store.setDefault(GENERAL_KEEP_FILE_TIMES, "true"); //$NON-NLS-1$    
60
61     store.setDefault(WRAP_ATT_VALS, "false"); //$NON-NLS-1$
62     store.setDefault(WRAP_SCRIPTLETS, "false"); //$NON-NLS-1$
63     store.setDefault(WRAP_SECTION, "true"); //$NON-NLS-1$
64     store.setDefault(WRAP_ASP, "true"); //$NON-NLS-1$
65     store.setDefault(WRAP_JSTE, "true"); //$NON-NLS-1$
66     store.setDefault(WRAP_PHP, "true"); //$NON-NLS-1$
67     store.setDefault(INDENT_ATTRIBUTES, "false"); //$NON-NLS-1$
68     store.setDefault(LITERAL_ATTRIBS, "false"); //$NON-NLS-1$
69
70     //    store.setDefault(TYPE_TREAD_AS_XML, "false"); //$NON-NLS-1$
71     //    store.setDefault(TYPE_DOCTYPE, "false"); //$NON-NLS-1$
72
73     store.setDefault(OUTPUT_MAKE_CLEAR, "false"); //$NON-NLS-1$
74     store.setDefault(OUTPUT_STRIP_WORD, "false"); //$NON-NLS-1$
75     store.setDefault(OUTPUT_ENCLOSE_BODY_TEXT, "false"); //$NON-NLS-1$
76     store.setDefault(OUTPUT_ENCLOSE_BLOCK_TEXT, "false"); //$NON-NLS-1$
77
78     store.setDefault(OUT_AS_RAW, "false"); //$NON-NLS-1$
79     store.setDefault(OUT_UPPER_TAGS, "false"); //$NON-NLS-1$
80     store.setDefault(OUT_UPPER_ATTR, "false"); //$NON-NLS-1$
81     store.setDefault(OUT_BREAK_BR, "false"); //$NON-NLS-1$
82     store.setDefault(OUT_WRAP_ATTR_VALUES, "false"); //$NON-NLS-1$
83     store.setDefault(OUT_WRAP_SCRIPS, "false"); //$NON-NLS-1$
84
85     store.setDefault(GENERAL_TIDY_MARK, "true"); //$NON-NLS-1$
86   }
87
88   /**
89    * Initialises the Tidy Instance and registers the Preference Listener.
90    */
91   private void initTidy() {
92     fTidy = new Tidy();
93     String rawConfigFileName = getPreferenceStore().getString(GENERAL_CONFIG_FILE);
94     updateTidyConfig(rawConfigFileName);
95     IPropertyChangeListener listener = new IPropertyChangeListener() {
96       public void propertyChange(PropertyChangeEvent event) {
97         String propName = event.getProperty();
98         IPreferenceStore store = JtidyPlugin.getDefault().getPreferenceStore();
99         Configuration configuration = fTidy.getConfiguration();
100         Object value = event.getNewValue();
101
102         if (value instanceof Boolean) {
103           boolean enabled = ((Boolean) value).booleanValue();
104           if (propName.equals(GENERAL_USE_CONFIG_FILE)) {
105             fUseConfigurationFile = enabled;
106             initConfiguration();
107             return;
108           }
109           fUseConfigurationFile = store.getBoolean(GENERAL_USE_CONFIG_FILE);
110           if (!fUseConfigurationFile) {
111             if (propName.equals(GENERAL_TIDY_MARK)) {
112               configuration.TidyMark = enabled;
113             }
114             if (propName.equals(GENERAL_SHOW_WARNINGS)) {
115               configuration.ShowWarnings = enabled;
116             }
117             if (propName.equals(GENERAL_QUIET)) {
118               configuration.Quiet = enabled;
119             }
120             if (propName.equals(GENERAL_EMACS)) {
121               configuration.Emacs = enabled;
122             }
123             if (propName.equals(GENERAL_KEEP_FILE_TIMES)) {
124               configuration.KeepFileTimes = enabled;
125             }
126             // wrap / indent
127             if (propName.equals(WRAP_ATT_VALS)) {
128               configuration.WrapAttVals = enabled;
129             }
130             if (propName.equals(WRAP_SCRIPTLETS)) {
131               configuration.WrapScriptlets = enabled;
132             }
133             if (propName.equals(WRAP_SECTION)) {
134               configuration.WrapSection = enabled;
135             }
136             if (propName.equals(WRAP_ASP)) {
137               configuration.WrapAsp = enabled;
138             }
139             if (propName.equals(WRAP_JSTE)) {
140               configuration.WrapJste = enabled;
141             }
142             if (propName.equals(WRAP_PHP)) {
143               configuration.WrapPhp = enabled;
144             }
145             if (propName.equals(INDENT_ATTRIBUTES)) {
146               configuration.IndentAttributes = enabled;
147             }
148             if (propName.equals(LITERAL_ATTRIBS)) {
149               configuration.LiteralAttribs = enabled;
150             }
151
152             //            if (propName.equals(TYPE_TREAD_AS_XML)) {
153             //              configuration.XmlTags = enabled;
154             //            }
155             //            if (propName.equals(TYPE_DOCTYPE)) {
156             //              configuration.XmlPi = enabled;
157             //            }                
158
159             if (propName.equals(OUTPUT_MAKE_CLEAR)) {
160               configuration.MakeClean = enabled;
161             }
162
163             if (propName.equals(OUTPUT_ENCLOSE_BODY_TEXT)) {
164               configuration.EncloseBodyText = enabled;
165             }
166             if (propName.equals(OUTPUT_ENCLOSE_BLOCK_TEXT)) {
167               configuration.EncloseBlockText = enabled;
168             }
169             if (propName.equals(OUTPUT_STRIP_WORD)) {
170               configuration.Word2000 = enabled;
171             }
172             //            if (propName.equals(OUTPUT_DEFAULT_ALT_TEXT)) {
173             //              configuration. = enabled;
174             //            } 
175
176             if (propName.equals(OUT_AS_RAW)) {
177               configuration.RawOut = enabled;
178             }
179             if (propName.equals(OUT_UPPER_TAGS)) {
180               configuration.UpperCaseTags = enabled;
181             }
182             if (propName.equals(OUT_UPPER_ATTR)) {
183               configuration.UpperCaseAttrs = enabled;
184             }
185
186           }
187         } else if (value instanceof String) {
188           if (fUseConfigurationFile) {
189             if (propName.equals(GENERAL_CONFIG_FILE)) {
190               updateTidyConfig((String) value);
191             }
192           } else {
193             if (propName.equals(OUTPUT_DEFAULT_ALT_TEXT)) {
194               configuration.altText = (String) value;
195             }
196             if (propName.equals(INPUT_NEW_EMPTY_TAGS)) {
197               configuration.parseEmptyTagNames((String) value, null);
198             }
199             if (propName.equals(INPUT_NEW_INLINE_TAGS)) {
200               configuration.parseInlineTagNames((String) value, null);
201             }
202             if (propName.equals(INPUT_NEW_BLOCKLEVEL_TAGS)) {
203               configuration.parseBlockTagNames((String) value, null);
204             }
205             if (propName.equals(INPUT_NEW_PRE_TAGS)) {
206               configuration.parsePreTagNames((String) value, null);
207             }
208           }
209         }
210       }
211     };
212
213     getPreferenceStore().addPropertyChangeListener(listener);
214     initConfiguration();
215   }
216
217   private void initConfiguration() {
218     IPreferenceStore store = JtidyPlugin.getDefault().getPreferenceStore();
219
220     fUseConfigurationFile = store.getBoolean(GENERAL_USE_CONFIG_FILE);
221     Configuration configuration = fTidy.getConfiguration();
222
223     String value;
224     if (fUseConfigurationFile) {
225       if ((value = store.getString(GENERAL_CONFIG_FILE)) != null) {
226         updateTidyConfig((String) value);
227       }
228     } else {
229
230       configuration.TidyMark = store.getBoolean(GENERAL_TIDY_MARK);
231       configuration.ShowWarnings = store.getBoolean(GENERAL_SHOW_WARNINGS);
232       configuration.Quiet = store.getBoolean(GENERAL_QUIET);
233       configuration.Emacs = store.getBoolean(GENERAL_EMACS);
234       configuration.KeepFileTimes = store.getBoolean(GENERAL_KEEP_FILE_TIMES);
235
236       configuration.WrapAttVals = store.getBoolean(WRAP_ATT_VALS);
237       configuration.WrapScriptlets = store.getBoolean(WRAP_SCRIPTLETS);
238       configuration.WrapSection = store.getBoolean(WRAP_SECTION);
239       configuration.WrapAsp = store.getBoolean(WRAP_ASP);
240       configuration.WrapJste = store.getBoolean(WRAP_JSTE);
241       configuration.WrapPhp = store.getBoolean(WRAP_PHP);
242       configuration.IndentAttributes = store.getBoolean(INDENT_ATTRIBUTES);
243       configuration.LiteralAttribs = store.getBoolean(LITERAL_ATTRIBS);
244
245       configuration.MakeClean = store.getBoolean(OUTPUT_MAKE_CLEAR);
246       configuration.EncloseBodyText = store.getBoolean(OUTPUT_ENCLOSE_BODY_TEXT);
247       configuration.EncloseBlockText = store.getBoolean(OUTPUT_ENCLOSE_BLOCK_TEXT);
248       configuration.Word2000 = store.getBoolean(OUTPUT_STRIP_WORD);
249       if ((value = store.getString(OUTPUT_DEFAULT_ALT_TEXT)) != null) {
250         configuration.altText = value;
251       }
252       
253       configuration.RawOut = store.getBoolean(OUT_AS_RAW);
254       configuration.UpperCaseTags = store.getBoolean(OUT_UPPER_TAGS);
255       configuration.UpperCaseAttrs = store.getBoolean(OUT_UPPER_ATTR);
256       
257       if ((value = store.getString(INPUT_NEW_EMPTY_TAGS)) != null) {
258         configuration.parseEmptyTagNames(value, null);
259       }
260       if ((value = store.getString(INPUT_NEW_INLINE_TAGS)) != null) {
261         configuration.parseInlineTagNames(value, null);
262       }
263       if ((value = store.getString(INPUT_NEW_BLOCKLEVEL_TAGS)) != null) {
264         configuration.parseBlockTagNames(value, null);
265       }
266       if ((value = store.getString(INPUT_NEW_PRE_TAGS)) != null) {
267         configuration.parsePreTagNames(value, null);
268       }
269     }
270   }
271
272   /**
273    * Updates the configuration of the tidy instance with content of the given
274    * file, if ths file exists. Returns silently on error.
275    * 
276    * @param rawConfigFileName
277    */
278   private void updateTidyConfig(String rawConfigFileName) {
279     File config = new File(rawConfigFileName);
280     if (config.exists()) {
281       fTidy.setConfigurationFromFile(config.getAbsolutePath());
282     }
283   }
284
285   /**
286    * Returns the shared instance.
287    */
288   public static JtidyPlugin getDefault() {
289     return fPlugin;
290   }
291
292   /**
293    * Returns the workspace instance.
294    */
295   public static IWorkspace getWorkspace() {
296     return ResourcesPlugin.getWorkspace();
297   }
298
299   /**
300    * Returns the string from the plugin's resource bundle,
301    * or 'key' if not found.
302    */
303   public static String getResourceString(String key) {
304     ResourceBundle bundle = JtidyPlugin.getDefault().getResourceBundle();
305     if (bundle == null) {
306       return key;
307     }
308     try {
309       return bundle.getString(key);
310     } catch (MissingResourceException e) {
311       return key;
312     }
313
314   }
315
316   /**
317    * Returns the plugin's resource bundle,
318    */
319   public ResourceBundle getResourceBundle() {
320     return fResourceBundle;
321   }
322
323   /**
324    * Returns the Tidy instance for this resource. Curently the resource can be
325    * null, I just copied a similar conzept from a differnt Project, which uses
326    * a per Project configuration.
327    * @param resource
328    * @return Tidy
329    */
330
331   public static Tidy getTidyInstance(IResource resource) {
332     //IProject project = resource.getProject();
333     //TODO: bind the instance to the resource...
334     if (getDefault().fTidy == null) {
335       getDefault().initTidy();
336     }
337     return getDefault().fTidy;
338   }
339   /**
340          * This method is called upon plug-in activation
341          */
342         public void start(BundleContext context) throws Exception {
343                 super.start(context);
344         }
345
346         /**
347          * This method is called when the plug-in is stopped
348          */
349         public void stop(BundleContext context) throws Exception {
350                 super.stop(context);
351         }
352 }