Example usage for javax.xml.transform.dom DOMResult DOMResult

List of usage examples for javax.xml.transform.dom DOMResult DOMResult

Introduction

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

Prototype

public DOMResult(Node node) 

Source Link

Document

Use a DOM node to create a new output target.

Usage

From source file:org.wso2.carbon.wsdl2form.Util.java

public static DOMSource getSigStream(AxisService service, ByteArrayOutputStream wsdlOutStream, Map paramMap)
        throws TransformerFactoryConfigurationError, TransformerException, ParserConfigurationException {
    Source wsdlSource = new StreamSource(new ByteArrayInputStream(wsdlOutStream.toByteArray()));
    InputStream sigStream = Util.class.getClassLoader().getResourceAsStream(WSDL2SIG_XSL_LOCATION);
    Source wsdl2sigXSLTSource = new StreamSource(sigStream);
    DocumentBuilder docB = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document docSig = docB.newDocument();
    Result resultSig = new DOMResult(docSig);
    Util.transform(wsdlSource, wsdl2sigXSLTSource, resultSig, paramMap, new SchemaURIResolver(service));
    return new DOMSource(docSig);
}

From source file:org.wso2.codegen.service.ajax.DynamicCodeGeneratorService.java

public String generate(String serviceName) throws AxisFault {

    MessageContext currentMessageContext = MessageContext.getCurrentMessageContext();
    AxisConfiguration axisConfiguration = currentMessageContext.getConfigurationContext()
            .getAxisConfiguration();/*from w w  w .ja v a 2s.  c o  m*/
    AxisService service = axisConfiguration.getService(serviceName);
    if (service == null) {
        String message = "Service " + serviceName + " cannot be found.";
        log.error(message);
        throw new AxisFault(message);
    }
    Parameter enableMTOM = service.getParameter(Constants.Configuration.ENABLE_MTOM);
    if (enableMTOM != null && enableMTOM.getValue().equals("true")) {
        throw new AxisFault("Try It feature is not available for MTOM enabled services");
    }

    Object parameterValue = service.getParameterValue("WSDLSupplier");
    if (parameterValue != null) {
        String message = "RPC/ENC WSDL found. Axis2 does not support RPC/ENC encoding scheme";
        log.warn(message);
        throw new AxisFault(message);
    }

    String uuidTryit;
    WSDLMetaDataFactory wsdlMetaDataFactory = WSDLMetaDataFactory.getFactory("2.0", serviceName,
            MessageContext.getCurrentMessageContext());

    WSDLMetaDataFactory.UniqueOutputDirInfo uniqueOutputDirInfo = wsdlMetaDataFactory
            .generateUniqueCodegenOutputDir();

    uuidTryit = uniqueOutputDirInfo.getUuid();

    String outputDir = wsdlMetaDataFactory.generateUniqueDir("dynamic_codegen", uniqueOutputDirInfo)
            .getOutputDir();

    String wsdlFileURI = wsdlMetaDataFactory.getWSDLFileURI(outputDir);

    //Generating the .sig foramat using wsdl2sig.xslt to intermediate DOM Document. This doc will be
    // used to transformed with tryit.xslt and jsstub.xslt.

    try {
        InputStream wsdl2InStream = new FileInputStream(new File(wsdlFileURI));
        InputStream wsdl2sigXSLTStream = getClass().getClassLoader().getResourceAsStream("wsdl2sig.xslt");

        Source wsdl2Source = new StreamSource(wsdl2InStream);
        Source wsdl2sigXSLTSource = new StreamSource(wsdl2sigXSLTStream);

        DocumentBuilder docB = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = docB.newDocument();

        //do the first transformation
        Result result = new DOMResult(doc);
        Transformer transformer = TransformerFactory.newInstance().newTransformer(wsdl2sigXSLTSource);
        transformer.transform(wsdl2Source, result);

        ///////////////////////////////////////////////////
        //            System.out.println(DOM2Writer.nodeToString(doc));
        ////////////////////////////////////////////////////

        DOMSource xmlSource = new DOMSource(doc);

        Map fileResourcesMap = (Map) MessageContext.getCurrentMessageContext().getConfigurationContext()
                .getProperty(WSO2Constants.FILE_RESOURCE_MAP);

        if (fileResourcesMap == null) {
            fileResourcesMap = new Hashtable();
            MessageContext.getCurrentMessageContext().getConfigurationContext()
                    .setProperty(WSO2Constants.FILE_RESOURCE_MAP, fileResourcesMap);
        }

        String uuidJsstub;
        WSDLMetaDataFactory.UniqueOutputDirInfo jsstubUniqueOutputDirInfo = wsdlMetaDataFactory
                .generateUniqueCodegenOutputDir();
        uuidJsstub = jsstubUniqueOutputDirInfo.getUuid();

        String jstubOutputDir = wsdlMetaDataFactory
                .generateUniqueDir("dynamic_codegen", jsstubUniqueOutputDirInfo).getOutputDir();
        File jstubOutputDirFile = new File(jstubOutputDir);
        jstubOutputDirFile.mkdirs();
        File jsStubFle = new File(jstubOutputDir, serviceName + ".stub.js");

        OutputStream jsStubOutputStream = new FileOutputStream(jsStubFle);
        Result jsStubResult = new StreamResult(jsStubOutputStream);
        InputStream jsStubXSLTStream = getClass().getClassLoader().getResourceAsStream("jsstub.xslt");
        Source jsStubXSLSource = new StreamSource(jsStubXSLTStream);
        transformer = TransformerFactory.newInstance().newTransformer(jsStubXSLSource);
        transformer.transform(xmlSource, jsStubResult);

        FileFilter jsStubFileFilter = new JSStubFilter();
        String absolutePath = null;
        File[] files0 = jstubOutputDirFile.listFiles(jsStubFileFilter);
        if ((files0 != null) && (files0[0] != null) && (files0[0].getAbsoluteFile() != null)) {
            absolutePath = files0[0].getAbsoluteFile().getAbsolutePath();
            fileResourcesMap.put(uuidJsstub, absolutePath);
        }

        if (absolutePath == null) {
            throw new AxisFault(DynamicCodeGeneratorService.class.getName() + " cannot continue"
                    + " since an instance of jsstub.xslt is not available");
        }

        InputStream tryItXSLTStream = getClass().getClassLoader().getResourceAsStream("tryit.xslt");
        Source tryItXSLSource = new StreamSource(tryItXSLTStream);
        File tryItOutFile = new File(outputDir, serviceName + ".html");
        OutputStream tryItOutFileStream = new FileOutputStream(tryItOutFile);
        Result tryItResult = new StreamResult(tryItOutFileStream);
        transformer = TransformerFactory.newInstance().newTransformer(tryItXSLSource);
        transformer.setParameter("wsrequest-location", "js/WSRequest.js");
        transformer.setParameter("stub-location", "filedownload?id=" + uuidJsstub);

        String injectValue = "<div>"
                + "<h4><a href=\"#\" onClick=\"javascript:top.showServiceInitializer(); return false;\">Services</a>&nbsp;&gt;&nbsp;"
                + "<a href=\"#\" onClick=\"javascript:top.listServiceData('" + serviceName
                + "'); return false;\">" + serviceName + "</a>&nbsp;&gt;&nbsp;" + "Try Web Service" + "</h4>"
                + "</div>";

        transformer.setParameter("show-alternate", "false");
        transformer.setParameter("breadcrumbs", injectValue);
        transformer.transform(xmlSource, tryItResult);

        FileFilter tryItFileFilter = new TryItFilter();
        File[] files1 = new File(outputDir).listFiles(tryItFileFilter);
        if ((files1 != null) && (files1[0] != null) && (files1[0].getAbsoluteFile() != null)) {
            fileResourcesMap.put(uuidTryit, files1[0].getAbsoluteFile().getAbsolutePath());
        }
        log.debug("Try it page has been generated for the service: " + serviceName);
        return "filedownload" + "?id=" + uuidTryit;
    } catch (Exception e) {
        log.error(e);
        throw AxisFault.makeFault(e);
    }

}

