001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml.util;
021:
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.apache.log4j.Logger;
026:
027: /**
028: * @author mleidig@schlund.de
029: */
030: public class XsltProvider {
031:
032: final static Logger LOG = Logger.getLogger(XsltProvider.class);
033:
034: final static String DETECT_SAXON1 = "com.icl.saxon.TransformerFactoryImpl";
035: final static String DETECT_SAXON2 = "net.sf.saxon.TransformerFactoryImpl";
036:
037: final static String XPATH_SAXON1 = "de.schlund.pfixxml.util.xsltimpl.XPathSaxon1";
038: final static String XPATH_SAXON2 = "de.schlund.pfixxml.util.xsltimpl.XPathSaxon2";
039: final static String XML_SAXON1 = "de.schlund.pfixxml.util.xsltimpl.XmlSaxon1";
040: final static String XML_SAXON2 = "de.schlund.pfixxml.util.xsltimpl.XmlSaxon2";
041: final static String XSLT_SAXON1 = "de.schlund.pfixxml.util.xsltimpl.XsltSaxon1";
042: final static String XSLT_SAXON2 = "de.schlund.pfixxml.util.xsltimpl.XsltSaxon2";
043:
044: static Map<XsltVersion, XmlSupport> xmlSupport = new HashMap<XsltVersion, XmlSupport>();
045: static Map<XsltVersion, XPathSupport> xpathSupport = new HashMap<XsltVersion, XPathSupport>();
046: static Map<XsltVersion, XsltSupport> xsltSupport = new HashMap<XsltVersion, XsltSupport>();
047:
048: static XsltVersion preferredXsltVersion = XsltVersion.XSLT1;
049:
050: static {
051: boolean saxon1Available = false;
052: try {
053: Class.forName(DETECT_SAXON1);
054: saxon1Available = true;
055: } catch (Exception x) {
056: LOG.warn("No Saxon XSLT1 implementation found!");
057: }
058: boolean saxon2Available = false;
059: try {
060: Class.forName(DETECT_SAXON2);
061: saxon2Available = true;
062: } catch (Exception x) {
063: LOG.warn("No Saxon XSLT2 implementation found!");
064: }
065: if (saxon1Available) {
066: try {
067: Class<? extends XmlSupport> clazz = Class.forName(
068: XML_SAXON1).asSubclass(XmlSupport.class);
069: xmlSupport.put(XsltVersion.XSLT1, clazz.newInstance());
070: } catch (Exception x) {
071: LOG.error("Can't initialize XmlSupport: " + XML_SAXON1,
072: x);
073: }
074: try {
075: Class<? extends XPathSupport> clazz = Class.forName(
076: XPATH_SAXON1).asSubclass(XPathSupport.class);
077: xpathSupport
078: .put(XsltVersion.XSLT1, clazz.newInstance());
079: } catch (Exception x) {
080: LOG.error("Can't initialize XPathSupport: "
081: + XPATH_SAXON1, x);
082: }
083: try {
084: Class<? extends XsltSupport> clazz = Class.forName(
085: XSLT_SAXON1).asSubclass(XsltSupport.class);
086: xsltSupport.put(XsltVersion.XSLT1, clazz.newInstance());
087: } catch (Exception x) {
088: LOG.error("Can't initialize XsltSupport: "
089: + XSLT_SAXON1, x);
090: }
091: }
092: if (saxon2Available) {
093: try {
094: Class<? extends XmlSupport> clazz = Class.forName(
095: XML_SAXON2).asSubclass(XmlSupport.class);
096: xmlSupport.put(XsltVersion.XSLT2, clazz.newInstance());
097: } catch (Exception x) {
098: LOG.error("Can't initialize XmlSupport: " + XML_SAXON2,
099: x);
100: }
101: try {
102: Class<? extends XPathSupport> clazz = Class.forName(
103: XPATH_SAXON2).asSubclass(XPathSupport.class);
104: xpathSupport
105: .put(XsltVersion.XSLT2, clazz.newInstance());
106: } catch (Exception x) {
107: LOG.error("Can't initialize XPathSupport: "
108: + XPATH_SAXON2, x);
109: }
110: try {
111: Class<? extends XsltSupport> clazz = Class.forName(
112: XSLT_SAXON2).asSubclass(XsltSupport.class);
113: xsltSupport.put(XsltVersion.XSLT2, clazz.newInstance());
114: } catch (Exception x) {
115: LOG.error("Can't initialize XsltSupport: "
116: + XSLT_SAXON2, x);
117: }
118: }
119: }
120:
121: public static Map<XsltVersion, XmlSupport> getXmlSupport() {
122: return xmlSupport;
123: }
124:
125: public static XmlSupport getXmlSupport(XsltVersion version) {
126: return xmlSupport.get(version);
127: }
128:
129: public static Map<XsltVersion, XPathSupport> getXpathSupport() {
130: return xpathSupport;
131: }
132:
133: public static XPathSupport getXPathSupport(XsltVersion version) {
134: return xpathSupport.get(version);
135: }
136:
137: public static Map<XsltVersion, XsltSupport> getXsltSupport() {
138: return xsltSupport;
139: }
140:
141: public static XsltSupport getXsltSupport(XsltVersion version) {
142: return xsltSupport.get(version);
143: }
144:
145: public static XsltVersion getPreferredXsltVersion() {
146: if (getXmlSupport().containsKey(XsltVersion.XSLT1))
147: return XsltVersion.XSLT1;
148: return XsltVersion.XSLT2;
149: }
150:
151: }
|