KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xsl > cookies > ValidateXSLSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xsl.cookies;
20
21 import org.xml.sax.*;
22 import org.xml.sax.helpers.DefaultHandler JavaDoc;
23 import javax.xml.transform.*;
24 import javax.xml.transform.sax.*;
25 import javax.xml.transform.stream.StreamSource JavaDoc;
26
27 import org.openide.filesystems.FileStateInvalidException;
28
29 import org.netbeans.api.xml.cookies.*;
30 import org.netbeans.spi.xml.cookies.*;
31
32 /**
33  * Validates XSL transformation
34  * @author asgeir@dimonsoftware.com
35  */

36 public class ValidateXSLSupport implements ValidateXMLCookie {
37
38     // associated input source
39
private final InputSource inputSource;
40
41     // it will viasualize our results
42
private CookieObserver console;
43
44     // fatal error counter
45
private int fatalErrors;
46     
47     // error counter
48
private int errors;
49
50     /** Creates a new instance of ValidateXSLSupport */
51     public ValidateXSLSupport(InputSource inputSource) {
52         this.inputSource = inputSource;
53     }
54     
55     // inherit JavaDoc
56
public boolean validateXML(CookieObserver l) {
57        try {
58             console = l;
59
60             int fatalErrors = 0;
61             int errors = 0;
62
63             String JavaDoc 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 JavaDoc 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     //
100
// class XslErrorListener
101
//
102
private class XslErrorListener implements ErrorListener {
103         public void error(TransformerException ex) throws TransformerException{
104             if (errors++ == getMaxErrorCount()) {
105                 String JavaDoc msg = Util.THIS.getString("MSG_too_many_errs");
106                 sendMessage(msg);
107                 throw ex; // stop the parser
108
} 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; //??? load from option
139
}
140     } // class XslErrorListener
141

142 }
143
Popular Tags