1 package net.sourceforge.phpdt.tidy;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
7 import net.sourceforge.phpdt.tidy.preferences.IPreferenceConstants;
8 import net.sourceforge.phpdt.tidy.w3c.Configuration;
9 import net.sourceforge.phpdt.tidy.w3c.Tidy;
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;
22 * The main plugin class to be used in the desktop.
24 public class JtidyPlugin extends AbstractUIPlugin implements IPreferenceConstants {
25 //The shared instance.
26 private static JtidyPlugin fPlugin;
28 private ResourceBundle fResourceBundle;
31 private boolean fUseConfigurationFile;
32 public static String MARKER_NAME = "net.sourceforge.phpdt.tidy.MarkerName";
37 public JtidyPlugin(IPluginDescriptor descriptor) {
41 fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpdt.tidy.JtidyPluginResources");
42 } catch (MissingResourceException x) {
43 fResourceBundle = null;
49 * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
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$
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$
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$
70 // store.setDefault(TYPE_TREAD_AS_XML, "false"); //$NON-NLS-1$
71 // store.setDefault(TYPE_DOCTYPE, "false"); //$NON-NLS-1$
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$
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$
85 store.setDefault(GENERAL_TIDY_MARK, "true"); //$NON-NLS-1$
89 * Initialises the Tidy Instance and registers the Preference Listener.
91 private void initTidy() {
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();
102 if (value instanceof Boolean) {
103 boolean enabled = ((Boolean) value).booleanValue();
104 if (propName.equals(GENERAL_USE_CONFIG_FILE)) {
105 fUseConfigurationFile = enabled;
109 fUseConfigurationFile = store.getBoolean(GENERAL_USE_CONFIG_FILE);
110 if (!fUseConfigurationFile) {
111 if (propName.equals(GENERAL_TIDY_MARK)) {
112 configuration.TidyMark = enabled;
114 if (propName.equals(GENERAL_SHOW_WARNINGS)) {
115 configuration.ShowWarnings = enabled;
117 if (propName.equals(GENERAL_QUIET)) {
118 configuration.Quiet = enabled;
120 if (propName.equals(GENERAL_EMACS)) {
121 configuration.Emacs = enabled;
123 if (propName.equals(GENERAL_KEEP_FILE_TIMES)) {
124 configuration.KeepFileTimes = enabled;
127 if (propName.equals(WRAP_ATT_VALS)) {
128 configuration.WrapAttVals = enabled;
130 if (propName.equals(WRAP_SCRIPTLETS)) {
131 configuration.WrapScriptlets = enabled;
133 if (propName.equals(WRAP_SECTION)) {
134 configuration.WrapSection = enabled;
136 if (propName.equals(WRAP_ASP)) {
137 configuration.WrapAsp = enabled;
139 if (propName.equals(WRAP_JSTE)) {
140 configuration.WrapJste = enabled;
142 if (propName.equals(WRAP_PHP)) {
143 configuration.WrapPhp = enabled;
145 if (propName.equals(INDENT_ATTRIBUTES)) {
146 configuration.IndentAttributes = enabled;
148 if (propName.equals(LITERAL_ATTRIBS)) {
149 configuration.LiteralAttribs = enabled;
152 // if (propName.equals(TYPE_TREAD_AS_XML)) {
153 // configuration.XmlTags = enabled;
155 // if (propName.equals(TYPE_DOCTYPE)) {
156 // configuration.XmlPi = enabled;
159 if (propName.equals(OUTPUT_MAKE_CLEAR)) {
160 configuration.MakeClean = enabled;
163 if (propName.equals(OUTPUT_ENCLOSE_BODY_TEXT)) {
164 configuration.EncloseBodyText = enabled;
166 if (propName.equals(OUTPUT_ENCLOSE_BLOCK_TEXT)) {
167 configuration.EncloseBlockText = enabled;
169 if (propName.equals(OUTPUT_STRIP_WORD)) {
170 configuration.Word2000 = enabled;
172 // if (propName.equals(OUTPUT_DEFAULT_ALT_TEXT)) {
173 // configuration. = enabled;
176 if (propName.equals(OUT_AS_RAW)) {
177 configuration.RawOut = enabled;
179 if (propName.equals(OUT_UPPER_TAGS)) {
180 configuration.UpperCaseTags = enabled;
182 if (propName.equals(OUT_UPPER_ATTR)) {
183 configuration.UpperCaseAttrs = enabled;
187 } else if (value instanceof String) {
188 if (fUseConfigurationFile) {
189 if (propName.equals(GENERAL_CONFIG_FILE)) {
190 updateTidyConfig((String) value);
193 if (propName.equals(OUTPUT_DEFAULT_ALT_TEXT)) {
194 configuration.altText = (String) value;
196 if (propName.equals(INPUT_NEW_EMPTY_TAGS)) {
197 configuration.parseEmptyTagNames((String) value, null);
199 if (propName.equals(INPUT_NEW_INLINE_TAGS)) {
200 configuration.parseInlineTagNames((String) value, null);
202 if (propName.equals(INPUT_NEW_BLOCKLEVEL_TAGS)) {
203 configuration.parseBlockTagNames((String) value, null);
205 if (propName.equals(INPUT_NEW_PRE_TAGS)) {
206 configuration.parsePreTagNames((String) value, null);
213 getPreferenceStore().addPropertyChangeListener(listener);
217 private void initConfiguration() {
218 IPreferenceStore store = JtidyPlugin.getDefault().getPreferenceStore();
220 fUseConfigurationFile = store.getBoolean(GENERAL_USE_CONFIG_FILE);
221 Configuration configuration = fTidy.getConfiguration();
224 if (fUseConfigurationFile) {
225 if ((value = store.getString(GENERAL_CONFIG_FILE)) != null) {
226 updateTidyConfig((String) value);
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);
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);
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;
253 configuration.RawOut = store.getBoolean(OUT_AS_RAW);
254 configuration.UpperCaseTags = store.getBoolean(OUT_UPPER_TAGS);
255 configuration.UpperCaseAttrs = store.getBoolean(OUT_UPPER_ATTR);
257 if ((value = store.getString(INPUT_NEW_EMPTY_TAGS)) != null) {
258 configuration.parseEmptyTagNames(value, null);
260 if ((value = store.getString(INPUT_NEW_INLINE_TAGS)) != null) {
261 configuration.parseInlineTagNames(value, null);
263 if ((value = store.getString(INPUT_NEW_BLOCKLEVEL_TAGS)) != null) {
264 configuration.parseBlockTagNames(value, null);
266 if ((value = store.getString(INPUT_NEW_PRE_TAGS)) != null) {
267 configuration.parsePreTagNames(value, null);
273 * Updates the configuration of the tidy instance with content of the given
274 * file, if ths file exists. Returns silently on error.
276 * @param rawConfigFileName
278 private void updateTidyConfig(String rawConfigFileName) {
279 File config = new File(rawConfigFileName);
280 if (config.exists()) {
281 fTidy.setConfigurationFromFile(config.getAbsolutePath());
286 * Returns the shared instance.
288 public static JtidyPlugin getDefault() {
293 * Returns the workspace instance.
295 public static IWorkspace getWorkspace() {
296 return ResourcesPlugin.getWorkspace();
300 * Returns the string from the plugin's resource bundle,
301 * or 'key' if not found.
303 public static String getResourceString(String key) {
304 ResourceBundle bundle = JtidyPlugin.getDefault().getResourceBundle();
305 if (bundle == null) {
309 return bundle.getString(key);
310 } catch (MissingResourceException e) {
317 * Returns the plugin's resource bundle,
319 public ResourceBundle getResourceBundle() {
320 return fResourceBundle;
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.
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();
337 return getDefault().fTidy;
340 * This method is called upon plug-in activation
342 public void start(BundleContext context) throws Exception {
343 super.start(context);
347 * This method is called when the plug-in is stopped
349 public void stop(BundleContext context) throws Exception {