001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.xsl.cookies;
042:
043: import org.xml.sax.*;
044: import javax.xml.transform.*;
045: import javax.xml.transform.sax.*;
046:
047: import org.netbeans.api.xml.cookies.*;
048: import org.netbeans.spi.xml.cookies.*;
049: import org.openide.util.NbBundle;
050:
051: /**
052: * Validates XSL transformation
053: * @author asgeir@dimonsoftware.com
054: */
055: public class ValidateXSLSupport implements ValidateXMLCookie {
056:
057: // associated input source
058: private final InputSource inputSource;
059:
060: // it will viasualize our results
061: private CookieObserver console;
062:
063: // fatal error counter
064: private int fatalErrors;
065:
066: // error counter
067: private int errors;
068:
069: /** Creates a new instance of ValidateXSLSupport */
070: public ValidateXSLSupport(InputSource inputSource) {
071: this .inputSource = inputSource;
072: }
073:
074: // inherit JavaDoc
075: public boolean validateXML(CookieObserver l) {
076: try {
077: console = l;
078:
079: int fatalErrors = 0;
080: int errors = 0;
081:
082: String checkedFile = inputSource.getSystemId();
083: sendMessage(NbBundle.getMessage(ValidateXSLSupport.class,
084: "MSG_checking", checkedFile));
085:
086: ErrorListener errorListener = new XslErrorListener();
087: try {
088: SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory
089: .newInstance();
090: factory.setErrorListener(errorListener);
091: TransformerHandler transformerHandler = factory
092: .newTransformerHandler(new SAXSource(
093: inputSource));
094: } catch (TransformerException ex) {
095: CookieMessage message = new CookieMessage(ex
096: .getLocalizedMessage(),
097: CookieMessage.FATAL_ERROR_LEVEL,
098: new DefaultXMLProcessorDetail(ex));
099: sendMessage(message);
100: }
101:
102: return errors == 0 && fatalErrors == 0;
103: } finally {
104: console = null;
105: }
106: }
107:
108: private void sendMessage(String message) {
109: if (console != null) {
110: console.receive(new CookieMessage(message));
111: }
112: }
113:
114: private void sendMessage(CookieMessage message) {
115: if (console != null) {
116: console.receive(message);
117: }
118: }
119:
120: //
121: // class XslErrorListener
122: //
123: private class XslErrorListener implements ErrorListener {
124: public void error(TransformerException ex)
125: throws TransformerException {
126: if (errors++ == getMaxErrorCount()) {
127: String msg = NbBundle.getMessage(
128: ValidateXSLSupport.class, "MSG_too_many_errs");
129: sendMessage(msg);
130: throw ex; // stop the parser
131: } else {
132: CookieMessage message = new CookieMessage(ex
133: .getLocalizedMessage(),
134: CookieMessage.ERROR_LEVEL,
135: new DefaultXMLProcessorDetail(ex));
136: sendMessage(message);
137: }
138: }
139:
140: public void fatalError(TransformerException ex)
141: throws TransformerException {
142: fatalErrors++;
143: CookieMessage message = new CookieMessage(ex
144: .getLocalizedMessage(),
145: CookieMessage.FATAL_ERROR_LEVEL,
146: new DefaultXMLProcessorDetail(ex));
147: sendMessage(message);
148: }
149:
150: public void warning(TransformerException ex)
151: throws TransformerException {
152: CookieMessage message = new CookieMessage(ex
153: .getLocalizedMessage(),
154: CookieMessage.WARNING_LEVEL,
155: new DefaultXMLProcessorDetail(ex));
156: sendMessage(message);
157: }
158:
159: private int getMaxErrorCount() {
160: return 20; //??? load from option
161: }
162: } // class XslErrorListener
163:
164: }
|