Search in sources :

Example 1 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project intellij-community by JetBrains.

the class ExportTestResultsAction method getOutputText.

@Nullable
private String getOutputText(ExportTestResultsConfiguration config) throws IOException, TransformerException, SAXException {
    ExportTestResultsConfiguration.ExportFormat exportFormat = config.getExportFormat();
    SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler handler;
    if (exportFormat == ExportTestResultsConfiguration.ExportFormat.Xml) {
        handler = transformerFactory.newTransformerHandler();
        handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
        handler.getTransformer().setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    } else {
        Source xslSource;
        if (config.getExportFormat() == ExportTestResultsConfiguration.ExportFormat.BundledTemplate) {
            URL bundledXsltUrl = getClass().getResource("intellij-export.xsl");
            xslSource = new StreamSource(URLUtil.openStream(bundledXsltUrl));
        } else {
            File xslFile = new File(config.getUserTemplatePath());
            if (!xslFile.isFile()) {
                showBalloon(myRunConfiguration.getProject(), MessageType.ERROR, ExecutionBundle.message("export.test.results.custom.template.not.found", xslFile.getPath()), null);
                return null;
            }
            xslSource = new StreamSource(xslFile);
        }
        handler = transformerFactory.newTransformerHandler(xslSource);
        handler.getTransformer().setParameter("TITLE", ExecutionBundle.message("export.test.results.filename", myRunConfiguration.getName(), myRunConfiguration.getType().getDisplayName()));
    }
    StringWriter w = new StringWriter();
    handler.setResult(new StreamResult(w));
    try {
        TestResultsXmlFormatter.execute(myModel.getRoot(), myRunConfiguration, myModel.getProperties(), handler);
    } catch (ProcessCanceledException e) {
        return null;
    }
    return w.toString();
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) URL(java.net.URL) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project adempiere by adempiere.

the class PackOut method doIt.

//	prepare
/**
	 * 	Start the transformation to XML
	 *	@return info
	 *	@throws Exception
	 */
