Search in sources :

Example 36 with TransformerException

use of javax.xml.transform.TransformerException in project spring-framework by spring-projects.

the class JibxMarshaller method transformAndMarshal.

private void transformAndMarshal(Object graph, Result result) throws IOException {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        marshalOutputStream(graph, os);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        Transformer transformer = this.transformerFactory.newTransformer();
        transformer.transform(new StreamSource(is), result);
    } catch (TransformerException ex) {
        throw new MarshallingFailureException("Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) MarshallingFailureException(org.springframework.oxm.MarshallingFailureException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformerException(javax.xml.transform.TransformerException)

Example 37 with TransformerException

use of javax.xml.transform.TransformerException in project robovm by robovm.

the class Compiler method compileFunction.

/**
   * Compile a built-in XPath function.
   * 
   * @param opPos The current position in the m_opMap array.
   *
   * @return reference to {@link org.apache.xpath.functions.Function} instance.
   *
   * @throws TransformerException if a error occurs creating the Expression.
   */
Expression compileFunction(int opPos) throws TransformerException {
    int endFunc = opPos + getOp(opPos + 1) - 1;
    opPos = getFirstChildPos(opPos);
    int funcID = getOp(opPos);
    opPos++;
    if (-1 != funcID) {
        Function func = m_functionTable.getFunction(funcID);
        if (func instanceof FuncExtFunctionAvailable)
            ((FuncExtFunctionAvailable) func).setFunctionTable(m_functionTable);
        func.postCompileStep(this);
        try {
            int i = 0;
            for (int p = opPos; p < endFunc; p = getNextOpPos(p), i++) {
                // System.out.println("argPos: "+ p);
                // System.out.println("argCode: "+ m_opMap[p]);
                func.setArg(compile(p), i);
            }
            func.checkNumberArgs(i);
        } catch (WrongNumberArgsException wnae) {
            java.lang.String name = m_functionTable.getFunctionName(funcID);
            m_errorHandler.fatalError(new TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ONLY_ALLOWS, new Object[] { name, wnae.getMessage() }), m_locator));
        //"name + " only allows " + wnae.getMessage() + " arguments", m_locator));
        }
        return func;
    } else {
        //"function token not found.");
        error(XPATHErrorResources.ER_FUNCTION_TOKEN_NOT_FOUND, null);
        return null;
    }
}
Also used : FuncExtFunction(org.apache.xpath.functions.FuncExtFunction) Function(org.apache.xpath.functions.Function) WrongNumberArgsException(org.apache.xpath.functions.WrongNumberArgsException) FuncExtFunctionAvailable(org.apache.xpath.functions.FuncExtFunctionAvailable) XString(org.apache.xpath.objects.XString) TransformerException(javax.xml.transform.TransformerException)

Example 38 with TransformerException

use of javax.xml.transform.TransformerException in project robovm by robovm.

the class DOM2Helper method parse.

/**
   * Parse an XML document.
   *
   * <p>Right now the Xerces DOMParser class is used.  This needs
   * fixing, either via jaxp, or via some other, standard method.</p>
   *
   * <p>The application can use this method to instruct the SAX parser
   * to begin parsing an XML document from any valid input
   * source (a character stream, a byte stream, or a URI).</p>
   *
   * <p>Applications may not invoke this method while a parse is in
   * progress (they should create a new Parser instead for each
   * additional XML document).  Once a parse is complete, an
   * application may reuse the same Parser object, possibly with a
   * different input source.</p>
   *
   * @param source The input source for the top-level of the
   *        XML document.
   *
   * @throws TransformerException if any checked exception is thrown.
   * @xsl.usage internal
   */
public void parse(InputSource source) throws TransformerException {
    try {
        // I guess I should use JAXP factory here... when it's legal.
        // org.apache.xerces.parsers.DOMParser parser 
        //  = new org.apache.xerces.parsers.DOMParser();
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        builderFactory.setValidating(true);
        DocumentBuilder parser = builderFactory.newDocumentBuilder();
        /*
      // domParser.setFeature("http://apache.org/xml/features/dom/create-entity-ref-nodes", getShouldExpandEntityRefs()? false : true);
      if(m_useDOM2getNamespaceURI)
      {
      parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
      parser.setFeature("http://xml.org/sax/features/namespaces", true);
      }
      else
      {
      parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
      }

      parser.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
      */
        parser.setErrorHandler(new org.apache.xml.utils.DefaultErrorHandler());
        // if(null != m_entityResolver)
        // {
        // System.out.println("Setting the entity resolver.");
        //  parser.setEntityResolver(m_entityResolver);
        // }
        setDocument(parser.parse(source));
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se);
    } catch (ParserConfigurationException pce) {
        throw new TransformerException(pce);
    } catch (IOException ioe) {
        throw new TransformerException(ioe);
    }
