1 19 20 package org.netbeans.modules.j2ee.ddloaders.common.xmlutils; 21 22 import org.openide.cookies.*; 23 import org.openide.nodes.CookieSet; 24 import org.openide.filesystems.FileObject; 25 import org.openide.loaders.MultiFileLoader; 26 import org.openide.loaders.XMLDataObject; 27 import org.openide.ErrorManager; 28 import org.openide.text.Line; 29 import org.openide.windows.*; 30 import org.openide.util.NbBundle; 31 32 import java.io.*; 33 import org.xml.sax.*; 34 import org.openide.xml.*; 35 import org.netbeans.api.xml.cookies.CheckXMLCookie; 36 import org.netbeans.spi.xml.cookies.*; 37 38 42 public abstract class XMLJ2eeDataObject extends XMLDataObject implements CookieSet.Factory { 43 44 protected boolean nodeDirty = false; 45 private boolean documentDirty = true; 46 private boolean savingDocument; 47 private InputStream inputStream; 48 private InputOutput inOut; 49 protected XMLJ2eeEditorSupport editor; 50 private boolean documentValid=true; 51 private SAXParseError error; 52 private org.openide.text.Annotation errorAnnotation; 53 54 private static final long serialVersionUID = -515751072013886985L; 55 56 57 public static final String PROP_DOC_VALID = "documentValid"; 59 public XMLJ2eeDataObject(FileObject pf, MultiFileLoader loader) 60 throws org.openide.loaders.DataObjectExistsException { 61 super(pf,loader); 62 63 getCookieSet().add(XMLJ2eeEditorSupport.class, this); 64 getCookieSet().add(EditCookie.class, this); 65 getCookieSet().add(EditorCookie.class, this); 66 getCookieSet().add(LineCookie.class, this); 67 getCookieSet().add(PrintCookie.class, this); 68 getCookieSet().add(CloseCookie.class, this); 69 InputSource in = DataObjectAdapters.inputSource(this); 71 CheckXMLCookie checkCookie = new CheckXMLSupport(in); 72 getCookieSet().add(checkCookie); 73 } 74 protected EditorCookie createEditorCookie () { 76 return null; 77 } 78 82 protected abstract SAXParseError updateNode(org.xml.sax.InputSource is) throws java.io.IOException ; 83 86 protected abstract String getIconBaseForValidDocument(); 87 88 91 protected abstract String getIconBaseForInvalidDocument(); 92 93 94 public org.openide.nodes.Node.Cookie createCookie(Class clazz) { 95 if(clazz.isAssignableFrom(XMLJ2eeEditorSupport.class)) 96 return getEditorSupport(); 97 else 98 return null; 99 } 100 101 102 protected synchronized XMLJ2eeEditorSupport getEditorSupport() { 103 if(editor == null) { 104 editor = new XMLJ2eeEditorSupport(this); 105 } 106 return editor; 107 } 108 112 public String getOutputStringForInvalidDocument(SAXParseError error){ 113 String mes = NbBundle.getMessage (XMLJ2eeDataObject.class, "TXT_errorMessage", 115 new Object [] { error.getErrorText(), 116 new Integer (error.getErrorLine()), 117 new Integer (error.getErrorColumn()) }); 118 return mes; 119 } 120 123 public boolean isNodeDirty(){ 124 return nodeDirty; 125 } 126 127 129 public void setDocumentDirty(boolean dirty){ 130 documentDirty=dirty; 131 } 132 133 136 public boolean isDocumentDirty(){ 137 return documentDirty; 138 } 139 140 143 public void setNodeDirty(boolean dirty){ 144 nodeDirty=dirty; 145 } 146 147 149 protected void repairNode(){ 150 if (inOut!=null) { 153 inOut.closeInputOutput(); 154 errorAnnotation.detach(); 155 } 156 } 157 158 161 public void parsingDocument(){ 162 SAXParseError err=null; 165 try { 166 err=updateNode(prepareInputSource()); 167 } 168 catch (Exception e) { 169 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e); 170 setDocumentValid(false); 171 return; 172 } 173 finally { 174 closeInputSource(); 175 documentDirty=false; 176 } 177 if (err==null){ 178 setDocumentValid(true); 179 }else { 180 setDocumentValid(false); 181 } 182 } 183 184 190 protected org.xml.sax.InputSource prepareInputSource() throws java.io.IOException { 191 if ((editor != null) && (editor.isDocumentLoaded())) { 192 final javax.swing.text.Document doc = editor.getDocument(); 194 final String [] str = new String [1]; 195 Runnable run = new Runnable () { 197 public void run() { 198 try { 199 str[0] = doc.getText(0, doc.getLength()); 200 } 201 catch (javax.swing.text.BadLocationException e) { 202 } 204 } 205 }; 206 207 doc.render(run); 208 StringReader reader = new StringReader(str[0]); 210 return new org.xml.sax.InputSource (reader); 211 } 212 else { 213 inputStream = new BufferedInputStream(getPrimaryFile().getInputStream()); 215 return new org.xml.sax.InputSource (inputStream); 216 } 217 } 218 219 224 protected void closeInputSource() { 225 InputStream is = inputStream; 226 if (is != null) { 227 try { 228 is.close(); 229 } 230 catch (IOException e) { 231 } 233 if (is == inputStream) { 234 inputStream = null; 235 } 236 } 237 } 238 public boolean isDocumentValid(){ 239 return documentValid; 240 } 241 public void setDocumentValid (boolean valid){ 242 if (documentValid!=valid) { 243 if (valid) 244 repairNode(); 245 documentValid=valid; 246 firePropertyChange (PROP_DOC_VALID, !documentValid ? Boolean.TRUE : Boolean.FALSE, documentValid ? Boolean.TRUE : Boolean.FALSE); 247 } 248 } 249 public void addSaveCookie(SaveCookie cookie){ 250 getCookieSet().add(cookie); 251 } 252 public void removeSaveCookie(){ 253 org.openide.nodes.Node.Cookie cookie = getCookie(SaveCookie.class); 254 if (cookie!=null) getCookieSet().remove(cookie); 255 } 256 257 public void setSavingDocument(boolean saving){ 258 savingDocument=saving; 259 } 260 public boolean isSavingDocument(){ 261 return savingDocument; 262 } 263 public void displayErrorMessage() { 264 if (error==null) return; 265 if (errorAnnotation==null) 266 errorAnnotation = new org.openide.text.Annotation() { 267 public String getAnnotationType() { 268 return "xml-j2ee-annotation"; } 270 String desc = NbBundle.getMessage(XMLJ2eeDataObject.class, "HINT_XMLErrorDescription"); 271 public String getShortDescription() { 272 return desc; 273 } 274 }; 275 if (inOut==null) 276 inOut=org.openide.windows.IOProvider.getDefault().getIO(NbBundle.getMessage(XMLJ2eeDataObject.class, "TXT_parser"), false); 277 inOut.setFocusTaken (false); 278 OutputWriter outputWriter = inOut.getOut(); 279 int line = Math.max(0,error.getErrorLine()); 280 282 LineCookie cookie = (LineCookie)getCookie(LineCookie.class); 283 Line xline = cookie.getLineSet ().getCurrent(line==0?0:line-1); 285 errorAnnotation.attach(xline); 287 288 try { 289 outputWriter.reset(); 290 IOCtl outList= new IOCtl(xline); 292 outputWriter.println(this.getOutputStringForInvalidDocument(error),outList); 293 }catch(IOException e){} 294 } 295 296 public void setValid(boolean valid) throws java.beans.PropertyVetoException { 297 if (!valid && inOut!=null) inOut.closeInputOutput(); 298 super.setValid(valid); 299 } 300 301 final class IOCtl implements OutputListener { 302 303 Line xline; 304 305 public IOCtl (Line xline) { 306 this.xline=xline; 307 } 308 309 public void outputLineSelected (OutputEvent ev) { 310 errorAnnotation.attach(xline); 311 xline.show(Line.SHOW_TRY_SHOW); 312 } 313 314 public void outputLineAction (OutputEvent ev) { 315 errorAnnotation.attach(xline); 316 xline.show(Line.SHOW_TRY_SHOW); 317 } 318 319 public void outputLineCleared (OutputEvent ev) { 320 errorAnnotation.detach(); 321 } 322 } 323 324 public static class J2eeErrorHandler implements ErrorHandler { 325 326 private XMLJ2eeDataObject xmlJ2eeDataObject; 327 328 public J2eeErrorHandler(XMLJ2eeDataObject obj) { 329 xmlJ2eeDataObject=obj; 330 } 331 332 public void error(SAXParseException exception) throws SAXException { 333 xmlJ2eeDataObject.createSAXParseError(exception); 334 throw exception; 335 } 336 337 public void fatalError(SAXParseException exception) throws SAXException { 338 xmlJ2eeDataObject.createSAXParseError(exception); 339 throw exception; 340 } 341 342 public void warning(SAXParseException exception) throws SAXException { 343 xmlJ2eeDataObject.createSAXParseError(exception); 344 throw exception; 345 } 346 } 347 348 private void createSAXParseError(SAXParseException error){ 349 this.error = new SAXParseError(error); 350 } 351 352 } 353 354 | Popular Tags |