From source file:org.wso2.validator.Util.java

/**
 * This method will do the transformation based on xml streams. First an intermediate structure
 * will be created using validator xsl. Then the intermediate structure will be transformed via
 * formatter xsl, which will result in an html document. Location of this html docuement will
 * be available via an return id, which can be obtained from http GET interface. The download of
 * this  file should be available with [context root]/filedownload context path.
 * ex: http://localhost:8080/wso2wsas/filedownload?id=1.234879899E3
 *
 * @param xmlStream            xml stream
 * @param inFilePath           input file
 * @param xslValidatorLocation xsl validator locatoin
 * @param xslFormatterLocation xsl formatter location
 * @param classLoader          classloader that should be used.
 * @return String ; id for request the transform object via http GET
 * @throws AxisFault will be thrown/*from   www .j  av  a 2s  .com*/
 */
public static String doTransformation(InputStream xmlStream, String inFilePath, String xslValidatorLocation,
        String xslFormatterLocation, ClassLoader classLoader) throws AxisFault {
    String uuid = String.valueOf(System.currentTimeMillis() + Math.random());
    try {
        InputStream xslStream = classLoader.getResourceAsStream(xslValidatorLocation);
        InputStream xslForamtterStream = classLoader.getResourceAsStream(xslFormatterLocation);
        Source xmlSource = new StreamSource(xmlStream);
        Source xslSource = new StreamSource(xslStream);
        Source xslForamtterSource = new StreamSource(xslForamtterStream);
        String fileName = uuid + ".html";
        File outFile = getOutputFilePath(inFilePath, fileName);
        DocumentBuilder docB = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = docB.newDocument();
        //do the first transformation
        Result result = new DOMResult(doc);
        Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
        transformer.transform(xmlSource, result);

        ///////////////////////////////////////////////////
        //            System.out.println(DOM2Writer.nodeToString(doc));
        ////////////////////////////////////////////////////

        //the second transformation
        OutputStream outFileStream = new FileOutputStream(outFile);
        Result finalResult = new StreamResult(outFileStream);
        transformer = TransformerFactory.newInstance().newTransformer(xslForamtterSource);
        //setting the FileoutputPath to ConfigurationContext
        transformer.transform(new DOMSource(doc), finalResult);

        ConfigurationContext configCtx = MessageContext.getCurrentMessageContext().getConfigurationContext();
        Map fileResourcesMap = (Map) configCtx.getProperty(WSO2Constants.FILE_RESOURCE_MAP);
        if (fileResourcesMap == null) {
            fileResourcesMap = new Hashtable();
            configCtx.setProperty(WSO2Constants.FILE_RESOURCE_MAP, fileResourcesMap);
        }
        fileResourcesMap.put(uuid, outFile.getAbsolutePath());
    } catch (Exception e) {
        String msg = "Error has been encounted while doing the transformation.";
        log.error(msg, e);
        throw new AxisFault(msg, e);
    }
    return WSO2Constants.ContextPaths.DOWNLOAD_PATH + "?id=" + uuid;

}

