001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * ConfigFileValidator.java
005: *
006: * SUN PROPRIETARY/CONFIDENTIAL.
007: * This software is the proprietary information of Sun Microsystems, Inc.
008: * Use is subject to license terms.
009: *
010: */
011: package com.sun.jbi.binding.file.util;
012:
013: import com.sun.jbi.binding.file.FileBindingContext;
014: import com.sun.jbi.binding.file.FileBindingResources;
015:
016: import org.w3c.dom.Document;
017:
018: import org.xml.sax.SAXException;
019: import org.xml.sax.SAXParseException;
020: import org.xml.sax.helpers.DefaultHandler;
021:
022: import java.io.IOException;
023:
024: import java.util.logging.Logger;
025:
026: import javax.xml.parsers.DocumentBuilder;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.parsers.ParserConfigurationException;
029:
030: /**
031: * This class is uses to validate the configuration file supplied during
032: * deployment conforms to the schema.
033: *
034: * @author Sun Microsystems, Inc.
035: */
036: public final class ConfigFileValidator extends DefaultHandler implements
037: FileBindingResources {
038: /**
039: * Parser setting.
040: */
041: static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
042:
043: /**
044: * Parser setting.
045: */
046: static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
047:
048: /**
049: * Parser setting.
050: */
051: static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
052:
053: /**
054: * Document object generated after parsing.
055: */
056: private Document mDocument;
057:
058: /**
059: * Factory object for document.
060: */
061: private DocumentBuilderFactory mFactory = null;
062:
063: /**
064: * Object to store exceptions
065: */
066: private Exception mException;
067:
068: /**
069: * Logger object.
070: */
071: private Logger mLog;
072:
073: /**
074: * Stores the error message , if any.
075: */
076: private String mErrorMsg = "";
077:
078: /**
079: * Name of the file to parse.
080: */
081: private String mFileName;
082:
083: /**
084: * Name of the schema file.
085: */
086: private String mSchemaFile;
087:
088: /**
089: * Helper class for i18n.
090: */
091: private StringTranslator mTranslator;
092:
093: /**
094: * Flag to denote validity.
095: */
096: private boolean mValid = true;
097:
098: /**
099: * Creates a new ConfigFileValidator object.
100: *
101: * @param schema schema file name
102: * @param xmlfile xml file name
103: */
104: public ConfigFileValidator(String schema, String xmlfile) {
105: mFactory = DocumentBuilderFactory.newInstance();
106: mFactory.setNamespaceAware(true);
107: mFactory.setValidating(false);
108: mSchemaFile = schema;
109: mFileName = xmlfile;
110: mLog = FileBindingContext.getInstance().getLogger();
111: mTranslator = new StringTranslator();
112: }
113:
114: /**
115: * Returns the document object obtained as a result of parsing.
116: *
117: * @return document object
118: */
119: public Document getDocument() {
120: return mDocument;
121: }
122:
123: /**
124: * Returns the error message if any.
125: *
126: * @return String error message.
127: */
128: public String getError() {
129: return mErrorMsg;
130: }
131:
132: /**
133: * Get the exception message if any.
134: *
135: * @return exception message if any occured.
136: */
137: public Exception getException() {
138: return mException;
139: }
140:
141: /**
142: * Returns true if the file conforms to schema.
143: *
144: * @return valid / invalid
145: */
146: public boolean isValid() {
147: return mValid;
148: }
149:
150: /**
151: * Sets the parser as validating or non-validating. The parser is
152: * non-validating if this method is not called before the parse method.
153: */
154: public void setValidating() {
155: setAttributes();
156: }
157:
158: /**
159: * Handler method for the parser.
160: *
161: * @param se sax parse exception object.
162: */
163: public void error(SAXParseException se) {
164: mLog.severe(se.getMessage());
165: setError(getError() + "\n\t" + "Line:" + se.getLineNumber()
166: + ", Column:" + se.getColumnNumber() + ":"
167: + se.getMessage());
168: setException(se);
169: mValid = false;
170: }
171:
172: /**
173: * Handler method for the parser.
174: *
175: * @param se SAXPrse exception.
176: */
177: public void fatalError(SAXParseException se) {
178: mLog.severe(getError() + "\n\t" + "Line:" + se.getLineNumber()
179: + ",Column:" + se.getColumnNumber() + ":"
180: + se.getMessage());
181: setError(se.getMessage());
182: setException(se);
183: mValid = false;
184: }
185:
186: /**
187: * This method has to be invoked to check the validity of the input
188: * document.
189: */
190: public void validate() {
191: parse();
192: mLog.fine(mTranslator.getString(FBC_CONFIG_FILE_CHECK)
193: + isValid());
194: }
195:
196: /**
197: * Handler method for the parser.
198: *
199: * @param se SAXParseexception.
200: */
201: public void warning(SAXParseException se) {
202: mLog.warning(se.getLineNumber() + ":" + se.getColumnNumber()
203: + ":" + se.getMessage());
204: }
205:
206: /**
207: * Sets the attributes of the parser.
208: */
209: private void setAttributes() {
210: mFactory.setValidating(true);
211:
212: try {
213: mFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
214: mFactory.setAttribute(JAXP_SCHEMA_SOURCE, mSchemaFile);
215: mFactory.setNamespaceAware(true);
216: } catch (IllegalArgumentException iae) {
217: setError(iae.getMessage());
218: setException(iae);
219: mValid = false;
220: iae.printStackTrace();
221: }
222: }
223:
224: /**
225: * Used to set error message if occurs during parsing.
226: *
227: * @param msg error message that occurred.
228: */
229: private void setError(String msg) {
230: mErrorMsg = msg;
231: }
232:
233: /**
234: * Sets the exception.
235: *
236: * @param e any exception
237: */
238: private void setException(Exception e) {
239: mException = e;
240: }
241:
242: /**
243: * Parses the input XML file.
244: */
245: private void parse() {
246: DocumentBuilder builder = null;
247:
248: try {
249: builder = mFactory.newDocumentBuilder();
250: } catch (ParserConfigurationException pce) {
251: mLog.severe(pce.getMessage());
252: setError(pce.getMessage());
253: setException(pce);
254: mValid = false;
255: }
256:
257: builder.setErrorHandler(this );
258:
259: try {
260: mDocument = builder.parse(mFileName);
261: } catch (SAXException se) {
262: mLog.severe(mTranslator.getString(FBC_CANNOT_PARSE));
263: mLog.severe(se.getMessage());
264: setError(mTranslator.getString(FBC_CANNOT_PARSE) + "\n"
265: + se.getMessage());
266: setException(se);
267: se.printStackTrace();
268: mValid = false;
269: } catch (IOException ioe) {
270: mLog.severe(mTranslator.getString(FBC_CANNOT_PARSE));
271: mLog.severe(ioe.getMessage());
272: setError(mTranslator.getString(FBC_CANNOT_PARSE) + "\n"
273: + ioe.getMessage());
274: setException(ioe);
275: ioe.printStackTrace();
276: mValid = false;
277: }
278: }
279: }
|