use of javax.xml.transform.sax.SAXTransformerFactory in project opennms by OpenNMS.
the class XmlSystemReportFormatter method write.
@Override
public void write(final SystemReportPlugin plugin) {
if (!hasDisplayable(plugin))
return;
if (m_handler == null) {
try {
StreamResult streamResult = new StreamResult(getOutputStream());
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
m_handler = tf.newTransformerHandler();
Transformer serializer = m_handler.getTransformer();
serializer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "entry");
m_handler.setResult(streamResult);
} catch (final Exception e) {
LOG.error("Unable to create XML stream writer.", e);
m_handler = null;
}
try {
m_handler.startDocument();
m_handler.startElement("", "", "systemReportPlugins", null);
} catch (final Exception e) {
LOG.warn("Unable to start document.", e);
m_handler = null;
}
}
if (m_handler == null) {
LOG.warn("Unable to write, no handler defined!");
return;
}
try {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "", "name", "CDATA", plugin.getName());
atts.addAttribute("", "", "description", "CDATA", plugin.getDescription());
m_handler.startElement("", "", "plugin", atts);
for (final Map.Entry<String, Resource> entry : plugin.getEntries().entrySet()) {
final boolean displayable = isDisplayable(entry.getValue());
atts = new AttributesImpl();
atts.addAttribute("", "", "key", "CDATA", entry.getKey());
if (!displayable) {
atts.addAttribute("", "", "skipped", "CDATA", "true");
}
m_handler.startElement("", "", "entry", atts);
if (displayable) {
final String value = getResourceText(entry.getValue());
if (value != null) {
m_handler.startCDATA();
m_handler.characters(value.toCharArray(), 0, value.length());
m_handler.endCDATA();
}
}
m_handler.endElement("", "", "entry");
}
m_handler.endElement("", "", "plugin");
} catch (final Exception e) {
LOG.warn("An error occurred while attempting to write XML data.", e);
}
}
use of javax.xml.transform.sax.SAXTransformerFactory in project enclojure by EricThorsen.
the class Processor method process.
public int process() throws TransformerException, IOException, SAXException {
ZipInputStream zis = new ZipInputStream(input);
final ZipOutputStream zos = new ZipOutputStream(output);
final OutputStreamWriter osw = new OutputStreamWriter(zos);
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
TransformerFactory tf = TransformerFactory.newInstance();
if (!tf.getFeature(SAXSource.FEATURE) || !tf.getFeature(SAXResult.FEATURE)) {
return 0;
}
SAXTransformerFactory saxtf = (SAXTransformerFactory) tf;
Templates templates = null;
if (xslt != null) {
templates = saxtf.newTemplates(xslt);
}
// configuring outHandlerFactory
// ///////////////////////////////////////////////////////
EntryElement entryElement = getEntryElement(zos);
ContentHandler outDocHandler = null;
switch(outRepresentation) {
case BYTECODE:
outDocHandler = new OutputSlicingHandler(new ASMContentHandlerFactory(zos, computeMax), entryElement, false);
break;
case MULTI_XML:
outDocHandler = new OutputSlicingHandler(new SAXWriterFactory(osw, true), entryElement, true);
break;
case SINGLE_XML:
ZipEntry outputEntry = new ZipEntry(SINGLE_XML_NAME);
zos.putNextEntry(outputEntry);
outDocHandler = new SAXWriter(osw, false);
break;
}
// configuring inputDocHandlerFactory
// /////////////////////////////////////////////////
ContentHandler inDocHandler;
if (templates == null) {
inDocHandler = outDocHandler;
} else {
inDocHandler = new InputSlicingHandler("class", outDocHandler, new TransformerHandlerFactory(saxtf, templates, outDocHandler));
}
ContentHandlerFactory inDocHandlerFactory = new SubdocumentHandlerFactory(inDocHandler);
if (inDocHandler != null && inRepresentation != SINGLE_XML) {
inDocHandler.startDocument();
inDocHandler.startElement("", "classes", "classes", new AttributesImpl());
}
int i = 0;
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
update(ze.getName(), n++);
if (isClassEntry(ze)) {
processEntry(zis, ze, inDocHandlerFactory);
} else {
OutputStream os = entryElement.openEntry(getName(ze));
copyEntry(zis, os);
entryElement.closeEntry();
}
i++;
}
if (inDocHandler != null && inRepresentation != SINGLE_XML) {
inDocHandler.endElement("", "classes", "classes");
inDocHandler.endDocument();
}
if (outRepresentation == SINGLE_XML) {
zos.closeEntry();
}
zos.flush();
zos.close();
return i;
}
use of javax.xml.transform.sax.SAXTransformerFactory 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";
}
use of javax.xml.transform.sax.SAXTransformerFactory 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;
}
use of javax.xml.transform.sax.SAXTransformerFactory in project intellij-community by JetBrains.
the class PlainTextFormatter method convert.
@Override
public void convert(@NotNull final String rawDataDirectoryPath, @Nullable final String outputPath, @NotNull final Map<String, Tools> tools, @NotNull final List<File> inspectionsResults) throws ConversionException {
final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
final URL descrExtractorXsltUrl = getClass().getResource("description-text.xsl");
final Source xslSource;
final Transformer transformer;
try {
xslSource = new StreamSource(URLUtil.openStream(descrExtractorXsltUrl));
transformer = transformerFactory.newTransformer(xslSource);
} catch (IOException e) {
throw new ConversionException("Cannot find inspection descriptions converter.");
} catch (TransformerConfigurationException e) {
throw new ConversionException("Fail to load inspection descriptions converter.");
}
// write to file/stdout:
final Writer w;
if (outputPath != null) {
final File outputFile = new File(outputPath);
try {
w = new FileWriter(outputFile);
} catch (IOException e) {
throw new ConversionException("Cannot edit file: " + outputFile.getPath());
}
} else {
w = new PrintWriter(System.out);
}
try {
for (File inspectionData : inspectionsResults) {
if (inspectionData.isDirectory()) {
warn("Folder isn't expected here: " + inspectionData.getName());
continue;
}
final String fileNameWithoutExt = FileUtil.getNameWithoutExtension(inspectionData);
if (InspectionApplication.DESCRIPTIONS.equals(fileNameWithoutExt)) {
continue;
}
InspectionToolWrapper toolWrapper = tools.get(fileNameWithoutExt).getTool();
// Tool name and group
w.append(getToolPresentableName(toolWrapper)).append("\n");
// Description is HTML based, need to be converted in plain text
writeInspectionDescription(w, toolWrapper, transformer);
// separator before file list
w.append("\n");
// parse xml and output results
final SAXBuilder builder = new SAXBuilder();
try {
final Document doc = builder.build(inspectionData);
final Element root = doc.getRootElement();
final List problems = root.getChildren(PROBLEM_ELEMENT);
// let's count max file path & line_number length to align problem descriptions
final int maxFileColonLineLength = getMaxFileColonLineNumLength(inspectionData, toolWrapper, problems);
for (Object problem : problems) {
// Format:
// file_path:line_num [severity] problem description
final Element fileElement = ((Element) problem).getChild(FILE_ELEMENT);
final String filePath = getPath(fileElement);
// skip suppressed results
if (resultsIgnored(inspectionData, toolWrapper)) {
continue;
}
final Element lineElement = ((Element) problem).getChild(LINE_ELEMENT);
final Element problemDescrElement = ((Element) problem).getChild(DESCRIPTION_ELEMENT);
final String severity = ((Element) problem).getChild(PROBLEM_CLASS_ELEMENT).getAttributeValue(SEVERITY_ATTRIBUTE);
final String fileLineNum = lineElement.getText();
w.append(" ").append(filePath).append(':').append(fileLineNum);
// align descriptions
for (int i = maxFileColonLineLength - 1 - filePath.length() - fileLineNum.length() + 4; i >= 0; i--) {
w.append(' ');
}
w.append("[").append(severity).append("] ");
w.append(problemDescrElement.getText()).append('\n');
}
} catch (JDOMException e) {
throw new ConversionException("Unknown results format, file = " + inspectionData.getPath() + ". Error: " + e.getMessage());
}
// separator between neighbour inspections
w.append("\n");
}
} catch (IOException e) {
throw new ConversionException("Cannot write inspection results: " + e.getMessage());
} finally {
if (w != null) {
try {
w.close();
} catch (IOException e) {
warn("Cannot save inspection results: " + e.getMessage());
}
}
}
}
Aggregations