001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: JAXPHelper.java,v 1.2 2007/07/16 16:42:02 ofung Exp $
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: /**
061: *
062: * @author JAX-RPC RI Development Team
063: */package util;
064:
065: import java.io.File;
066:
067: import javax.xml.parsers.DocumentBuilder;
068: import javax.xml.parsers.DocumentBuilderFactory;
069: import javax.xml.transform.*;
070:
071: import org.w3c.dom.Document;
072: import org.xml.sax.SAXParseException;
073:
074: public class JAXPHelper {
075: private static DocumentBuilder builder;
076: private static Transformer transformer;
077:
078: static {
079: DocumentBuilderFactory factory = new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl();
080: try {
081: builder = factory.newDocumentBuilder();
082: } catch (Exception e) {
083: e.printStackTrace();
084: }
085:
086: try {
087: // Create an instance of our own transformer factory impl
088: TransformerFactory transFactory = new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl();
089:
090: // create Transformer
091: transformer = transFactory.newTransformer();
092:
093: } catch (TransformerConfigurationException tce) {
094: // Error generated by the parser
095: System.out.println("* Transformer Factory error");
096: System.out.println(" " + tce.getMessage());
097:
098: // Use the contained exception, if any
099: Throwable x = tce;
100: if (tce.getException() != null)
101: x = tce.getException();
102: x.printStackTrace();
103:
104: }
105: }
106:
107: public static Source readXMLFile(String xmlFileName) {
108:
109: Source domSource = null;
110:
111: try {
112: Document domDoc = builder.parse(new File(xmlFileName));
113: domSource = new javax.xml.transform.dom.DOMSource(domDoc);
114:
115: } catch (SAXParseException spe) {
116: // Error generated by the parser
117: System.out.println("\n** Parsing error" + ", line "
118: + spe.getLineNumber() + ", uri "
119: + spe.getSystemId());
120: System.out.println(" " + spe.getMessage());
121:
122: // Use the contained exception, if any
123: Exception x = spe;
124: if (spe.getException() != null)
125: x = spe.getException();
126: x.printStackTrace();
127: } catch (Exception e) {
128: e.printStackTrace();
129: }
130:
131: return domSource;
132: }
133: }
|