1 19 20 package org.netbeans.modules.websvc.wsdl.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.text.MessageFormat ; 33 import java.io.*; 34 import org.xml.sax.*; 35 import org.openide.xml.*; 36 import org.netbeans.api.xml.cookies.CheckXMLCookie; 37 import org.netbeans.spi.xml.cookies.*; 38 39 43 public abstract class XMLJ2eeDataObject extends XMLDataObject implements CookieSet.Factory { 44 45 protected boolean nodeDirty = false; 46 private boolean documentDirty = true; 47 private boolean savingDocument; 48 private InputStream inputStream; 49 private InputOutput inOut; 50 protected XMLJ2eeEditorSupport editor; 51 private boolean documentValid=true; 52 private SAXParseError error; 53 private org.openide.text.Annotation errorAnnotation; 54 55 private static final long serialVersionUID = -515751072013886985L; 56 57 58 public static final String PROP_DOC_VALID = "documentValid"; 60 public XMLJ2eeDataObject(FileObject pf, MultiFileLoader loader) 61 throws org.openide.loaders.DataObjectExistsException { 62 super(pf,loader); 63 64 getCookieSet().add(XMLJ2eeEditorSupport.class, this); 65 getCookieSet().add(EditCookie.class, this); 66 getCookieSet().add(EditorCookie.class, this); 67 getCookieSet().add(LineCookie.class, this); 68 getCookieSet().add(PrintCookie.class, this); 69 getCookieSet().add(CloseCookie.class, this); 70 InputSource in = DataObjectAdapters.inputSource(this); 72 CheckXMLCookie checkCookie = new CheckXMLSupport(in); 73 getCookieSet().add(checkCookie); 74 } 75 protected EditorCookie createEditorCookie () { 77 return null; 78 } 79 83 protected abstract SAXParseError updateNode(org.xml.sax.InputSource is) throws java.io.IOException ; 84 87 protected abstract String getIconBaseForValidDocument(); 88 89 92 protected abstract String getIconBaseForInvalidDocument(); 93 94 95 public org.openide.nodes.Node.Cookie createCookie(Class clazz) { 96 if(clazz.isAssignableFrom(XMLJ2eeEditorSupport.class)) 97 return getEditorSupport(); 98 else 99 return null; 100 } 101 102 103 protected synchronized XMLJ2eeEditorSupport getEditorSupport() { 104 if(editor == null) { 105 editor = new XMLJ2eeEditorSupport(this); 106 } 107 return editor; 108 } 109 110 115 public String getStringForInvalidDocument() { 116 if(error != null) { 117 return getOutputStringForInvalidDocument(error); 118 } else { 119 return "No errors"; 120 } 121 } 122 123 127 public String getOutputStringForInvalidDocument(SAXParseError error){ 128 String mes = MessageFormat.format (NbBundle.getMessage (XMLJ2eeDataObject.class, "TXT_errorMessage"), 130 new Object [] { error.getErrorText(), 131 new Integer (error.getErrorLine()), 132 new Integer (error.getErrorColumn()) }); 133 return mes; 134 } 135 136 139 public boolean isNodeDirty(){ 140 return nodeDirty; 141 } 142 143 145 public void setDocumentDirty(boolean dirty){ 146 documentDirty=dirty; 147 } 148 149 152 public boolean isDocumentDirty(){ 153 return documentDirty; 154 } 155 156 159 public void setNodeDirty(boolean dirty){ 160 nodeDirty=dirty; 161 } 162 163 165 protected void repairNode(){ 166 org.openide.awt.StatusDisplayer.getDefault().setStatusText(""); if (inOut!=null) { 170 inOut.closeInputOutput(); 171 errorAnnotation.detach(); 172 } 173 } 174 175 178 public void parsingDocument(){ 179 error = null; 183 try { 184 error = updateNode(prepareInputSource()); 185 } 186 catch (Exception e) { 187 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e); 188 setDocumentValid(false); 189 return; 190 } 191 finally { 192 closeInputSource(); 193 documentDirty=false; 194 } 195 if (error == null){ 196 setDocumentValid(true); 197 }else { 198 setDocumentValid(false); 199 } 200 } 201 202 208 protected org.xml.sax.InputSource prepareInputSource() throws java.io.IOException { 209 if ((editor != null) && (editor.isDocumentLoaded())) { 210 final javax.swing.text.Document doc = editor.getDocument(); 212 final String [] str = new String [1]; 213 Runnable run = new Runnable () { 215 public void run() { 216 try { 217 str[0] = doc.getText(0, doc.getLength()); 218 } 219 catch (javax.swing.text.BadLocationException e) { 220 } 222 } 223 }; 224 225 doc.render(run); 226 StringReader reader = new StringReader(str[0]); 228 return new org.xml.sax.InputSource (reader); 229 } 230 else { 231 inputStream = new BufferedInputStream(getPrimaryFile().getInputStream()); 233 return new org.xml.sax.InputSource (inputStream); 234 } 235 } 236 237 242 protected void closeInputSource() { 243 InputStream is = inputStream; 244 if (is != null) { 245 try { 246 is.close(); 247 } 248 catch (IOException e) { 249 } 251 if (is == inputStream) { 252 inputStream = null; 253 } 254 } 255 } 256 public boolean isDocumentValid(){ 257 return documentValid; 258 } 259 public void setDocumentValid (boolean valid){ 260 if (documentValid!=valid) { 261 if (valid) 262 repairNode(); 263 documentValid=valid; 264 firePropertyChange (PROP_DOC_VALID, !documentValid ? Boolean.TRUE : Boolean.FALSE, documentValid ? Boolean.TRUE : Boolean.FALSE); 265 } 266 } 267 public void addSaveCookie(SaveCookie cookie){ 268 getCookieSet().add(cookie); 269 } 270 public void removeSaveCookie(){ 271 org.openide.nodes.Node.Cookie cookie = getCookie(SaveCookie.class); 272 if (cookie!=null) getCookieSet().remove(cookie); 273 } 274 275 public void setSavingDocument(boolean saving){ 276 savingDocument=saving; 277 } 278 public boolean isSavingDocument(){ 279 return savingDocument; 280 } 281 public void displayErrorMessage() { 282 if (error==null) return; 283 if (errorAnnotation==null) 284 errorAnnotation = new org.openide.text.Annotation() { 285 public String getAnnotationType() { 286 return "xml-j2ee-annotation"; } 288 String desc = NbBundle.getMessage(XMLJ2eeDataObject.class, "HINT_XMLErrorDescription"); 289 public String getShortDescription() { 290 return desc; 291 } 292 }; 293 if (inOut==null) 294 inOut=org.openide.windows.IOProvider.getDefault().getIO(NbBundle.getMessage(XMLJ2eeDataObject.class, "TXT_parser"), false); 295 inOut.setFocusTaken (false); 296 OutputWriter outputWriter = inOut.getOut(); 297 int line = Math.max(0,error.getErrorLine()); 298 int column = Math.max(0,error.getErrorColumn()); 299 300 LineCookie cookie = (LineCookie)getCookie(LineCookie.class); 301 Line xline = cookie.getLineSet ().getCurrent(line==0?0:line-1); 303 errorAnnotation.attach(xline); 305 306 try { 307 outputWriter.reset(); 308 IOCtl outList= new IOCtl(xline); 310 outputWriter.println(this.getOutputStringForInvalidDocument(error),outList); 311 } catch(IOException e){ 312 ErrorManager.getDefault().notify(e); 313 } 314 } 315 316 public void setValid(boolean valid) throws java.beans.PropertyVetoException { 317 if (!valid && inOut!=null) inOut.closeInputOutput(); 318 super.setValid(valid); 319 } 320 321 final class IOCtl implements OutputListener { 322 323 Line xline; 324 325 public IOCtl (Line xline) { 326 this.xline=xline; 327 } 328 329 public void outputLineSelected (OutputEvent ev) { 330 errorAnnotation.attach(xline); 331 xline.show(Line.SHOW_TRY_SHOW); 332 } 333 334 public void outputLineAction (OutputEvent ev) { 335 errorAnnotation.attach(xline); 336 xline.show(Line.SHOW_TRY_SHOW); 337 } 338 339 public void outputLineCleared (OutputEvent ev) { 340 errorAnnotation.detach(); 341 } 342 } 343 344 public static class J2eeErrorHandler implements ErrorHandler { 345 346 private XMLJ2eeDataObject xmlJ2eeDataObject; 347 348 public J2eeErrorHandler(XMLJ2eeDataObject obj) { 349 xmlJ2eeDataObject=obj; 350 } 351 352 public void error(SAXParseException exception) throws SAXException { 353 xmlJ2eeDataObject.createSAXParseError(exception); 354 throw exception; 355 } 356 357 public void fatalError(SAXParseException exception) throws SAXException { 358 xmlJ2eeDataObject.createSAXParseError(exception); 359 throw exception; 360 } 361 362 public void warning(SAXParseException exception) throws SAXException { 363 xmlJ2eeDataObject.createSAXParseError(exception); 364 throw exception; 365 } 366 } 367 368 private void createSAXParseError(SAXParseException error){ 369 this.error = new SAXParseError(error); 370 } 371 372 } 373 374 | Popular Tags |