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