1 19 20 package org.netbeans.modules.tasklist.html; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import org.openide.text.Line; 25 import org.openide.loaders.DataObject; 26 import java.io.*; 27 import org.openide.ErrorManager; 28 29 import javax.swing.text.*; 30 import org.openide.DialogDescriptor; 31 import org.openide.DialogDisplayer; 32 import org.openide.NotifyDescriptor; 33 import org.openide.nodes.Node; 34 import org.openide.text.DataEditorSupport; 35 import org.openide.util.HelpCtx; 36 import org.openide.util.NbBundle; 37 import org.openide.util.actions.NodeAction; 38 import org.openide.cookies.EditorCookie; 39 import org.openide.cookies.OpenCookie; 40 import org.openide.cookies.EditCookie; 41 42 import org.netbeans.modules.html.*; 43 import org.netbeans.api.diff.*; 44 45 import org.netbeans.modules.tasklist.core.*; 46 import org.netbeans.modules.tasklist.client.Suggestion; 47 48 import org.w3c.tidy.*; 49 50 58 59 public class RewriteAction extends NodeAction 60 implements ErrorReporter { 61 62 private static final long serialVersionUID = 1; 63 64 protected boolean asynchronous() { 65 return false; 66 } 67 68 public void reportError(int line, int col, boolean error, String message) { 69 } 71 72 protected boolean enable(Node[] node) { 73 if ((node == null) || (node.length != 1)) { 74 return false; 75 } 76 77 DataObject dobj = (DataObject)node[0].getCookie(DataObject.class); 78 if (dobj == null) { 79 return false; 80 } 81 EditorCookie edit = (EditorCookie)dobj.getCookie( 82 EditorCookie.class); 83 if (edit == null) { 84 return false; 85 } 86 87 if (TidySuggester.isHTML(dobj)) { 88 return true; 89 } 90 if (TidySuggester.isJSP(dobj)) { 91 return true; 92 } 93 if (TidySuggester.isXML(dobj)) { 94 return true; 95 } 96 return false; 97 } 98 99 private Tidy tidy = null; 100 101 protected void performAction(Node[] node) { 102 Suggestion item = (Suggestion)TaskNode.getTask(node[0]); 110 DataObject dobj; 111 if (item != null) { 112 Line l = item.getLine(); 113 dobj = DataEditorSupport.findDataObject(l); 114 } else { 115 dobj = (DataObject)node[0].getCookie(DataObject.class); 116 } 117 if (dobj == null) { 118 return; 119 } 120 Document doc = TLUtils.getDocument(dobj); 121 if (doc == null) { 122 EditorCookie edit = (EditorCookie)dobj.getCookie( 123 EditorCookie.class); 124 if (edit == null) { 125 return; } 129 EditCookie ec = (EditCookie)dobj.getCookie(EditCookie.class); 130 if (ec == null) { 131 OpenCookie oc = (OpenCookie)dobj.getCookie(OpenCookie.class); 132 if (oc != null) oc.open(); 133 } else { 134 ec.edit(); 135 } 136 doc = TLUtils.getDocument(dobj); 137 if (doc == null) { 138 try { 140 doc = edit.openDocument(); 141 } catch (java.io.IOException e) { 142 ErrorManager.getDefault().notify(e); 143 return; 144 } 145 } 146 } 147 148 boolean isHTML = TidySuggester.isHTML(dobj); 149 boolean isJSP = false; 150 boolean isXML = false; 151 if (!isHTML) { 152 isJSP = TidySuggester.isJSP(dobj); 153 if (!isJSP) { 154 isXML = TidySuggester.isXML(dobj); 155 } 156 } 157 if (!(isHTML || isJSP || isXML)) { 158 return; 159 } 160 161 if (tidy == null) { 163 tidy = new Tidy(); 164 } 165 tidy.setOnlyErrors(false); 166 tidy.setShowWarnings(false); 167 tidy.setQuiet(true); 168 169 tidy.setXmlTags(isXML || isJSP); 172 173 RewritePanel panel = new RewritePanel(this, doc, dobj); 174 panel.setXHTML(tidy.getXHTML()); 175 176 panel.setWrapCol(tidy.getWraplen()); 177 panel.setOmit(tidy.getHideEndTags()); 178 panel.setUpper(tidy.getUpperCaseTags()); 179 panel.setIndent(tidy.getIndentContent()); 180 panel.setReplace(tidy.getMakeClean()); 181 panel.setXML(tidy.getXmlTags()); 182 183 DialogDescriptor d = new DialogDescriptor(panel, 184 NbBundle.getMessage(RewriteAction.class, 185 "TITLE_rewrite")); d.setModal(true); 187 d.setMessageType(NotifyDescriptor.PLAIN_MESSAGE); 188 d.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION); 189 Dialog dlg = DialogDisplayer.getDefault().createDialog(d); 190 dlg.pack(); 191 dlg.show(); 192 if (d.getValue() != NotifyDescriptor.OK_OPTION) { 193 return; 194 } 195 196 tidy.setXHTML(panel.getXHTML()); 197 tidy.setMakeClean(panel.getReplace()); 198 tidy.setIndentContent(panel.getIndent()); 199 tidy.setSmartIndent(panel.getIndent()); 200 tidy.setUpperCaseTags(panel.getUpper()); 201 tidy.setHideEndTags(panel.getOmit()); 202 tidy.setWraplen(panel.getWrapCol()); 203 204 String rewritten = rewrite(doc); 205 206 try { 207 215 doc.remove(0, doc.getLength()); 216 doc.insertString(0, rewritten, null); 217 } catch (BadLocationException e) { 219 ErrorManager.getDefault().notify(e); 220 } 221 Suggestion s = (Suggestion)node[0].getCookie(Suggestion.class); 222 if (s != null) { 223 Object seed = s.getSeed(); 224 if (seed instanceof TidySuggester) { 225 } 227 } 228 } 229 230 private String rewrite(Document doc) { 231 InputStream input = null; 232 try { 233 String text = doc.getText(0, doc.getLength()); 234 input = new StringBufferInputStream(text); 235 } catch (BadLocationException e) { 236 ErrorManager.getDefault(). 237 notify(ErrorManager.WARNING, e); 238 return ""; 239 } 240 StringBuffer sb = new StringBuffer (doc.getLength()+500); 241 OutputStream output = new StringBufferOutputStream(sb); 242 tidy.parse(input, output); 243 return sb.toString(); 244 } 245 246 void preview(RewritePanel panel, Document doc, DataObject dobj) { 247 tidy.setXHTML(panel.getXHTML()); 248 tidy.setMakeClean(panel.getReplace()); 249 tidy.setIndentContent(panel.getIndent()); 250 tidy.setSmartIndent(panel.getIndent()); 251 tidy.setUpperCaseTags(panel.getUpper()); 252 tidy.setHideEndTags(panel.getOmit()); 253 tidy.setWraplen(panel.getWrapCol()); 254 255 String before; 256 try { 257 before = doc.getText(0, doc.getLength()); 258 } catch (BadLocationException e) { 259 ErrorManager.getDefault(). 260 notify(ErrorManager.WARNING, e); 261 return; 262 } 263 String rewritten = rewrite(doc); 264 String mime = dobj.getPrimaryFile().getMIMEType(); 265 diff(before, rewritten, mime); 266 267 } 268 269 void diff(String before, String after, String mime) { 270 Diff diff = Diff.getDefault(); 271 if (diff == null) { 272 return ; 275 } 276 277 String beforeDesc = NbBundle.getMessage(RewriteAction.class, 278 "DiffBefore"); String afterDesc = NbBundle.getMessage(RewriteAction.class, 280 "DiffAfter"); String beforeTitle = beforeDesc; 282 String afterTitle = afterDesc; 283 284 Component tp = null; 285 try { 286 tp = diff.createDiff(beforeDesc, beforeTitle, 287 new StringReader(before), 288 afterDesc, afterTitle, 289 new StringReader(after), 290 mime); 291 } catch (IOException ioex) { 292 ErrorManager.getDefault().notify(ioex); 293 return ; 294 } 295 if (tp == null) { 296 return; 297 } 298 299 303 304 DialogDescriptor d = new DialogDescriptor(tp, 305 NbBundle.getMessage(RewriteAction.class, 306 "TITLE_diff")); d.setModal(true); 308 d.setMessageType(NotifyDescriptor.PLAIN_MESSAGE); 309 d.setOptionType(NotifyDescriptor.DEFAULT_OPTION); 310 Dialog dlg = DialogDisplayer.getDefault().createDialog(d); 311 dlg.pack(); 312 dlg.show(); 313 } 314 315 316 public String getName() { 317 return NbBundle.getMessage(RewriteAction.class, 318 "Rewrite"); } 320 321 protected String iconResource() { 322 return "org/netbeans/modules/tasklist/html/rewrite.gif"; } 324 325 public HelpCtx getHelpCtx() { 326 return HelpCtx.DEFAULT_HELP; 327 } 330 331 private class StringBufferOutputStream extends OutputStream { 333 private StringBuffer sb; 334 StringBufferOutputStream(StringBuffer sb) { 335 this.sb = sb; 336 } 337 338 public void write(int b) { 339 sb.append((char)b); 340 } 341 } 342 343 344 } 345 | Popular Tags |