protected String doIt() throws java.lang.Exception {
    initContext();
    OutputStream packageDocStream = null;
    OutputStream packOutDocStream = null;
    log.info("doIt - AD_PACKAGE_EXP_ID=" + p_PackOut_ID);
    if (p_PackOut_ID == 0)
        throw new IllegalArgumentException("No Record");
    String sql1 = "SELECT * FROM AD_Package_Exp WHERE AD_Package_Exp_ID = " + p_PackOut_ID;
    PreparedStatement pstmt1 = null;
    pstmt1 = DB.prepareStatement(sql1, get_TrxName());
    try {
        ResultSet rs1 = pstmt1.executeQuery();
        while (rs1.next()) {
            //Create the package documentation
            packagedir = rs1.getString(X_AD_Package_Exp.COLUMNNAME_File_Directory).trim();
            if (!packagedir.endsWith("/") && !packagedir.endsWith("\\"))
                packagedir += File.separator;
            packagename = packagedir + rs1.getString(X_AD_Package_Exp.COLUMNNAME_Name);
            includesdir = rs1.getString(X_AD_Package_Exp.COLUMNNAME_Name) + File.separator + "**";
            boolean success = (new File(packagename + File.separator + "doc" + File.separator)).mkdirs();
            String file_document = packagename + File.separator + "doc" + File.separator + rs1.getString(X_AD_Package_Exp.COLUMNNAME_Name) + "Doc.xml";
            packageDocStream = new FileOutputStream(file_document, false);
            StreamResult streamResult_document = new StreamResult(new OutputStreamWriter(packageDocStream, "ISO-8859-1"));
            SAXTransformerFactory tf_document = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
            tf_document.setAttribute("indent-number", new Integer(4));
            TransformerHandler packageDocument = tf_document.newTransformerHandler();
            Transformer serializer_document = packageDocument.getTransformer();
            serializer_document.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
            serializer_document.setOutputProperty(OutputKeys.INDENT, "yes");
            packageDocument.setResult(streamResult_document);
            packageDocument.startDocument();
            AttributesImpl atts = new AttributesImpl();
            atts.clear();
            packageDocument.processingInstruction("xml-stylesheet", "type=\"text/css\" href=\"adempiereDocument.css\"");
            packageDocument.startElement("", "", "adempiereDocument", atts);
            packageDocument.startElement("", "", "header", atts);
            packageDocument.characters((rs1.getString(X_AD_Package_Exp.COLUMNNAME_Name) + " Package Description").toCharArray(), 0, (rs1.getString(X_AD_Package_Exp.COLUMNNAME_Name) + " Package Description").length());
            packageDocument.endElement("", "", "header");
            packageDocument.startElement("", "", "H1", atts);
            packageDocument.characters(("Package Name:").toCharArray(), 0, ("Package Name:").length());
            packageDocument.endElement("", "", "H1");
            packageDocument.startElement("", "", "packagename", atts);
            packageDocument.characters(rs1.getString(X_AD_Package_Exp.COLUMNNAME_Name).toCharArray(), 0, rs1.getString(X_AD_Package_Exp.COLUMNNAME_Name).length());
            packageDocument.endElement("", "", "packagename");
            packageDocument.startElement("", "", "H1", atts);
            packageDocument.characters(("Creator:").toCharArray(), 0, ("Creator:").length());
            packageDocument.endElement("", "", "H1");
            packageDocument.startElement("", "", "creator", atts);
            packageDocument.characters(rs1.getString(X_AD_Package_Exp.COLUMNNAME_UserName).toCharArray(), 0, rs1.getString(X_AD_Package_Exp.COLUMNNAME_UserName).length());
            packageDocument.endElement("", "", "creator");
            packageDocument.startElement("", "", "H1", atts);
            packageDocument.characters(("Email Address:").toCharArray(), 0, ("Email Address:").length());
            packageDocument.endElement("", "", "H1");
            packageDocument.startElement("", "", "creatorcontact", atts);
            packageDocument.characters(rs1.getString(X_AD_Package_Exp.COLUMNNAME_EMail).toCharArray(), 0, rs1.getString(X_AD_Package_Exp.COLUMNNAME_EMail).length());
            packageDocument.endElement("", "", "creatorcontact");
            packageDocument.startElement("", "", "H1", atts);
            packageDocument.characters(("Created:").toCharArray(), 0, ("Created:").length());
            packageDocument.endElement("", "", "H1");
            packageDocument.startElement("", "", "createddate", atts);
            packageDocument.characters(rs1.getString("Created").toString().toCharArray(), 0, rs1.getString("Created").toString().length());
            packageDocument.endElement("", "", "createddate");
            packageDocument.startElement("", "", "H1", atts);
            packageDocument.characters(("Updated:").toCharArray(), 0, ("Updated:").length());
            packageDocument.endElement("", "", "H1");
            packageDocument.startElement("", "", "updateddate", atts);
            packageDocument.characters(rs1.getString("Updated").toString().toCharArray(), 0, rs1.getString("Updated".toString()).length());
            packageDocument.endElement("", "", "updateddate");
            packageDocument.startElement("", "", "H1", atts);
            packageDocument.characters(("Description:").toCharArray(), 0, ("Description:").length());
            packageDocument.endElement("", "", "H1");
            packageDocument.startElement("", "", "description", atts);
            packageDocument.characters(rs1.getString(X_AD_Package_Exp.COLUMNNAME_Description).toCharArray(), 0, rs1.getString(X_AD_Package_Exp.COLUMNNAME_Description).length());
            packageDocument.endElement("", "", "description");
            packageDocument.startElement("", "", "H1", atts);
            packageDocument.characters(("Instructions:").toCharArray(), 0, ("Instructions:").length());
            packageDocument.endElement("", "", "H1");
            packageDocument.startElement("", "", "instructions", atts);
            packageDocument.characters(rs1.getString(X_AD_Package_Exp.COLUMNNAME_Instructions).toCharArray(), 0, rs1.getString(X_AD_Package_Exp.COLUMNNAME_Instructions).length());
            packageDocument.endElement("", "", "instructions");
            packageDocument.startElement("", "", "H1", atts);
            packageDocument.characters(("Files in Package:").toCharArray(), 0, ("Files in Package:").length());
            packageDocument.endElement("", "", "H1");
            packageDocument.startElement("", "", "file", atts);
            packageDocument.characters(("File: PackOut.xml").toCharArray(), 0, ("File: PackOut.xml").length());
            packageDocument.endElement("", "", "file");
            packageDocument.startElement("", "", "filedirectory", atts);
            packageDocument.characters("Directory: \\dict\\".toCharArray(), 0, ("Directory: \\dict\\").length());
            packageDocument.endElement("", "", "filedirectory");
            packageDocument.startElement("", "", "filenotes", atts);
            packageDocument.characters("Notes: Contains all application/object settings for package".toCharArray(), 0, "Notes: Contains all application/object settings for package".length());
            packageDocument.endElement("", "", "filenotes");
            success = (new File(packagename + File.separator + "dict" + File.separator)).mkdirs();
            String file_menu = packagename + File.separator + "dict" + File.separator + "PackOut.xml";
            packOutDocStream = new FileOutputStream(file_menu, false);
            StreamResult streamResult_menu = new StreamResult(new OutputStreamWriter(packOutDocStream, "ISO-8859-1"));
            SAXTransformerFactory tf_menu = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
            tf_menu.setAttribute("indent-number", new Integer(4));
            TransformerHandler packOutDocument = tf_menu.newTransformerHandler();
            Transformer serializer_menu = packOutDocument.getTransformer();
            serializer_menu.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
            serializer_menu.setOutputProperty(OutputKeys.INDENT, "yes");
            packOutDocument.setResult(streamResult_menu);
            packOutDocument.startDocument();
            atts.clear();
            atts.addAttribute("", "", "Name", "CDATA", rs1.getString(X_AD_Package_Exp.COLUMNNAME_Name));
            atts.addAttribute("", "", "Version", "CDATA", rs1.getString(X_AD_Package_Exp.COLUMNNAME_PK_Version));
            atts.addAttribute("", "", "CompVer", "CDATA", rs1.getString(X_AD_Package_Exp.COLUMNNAME_ReleaseNo));
            atts.addAttribute("", "", "DataBase", "CDATA", rs1.getString(X_AD_Package_Exp.COLUMNNAME_Version));
            atts.addAttribute("", "", "Description", "CDATA", rs1.getString(X_AD_Package_Exp.COLUMNNAME_Description));
            atts.addAttribute("", "", "creator", "CDATA", rs1.getString(X_AD_Package_Exp.COLUMNNAME_UserName));
            atts.addAttribute("", "", "creatorcontact", "CDATA", rs1.getString(X_AD_Package_Exp.COLUMNNAME_EMail));
            atts.addAttribute("", "", "createddate", "CDATA", rs1.getString("Created"));
            atts.addAttribute("", "", "updateddate", "CDATA", rs1.getString("Updated"));
            atts.addAttribute("", "", "PackOutVer", "CDATA", PackOutVer);
            packOutDocument.startElement("", "", "adempiereAD", atts);
            atts.clear();
            final String sql = "SELECT * FROM AD_Package_Exp_Detail WHERE AD_Package_Exp_ID = " + p_PackOut_ID + " AND IsActive='Y' ORDER BY Line ASC";
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            try {
                pstmt = DB.prepareStatement(sql, get_TrxName());
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    final String Type = rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Type);
                    final int AD_EntityType_ID = rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_EntityType_ID);
                    Env.setContext(getCtx(), X_AD_Package_Exp_Detail.COLUMNNAME_AD_EntityType_ID, AD_EntityType_ID);
                    //
                    log.info(rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Line));
                    if (Type.compareTo("M") == 0) {
                        createMenu(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Menu_ID), packOutDocument);
                    } else if (Type.compareTo("P") == 0)
                        createProcess(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Process_ID), packOutDocument);
                    else if (Type.compareTo("R") == 0)
                        createReportview(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_ReportView_ID), packOutDocument);
                    else if (Type.compareTo("D") == 0)
                        createData(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Table_ID), rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_SQLStatement), packOutDocument);
                    else if (Type.compareTo("T") == 0)
                        createTable(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Table_ID), packOutDocument);
                    else if (Type.compareTo("X") == 0)
                        createForm(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Form_ID), packOutDocument);
                    else if (Type.compareTo("W") == 0)
                        createWindow(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Window_ID), packOutDocument);
                    else if (Type.compareTo("SV") == 0)
                        createView(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_View_ID), packOutDocument);
                    else if (Type.compareTo("SB") == 0)
                        createBrowse(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Browse_ID), packOutDocument);
                    else if (Type.compareTo("S") == 0)
                        createRoles(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Role_ID), packOutDocument);
                    else if (Type.compareTo("SQL") == 0)
                        createSQL(rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_SQLStatement), rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_DBType), packOutDocument);
                    else if (Type.compareTo("IMP") == 0)
                        createImpFormat(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_ImpFormat_ID), packOutDocument);
                    else if (Type.compareTo("REF") == 0)
                        createReference(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Reference_ID), packOutDocument);
                    else if (Type.compareTo("SNI") == 0)
                        createSnipit(rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Destination_Directory), rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Destination_FileName), rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Package_Code_Old), rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Package_Code_New), rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_ReleaseNo), packOutDocument);
                    else if (Type.compareTo("F") == 0)
                        createWorkflow(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Workflow_ID), packOutDocument);
                    else if (Type.compareTo("V") == 0)
                        createDynamicRuleValidation(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Val_Rule_ID), packOutDocument);
                    else if (Type.compareTo("MSG") == 0)
                        createMessage(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_Message_ID), packOutDocument);
                    else if (Type.compareTo("PFT") == 0)
                        createPrintFormat(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_PrintFormat_ID), packOutDocument);
                    else if (Type.compareTo(X_AD_Package_Exp_Detail.TYPE_ModelValidator) == 0)
                        createModelValidator(rs.getInt(X_AD_Package_Exp_Detail.COLUMNNAME_AD_ModelValidator_ID), packOutDocument);
                    else if (Type.compareTo(X_AD_Package_Exp_Detail.TYPE_EntityType) == 0)
                        createEntityType(AD_EntityType_ID, packOutDocument);
                    else if (Type.compareTo("C") == 0) {
                        log.log(Level.INFO, "In PackOut.java handling Code or Other 2pack module creation");
                        String fullDirectory = rs1.getString(X_AD_Package_Exp.COLUMNNAME_File_Directory) + rs1.getString(X_AD_Package_Exp.COLUMNNAME_Name) + rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Target_Directory);
                        log.log(Level.INFO, "fullDirectory" + fullDirectory);
                        String targetDirectoryModified = null;
                        char fileseperator1 = '/';
                        char fileseperator2 = '\\';
                        //Correct package for proper file separator
                        if (File.separator.equals("/")) {
                            targetDirectoryModified = fullDirectory.replace(fileseperator2, fileseperator1);
                        } else
                            targetDirectoryModified = fullDirectory.replace(fileseperator1, fileseperator2);
                        String target_File = (targetDirectoryModified);
                        success = (new File(target_File).mkdirs());
                        fullDirectory = rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_File_Directory);
                        targetDirectoryModified = null;
                        //Correct package for proper file separator
                        if (File.separator.equals("/")) {
                            targetDirectoryModified = fullDirectory.replace(fileseperator2, fileseperator1);
                        } else
                            targetDirectoryModified = fullDirectory.replace(fileseperator2, fileseperator1);
                        copyCode(targetDirectoryModified + rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_FileName), target_File + rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_FileName));
                        atts.clear();
                        if (rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Destination_Directory) != null) {
                            fullDirectory = rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Destination_Directory);
                            String destinationDirectoryModified = null;
                            //Correct package for proper file separator
                            if (File.separator.equals("/")) {
                                destinationDirectoryModified = fullDirectory.replace(fileseperator2, fileseperator1);
                            } else
                                destinationDirectoryModified = fullDirectory.replace(fileseperator2, fileseperator1);
                            createDistributeFile(rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_FileName), rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Target_Directory), rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_ReleaseNo), destinationDirectoryModified, packOutDocument);
                        }
                        if (rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_FileName) != null) {
                            packageDocument.startElement("", "", "file", atts);
                            packageDocument.characters(("File: " + rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_FileName)).toCharArray(), 0, ("File: " + rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_FileName)).length());
                            packageDocument.endElement("", "", "file");
                        }
                        packageDocument.startElement("", "", "filedirectory", atts);
                        packageDocument.characters(("Directory: " + rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Target_Directory)).toCharArray(), 0, ("Directory: " + rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Target_Directory)).length());
                        packageDocument.endElement("", "", "filedirectory");
                        packageDocument.startElement("", "", "filenotes", atts);
                        packageDocument.characters(("Notes: " + rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Description)).toCharArray(), 0, (("Notes: " + rs.getString(X_AD_Package_Exp_Detail.COLUMNNAME_Description)).length()));
                        packageDocument.endElement("", "", "filenotes");
                    }
                }
                //
                getCtx().remove(X_AD_Package_Exp_Detail.COLUMNNAME_AD_EntityType_ID);
            } finally {
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
            }
            atts.clear();
            //no longer use
            //packOutDocument.startElement("","","menuset",atts);
            //packOutDocument.endElement("","","menuset");
            packOutDocument.endElement("", "", "adempiereAD");
            packOutDocument.endDocument();
            packageDocument.endElement("", "", "adempiereDocument");
            packageDocument.endDocument();
        //m_Exp.setProcessed(true);
        //m_Exp.saveEx();
        }
        rs1.close();
        pstmt1.close();
        pstmt1 = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        throw e;
    } finally {
        try {
            if (pstmt1 != null)
                pstmt1.close();
        } catch (Exception e) {
        }
        pstmt1 = null;
        // Close streams - teo_sarca [ 1704762 ]
        if (packageDocStream != null)
            try {
                packageDocStream.close();
            } catch (Exception e) {
            }
        if (packOutDocStream != null)
            try {
                packOutDocStream.close();
            } catch (Exception e) {
            }
    }
    //create compressed packages
    //set the files	
    File srcFolder = new File(packagedir);
    File destZipFile = new File(packagename + ".zip");
    File destTarFile = new File(packagename + ".tar");
    File destGZipFile = new File(packagename + ".tar.gz");
    //delete the old packages if necessary
    boolean success = destZipFile.delete();
    success = destTarFile.delete();
    success = destGZipFile.delete();
    //create the compressed packages
    CreateZipFile.zipFolder(srcFolder, destZipFile, includesdir);
    CreateZipFile.tarFolder(srcFolder, destTarFile, includesdir);
    CreateZipFile.gzipFile(destTarFile, destGZipFile);
    //Clean .tar file up
    success = destTarFile.delete();
    return "Finish Process";
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) PreparedStatement(java.sql.PreparedStatement) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) AttributesImpl(org.xml.sax.helpers.AttributesImpl) FileOutputStream(java.io.FileOutputStream) ResultSet(java.sql.ResultSet) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 3 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project lucene-solr by apache.

