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