001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.core;
020:
021: import org.xml.sax.*;
022: import javax.xml.transform.*;
023: import javax.xml.transform.sax.*;
024:
025: import org.netbeans.api.xml.cookies.*;
026: import org.netbeans.spi.xml.cookies.*;
027: import org.openide.util.NbBundle;
028:
029: /**
030: * Validates XSL transformation
031: * @author asgeir@dimonsoftware.com
032: */
033: public class ValidateXSLSupport implements ValidateXMLCookie {
034:
035: // associated input source
036: private final InputSource inputSource;
037:
038: // it will viasualize our results
039: private CookieObserver console;
040:
041: // fatal error counter
042: private int fatalErrors;
043:
044: // error counter
045: private int errors;
046:
047: /** Creates a new instance of ValidateXSLSupport */
048: public ValidateXSLSupport(InputSource inputSource) {
049: this .inputSource = inputSource;
050: }
051:
052: // inherit JavaDoc
053: public boolean validateXML(CookieObserver l) {
054: try {
055: console = l;
056:
057: int fatalErrors = 0;
058: int errors = 0;
059:
060: String checkedFile = inputSource.getSystemId();
061:
062: sendMessage(NbBundle.getMessage(ValidateXSLSupport.class,
063: "MSG_checking", checkedFile));//NOI18N
064:
065: ErrorListener errorListener = new XslErrorListener();
066: try {
067: SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory
068: .newInstance();
069: factory.setErrorListener(errorListener);
070: TransformerHandler transformerHandler = factory
071: .newTransformerHandler(new SAXSource(
072: inputSource));
073: } catch (TransformerException ex) {
074: CookieMessage message = new CookieMessage(ex
075: .getLocalizedMessage(),
076: CookieMessage.FATAL_ERROR_LEVEL,
077: new DefaultXMLProcessorDetail(ex));
078: sendMessage(message);
079: }
080:
081: return errors == 0 && fatalErrors == 0;
082: } finally {
083: console = null;
084: }
085: }
086:
087: private void sendMessage(String message) {
088: if (console != null) {
089: console.receive(new CookieMessage(message));
090: }
091: }
092:
093: private void sendMessage(CookieMessage message) {
094: if (console != null) {
095: console.receive(message);
096: }
097: }
098:
099: //
100: // class XslErrorListener
101: //
102: private class XslErrorListener implements ErrorListener {
103: public void error(TransformerException ex)
104: throws TransformerException {
105: if (errors++ == getMaxErrorCount()) {
106: sendMessage(NbBundle.getMessage(
107: ValidateXSLSupport.class, "MSG_too_many_errs"));//NOI18N
108: throw ex; // stop the parser
109: } else {
110: CookieMessage message = new CookieMessage(ex
111: .getLocalizedMessage(),
112: CookieMessage.ERROR_LEVEL,
113: new DefaultXMLProcessorDetail(ex));
114: sendMessage(message);
115: }
116: }
117:
118: public void fatalError(TransformerException ex)
119: throws TransformerException {
120: fatalErrors++;
121: CookieMessage message = new CookieMessage(ex
122: .getLocalizedMessage(),
123: CookieMessage.FATAL_ERROR_LEVEL,
124: new DefaultXMLProcessorDetail(ex));
125: sendMessage(message);
126: }
127:
128: public void warning(TransformerException ex)
129: throws TransformerException {
130: CookieMessage message = new CookieMessage(ex
131: .getLocalizedMessage(),
132: CookieMessage.WARNING_LEVEL,
133: new DefaultXMLProcessorDetail(ex));
134: sendMessage(message);
135: }
136:
137: private int getMaxErrorCount() {
138: return 20; //??? load from option
139: }
140: } // class XslErrorListener
141:
142: }
|