From source file:org.xchain.namespaces.sax.ResultCommand.java

/**
 * <p>Returns the result for the select attribute.</p>
 * @param context the JXPathContext to evaluate against.
 * @return the correct result object for the type of object selected from the context.
 *///from   w ww.j  a  v a  2  s . c  o m
public Result createResultForSelect(JXPathContext context) throws Exception {
    Object object = getSelect(context);

    if (object == null) {
        throw new IllegalArgumentException("The selected object cannot be null.");
    }
    // if the object is a result, then use it.
    else if (object instanceof Result) {
        return (Result) object;
    }

    // if the object is a stream, then create a stream source.
    else if (object instanceof OutputStream) {
        return new StreamResult((OutputStream) object);
    } else if (object instanceof Writer) {
        return new StreamResult((Writer) object);
    } else if (object instanceof File) {
        return new StreamResult((File) object);
    }

    // if the object is a content handler, then create a sax result.
    else if (object instanceof ContentHandler) {
        return new SAXResult((ContentHandler) object);
    }

    // if the result is a node, then create a dom result.
    else if (object instanceof Node) {
        return new DOMResult((Node) object);
    }

    // we do not how to make a result for this object, so bail out.
    else {
        throw new IllegalArgumentException("The selected result object (" + object.getClass().getName()
                + ") is not a result object nor is it an output stream.");
    }
}