// setDocument(((org.apache.xerces.parsers.DOMParser)parser).getDocument());
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException)

Example 39 with TransformerException

use of javax.xml.transform.TransformerException in project bazel by bazelbuild.

the class XmlOutputFormatter method createPostFactoStreamCallback.

@Override
public OutputFormatterCallback<Target> createPostFactoStreamCallback(final OutputStream out, final QueryOptions options) {
    return new OutputFormatterCallback<Target>() {

        private Document doc;

        private Element queryElem;

        @Override
        public void start() {
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                doc = factory.newDocumentBuilder().newDocument();
            } catch (ParserConfigurationException e) {
                // This shouldn't be possible: all the configuration is hard-coded.
                throw new IllegalStateException("XML output failed", e);
            }
            doc.setXmlVersion("1.1");
            queryElem = doc.createElement("query");
            queryElem.setAttribute("version", "2");
            doc.appendChild(queryElem);
        }

        @Override
        public void processOutput(Iterable<Target> partialResult) throws IOException, InterruptedException {
            for (Target target : partialResult) {
                queryElem.appendChild(createTargetElement(doc, target));
            }
        }

        @Override
        public void close(boolean failFast) throws IOException {
            if (!failFast) {
                try {
                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                    transformer.transform(new DOMSource(doc), new StreamResult(out));
                } catch (TransformerFactoryConfigurationError | TransformerException e) {
                    // This shouldn't be possible: all the configuration is hard-coded.
                    throw new IllegalStateException("XML output failed", e);
                }
            }
        }
    };
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) SynchronizedDelegatingOutputFormatterCallback(com.google.devtools.build.lib.query2.engine.SynchronizedDelegatingOutputFormatterCallback) ThreadSafeOutputFormatterCallback(com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback) OutputFormatterCallback(com.google.devtools.build.lib.query2.engine.OutputFormatterCallback) Document(org.w3c.dom.Document) FakeSubincludeTarget(com.google.devtools.build.lib.query2.FakeSubincludeTarget) Target(com.google.devtools.build.lib.packages.Target) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 40 with TransformerException

use of javax.xml.transform.TransformerException in project robovm by robovm.

the class ExtensionNamespaceSupport method launch.

/**
   * Launch the ExtensionHandler that this ExtensionNamespaceSupport object defines.
   */
public ExtensionHandler launch() throws TransformerException {
    ExtensionHandler handler = null;
    try {
        Class cl = ExtensionHandler.getClassForName(m_handlerClass);
        Constructor con = null;
        //System.out.println("class " + cl + " " + m_args + " " + m_args.length + " " + m_sig);
        if (m_sig != null)
            con = cl.getConstructor(m_sig);
        else // Pick the constructor based on number of args.
        {
            Constructor[] cons = cl.getConstructors();
            for (int i = 0; i < cons.length; i++) {
                if (cons[i].getParameterTypes().length == m_args.length) {
                    con = cons[i];
                    break;
                }
            }
        }
        // System.out.println("constructor " + con);
        if (con != null)
            handler = (ExtensionHandler) con.newInstance(m_args);
        else
            throw new TransformerException("ExtensionHandler constructor not found");
    } catch (Exception e) {
        throw new TransformerException(e);
    }
    return handler;
}
Also used : Constructor(java.lang.reflect.Constructor) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerException (javax.xml.transform.TransformerException)808 Transformer (javax.xml.transform.Transformer)364 StreamResult (javax.xml.transform.stream.StreamResult)362 DOMSource (javax.xml.transform.dom.DOMSource)311 IOException (java.io.IOException)277 TransformerFactory (javax.xml.transform.TransformerFactory)184 Document (org.w3c.dom.Document)161 StringWriter (java.io.StringWriter)159 SAXException (org.xml.sax.SAXException)157 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)156 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)131 Source (javax.xml.transform.Source)100 StreamSource (javax.xml.transform.stream.StreamSource)94 Element (org.w3c.dom.Element)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)83 File (java.io.File)74 Node (org.w3c.dom.Node)65 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)64 ByteArrayOutputStream (java.io.ByteArrayOutputStream)62 StringReader (java.io.StringReader)59