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