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;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.core.resources.IMarker;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.texteditor.ITextEditor;
29 public abstract class AbstractJTidyAction {
30 private ITextEditor fTextEditor = null;
31 private IStructuredSelection fSelection = null;
34 * Parses the given stream with a Tidy Instance, which belongs to this
35 * IFile. Problems will be marked on this file.
40 protected byte[] parseStreamOfFile(InputStream in, IFile file) {
41 deleteTidyMarker(file);
42 ByteArrayOutputStream out = new ByteArrayOutputStream();
43 JtidyPlugin.getTidyInstance(file).parse(file, in, out);
44 return out.toByteArray();
49 * Deletes all JTidy Marker of this File
53 protected void deleteTidyMarker(IFile file) {
55 IMarker[] markers = file.findMarkers(null, false, IResource.DEPTH_ZERO);
56 for (int i = 0; i < markers.length; i++) {
57 IMarker marker = markers[i];
58 if (marker.getAttribute(JtidyPlugin.MARKER_NAME) != null) {
62 } catch (CoreException e) {
68 * Parses the Document with Tidy.
70 protected void parseDocument(boolean writeBack) throws ParseFailedException {
71 IDocument doku = fTextEditor.getDocumentProvider().getDocument(fTextEditor.getEditorInput());
73 String content = doku.get();
74 IFile file = (IFile) fTextEditor.getEditorInput().getAdapter(IFile.class);
76 byte[] ret = parseStreamOfFile(new ByteArrayInputStream(content.getBytes()), file);
78 if (ret.length != 0) {
79 String cleanedContent = new String(ret);
80 doku.set(cleanedContent);
84 "This document has errors that must be fixed before using HTML Tidy to generate a tidied up version.");
91 * Throws a ParseFailedException, if the given obj is null
94 * @throws ParseFailedException
96 protected void assertNotNull(Object obj) throws ParseFailedException {
98 throw new ParseFailedException("A expected 'non-null' Value was null");
103 * Updates the enable state of the parent action
106 protected void updateParent(IAction action) {
107 action.setEnabled(fTextEditor != null || fSelection != null);
111 * Parses all Files in the given selection...
113 protected void parseSelection(boolean writeBack) {
114 Iterator iterator = null;
115 iterator = fSelection.iterator();
116 while (iterator.hasNext()) {
117 // obj => selected object in the view
118 Object obj = iterator.next();
121 if (obj instanceof IResource) {
122 IResource resource = (IResource) obj;
124 // check if it's a file resource
125 switch (resource.getType()) {
127 case IResource.FILE :
129 IFile file = (IFile) resource;
133 in = file.getContents();
134 byte[] ret = parseStreamOfFile(in, file);
137 if (ret.length != 0) {
138 InputStream source = new ByteArrayInputStream(ret);
139 file.setContents(source, IFile.KEEP_HISTORY, null);
143 "Formatting skipped",
144 "This document has errors that must be fixed before using HTML Tidy to generate a tidied up version.");
147 } catch (CoreException e) {
158 private void error(String lable, String message) {
164 * Opens an error dialog to display the given message.
166 * @param message the error message to show
168 private void displayError(final String lable, final String message) {
169 final Shell parentShell = getShell();
170 parentShell.getDisplay().syncExec(new Runnable() {
172 MessageDialog.openError(parentShell, lable, message);
180 protected abstract Shell getShell();
183 * Updates the Selection: if the given selection is of type
184 * IStruckturedSelection, fSection is set to this, otehrwise the field is
188 protected void updateSelection(ISelection sel) {
189 if (sel instanceof IStructuredSelection) {
190 fSelection = (IStructuredSelection) sel;
191 // REVISIT: further determination of types?
198 * If the given WorkbenchPart is of type ITextEditor, fTextEditor is set to
199 * this value, otherwise to null
202 protected void updateEditor(IEditorPart part) {
203 if (part instanceof ITextEditor) {
204 fTextEditor = (ITextEditor) part;
210 protected IEditorPart getEditor() {