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.xslt.util;
030:
031: import com.sun.jbi.engine.xslt.TEResources;
032:
033: import org.w3c.dom.Document;
034:
035: import org.xml.sax.SAXException;
036: import org.xml.sax.SAXParseException;
037: import org.xml.sax.helpers.DefaultHandler;
038:
039: import java.io.IOException;
040:
041: import javax.xml.parsers.DocumentBuilder;
042: import javax.xml.parsers.DocumentBuilderFactory;
043: import javax.xml.parsers.ParserConfigurationException;
044:
045: /**
046: * DOCUMENT ME!
047: *
048: * @author Sun Microsystems, Inc.
049: */
050: public final class ServiceListValidator extends DefaultHandler
051: implements TEResources {
052: /**
053: * DOCUMENT ME!
054: */
055: static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
056:
057: /**
058: * DOCUMENT ME!
059: */
060: static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
061:
062: /**
063: * DOCUMENT ME!
064: */
065: static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
066:
067: /**
068: * DOCUMENT ME!
069: */
070: private DocumentBuilderFactory mDocFactory = null;
071:
072: /**
073: * DOCUMENT ME!
074: */
075: private String mErrorMsg = null;
076:
077: /**
078: * DOCUMENT ME!
079: */
080: private String mFileName = "servicelist.xml";
081:
082: /**
083: * DOCUMENT ME!
084: */
085: private String mSchemaFile = "servicelist.xsd";
086:
087: /**
088: *
089: */
090: private StringTranslator mTranslator = null;
091:
092: /**
093: * DOCUMENT ME!
094: */
095: private boolean mValid = true;
096:
097: /**
098: * DOCUMENT ME!
099: */
100: private Document mDocument = null;
101:
102: /**
103: * Creates a new ServiceListValidator object.
104: *
105: * @param schema DOCUMENT ME!
106: * @param xmlfile DOCUMENT ME!
107: */
108: public ServiceListValidator(String schema, String xmlfile) {
109: mTranslator = new StringTranslator("com.sun.jbi.engine.xslt",
110: this .getClass().getClassLoader());
111: mDocFactory = DocumentBuilderFactory.newInstance();
112: mDocFactory.setNamespaceAware(true);
113: mDocFactory.setValidating(false);
114: mSchemaFile = schema;
115: mFileName = xmlfile;
116: }
117:
118: /**
119: * DOCUMENT ME!
120: *
121: * @return DOCUMENT ME!
122: */
123: public String getError() {
124: return mErrorMsg;
125: }
126:
127: /**
128: * DOCUMENT ME!
129: *
130: * @return DOCUMENT ME!
131: */
132: public boolean isValid() {
133: return mValid;
134: }
135:
136: /**
137: * DOCUMENT ME!
138: *
139: * @param se DOCUMENT ME!
140: */
141: public void error(SAXParseException se) {
142: //System.out.println(se.getMessage());
143: mValid = false;
144: }
145:
146: /**
147: * DOCUMENT ME!
148: *
149: * @param se DOCUMENT ME!
150: */
151: public void fatalError(SAXParseException se) {
152: //System.out.println(se.getMessage());
153: mValid = false;
154: }
155:
156: /**
157: * DOCUMENT ME!
158: *
159: * @return DOCUMENT ME!
160: */
161: public Document parse() {
162: DocumentBuilder builder = null;
163:
164: try {
165: builder = mDocFactory.newDocumentBuilder();
166: } catch (ParserConfigurationException pce) {
167: setError(pce.getMessage());
168: }
169:
170: builder.setErrorHandler(this );
171:
172: Document document = null;
173:
174: try {
175: document = builder.parse(mFileName);
176: } catch (SAXException se) {
177: setError(se.getMessage());
178: se.printStackTrace();
179: } catch (IOException ioe) {
180: setError(ioe.getMessage());
181: ioe.printStackTrace();
182: }
183:
184: return document;
185: }
186:
187: /**
188: * DOCUMENT ME!
189: */
190: public void validate() {
191: setAttributes();
192:
193: Document d = parse();
194:
195: if (!isValid()) {
196: //System.out.println(getError());
197: }
198: mDocument = d;
199: }
200:
201: /**
202: * DOCUMENT ME!
203: */
204: public Document getDocument() {
205: return mDocument;
206: }
207:
208: /**
209: * DOCUMENT ME!
210: *
211: * @param se DOCUMENT ME!
212: */
213: public void warning(SAXParseException se) {
214: //System.out.println("WARNIGN " + se.getMessage());
215: }
216:
217: /**
218: * DOCUMENT ME!
219: */
220: private void setAttributes() {
221: mDocFactory.setValidating(true);
222:
223: try {
224: mDocFactory.setAttribute(JAXP_SCHEMA_LANGUAGE,
225: W3C_XML_SCHEMA);
226: mDocFactory.setAttribute(JAXP_SCHEMA_SOURCE, mSchemaFile);
227: } catch (IllegalArgumentException iae) {
228: setError(iae.getMessage());
229: iae.printStackTrace();
230: }
231: }
232:
233: /**
234: * DOCUMENT ME!
235: *
236: * @param msg DOCUMENT ME!
237: */
238: private void setError(String msg) {
239: mErrorMsg = msg;
240: }
241: }
|