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: * @(#)Loader.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * Loader.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: */package com.sun.jbi.internal.security.util;
037:
038: import java.io.File;
039: import java.io.FileInputStream;
040: import java.io.InputStream;
041: import java.io.Reader;
042:
043: import javax.xml.parsers.DocumentBuilder;
044: import javax.xml.parsers.DocumentBuilderFactory;
045: import javax.xml.parsers.ParserConfigurationException;
046:
047: import org.w3c.dom.Document;
048: import org.xml.sax.SAXException;
049: import org.xml.sax.InputSource;
050:
051: /**
052: * This class is a helper class which aids in converting
053: * a DOM Document to a File and vice-versa.
054: *
055: * @author Sun Microsystems, Inc.
056: */
057: public class Loader {
058:
059: /** Schema Language. */
060: private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
061:
062: /** W3C Schema. */
063: private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
064:
065: /** Schema Source. */
066: private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
067:
068: /**
069: * Given a filename load an XML document into
070: * a Document object and return it.
071: *
072: * @param filename is the String filename of the XML file
073: * references, if this paramter is null the XML parsers default resolution is used.
074: * @param validate indicates that the document is to be validated.
075: * @param schemaFiles are the schema files to be used if validation is to be done,
076: * this parameter can be null if validate = false.
077: * @param handler is the ErrorHandler
078: * @throws java.io.FileNotFoundException if the file with name filename is not found.
079: * @throws java.io.IOException on IO errors
080: * @throws SAXException on XML parsing errors.
081: * @throws ParserConfigurationException if the XML Parser is not configured correctly.
082: * @return the XML Document object
083: */
084: public Document load(String filename, boolean validate,
085: org.xml.sax.ErrorHandler handler, String[] schemaFiles)
086: throws java.io.IOException, java.io.FileNotFoundException,
087: SAXException, ParserConfigurationException {
088:
089: return load(new FileInputStream(new File(filename)), validate,
090: handler, schemaFiles);
091:
092: };
093:
094: /**
095: * Given a Inputstream load an XML document into
096: * a Document object and return it.
097: *
098: * @param istr is a stream to the input XML file.
099: * @param validate if true indicates that the document is to be validated
100: * against it's schema.
101: * @param handler is the ErrorHandler which is to be delegated XML parsing Error
102: * handling tasks.
103: * @param schemaFiles are the schema files to be used if validation is to be done,
104: * this parameter can be null if validate = false.
105: * @return the XML Document object
106: * @throws SAXException on XML Parsing errors.
107: * @throws ParserConfigurationException if the parser is not configured correctly.
108: * @throws java.io.IOException on IO Errors.
109: */
110: public Document load(InputStream istr, boolean validate,
111: org.xml.sax.ErrorHandler handler, String[] schemaFiles)
112: throws java.io.IOException, SAXException,
113: ParserConfigurationException {
114:
115: return load(new InputSource(istr), validate, handler,
116: schemaFiles);
117: };
118:
119: /**
120: * Given a Inputstream load an XML document into
121: * a Document object and return it.
122: *
123: * @param reader is a reader to the input XML file
124: * @param validate if true indicates that the document is to be validated
125: * against it's schema.
126: * @param handler is the ErrorHandler which is to be delegated XML parsing Error
127: * handling tasks.
128: * @param schemaFiles are the schema file to be used if validation is to be done,
129: * this parameter can be null if validate = false.
130: * @return the XML Document object
131: * @throws SAXException on XML Parsing errors.
132: * @throws ParserConfigurationException if the parser is not configured correctly.
133: * @throws java.io.IOException on IO Errors.
134: */
135: public Document load(Reader reader, boolean validate,
136: org.xml.sax.ErrorHandler handler, String[] schemaFiles)
137: throws java.io.IOException, SAXException,
138: ParserConfigurationException {
139:
140: return load(new InputSource(reader), validate, handler,
141: schemaFiles);
142: };
143:
144: /**
145: * Given a Inputstream load an XML document into
146: * a Document object and return it.
147: *
148: * @param ipsrc is a SAX InputSource to the XML file
149: * @param validate if true indicates that the document is to be validated
150: * against it's schema.
151: * @param handler is the ErrorHandler which is to be delegated XML parsing Error
152: * handling tasks.
153: * @param schemaFiles are the schema file to be used if validation is to be done,
154: * this parameter can be null if validate = false.
155: * @return the XML Document object
156: * @throws SAXException on XML Parsing errors.
157: * @throws ParserConfigurationException if the parser is not configured correctly.
158: * @throws java.io.IOException on IO Errors.
159: */
160: public Document load(InputSource ipsrc, boolean validate,
161: org.xml.sax.ErrorHandler handler, String[] schemaFiles)
162: throws java.io.IOException, SAXException,
163: ParserConfigurationException {
164:
165: DocumentBuilderFactory docFactory = DocumentBuilderFactory
166: .newInstance();
167: docFactory.setValidating(validate);
168: docFactory.setNamespaceAware(true);
169:
170: if (validate) {
171: docFactory.setAttribute(JAXP_SCHEMA_LANGUAGE,
172: W3C_XML_SCHEMA);
173: docFactory.setAttribute(JAXP_SCHEMA_SOURCE, schemaFiles);
174: }
175:
176: DocumentBuilder bldr = docFactory.newDocumentBuilder();
177: if (handler != null) {
178: bldr.setErrorHandler(handler);
179: }
180:
181: return bldr.parse(ipsrc);
182: };
183: }
|