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