b288394ff6cbc7ea49f62dce985a5015af749313
[phpeclipse.git] /
1 /**
2  * Created on 13.01.2003 by Jan Schulz
3  */
4 package net.sourceforge.phpdt.tidy.actions;
5
6 import java.io.ByteArrayInputStream;
7 import java.io.ByteArrayOutputStream;
8 import java.io.InputStream;
9 import java.util.Iterator;
10
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; 
24
25 /**
26  * @author jan
27  * @since 13.01.2003
28  */
29 public abstract class AbstractJTidyAction {
30         private ITextEditor fTextEditor = null;
31         private IStructuredSelection fSelection = null;
32
33         /**
34          * Parses the given stream with a Tidy Instance, which belongs to this
35          * IFile. Problems will be marked on this file.
36          * @param in
37          * @param file
38          * @return InputStream
39          */
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();
45
46         }
47
48         /**
49          * Deletes all JTidy Marker of this File
50          * 
51          * @param file
52          */
53         protected void deleteTidyMarker(IFile file) {
54                 try {
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) {
59                                         marker.delete();
60                                 }
61                         }
62                 } catch (CoreException e) {
63                         //LOGGING
64                 }
65         }
66
67         /**
68          * Parses the Document with Tidy.
69          */
70         protected void parseDocument(boolean writeBack) throws ParseFailedException {
71                 IDocument doku = fTextEditor.getDocumentProvider().getDocument(fTextEditor.getEditorInput());
72                 assertNotNull(doku);
73                 String content = doku.get();
74                 IFile file = (IFile) fTextEditor.getEditorInput().getAdapter(IFile.class);
75                 assertNotNull(file);
76                 byte[] ret = parseStreamOfFile(new ByteArrayInputStream(content.getBytes()), file);
77                 if (writeBack) {
78                         if (ret.length != 0) {
79                                 String cleanedContent = new String(ret);
80                                 doku.set(cleanedContent);
81                         } else {
82                                 displayError(
83                                         "Formatting skipped",
84                                         "This document has errors that must be fixed before using HTML Tidy to generate a tidied up version.");
85                         }
86
87                 }
88         }
89
90         /**
91          * Throws a ParseFailedException, if the given obj is null
92          * 
93          * @param obj
94          * @throws ParseFailedException
95          */
96         protected void assertNotNull(Object obj) throws ParseFailedException {
97                 if (obj == null) {
98                         throw new ParseFailedException("A expected 'non-null' Value was null");
99                 }
100         }
101
102         /**
103          * Updates the enable state of the parent action
104          * @param action
105          */
106         protected void updateParent(IAction action) {
107                 action.setEnabled(fTextEditor != null || fSelection != null);
108         }
109
110         /**
111          * Parses all Files in the given selection...
112          */
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();
119
120                         // is it a resource
121                         if (obj instanceof IResource) {
122                                 IResource resource = (IResource) obj;
123
124                                 // check if it's a file resource
125                                 switch (resource.getType()) {
126
127                                         case IResource.FILE :
128                                                 // single file:
129                                                 IFile file = (IFile) resource;
130
131                                                 InputStream in;
132                                                 try {
133                                                         in = file.getContents();
134                                                         byte[] ret = parseStreamOfFile(in, file);
135
136                                                         if (writeBack) {
137                                                                 if (ret.length != 0) {
138                                                                         InputStream source = new ByteArrayInputStream(ret);
139                                                                         file.setContents(source, IFile.KEEP_HISTORY, null);
140
141                                                                 } else {
142                                                                         displayError(
143                                                                                 "Formatting skipped",
144                                                                                 "This document has errors that must be fixed before using HTML Tidy to generate a tidied up version.");
145                                                                 }
146                                                         }
147                                                 } catch (CoreException e) {
148                                                 }
149                                 }
150                         }
151                 }
152         }
153         /**
154          * Method error.
155          * @param string
156          * @param string1
157          */
158         private void error(String lable, String message) {
159
160                 // TODO:
161         }
162
163         /**
164                  * Opens an error dialog to display the given message.
165                  *
166                  * @param message the error message to show
167                  */
168         private void displayError(final String lable, final String message) {
169                 final Shell parentShell = getShell();
170                 parentShell.getDisplay().syncExec(new Runnable() {
171                         public void run() {
172                                 MessageDialog.openError(parentShell, lable, message);
173                         }
174                 });
175         }
176         /**
177          * Method getShell.
178          * @return Shell
179          */
180         protected abstract Shell getShell();
181
182         /**
183          * Updates the Selection: if the given selection is of type
184          * IStruckturedSelection, fSection is set to this, otehrwise the field is
185          * set to null.
186          * @param sel
187          */
188         protected void updateSelection(ISelection sel) {
189                 if (sel instanceof IStructuredSelection) {
190                         fSelection = (IStructuredSelection) sel;
191                         // REVISIT: further determination of types?
192                 } else {
193                         fSelection = null;
194                 }
195         }
196
197         /**
198          * If the given WorkbenchPart is of type ITextEditor, fTextEditor is set to
199          * this value, otherwise to null
200          * 
201          */
202         protected void updateEditor(IEditorPart part) {
203                 if (part instanceof ITextEditor) {
204                         fTextEditor = (ITextEditor) part;
205                 } else {
206                         fTextEditor = null;
207                 }
208         }
209
210         protected IEditorPart getEditor() {
211                 return fTextEditor;
212         }
213 }