1 19 20 package org.netbeans.modules.web.struts; 21 22 import java.io.IOException ; 24 import java.io.InputStream ; 25 import org.netbeans.modules.web.struts.config.model.StrutsConfig; 26 import org.openide.filesystems.FileObject; 27 import org.openide.loaders.DataObjectExistsException; 28 import org.openide.loaders.MultiDataObject; 29 import org.openide.nodes.CookieSet; 30 import org.openide.nodes.Node; 31 import org.xml.sax.InputSource ; 32 33 import org.netbeans.api.xml.cookies.ValidateXMLCookie; 34 import org.netbeans.api.xml.cookies.CheckXMLCookie; 35 import org.netbeans.spi.xml.cookies.*; 36 import org.openide.ErrorManager; 37 import org.w3c.dom.Document ; 38 import org.xml.sax.ErrorHandler ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.SAXParseException ; 41 42 46 public class StrutsConfigDataObject extends MultiDataObject 47 implements org.openide.nodes.CookieSet.Factory { 48 49 private static StrutsCatalog strutsCatalog = new StrutsCatalog(); 50 private boolean documentDirty = true; 51 private boolean documentValid=true; 52 protected boolean nodeDirty = false; 53 private InputStream inputStream; 54 private SAXParseError error; 55 private StrutsConfig lastGoodConfig = null; 56 57 58 private transient StrutsConfigEditorSupport editorSupport; 59 60 61 public static final String PROP_DOC_VALID = "documentValid"; 63 64 public StrutsConfigDataObject(FileObject pf, StrutsConfigLoader loader) throws DataObjectExistsException { 65 super(pf, loader); 66 init(); 67 68 } 69 70 private void init() { 71 CookieSet cookies = getCookieSet(); 72 73 getCookieSet().add(StrutsConfigEditorSupport.class, this); 74 75 InputSource in = DataObjectAdapters.inputSource(this); 77 CheckXMLCookie checkCookie = new CheckXMLSupport(in); 78 getCookieSet().add(checkCookie); 79 ValidateXMLCookie validateCookie = new ValidateXMLSupport(in); 80 getCookieSet().add(validateCookie); 81 } 82 83 95 protected synchronized Node createNodeDelegate () { 96 return new StrutsConfigNode(this); 97 } 98 99 100 public Node.Cookie createCookie(Class clazz) { 101 if(clazz.isAssignableFrom(StrutsConfigEditorSupport.class)) 102 return getEditorSupport(); 103 else 104 return null; 105 } 106 107 108 public StrutsConfigEditorSupport getEditorSupport() { 109 if(editorSupport == null) { 110 synchronized(this) { 111 if(editorSupport == null) 112 editorSupport = new StrutsConfigEditorSupport(this); 113 } 114 } 115 116 return editorSupport; 117 } 118 119 public StrutsConfig getStrutsConfig() throws java.io.IOException { 120 if (lastGoodConfig == null) 121 parsingDocument(); 122 return lastGoodConfig; 123 } 124 125 public StrutsConfig getStrutsConfig (boolean parsenow) throws java.io.IOException { 126 if (parsenow){ 127 StrutsConfig previous = lastGoodConfig; 128 parsingDocument(); 129 if (lastGoodConfig == null) 130 lastGoodConfig = previous; 131 } 132 return getStrutsConfig(); 133 } 134 135 141 protected InputStream prepareInputSource() throws java.io.IOException { 142 if ((getEditorSupport() != null) && (getEditorSupport().isDocumentLoaded())) { 143 return getEditorSupport().getInputStream(); 145 } 146 else { 147 return getPrimaryFile().getInputStream(); 148 } 149 } 150 151 156 protected void closeInputSource() { 157 InputStream is = inputStream; 158 if (is != null) { 159 try { 160 is.close(); 161 } 162 catch (IOException e) { 163 } 165 if (is == inputStream) { 166 inputStream = null; 167 } 168 } 169 } 170 171 public void write(StrutsConfig config) throws java.io.IOException { 172 java.io.File file = org.openide.filesystems.FileUtil.toFile(getPrimaryFile()); 173 org.openide.filesystems.FileObject configFO = getPrimaryFile(); 174 try { 175 org.openide.filesystems.FileLock lock = configFO.lock(); 176 try { 177 java.io.OutputStream os =configFO.getOutputStream(lock); 178 try { 179 config.write(os); 180 } finally { 181 os.close(); 182 } 183 } 184 finally { 185 lock.releaseLock(); 186 } 187 } catch (org.openide.filesystems.FileAlreadyLockedException ex) { 188 } 190 } 191 192 195 public void parsingDocument(){ 196 error = null; 197 try { 198 error = updateNode(prepareInputSource()); 199 } 200 catch (Exception e) { 201 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e); 202 setDocumentValid(false); 203 return; 204 } 205 finally { 206 closeInputSource(); 207 documentDirty=false; 208 } 209 if (error == null){ 210 setDocumentValid(true); 211 }else { 212 setDocumentValid(false); 213 } 214 setNodeDirty(false); 215 } 216 217 public void setDocumentValid (boolean valid){ 218 if (documentValid!=valid) { 219 if (valid) 220 repairNode(); 221 documentValid=valid; 222 firePropertyChange (PROP_DOC_VALID, !documentValid ? Boolean.TRUE : Boolean.FALSE, documentValid ? Boolean.TRUE : Boolean.FALSE); 223 } 224 } 225 226 228 protected void repairNode(){ 229 org.openide.awt.StatusDisplayer.getDefault().setStatusText(""); 236 } 237 238 private org.w3c.dom.Document getDomDocument(InputStream inputSource) throws SAXParseException { 239 try { 240 org.w3c.dom.Document doc = org.netbeans.modules.schema2beans.GraphManager. 242 createXmlDocument(new org.xml.sax.InputSource (inputSource), false, strutsCatalog, 243 new J2eeErrorHandler(this)); 244 return doc; 245 } catch(Exception e) { 246 throw new SAXParseException (e.getMessage(), new org.xml.sax.helpers.LocatorImpl ()); 248 } 249 } 250 251 255 protected SAXParseError updateNode(InputStream is) throws java.io.IOException { 257 try { 258 Document doc = getDomDocument(is); 259 lastGoodConfig = StrutsConfig.createGraph(doc); 260 } 261 catch(SAXParseException ex) { 262 return new SAXParseError(ex); 263 } catch(SAXException ex) { 264 throw new IOException (); 265 } 266 return null; 267 } 268 269 public boolean isDocumentValid(){ 270 return documentValid; 271 } 272 274 public void setDocumentDirty(boolean dirty){ 275 documentDirty=dirty; 276 } 277 278 281 public boolean isDocumentDirty(){ 282 return documentDirty; 283 } 284 285 288 public boolean isNodeDirty(){ 289 return nodeDirty; 290 } 291 292 295 public void setNodeDirty(boolean dirty){ 296 nodeDirty=dirty; 297 } 298 org.openide.nodes.CookieSet getCookieSet0() { 299 return getCookieSet(); 300 } 301 302 public static class J2eeErrorHandler implements ErrorHandler { 303 304 private StrutsConfigDataObject dataObject; 305 306 public J2eeErrorHandler(StrutsConfigDataObject obj) { 307 dataObject=obj; 308 } 309 310 public void error(SAXParseException exception) throws SAXException { 311 dataObject.createSAXParseError(exception); 312 throw exception; 313 } 314 315 public void fatalError(SAXParseException exception) throws SAXException { 316 dataObject.createSAXParseError(exception); 317 throw exception; 318 } 319 320 public void warning(SAXParseException exception) throws SAXException { 321 dataObject.createSAXParseError(exception); 322 throw exception; 323 } 324 } 325 326 private void createSAXParseError(SAXParseException error){ 327 this.error = new SAXParseError(error); 328 } 329 } 330 | Popular Tags |