Example usage for javax.xml.transform TransformerConfigurationException TransformerConfigurationException

List of usage examples for javax.xml.transform TransformerConfigurationException TransformerConfigurationException

Introduction

In this page you can find the example usage for javax.xml.transform TransformerConfigurationException TransformerConfigurationException.

Prototype

public TransformerConfigurationException(String message, SourceLocator locator) 

Source Link

Document

Create a new TransformerConfigurationException from a message and a Locator.

Usage

From source file:net.sf.joost.trax.ConfigurationErrListener.java

/**
 * Receive notification of a warning./*www . ja v a 2  s.c o m*/
 * Details {@link ErrorListener#warning}
 */
public void warning(TransformerException tE) throws TransformerConfigurationException {
    if (userErrorListener != null) {
        try {
            userErrorListener.warning(tE);
        } catch (TransformerException e2) {
            if (log != null)
                log.warn(e2);
            if (e2 instanceof TransformerConfigurationException) {
                throw (TransformerConfigurationException) tE;
            } else {
                throw new TransformerConfigurationException(tE.getMessage(), tE);
            }
        }
    } else {
        if (log != null)
            log.warn(tE);
        // no user defined errorlistener, so throw this exception
        if (tE instanceof TransformerConfigurationException) {
            throw (TransformerConfigurationException) tE;
        } else {
            throw new TransformerConfigurationException(tE.getMessage(), tE);
        }
    }
}

From source file:net.sf.joost.trax.ConfigurationErrListener.java

/**
 * Receive notification of a recoverable error.
 * Details {@link ErrorListener#error}// w w  w  .ja va2  s  . c o m
 */
public void error(TransformerException tE) throws TransformerConfigurationException {
    if (userErrorListener != null) {
        try {
            userErrorListener.error(tE);
        } catch (TransformerException e2) {
            if (log != null)
                log.error(e2);
            if (e2 instanceof TransformerConfigurationException) {
                throw (TransformerConfigurationException) tE;
            } else {
                throw new TransformerConfigurationException(tE.getMessage(), tE);
            }
        }
    } else {
        if (log != null)
            log.error(tE);
        // no user defined errorlistener, so throw this exception
        if (tE instanceof TransformerConfigurationException) {
            throw (TransformerConfigurationException) tE;
        } else {
            throw new TransformerConfigurationException(tE.getMessage(), tE);
        }
    }
}

From source file:net.sf.joost.trax.TransformerHandlerImpl.java

/**
 * Setter for {@link #result}/*ww w  .  j  ava2s.co m*/
 * @param result A <code>Result</code>
 * @throws IllegalArgumentException
 */
public void setResult(Result result) throws IllegalArgumentException {

    if (DEBUG)
        log.debug("setting Result - here SAXResult");

    try {
        this.result = result;
        // init saxresult
        init(result);
    } catch (TransformerException e) {
        if (transformer instanceof TransformerImpl) {
            TransformerConfigurationException tE = new TransformerConfigurationException(e.getMessage(), e);
            try {
                transformer.getErrorListener().fatalError(tE);
            } catch (TransformerException innerE) {
                throw new IllegalArgumentException(innerE.getMessage());
            }
        } else {
            if (log != null)
                log.fatal(e);
            throw new IllegalArgumentException("result is invalid.");
        }
    }
}

From source file:net.sf.joost.trax.ConfigurationErrListener.java

/**
 * Receive notification of a non-recoverable error.
 * Details {@link ErrorListener#fatalError}
 *//*from  w w w .  jav  a 2 s. co m*/
public void fatalError(TransformerException tE) throws TransformerConfigurationException {
    if (userErrorListener != null) {
        try {
            userErrorListener.fatalError(tE);
        } catch (TransformerException e2) {
            if (log != null)
                log.fatal(e2);
            if (e2 instanceof TransformerConfigurationException) {
                throw (TransformerConfigurationException) tE;
            } else {
                throw new TransformerConfigurationException(tE.getMessage(), tE);
            }
        }
    } else {
        if (log != null)
            log.fatal(tE);
        // no user defined errorlistener, so throw this exception
        if (tE instanceof TransformerConfigurationException) {
            throw (TransformerConfigurationException) tE;
        } else {
            throw new TransformerConfigurationException(tE.getMessage(), tE);
        }
    }
}

From source file:net.sf.joost.trax.TrAXFilter.java

/**
 * Parses the <code>InputSource</code>
 * @param input A <code>InputSource</code> object.
 * @throws SAXException//  ww  w .j a  v  a 2  s. c  o  m
 * @throws IOException
 */
public void parse(InputSource input) throws SAXException, IOException {

    Transformer transformer = null;
    if (DEBUG) {
        if (log.isDebugEnabled())
            log.debug("parsing InputSource " + input.getSystemId());
    }

    try {
        // get a new Transformer
        transformer = this.templates.newTransformer();
        if (transformer instanceof TransformerImpl) {
            this.processor = ((TransformerImpl) transformer).getStxProcessor();
        } else {
            String msg = "An error is occured, because the given transformer is "
                    + "not an instance of TransformerImpl";
            if (log != null)
                log.fatal(msg);
            else
                System.err.println("Fatal error - " + msg);

        }
        XMLReader parent = this.getParent();

        if (parent == null) {
            parent = XMLReaderFactory.createXMLReader();
            setParent(parent);
        }
        ContentHandler handler = this.getContentHandler();

        if (handler == null) {
            handler = parent.getContentHandler();
        }
        if (handler == null) {
            throw new SAXException("no ContentHandler registered");
        }
        //init StxEmitter
        StxEmitter out = null;

        //SAX specific Implementation
        out = new SAXEmitter(handler);

        if (this.processor != null) {
            this.processor.setContentHandler(out);
            this.processor.setLexicalHandler(out);
        } else {
            throw new SAXException("Joost-Processor is not correct configured.");
        }
        if (parent == null) {
            throw new SAXException("No parent for filter");
        }
        parent.setContentHandler(this.processor);
        parent.setProperty("http://xml.org/sax/properties/lexical-handler", this.processor);
        parent.setEntityResolver(this);
        parent.setDTDHandler(this);
        parent.setErrorHandler(this);
        parent.parse(input);

    } catch (TransformerConfigurationException tE) {
        try {
            configErrListener.fatalError(tE);
        } catch (TransformerConfigurationException innerE) {
            throw new SAXException(innerE.getMessage(), innerE);
        }
    } catch (SAXException sE) {
        try {
            configErrListener.fatalError(new TransformerConfigurationException(sE.getMessage(), sE));
        } catch (TransformerConfigurationException innerE) {
            throw new SAXException(innerE.getMessage(), innerE);
        }
    } catch (IOException iE) {
        try {
            configErrListener.fatalError(new TransformerConfigurationException(iE.getMessage(), iE));
        } catch (TransformerConfigurationException innerE) {
            throw new IOException(innerE.getMessage());
        }
    }
}

From source file:gov.nih.nci.cabig.caaers2adeers.xslt.CustomXsltComponent.java

private void loadResource(XsltBuilder xslt, Resource resource) throws TransformerConfigurationException {
    log.debug("Loading resource by :" + this + " , resource : " + resource);
    try {//  w ww  . j a v  a2s .  c om
        if (resource instanceof UrlResource) {
            // prefer to use file when a file based url
            File file = resource.getFile();
            if (file != null) {
                // check if the file exists and report a better error as the XSLT
                // will just say it cannot compile the stylesheet file
                if (!file.exists()) {
                    throw new FileNotFoundException("File: " + file + " not found.");
                }
                xslt.setTransformerFile(file);
            } else {
                xslt.setTransformerURL(resource.getURL());
            }
        } else {
            // fallback and use input stream
            xslt.setTransformerInputStream(resource.getInputStream());
        }
    } catch (Exception e) {
        // include information about the resource in the caused exception, so its easier for
        // end users to know which resource failed
        throw new TransformerConfigurationException(e.getMessage() + " " + resource.toString(), e);
    }
}

From source file:net.sf.joost.trax.TemplatesImpl.java

/**
 * Configures the <code>Templates</code> - initializing by parsing the
 * stylesheet.//from  w  w w .  j  a v a  2s.c om
 * @param reader The <code>XMLReader</code> for parsing the stylesheet
 * @param isource The <code>InputSource</code> of the stylesheet
 * @throws TransformerConfigurationException When an error occurs while
 *  initializing the <code>Templates</code>.
 */
private void init(XMLReader reader, InputSource isource) throws TransformerConfigurationException {

    if (DEBUG)
        log.debug("init with InputSource " + isource.getSystemId());
    try {
        /**
         * Register ErrorListener from
         * {@link TransformerFactoryImpl#getErrorListener()}
         * if available.
         */
        // check if transformerfactory is in debug mode
        boolean debugmode = ((Boolean) this.factory.getAttribute(DEBUG_FEATURE)).booleanValue();

        ParseContext pContext = new ParseContext();
        pContext.allowExternalFunctions = factory.allowExternalFunctions;
        pContext.setErrorListener(factory.getErrorListener());
        pContext.uriResolver = factory.getURIResolver();
        if (debugmode) {
            if (DEBUG)
                log.info("init transformer in debug mode");
            pContext.parserListener = factory.getParserListenerMgr();
            processor = new DebugProcessor(reader, isource, pContext, factory.getMessageEmitter());
        } else {
            processor = new Processor(reader, isource, pContext);
        }
        processor.setTransformerHandlerResolver(factory.thResolver);
        processor.setOutputURIResolver(factory.outputUriResolver);
    } catch (java.io.IOException iE) {
        if (DEBUG)
            log.debug(iE);
        throw new TransformerConfigurationException(iE.getMessage(), iE);
    } catch (org.xml.sax.SAXException sE) {
        Exception emb = sE.getException();
        if (emb instanceof TransformerConfigurationException)
            throw (TransformerConfigurationException) emb;
        if (DEBUG)
            log.debug(sE);
        throw new TransformerConfigurationException(sE.getMessage(), sE);
    } catch (java.lang.NullPointerException nE) {
        if (DEBUG)
            log.debug(nE);
        nE.printStackTrace(System.err);
        throw new TransformerConfigurationException(
                "could not found value for property javax.xml.parsers.SAXParser ", nE);
    }
}

From source file:net.sf.joost.trax.TrAXHelper.java

/**
* Converts a supplied <code>Source</code> to a <code>SAXSource</code>.
* @param source The supplied input source
* @param errorListener an ErrorListener object
* @return a <code>SAXSource</code>
*///w  w  w.j  a  v  a 2  s  .c  om
public static SAXSource getSAXSource(Source source, ErrorListener errorListener) throws TransformerException {

    if (DEBUG)
        log.debug("getting a SAXSource from a Source");
    //SAXSource
    if (source instanceof SAXSource) {
        if (DEBUG)
            log.debug("source is an instance of SAXSource, so simple return");
        return (SAXSource) source;
    }
    //DOMSource
    if (source instanceof DOMSource) {
        if (DEBUG)
            log.debug("source is an instance of DOMSource");
        InputSource is = new InputSource();
        Node startNode = ((DOMSource) source).getNode();
        Document doc;
        if (startNode instanceof Document) {
            doc = (Document) startNode;
        } else {
            doc = startNode.getOwnerDocument();
        }
        if (DEBUG)
            log.debug("using DOMDriver");
        DOMDriver driver = new DOMDriver();
        driver.setDocument(doc);
        is.setSystemId(source.getSystemId());
        driver.setSystemId(source.getSystemId());
        return new SAXSource(driver, is);
    }
    //StreamSource
    if (source instanceof StreamSource) {
        if (DEBUG)
            log.debug("source is an instance of StreamSource");
        InputSource isource = getInputSourceForStreamSources(source, errorListener);
        return new SAXSource(isource);
    } else {
        String errMsg = "Unknown type of source";
        if (log != null)
            log.error(errMsg);
        IllegalArgumentException iE = new IllegalArgumentException(errMsg);
        TransformerConfigurationException tE = new TransformerConfigurationException(iE.getMessage(), iE);
        if (errorListener != null)
            errorListener.error(tE);
        else
            throw tE;
        return null;
    }
}

From source file:net.sf.joost.trax.TransformerFactoryImpl.java

/**
 * Creates an <code>XMLFilter</code> that uses the given <code>Source</code>
 * as the transformation instructions./*from   ww w .  ja va 2 s  .  c o m*/
 * Implementation of the {@link SAXTransformerFactory}
 * @param src - The Source of the transformation instructions.
 * @return An {@link XMLFilter} object, or <code>null</code> if this feature is not
 *  supported.
 * @throws TransformerConfigurationException
 */
public XMLFilter newXMLFilter(Source src) throws TransformerConfigurationException {

    if (DEBUG)
        if (log.isDebugEnabled())
            log.debug("getting SAXTransformerFactory.FEATURE_XMLFILTER " + "from Source " + src.getSystemId());
    XMLFilter xFilter = null;
    try {
        Templates templates = newTemplates(src);
        //get a XMLReader
        XMLReader parser = Processor.createXMLReader();
        xFilter = newXMLFilter(templates);
        xFilter.setParent(parser);
        return xFilter;
    } catch (SAXException ex) {
        TransformerConfigurationException tE = new TransformerConfigurationException(ex.getMessage(), ex);
        defaultErrorListener.fatalError(tE);
        return null;
    }
}

From source file:net.sf.joost.trax.TransformerFactoryImpl.java

private Class loadClass(String className) throws TransformerConfigurationException {
    try {/*from  w  w  w  .j  a va2s.  c om*/
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader != null) {
            try {
                return loader.loadClass(className);
            } catch (Exception ex) {
                return Class.forName(className);
            }
        } else {
            return Class.forName(className);
        }
    } catch (Exception e) {
        throw new TransformerConfigurationException("Failed to load " + className, e);
    }
}