1 19 20 package org.netbeans.modules.web.core.jsploader; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeEvent ; 24 import java.io.IOException ; 25 import java.lang.ref.WeakReference ; 26 import java.nio.charset.Charset ; 27 import java.util.Date ; 28 import java.util.EventListener ; 29 30 import org.openide.*; 31 import org.openide.cookies.EditorCookie; 32 import org.openide.cookies.SaveCookie; 33 import org.openide.filesystems.*; 34 import org.openide.loaders.*; 35 import org.openide.util.*; 36 import org.openide.nodes.Node; 37 import org.openide.nodes.CookieSet; 38 39 import org.netbeans.modules.web.core.QueryStringCookie; 40 import org.netbeans.modules.web.core.WebExecSupport; 41 42 46 public class JspDataObject extends MultiDataObject implements QueryStringCookie { 47 48 public static final String EA_JSP_ERRORPAGE = "jsp_errorpage"; public static final String PROP_SERVLET_DATAOBJECT = "servlet_do"; public static final String PROP_CONTENT_LANGUAGE = "contentLanguage"; public static final String PROP_SCRIPTING_LANGUAGE = "scriptingLanguage"; public static final String PROP_ENCODING = "encoding"; public static final String PROP_SERVER_CHANGE = "PROP_SERVER_CHANGE"; public static final String PROP_REQUEST_PARAMS = "PROP_REQUEST_PARAMS"; 57 static final String ATTR_FILE_ENCODING = "Content-Encoding"; 59 transient private EditorCookie servletEdit; 60 transient protected JspServletDataObject servletDataObject; 61 transient private Date servletDataObjectDate; 64 transient private CompileData compileData; 65 transient private boolean firstStart; 66 transient private Listener listener; 67 transient private BaseJspEditorSupport editorSupport; 68 transient final private static boolean debug = false; 69 70 public JspDataObject(FileObject pf, final UniFileLoader l) throws DataObjectExistsException { 71 super(pf, l); 72 CookieSet cookies = getCookieSet(); 73 initialize(); 74 } 75 76 public org.openide.nodes.CookieSet getCookieSet0() { 79 return super.getCookieSet(); 80 } 81 82 public Node.Cookie getCookie(Class type) { 83 if (type.isAssignableFrom(BaseJspEditorSupport.class)) { 84 return getJspEditorSupport(); 85 } 86 return super.getCookie(type); 87 } 88 89 protected org.openide.nodes.Node createNodeDelegate() { 90 return new JspNode(this); 91 } 92 93 private synchronized BaseJspEditorSupport getJspEditorSupport() { 94 if (editorSupport == null) { 95 editorSupport = new BaseJspEditorSupport(this); 96 } 97 return editorSupport; 98 } 99 100 protected EditorCookie createServletEditor() { 101 return new ServletEditor(this); 102 } 103 104 public synchronized CompileData getPlugin() { 105 if (compileData == null) { 106 if ( firstStart ) { 107 firstStart=false; 108 } 109 compileData = new CompileData(this); 110 checkRefreshServlet(); 111 } 112 return compileData; 113 } 114 115 118 public synchronized void refreshPlugin(boolean reload) { 119 compileData = null; 121 if (reload) 122 getPlugin(); 123 } 124 125 public void refreshPlugin() { 126 refreshPlugin(true); 127 } 128 129 public JspServletDataObject getServletDataObject() { 130 getPlugin(); 132 return servletDataObject; 133 } 134 135 138 public String getContentLanguage() { 139 return "text/html"; } 141 142 145 public String getScriptingLanguage() { 146 return "text/x-java"; } 148 149 public String getFileEncoding(boolean forceParse, boolean useEditor) { 150 String encoding = (String )getPrimaryFile().getAttribute(PROP_ENCODING); 152 if(encoding != null) { 153 return encoding; 154 } else { 155 TagLibParseSupport tlps = (TagLibParseSupport)getCookie(TagLibParseSupport.class); 156 return tlps.getCachedOpenInfo(forceParse, useEditor).getEncoding(); 157 } 158 } 159 160 public void setFileEncoding(String encoding) { 161 encoding = encoding.trim(); 162 if(encoding.length() == 0) { 163 encoding = null; } 165 try { 166 getPrimaryFile().setAttribute(PROP_ENCODING, encoding); 167 } catch(IOException e) { 168 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 169 } 170 } 171 172 private static final String CORRECT_WINDOWS_31J = "windows-31j"; 173 private static final String CORRECT_EUC_JP = "EUC-JP"; 174 private static final String CORRECT_GB2312 = "GB2312"; 175 private static final String CORRECT_BIG5 = "BIG5"; 176 177 private static String canonizeEncoding(String encodingAlias) { 178 179 if (Charset.isSupported(encodingAlias)) { 181 Charset cs = Charset.forName(encodingAlias); 182 encodingAlias = cs.name(); 183 } 184 185 if (encodingAlias.equalsIgnoreCase("MS932")) { 187 return CORRECT_WINDOWS_31J; 188 } 189 if (encodingAlias.equalsIgnoreCase("euc-jp-linux")) { 191 return CORRECT_EUC_JP; 192 } 193 if (encodingAlias.equalsIgnoreCase("EUC-CN")) { 195 return CORRECT_GB2312; 196 } 197 if (encodingAlias.equalsIgnoreCase("GBK")) { 198 return CORRECT_GB2312; 199 } 200 if (encodingAlias.equalsIgnoreCase("GB18030")) { 201 return CORRECT_GB2312; 202 } 203 if (encodingAlias.equalsIgnoreCase("EUC-TW")) { 204 return CORRECT_BIG5; 205 } 206 207 return encodingAlias; 208 } 209 210 private void initialize() { 211 firstStart = true; 212 listener = new Listener (); 213 listener.register(getPrimaryFile()); 214 refreshPlugin(false); 215 } 216 217 221 private void checkRefreshServlet() { 222 223 final DataObject oldServlet = servletDataObject; 224 if (debug) 225 System.out.println("refreshing servlet, old = " + oldServlet); 227 try { 229 FileObject servletFileObject = updateServletFileObject(); 230 if(debug) System.out.println("refreshing servlet, new servletFile = " + servletFileObject); if (servletFileObject != null) { 232 if ((oldServlet != null) && 234 (oldServlet.getPrimaryFile() == servletFileObject) && 235 (servletFileObject.lastModified().equals(servletDataObjectDate))) 236 return; 238 JspServletDataObject.setSourceJspPage(servletFileObject, this); 240 241 DataLoaderPool.setPreferredLoader(servletFileObject, DataLoader.getLoader(JspServletDataLoader.class)); 243 244 245 DataObject dObj= DataObject.find(servletFileObject); 247 if (debug) { 248 System.out.println("checkRefr::servletDObj=" + ((dObj == null) ? "null" : dObj.getClass().getName()) + "/" + dObj); } 252 256 if (dObj instanceof JspServletDataObject) { 257 servletDataObject = (JspServletDataObject)dObj; 258 servletDataObjectDate = dObj.getPrimaryFile().lastModified(); 259 } 260 String encoding = compileData.getServletEncoding(); 262 if (encoding != null) { 263 if (!"".equals(encoding)) { 264 try { 265 Charset.forName(encoding); 266 } catch (IllegalArgumentException ex) { 267 IOException t = new IOException ( 268 NbBundle.getMessage(JspDataObject.class, "FMT_UnsupportedEncoding", encoding) 269 ); 270 ErrorManager.getDefault().annotate(t, ex); 271 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, t); 272 } 273 } else 274 encoding = null; 275 } 276 try { 277 servletFileObject.setAttribute(ATTR_FILE_ENCODING, encoding); } catch (IOException ex) { 280 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 281 } 282 } else 283 servletDataObject = null; 284 } catch (IOException e) { 285 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 286 servletDataObject = null; 287 } 288 289 if ((oldServlet == null)) { 291 } else { 292 RequestProcessor.postRequest( 293 new Runnable () { 294 public void run() { 295 updateServletEditor(); 296 JspDataObject.this.firePropertyChange0(PROP_SERVLET_DATAOBJECT, null, getServletDataObject()); 298 JspDataObject.this.firePropertyChange0(PROP_COOKIE, null, null); 300 } 301 } 302 ); 303 } 304 } 305 306 310 325 326 327 public void firePropertyChange0(String propertyName, Object oldValue, Object newValue) { 328 super.firePropertyChange(propertyName, oldValue, newValue); 329 } 330 331 334 public EditorCookie getServletEditor() { 335 DataObject obj = getServletDataObject(); 336 if ((obj == null) != (servletEdit == null)) 337 updateServletEditor(); 338 return servletEdit; 339 } 340 341 private void updateServletEditor() { 342 if (servletDataObject == null) { 343 if (servletEdit != null) { 344 servletEdit.close(); 345 servletEdit = null; 346 } 347 } else { 348 if (servletEdit == null) { 349 servletEdit = createServletEditor(); 350 } 351 } 352 } 353 354 355 360 private FileObject updateServletFileObject() throws IOException { 361 return compileData.getServletFileObject(); 362 } 363 364 365 367 369 375 376 public void setQueryString(String params) throws java.io.IOException { 377 WebExecSupport.setQueryString(getPrimaryEntry().getFile(), params); 378 firePropertyChange(PROP_REQUEST_PARAMS, null, null); 379 } 380 381 protected org.openide.filesystems.FileObject handleRename(String str) throws java.io.IOException { 382 if ("".equals(str)) throw new IOException (NbBundle.getMessage(JspDataObject.class, "FMT_Not_Valid_FileName")); 384 385 org.openide.filesystems.FileObject retValue; 386 387 retValue = super.handleRename(str); 388 return retValue; 389 } 390 391 public void addSaveCookie(SaveCookie cookie){ 392 getCookieSet().add(cookie); 393 } 394 395 public void removeSaveCookie(){ 396 Node.Cookie cookie = getCookie(SaveCookie.class); 397 if (cookie!=null) getCookieSet().remove(cookie); 398 } 399 400 protected FileObject handleMove(DataFolder df) throws IOException { 401 402 FileObject retValue; 403 404 retValue = super.handleMove(df); 405 406 TagLibParseSupport tlps = null; 408 tlps = (TagLibParseSupport)getCookie(TagLibParseSupport.class); 409 if (tlps != null){ 410 getCookieSet().remove(tlps); 411 tlps = new TagLibParseSupport(retValue); 412 getCookieSet().add(tlps); 413 } 414 return retValue; 415 } 416 417 418 420 private class Listener extends FileChangeAdapter implements PropertyChangeListener { 421 WeakReference weakListener; 422 423 Listener() { 424 } 425 426 private void register(FileObject fo) { 427 EventListener el = WeakListeners.create(FileChangeListener.class, this, fo); 428 fo.addFileChangeListener((FileChangeListener) el); 429 weakListener = new WeakReference (el); 430 } 431 private void unregister(FileObject fo) { 432 FileChangeListener listener = (FileChangeListener) weakListener.get(); 433 if (listener != null) { 434 fo.removeFileChangeListener(listener); 435 } 436 } 437 public void propertyChange(PropertyChangeEvent evt) { 438 if (PROP_MODIFIED.equals(evt.getPropertyName())) { 441 if ((Boolean.FALSE).equals(evt.getNewValue())) { 442 refreshPlugin(false); 443 } 444 } 445 if (PROP_PRIMARY_FILE.equals(evt.getPropertyName()) || 447 PROP_FILES.equals(evt.getPropertyName())) { 448 if (evt.getOldValue() instanceof FileObject) 449 unregister((FileObject)evt.getOldValue()); 450 if (evt.getNewValue() instanceof FileObject) 451 register((FileObject)evt.getNewValue());; 452 refreshPlugin(true); 453 } 454 if (DataObject.PROP_VALID.equals(evt.getPropertyName())) { 456 if (evt.getSource() instanceof DataObject) { 457 DataObject dobj = (DataObject)evt.getSource(); 458 if (dobj.getPrimaryFile().getPackageNameExt('/','.').equals("")) { dobj.removePropertyChangeListener(this); 460 } 464 } 465 } 466 467 } 468 469 public void fileRenamed(FileRenameEvent fe) { 470 refreshPlugin(true); 471 } 472 473 492 493 private void serverChange() { 494 refreshPlugin(true); 495 firePropertyChange0(PROP_SERVER_CHANGE, null, null); 496 } 497 } 498 } 499 500 | Popular Tags |