1 19 package org.netbeans.modules.xsl.cookies; 20 21 import org.xml.sax.*; 22 import org.xml.sax.helpers.DefaultHandler ; 23 import javax.xml.transform.*; 24 import javax.xml.transform.sax.*; 25 import javax.xml.transform.stream.StreamSource ; 26 27 import org.openide.filesystems.FileStateInvalidException; 28 29 import org.netbeans.api.xml.cookies.*; 30 import org.netbeans.spi.xml.cookies.*; 31 32 36 public class ValidateXSLSupport implements ValidateXMLCookie { 37 38 private final InputSource inputSource; 40 41 private CookieObserver console; 43 44 private int fatalErrors; 46 47 private int errors; 49 50 51 public ValidateXSLSupport(InputSource inputSource) { 52 this.inputSource = inputSource; 53 } 54 55 public boolean validateXML(CookieObserver l) { 57 try { 58 console = l; 59 60 int fatalErrors = 0; 61 int errors = 0; 62 63 String checkedFile = inputSource.getSystemId(); 64 sendMessage(Util.THIS.getString("MSG_checking", checkedFile)); 65 66 ErrorListener errorListener = new XslErrorListener(); 67 try { 68 SAXTransformerFactory factory = (SAXTransformerFactory)TransformerFactory.newInstance(); 69 factory.setErrorListener(errorListener); 70 TransformerHandler transformerHandler = factory.newTransformerHandler(new SAXSource(inputSource)); 71 } catch (TransformerException ex) { 72 CookieMessage message = new CookieMessage( 73 ex.getLocalizedMessage(), 74 CookieMessage.FATAL_ERROR_LEVEL, 75 new DefaultXMLProcessorDetail(ex) 76 ); 77 sendMessage (message); 78 } 79 80 return errors == 0 && fatalErrors == 0; 81 } finally { 82 console = null; 83 } 84 } 85 86 private void sendMessage(String message) { 87 if (console != null) { 88 console.receive(new CookieMessage(message)); 89 } 90 } 91 92 private void sendMessage (CookieMessage message) { 93 if (console != null) { 94 console.receive (message); 95 } 96 } 97 98 99 private class XslErrorListener implements ErrorListener { 103 public void error(TransformerException ex) throws TransformerException{ 104 if (errors++ == getMaxErrorCount()) { 105 String msg = Util.THIS.getString("MSG_too_many_errs"); 106 sendMessage(msg); 107 throw ex; } else { 109 CookieMessage message = new CookieMessage( 110 ex.getLocalizedMessage(), 111 CookieMessage.ERROR_LEVEL, 112 new DefaultXMLProcessorDetail(ex) 113 ); 114 sendMessage (message); 115 } 116 } 117 118 public void fatalError(TransformerException ex) throws TransformerException{ 119 fatalErrors++; 120 CookieMessage message = new CookieMessage( 121 ex.getLocalizedMessage(), 122 CookieMessage.FATAL_ERROR_LEVEL, 123 new DefaultXMLProcessorDetail(ex) 124 ); 125 sendMessage (message); 126 } 127 128 public void warning(TransformerException ex) throws TransformerException{ 129 CookieMessage message = new CookieMessage( 130 ex.getLocalizedMessage(), 131 CookieMessage.WARNING_LEVEL, 132 new DefaultXMLProcessorDetail(ex) 133 ); 134 sendMessage (message); 135 } 136 137 private int getMaxErrorCount() { 138 return 20; } 140 } 142 } 143 | Popular Tags |