the class TikaEntityProcessor method getXmlContentHandler.

private static ContentHandler getXmlContentHandler(Writer writer) throws TransformerConfigurationException {
    SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance();
    TransformerHandler handler = factory.newTransformerHandler();
    handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml");
    handler.setResult(new StreamResult(writer));
    return handler;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory)

Example 4 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project crawler4j by yasserg.

the class BinaryParseData method setBinaryContent.

public void setBinaryContent(byte[] data) {
    InputStream inputStream = new ByteArrayInputStream(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        TransformerHandler handler = getTransformerHandler(outputStream, DEFAULT_OUTPUT_FORMAT, DEFAULT_ENCODING);
        AUTO_DETECT_PARSER.parse(inputStream, handler, new Metadata(), context);
        // Hacking the following line to remove Tika's inserted DocType
        this.html = new String(outputStream.toByteArray(), DEFAULT_ENCODING).replace("http://www.w3.org/1999/xhtml", "");
    } catch (Exception e) {
        logger.error("Error parsing file", e);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException)

Example 5 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project crawler4j by yasserg.

the class BinaryParseData method getTransformerHandler.

/**
 * Returns a transformer handler that serializes incoming SAX events to
 * XHTML or HTML (depending the given method) using the given output encoding.
 *
 * @param encoding output encoding, or <code>null</code> for the platform default
 */
private static TransformerHandler getTransformerHandler(OutputStream out, String method, String encoding) throws TransformerConfigurationException {
    TransformerHandler transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();
    Transformer transformer = transformerHandler.getTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, method);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    if (encoding != null) {
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    }
    transformerHandler.setResult(new StreamResult(new PrintStream(out)));
    return transformerHandler;
}
Also used : PrintStream(java.io.PrintStream) TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult)

Aggregations

TransformerHandler (javax.xml.transform.sax.TransformerHandler)79 StreamResult (javax.xml.transform.stream.StreamResult)53 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)46 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)32 Transformer (javax.xml.transform.Transformer)28 SAXException (org.xml.sax.SAXException)21 IOException (java.io.IOException)20 AttributesImpl (org.xml.sax.helpers.AttributesImpl)17 SAXResult (javax.xml.transform.sax.SAXResult)13 File (java.io.File)11 StringWriter (java.io.StringWriter)11 XMLReader (org.xml.sax.XMLReader)11 FileOutputStream (java.io.FileOutputStream)10 InputStream (java.io.InputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 OutputStream (java.io.OutputStream)8 Test (org.junit.Test)8 InputSource (org.xml.sax.InputSource)8 Metadata (org.apache.tika.metadata.Metadata)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6