2 * Created on 13.01.2003 by Jan Schulz
4 package net.sourceforge.phpdt.tidy.actions;
6 import java.io.ByteArrayInputStream;
7 import java.io.ByteArrayOutputStream;
8 import java.io.InputStream;
9 import java.util.Iterator;
11 import net.sourceforge.phpdt.tidy.JtidyPlugin;
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IMarker;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.editors.text.TextEditor;
24 import org.eclipse.ui.texteditor.ITextEditor;
30 public abstract class AbstractJTidyAction {
31 private ITextEditor fTextEditor = null;
32 private IStructuredSelection fSelection = null;
35 * Parses the given stream with a Tidy Instance, which belongs to this
36 * IFile. Problems will be marked on this file.
41 protected byte[] parseStreamOfFile(InputStream in, IFile file) {
42 deleteTidyMarker(file);
43 ByteArrayOutputStream out = new ByteArrayOutputStream();
44 JtidyPlugin.getTidyInstance(file).parse(file, in, out);
45 return out.toByteArray();
50 * Deletes all JTidy Marker of this File
54 protected void deleteTidyMarker(IFile file) {
56 IMarker[] markers = file.findMarkers(null, false, IResource.DEPTH_ZERO);
57 for (int i = 0; i < markers.length; i++) {
58 IMarker marker = markers[i];
59 if (marker.getAttribute(JtidyPlugin.MARKER_NAME) != null) {
63 } catch (CoreException e) {
69 * Parses the Document with Tidy.
71 protected void parseDocument(boolean writeBack) throws ParseFailedException {
72 IDocument doku = fTextEditor.getDocumentProvider().getDocument(fTextEditor.getEditorInput());
74 String content = doku.get();
75 IFile file = (IFile) fTextEditor.getEditorInput().getAdapter(IFile.class);
77 byte[] ret = parseStreamOfFile(new ByteArrayInputStream(content.getBytes()), file);
79 if (ret.length != 0) {
80 String cleanedContent = new String(ret);
81 doku.set(cleanedContent);
85 "This document has errors that must be fixed before using HTML Tidy to generate a tidied up version.");
92 * Throws a ParseFailedException, if the given obj is null
95 * @throws ParseFailedException
97 protected void assertNotNull(Object obj) throws ParseFailedException {
99 throw new ParseFailedException("A expected 'non-null' Value was null");
104 * Updates the enable state of the parent action
107 protected void updateParent(IAction action) {
108 action.setEnabled(fTextEditor != null || fSelection != null);
112 * Parses all Files in the given selection...
114 protected void parseSelection(boolean writeBack) {
115 Iterator iterator = null;
116 iterator = fSelection.iterator();
117 while (iterator.hasNext()) {
118 // obj => selected object in the view
119 Object obj = iterator.next();
122 if (obj instanceof IResource) {
123 IResource resource = (IResource) obj;
125 // check if it's a file resource
126 switch (resource.getType()) {
128 case IResource.FILE :
130 IFile file = (IFile) resource;
134 in = file.getContents();
135 byte[] ret = parseStreamOfFile(in, file);
138 if (ret.length != 0) {
139 InputStream source = new ByteArrayInputStream(ret);
140 file.setContents(source, IFile.KEEP_HISTORY, null);
144 "Formatting skipped",
145 "This document has errors that must be fixed before using HTML Tidy to generate a tidied up version.");
148 } catch (CoreException e) {
159 private void error(String lable, String message) {
165 * Opens an error dialog to display the given message.
167 * @param message the error message to show
169 private void displayError(final String title, final String message) {
170 MessageDialog.openError(null, title, message);
171 // final Shell parentShell = getShell();
172 // parentShell.getDisplay().syncExec(new Runnable() {
173 // public void run() {
174 // MessageDialog.openError(parentShell, lable, message);
179 // * Method getShell.
182 // protected abstract Shell getShell();
185 * Updates the Selection: if the given selection is of type
186 * IStruckturedSelection, fSection is set to this, otehrwise the field is
190 protected void updateSelection(ISelection sel) {
191 if (sel instanceof IStructuredSelection) {
192 fSelection = (IStructuredSelection) sel;
193 // REVISIT: further determination of types?
200 * If the given WorkbenchPart is of type ITextEditor, fTextEditor is set to
201 * this value, otherwise to null
204 protected void updateEditor(IEditorPart part) {
205 if (part instanceof ITextEditor) {
206 fTextEditor = (ITextEditor) part;
208 if (part instanceof TextEditor) {
209 fTextEditor = (TextEditor) part;
216 protected IEditorPart getEditor() {