1 19 20 package org.netbeans.modules.web.jsf; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import org.netbeans.editor.BaseDocument; 25 import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig; 26 import org.openide.ErrorManager; 27 import org.openide.filesystems.FileObject; 28 import org.openide.loaders.DataObjectExistsException; 29 import org.openide.loaders.MultiDataObject; 30 import org.openide.nodes.CookieSet; 31 import org.openide.nodes.Node; 32 import org.w3c.dom.Document ; 33 import org.xml.sax.*; 34 import org.netbeans.api.xml.cookies.ValidateXMLCookie; 35 import org.netbeans.api.xml.cookies.CheckXMLCookie; 36 import org.netbeans.spi.xml.cookies.*; 37 38 42 public class JSFConfigDataObject extends MultiDataObject 43 implements org.openide.nodes.CookieSet.Factory { 44 45 private static JSFCatalog jsfCatalog = new JSFCatalog(); 46 private boolean documentDirty = true; 47 private boolean documentValid=true; 48 protected boolean nodeDirty = false; 49 private InputStream inputStream; 50 51 private transient JSFConfigEditorSupport editorSupport; 52 private SAXParseError error; 53 private FacesConfig lastGoodFacesConfig = null; 54 55 56 public static final String PROP_DOC_VALID = "documentValid"; 58 59 60 public JSFConfigDataObject(FileObject pf, JSFConfigLoader loader) throws DataObjectExistsException { 61 super(pf, loader); 62 init(); 63 64 } 65 66 private void init() { 67 CookieSet cookies = getCookieSet(); 68 69 getCookieSet().add(JSFConfigEditorSupport.class, this); 70 71 InputSource in = DataObjectAdapters.inputSource(this); 73 CheckXMLCookie checkCookie = new CheckXMLSupport(in); 74 getCookieSet().add(checkCookie); 75 ValidateXMLCookie validateCookie = new ValidateXMLSupport(in); 76 getCookieSet().add(validateCookie); 77 } 78 79 91 protected synchronized Node createNodeDelegate () { 92 return new JSFConfigNode(this); 93 } 94 95 96 public Node.Cookie createCookie(Class clazz) { 97 if(clazz.isAssignableFrom(JSFConfigEditorSupport.class)) 98 return getEditorSupport(); 99 else 100 return null; 101 } 102 103 104 public JSFConfigEditorSupport getEditorSupport() { 105 if(editorSupport == null) { 106 synchronized(this) { 107 if(editorSupport == null) 108 editorSupport = new JSFConfigEditorSupport(this); 109 } 110 } 111 112 return editorSupport; 113 } 114 115 public FacesConfig getFacesConfig() throws java.io.IOException { 116 if (lastGoodFacesConfig == null) 117 parsingDocument(); 118 return lastGoodFacesConfig; 119 } 120 121 127 protected InputStream prepareInputSource() throws java.io.IOException { 128 if ((getEditorSupport() != null) && (getEditorSupport().isDocumentLoaded())) { 129 return getEditorSupport().getInputStream(); 131 } 132 else { 133 return getPrimaryFile().getInputStream(); 134 } 135 } 136 137 142 protected void closeInputSource() { 143 InputStream is = inputStream; 144 if (is != null) { 145 try { 146 is.close(); 147 } 148 catch (IOException e) { 149 } 151 if (is == inputStream) { 152 inputStream = null; 153 } 154 } 155 } 156 157 160 public void parsingDocument(){ 161 error = null; 162 try { 163 error = updateNode(prepareInputSource()); 164 } 165 catch (Exception e) { 166 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e); 167 setDocumentValid(false); 168 return; 169 } 170 finally { 171 closeInputSource(); 172 documentDirty=false; 173 } 174 if (error == null){ 175 setDocumentValid(true); 176 }else { 177 setDocumentValid(false); 178 } 179 setNodeDirty(false); 180 } 181 182 public void setDocumentValid (boolean valid){ 183 if (documentValid!=valid) { 184 if (valid) 185 repairNode(); 186 documentValid=valid; 187 firePropertyChange (PROP_DOC_VALID, !documentValid ? Boolean.TRUE : Boolean.FALSE, documentValid ? Boolean.TRUE : Boolean.FALSE); 188 } 189 } 190 191 193 protected void repairNode(){ 194 org.openide.awt.StatusDisplayer.getDefault().setStatusText(""); 201 } 202 203 private org.w3c.dom.Document getDomDocument(InputStream inputSource) throws SAXParseException { 204 try { 205 org.w3c.dom.Document doc = org.netbeans.modules.schema2beans.GraphManager. 207 createXmlDocument(new org.xml.sax.InputSource (inputSource), false, jsfCatalog, 208 new J2eeErrorHandler(this)); 209 return doc; 210 } catch(Exception e) { 211 throw new SAXParseException(e.getMessage(), new org.xml.sax.helpers.LocatorImpl ()); 213 } 214 } 215 216 217 221 protected SAXParseError updateNode(InputStream is) throws java.io.IOException { 223 try { 224 Document doc = getDomDocument(is); 225 226 } 240 catch(SAXParseException ex) { 241 return new SAXParseError(ex); 242 } catch(SAXException ex) { 243 throw new IOException (); 244 } 245 return null; 246 } 247 248 public boolean isDocumentValid(){ 249 return documentValid; 250 } 251 253 public void setDocumentDirty(boolean dirty){ 254 documentDirty=dirty; 255 } 256 257 260 public boolean isDocumentDirty(){ 261 return documentDirty; 262 } 263 264 267 public boolean isNodeDirty(){ 268 return nodeDirty; 269 } 270 271 274 public void setNodeDirty(boolean dirty){ 275 nodeDirty=dirty; 276 } 277 org.openide.nodes.CookieSet getCookieSet0() { 278 return getCookieSet(); 279 } 280 281 public static class J2eeErrorHandler implements ErrorHandler { 282 283 private JSFConfigDataObject dataObject; 284 285 public J2eeErrorHandler(JSFConfigDataObject obj) { 286 dataObject=obj; 287 } 288 289 public void error(SAXParseException exception) throws SAXException { 290 dataObject.createSAXParseError(exception); 291 throw exception; 292 } 293 294 public void fatalError(SAXParseException exception) throws SAXException { 295 dataObject.createSAXParseError(exception); 296 throw exception; 297 } 298 299 public void warning(SAXParseException exception) throws SAXException { 300 dataObject.createSAXParseError(exception); 301 throw exception; 302 } 303 } 304 305 private void createSAXParseError(SAXParseException error){ 306 this.error = new SAXParseError(error); 307 } 308 309 310 } 311 | Popular Tags |