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