1 package net.sourceforge.phpdt.tidy;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
7 import org.eclipse.core.resources.IResource;
8 import org.eclipse.core.resources.IWorkspace;
9 import org.eclipse.core.resources.ResourcesPlugin;
10 import org.eclipse.core.runtime.IPluginDescriptor;
11 import org.eclipse.jface.preference.IPreferenceStore;
12 import org.eclipse.jface.util.IPropertyChangeListener;
13 import org.eclipse.jface.util.PropertyChangeEvent;
14 import org.eclipse.ui.plugin.AbstractUIPlugin;
15 import net.sourceforge.phpdt.tidy.w3c.Configuration;
16 import net.sourceforge.phpdt.tidy.w3c.Tidy;
18 import net.sourceforge.phpdt.tidy.preferences.IPreferenceConstants;
21 * The main plugin class to be used in the desktop.
23 public class JtidyPlugin extends AbstractUIPlugin implements IPreferenceConstants {
24 //The shared instance.
25 private static JtidyPlugin fPlugin;
27 private ResourceBundle fResourceBundle;
30 private boolean fUseConfigurationFile;
31 public static String MARKER_NAME = "net.sourceforge.phpdt.tidy.MarkerName";
36 public JtidyPlugin(IPluginDescriptor descriptor) {
40 fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpdt.tidy.JtidyPluginResources");
41 } catch (MissingResourceException x) {
42 fResourceBundle = null;
48 * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
50 protected void initializeDefaultPreferences(IPreferenceStore store) {
51 store.setDefault(GENERAL_CONFIG_FILE, ""); //$NON-NLS-1$
52 store.setDefault(GENERAL_USE_CONFIG_FILE, "false"); //$NON-NLS-1$
54 store.setDefault(GENERAL_TIDY_MARK, "true"); //$NON-NLS-1$
55 store.setDefault(GENERAL_SHOW_WARNINGS, "true"); //$NON-NLS-1$
56 store.setDefault(GENERAL_QUIET, "false"); //$NON-NLS-1$
57 store.setDefault(GENERAL_EMACS, "false"); //$NON-NLS-1$
58 store.setDefault(GENERAL_KEEP_FILE_TIMES, "true"); //$NON-NLS-1$
60 store.setDefault(WRAP_ATT_VALS, "false"); //$NON-NLS-1$
61 store.setDefault(WRAP_SCRIPTLETS, "false"); //$NON-NLS-1$
62 store.setDefault(WRAP_SECTION, "true"); //$NON-NLS-1$
63 store.setDefault(WRAP_ASP, "true"); //$NON-NLS-1$
64 store.setDefault(WRAP_JSTE, "true"); //$NON-NLS-1$
65 store.setDefault(WRAP_PHP, "true"); //$NON-NLS-1$
66 store.setDefault(INDENT_ATTRIBUTES, "false"); //$NON-NLS-1$
67 store.setDefault(LITERAL_ATTRIBS, "false"); //$NON-NLS-1$
69 // store.setDefault(TYPE_TREAD_AS_XML, "false"); //$NON-NLS-1$
70 // store.setDefault(TYPE_DOCTYPE, "false"); //$NON-NLS-1$
72 store.setDefault(OUTPUT_MAKE_CLEAR, "false"); //$NON-NLS-1$
73 store.setDefault(OUTPUT_STRIP_WORD, "false"); //$NON-NLS-1$
74 store.setDefault(OUTPUT_ENCLOSE_BODY_TEXT, "false"); //$NON-NLS-1$
75 store.setDefault(OUTPUT_ENCLOSE_BLOCK_TEXT, "false"); //$NON-NLS-1$
77 store.setDefault(OUT_AS_RAW, "false"); //$NON-NLS-1$
78 store.setDefault(OUT_UPPER_TAGS, "false"); //$NON-NLS-1$
79 store.setDefault(OUT_UPPER_ATTR, "false"); //$NON-NLS-1$
80 store.setDefault(OUT_BREAK_BR, "false"); //$NON-NLS-1$
81 store.setDefault(OUT_WRAP_ATTR_VALUES, "false"); //$NON-NLS-1$
82 store.setDefault(OUT_WRAP_SCRIPS, "false"); //$NON-NLS-1$
84 store.setDefault(GENERAL_TIDY_MARK, "true"); //$NON-NLS-1$
88 * Initialises the Tidy Instance and registers the Preference Listener.
90 private void initTidy() {
92 String rawConfigFileName = getPreferenceStore().getString(GENERAL_CONFIG_FILE);
93 updateTidyConfig(rawConfigFileName);
94 IPropertyChangeListener listener = new IPropertyChangeListener() {
95 public void propertyChange(PropertyChangeEvent event) {
96 String propName = event.getProperty();
97 IPreferenceStore store = JtidyPlugin.getDefault().getPreferenceStore();
98 Configuration configuration = fTidy.getConfiguration();
99 Object value = event.getNewValue();
101 if (value instanceof Boolean) {
102 boolean enabled = ((Boolean) value).booleanValue();
103 if (propName.equals(GENERAL_USE_CONFIG_FILE)) {
104 fUseConfigurationFile = enabled;
108 fUseConfigurationFile = store.getBoolean(GENERAL_USE_CONFIG_FILE);
109 if (!fUseConfigurationFile) {
110 if (propName.equals(GENERAL_TIDY_MARK)) {
111 configuration.TidyMark = enabled;
113 if (propName.equals(GENERAL_SHOW_WARNINGS)) {
114 configuration.ShowWarnings = enabled;
116 if (propName.equals(GENERAL_QUIET)) {
117 configuration.Quiet = enabled;
119 if (propName.equals(GENERAL_EMACS)) {
120 configuration.Emacs = enabled;
122 if (propName.equals(GENERAL_KEEP_FILE_TIMES)) {
123 configuration.KeepFileTimes = enabled;
126 if (propName.equals(WRAP_ATT_VALS)) {
127 configuration.WrapAttVals = enabled;
129 if (propName.equals(WRAP_SCRIPTLETS)) {
130 configuration.WrapScriptlets = enabled;
132 if (propName.equals(WRAP_SECTION)) {
133 configuration.WrapSection = enabled;
135 if (propName.equals(WRAP_ASP)) {
136 configuration.WrapAsp = enabled;
138 if (propName.equals(WRAP_JSTE)) {
139 configuration.WrapJste = enabled;
141 if (propName.equals(WRAP_PHP)) {
142 configuration.WrapPhp = enabled;
144 if (propName.equals(INDENT_ATTRIBUTES)) {
145 configuration.IndentAttributes = enabled;
147 if (propName.equals(LITERAL_ATTRIBS)) {
148 configuration.LiteralAttribs = enabled;
151 // if (propName.equals(TYPE_TREAD_AS_XML)) {
152 // configuration.XmlTags = enabled;
154 // if (propName.equals(TYPE_DOCTYPE)) {
155 // configuration.XmlPi = enabled;
158 if (propName.equals(OUTPUT_MAKE_CLEAR)) {
159 configuration.MakeClean = enabled;
162 if (propName.equals(OUTPUT_ENCLOSE_BODY_TEXT)) {
163 configuration.EncloseBodyText = enabled;
165 if (propName.equals(OUTPUT_ENCLOSE_BLOCK_TEXT)) {
166 configuration.EncloseBlockText = enabled;
168 if (propName.equals(OUTPUT_STRIP_WORD)) {
169 configuration.Word2000 = enabled;
171 // if (propName.equals(OUTPUT_DEFAULT_ALT_TEXT)) {
172 // configuration. = enabled;
175 if (propName.equals(OUT_AS_RAW)) {
176 configuration.RawOut = enabled;
178 if (propName.equals(OUT_UPPER_TAGS)) {
179 configuration.UpperCaseTags = enabled;
181 if (propName.equals(OUT_UPPER_ATTR)) {
182 configuration.UpperCaseAttrs = enabled;
186 } else if (value instanceof String) {
187 if (fUseConfigurationFile) {
188 if (propName.equals(GENERAL_CONFIG_FILE)) {
189 updateTidyConfig((String) value);
192 if (propName.equals(OUTPUT_DEFAULT_ALT_TEXT)) {
193 configuration.altText = (String) value;
195 if (propName.equals(INPUT_NEW_EMPTY_TAGS)) {
196 configuration.parseEmptyTagNames((String) value, null);
198 if (propName.equals(INPUT_NEW_INLINE_TAGS)) {
199 configuration.parseInlineTagNames((String) value, null);
201 if (propName.equals(INPUT_NEW_BLOCKLEVEL_TAGS)) {
202 configuration.parseBlockTagNames((String) value, null);
204 if (propName.equals(INPUT_NEW_PRE_TAGS)) {
205 configuration.parsePreTagNames((String) value, null);
212 getPreferenceStore().addPropertyChangeListener(listener);
216 private void initConfiguration() {
217 IPreferenceStore store = JtidyPlugin.getDefault().getPreferenceStore();
219 fUseConfigurationFile = store.getBoolean(GENERAL_USE_CONFIG_FILE);
220 Configuration configuration = fTidy.getConfiguration();
223 if (fUseConfigurationFile) {
224 if ((value = store.getString(GENERAL_CONFIG_FILE)) != null) {
225 updateTidyConfig((String) value);
229 configuration.TidyMark = store.getBoolean(GENERAL_TIDY_MARK);
230 configuration.ShowWarnings = store.getBoolean(GENERAL_SHOW_WARNINGS);
231 configuration.Quiet = store.getBoolean(GENERAL_QUIET);
232 configuration.Emacs = store.getBoolean(GENERAL_EMACS);
233 configuration.KeepFileTimes = store.getBoolean(GENERAL_KEEP_FILE_TIMES);
235 configuration.WrapAttVals = store.getBoolean(WRAP_ATT_VALS);
236 configuration.WrapScriptlets = store.getBoolean(WRAP_SCRIPTLETS);
237 configuration.WrapSection = store.getBoolean(WRAP_SECTION);
238 configuration.WrapAsp = store.getBoolean(WRAP_ASP);
239 configuration.WrapJste = store.getBoolean(WRAP_JSTE);
240 configuration.WrapPhp = store.getBoolean(WRAP_PHP);
241 configuration.IndentAttributes = store.getBoolean(INDENT_ATTRIBUTES);
242 configuration.LiteralAttribs = store.getBoolean(LITERAL_ATTRIBS);
244 configuration.MakeClean = store.getBoolean(OUTPUT_MAKE_CLEAR);
245 configuration.EncloseBodyText = store.getBoolean(OUTPUT_ENCLOSE_BODY_TEXT);
246 configuration.EncloseBlockText = store.getBoolean(OUTPUT_ENCLOSE_BLOCK_TEXT);
247 configuration.Word2000 = store.getBoolean(OUTPUT_STRIP_WORD);
248 if ((value = store.getString(OUTPUT_DEFAULT_ALT_TEXT)) != null) {
249 configuration.altText = value;
252 configuration.RawOut = store.getBoolean(OUT_AS_RAW);
253 configuration.UpperCaseTags = store.getBoolean(OUT_UPPER_TAGS);
254 configuration.UpperCaseAttrs = store.getBoolean(OUT_UPPER_ATTR);
256 if ((value = store.getString(INPUT_NEW_EMPTY_TAGS)) != null) {
257 configuration.parseEmptyTagNames(value, null);
259 if ((value = store.getString(INPUT_NEW_INLINE_TAGS)) != null) {
260 configuration.parseInlineTagNames(value, null);
262 if ((value = store.getString(INPUT_NEW_BLOCKLEVEL_TAGS)) != null) {
263 configuration.parseBlockTagNames(value, null);
265 if ((value = store.getString(INPUT_NEW_PRE_TAGS)) != null) {
266 configuration.parsePreTagNames(value, null);
272 * Updates the configuration of the tidy instance with content of the given
273 * file, if ths file exists. Returns silently on error.
275 * @param rawConfigFileName
277 private void updateTidyConfig(String rawConfigFileName) {
278 File config = new File(rawConfigFileName);
279 if (config.exists()) {
280 fTidy.setConfigurationFromFile(config.getAbsolutePath());
285 * Returns the shared instance.
287 public static JtidyPlugin getDefault() {
292 * Returns the workspace instance.
294 public static IWorkspace getWorkspace() {
295 return ResourcesPlugin.getWorkspace();
299 * Returns the string from the plugin's resource bundle,
300 * or 'key' if not found.
302 public static String getResourceString(String key) {
303 ResourceBundle bundle = JtidyPlugin.getDefault().getResourceBundle();
304 if (bundle == null) {
308 return bundle.getString(key);
309 } catch (MissingResourceException e) {
316 * Returns the plugin's resource bundle,
318 public ResourceBundle getResourceBundle() {
319 return fResourceBundle;
323 * Returns the Tidy instance for this resource. Curently the resource can be
324 * null, I just copied a similar conzept from a differnt Project, which uses
325 * a per Project configuration.
330 public static Tidy getTidyInstance(IResource resource) {
331 //IProject project = resource.getProject();
332 //TODO: bind the instance to the resource...
333 if (getDefault().fTidy == null) {
334 getDefault().initTidy();
336 return getDefault().fTidy;