001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * XMLFactoryUtil.java
044: *
045: * Created on October 12, 2001, 5:08 PM
046: */
047:
048: package org.netbeans.xtest.util;
049:
050: import java.util.Properties;
051: import javax.xml.parsers.DocumentBuilder;
052: import javax.xml.parsers.DocumentBuilderFactory;
053: import javax.xml.parsers.ParserConfigurationException;
054: import javax.xml.transform.ErrorListener;
055: import javax.xml.transform.Result;
056: import javax.xml.transform.Source;
057: import javax.xml.transform.Transformer;
058: import javax.xml.transform.TransformerConfigurationException;
059: import javax.xml.transform.TransformerException;
060: import javax.xml.transform.URIResolver;
061: import javax.xml.transform.stream.StreamSource;
062: import javax.xml.transform.TransformerFactory;
063:
064: /**
065: *
066: * @author mb115822
067: */
068: public class XMLFactoryUtil {
069:
070: private static final String[] names = new String[] {};
071: //{"javax.xml.parsers.DocumentBuilderFactory",
072: //"javax.xml.parsers.SAXParserFactory",
073: //"org.apache.xerces.xni.parser.XMLParserConfiguration",
074: //"org.xml.sax.driver",
075: //"javax.xml.transform.TransformerFactory"};
076: private static final String[] values = new String[] {};
077:
078: //{"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl",
079: //"org.apache.xerces.jaxp.SAXParserFactoryImpl",
080: //"org.apache.xerces.parsers.StandardParserConfiguration",
081: //"org.apache.xerces.parsers.SAXParser",
082: //"org.apache.xalan.processor.TransformerFactoryImpl"};
083:
084: private static String[] setNewProperties() {
085:
086: String oldValues[] = new String[names.length];
087: for (int i = 0; i < names.length; i++)
088: oldValues[i] = System.setProperty(names[i], values[i]);
089:
090: return oldValues;
091:
092: }
093:
094: private static void setOriginalProperties(String[] oldValues) {
095: for (int i = 0; i < names.length; i++)
096: if (oldValues[i] == null)
097: System.getProperties().remove(names[i]);
098: else
099: System.setProperty(names[i], oldValues[i]);
100:
101: }
102:
103: public static DocumentBuilder newDocumentBuilder()
104: throws ParserConfigurationException {
105: String[] oldProperties = setNewProperties();
106: try {
107: return DocumentBuilderFactory.newInstance()
108: .newDocumentBuilder();
109: } finally {
110: setOriginalProperties(oldProperties);
111: }
112: }
113:
114: public static Transformer newTransformer()
115: throws TransformerConfigurationException {
116: String[] oldProperties = setNewProperties();
117: try {
118: return (TransformerFactory.newInstance()).newTransformer();
119: } finally {
120: setOriginalProperties(oldProperties);
121: }
122: }
123:
124: public static Transformer newTransformer(StreamSource xsltSource)
125: throws TransformerConfigurationException {
126: String[] oldProperties = setNewProperties();
127: try {
128: return (TransformerFactory.newInstance())
129: .newTransformer(xsltSource);
130: } finally {
131: setOriginalProperties(oldProperties);
132: }
133: }
134:
135: }
|