List of usage examples for javax.xml.transform TransformerConfigurationException printStackTrace
@Override public void printStackTrace()
From source file:hudson.plugins.project_inheritance.projects.InheritanceProject.java
public String renderSVGRelationGraph(int width, int height) { SVGTreeRenderer tree = new SVGTreeRenderer(this.getSVGRelationGraph(), width, height); Document doc = tree.render(); try {/*from ww w. j a v a 2 s . c om*/ DOMSource source = new DOMSource(doc); StringWriter stringWriter = new StringWriter(); StreamResult result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return ""; }
From source file:org.codehaus.preon.emitter.Exporter.java
private static void saveEmitted(byte[] bytes, File structure) { ByteArrayInputStream in = new ByteArrayInputStream(bytes); OutputStream out = null;/*from w w w .j av a 2 s.co m*/ try { out = new FileOutputStream(structure); StreamSource xslt = new StreamSource(Exporter.class.getResourceAsStream("/structure-to-html.xsl")); Transformer transformer = TransformerFactory.newInstance().newTransformer(xslt); transformer.transform(new StreamSource(in), new StreamResult(out)); } catch (TransformerConfigurationException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (TransformerException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } }
From source file:org.dataconservancy.dcs.access.server.TransformerServiceImpl.java
@Override public String fgdcToHtml(String inputUrl, String format) { if (format.contains("fgdc")) { TransformerFactory factory = TransformerFactory.newInstance(); Source xslt = new StreamSource(new File(homeDir + "queryFgdcResult.xsl")); Transformer transformer;//from www .ja va 2 s .c o m try { transformer = factory.newTransformer(xslt); String inputPath = homeDir + UUID.randomUUID().toString() + "fgdcinput.xml"; saveUrl(inputPath, inputUrl); Source text = new StreamSource(new File(inputPath)); String outputPath = homeDir + UUID.randomUUID().toString() + "fgdcoutput.html"; File outputFile = new File(outputPath); transformer.transform(text, new StreamResult(outputFile)); FileInputStream stream = new FileInputStream(new File(outputPath)); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { try { String inputPath = //getServletContext().getContextPath()+"/xml/"+ homeDir + UUID.randomUUID().toString() + "fgdcinput.xml"; saveUrl(inputPath, inputUrl); Source text = new StreamSource(new File( //"/home/kavchand/Desktop/fgdc.xml" inputPath)); FileInputStream stream = new FileInputStream(new File(inputPath)); FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ return Charset.defaultCharset().decode(bb).toString(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; }
From source file:org.giavacms.base.common.util.HtmlUtils.java
public static String prettyXml(String code) { if (code == null) { code = ""; }//from ww w . j a v a 2s . c o m try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); Transformer transformer; transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); StreamSource source = new StreamSource(new StringReader(code)); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; }
From source file:org.gnucash.android.export.ExporterTask.java
/** * Writes out the document held in <code>node</code> to <code>outputWriter</code> * @param node {@link Node} containing the OFX document structure. Usually the parent node * @param outputWriter {@link Writer} to use in writing the file to stream * @param omitXmlDeclaration Flag which causes the XML declaration to be omitted *///from w w w. j a va 2s .c om public void write(Node node, Writer outputWriter, boolean omitXmlDeclaration) { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(outputWriter); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (omitXmlDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(source, result); } catch (TransformerConfigurationException txconfigException) { txconfigException.printStackTrace(); } catch (TransformerException tfException) { tfException.printStackTrace(); } }
From source file:org.gnucash.android.ui.accounts.ExportDialogFragment.java
/** * Writes out the file held in <code>document</code> to <code>outputWriter</code> * @param document {@link Document} containing the OFX document structure * @param outputWriter {@link Writer} to use in writing the file to stream *///w ww. j a v a 2 s .co m public void write(Document document, Writer outputWriter) { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(outputWriter); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); } catch (TransformerConfigurationException txconfigException) { txconfigException.printStackTrace(); } catch (TransformerException tfException) { tfException.printStackTrace(); } }
From source file:org.lilyproject.runtime.tools.plugin.genclassloader.ClassloaderMojo.java
/** * Create a project file containing the dependencies. * * @throws IOException//from ww w.j av a2 s . co m * @throws SAXException */ private void createDependencyListing(File classloaderTemplate) throws IOException, SAXException { if (classloaderTemplate != null && classloaderTemplate.exists()) { getLog().info("Found classloader template, trying to parse it..."); parseClassloaderTemplate(classloaderTemplate); } final boolean hasEntries = entryMap.size() > 0; // fill in file with all dependencies FileOutputStream fos = new FileOutputStream(projectDescriptorFile); TransformerHandler ch = null; TransformerFactory factory = TransformerFactory.newInstance(); if (factory.getFeature(SAXTransformerFactory.FEATURE)) { try { ch = ((SAXTransformerFactory) factory).newTransformerHandler(); // set properties Transformer serializer = ch.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); // set result Result result = new StreamResult(fos); ch.setResult(result); // start file generation ch.startDocument(); AttributesImpl atts = new AttributesImpl(); if (this.shareSelf != null) { atts.addAttribute("", "share-self", "share-self", "CDATA", this.shareSelf); } ch.startElement("", "classloader", "classloader", atts); atts.clear(); ch.startElement("", "classpath", "classpath", atts); SortedSet<Artifact> sortedArtifacts = new TreeSet<Artifact>(dependenciesToList); Entry entry; String entryShare; String entryVersion; for (Artifact artifact : sortedArtifacts) { atts.addAttribute("", "groupId", "groupId", "CDATA", artifact.getGroupId()); atts.addAttribute("", "artifactId", "artifactId", "CDATA", artifact.getArtifactId()); if (artifact.getClassifier() != null) { atts.addAttribute("", "classifier", "classifier", "CDATA", artifact.getClassifier()); } if (!artifact.getGroupId().equals("org.lilyproject")) { atts.addAttribute("", "version", "version", "CDATA", artifact.getBaseVersion()); } entry = new Entry(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier()); if (hasEntries && entryMap.containsKey(entry)) { entryVersion = extractAttVal(entryMap.get(entry).getAttributes(), "version"); entryShare = extractAttVal(entryMap.get(entry).getAttributes(), "share"); entryMap.remove(entry); if (entryVersion != null && !entryVersion.equals("") && !entryVersion.equals(artifact.getBaseVersion())) { getLog().warn( "version conflict between entry in template and artifact on classpath for " + entry); } if (entryShare != null) { atts.addAttribute("", "", "share", "CDATA", entryShare); } } else { // atts.addAttribute("", "", "share", "CDATA", SHARE_DEFAULT); } ch.startElement("", "artifact", "artifact", atts); ch.endElement("", "artifact", "artifact"); atts.clear(); } ch.endElement("", "classpath", "classpath"); ch.endElement("", "classloader", "classloader"); ch.endDocument(); fos.close(); if (entryMap.size() > 0) { getLog().warn("Classloader template contains entries that could not be resolved."); } } catch (TransformerConfigurationException ex) { ex.printStackTrace(); throw new SAXException("Unable to get a TransformerHandler."); } } else { throw new RuntimeException("Could not load SAXTransformerFactory."); } }
From source file:org.openlmis.odkapi.service.ODKSubmissionService.java
public void saveZNZSurveyData(MultipartFile XMLSubmissionFile) throws ODKAccountNotFoundException, FacilityPictureNotFoundException, ODKCollectXMLSubmissionFileNotFoundException, ODKCollectXMLSubmissionParserConfigurationException, ODKCollectXMLSubmissionSAXException { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); ODKZNZSurveySubmissionSAXHandler handler; handler = new ODKZNZSurveySubmissionSAXHandler(); String filePath = "/public/odk-collect-submissions/"; InputStream inputStream;//from w ww .j a va 2s .c om FileOutputStream outputStream; // first save the xml submission file try { inputStream = XMLSubmissionFile.getInputStream(); File newFile = new File(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); outputStream = new FileOutputStream(newFile); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } inputStream.close(); outputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } // read the submission file into a string String xmlString = ""; try { File savedSubmissionFile = new File( servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); xmlString = readFile(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename()), Charset.defaultCharset()); } catch (Exception ex) { ex.printStackTrace(); } // use XML Formatter XmlFormatter formatter = new XmlFormatter(); String formattedXmlSubmission = formatter.format(xmlString); // get input stream from the document object of the formatted xml Document xmlSubmissionDocument; try { xmlSubmissionDocument = stringToDom(formattedXmlSubmission); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } ByteArrayOutputStream outputStreamForDocument = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(xmlSubmissionDocument); Result outputTarget = new StreamResult(outputStreamForDocument); try { TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); } catch (TransformerConfigurationException transformerConfigurationException) { transformerConfigurationException.printStackTrace(); } catch (TransformerException transformerException) { transformerException.printStackTrace(); } InputStream inputStreamForDocument = new ByteArrayInputStream(outputStreamForDocument.toByteArray()); try { SAXParser saxParser = saxParserFactory.newSAXParser(); saxParser.parse(inputStreamForDocument, handler); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } odkSubmission = handler.getOdkSubmission(); odkAccount = handler.getOdkAccount(); pictures = handler.getPictures(); ODKAccount tempAccount; tempAccount = odkAccountService.authenticate(odkAccount); odkSubmission.setOdkAccountId(tempAccount.getId()); odkSubmission.setActive(true); this.insertODKSubmission(odkSubmission); Long lastODKSubmissionId = odkSubmissionRepository.getLastSubmissionId(); // save storage part with gps coordinates and pictures odkStorageSurveySubmission = handler.getOdkStorageSurveySubmission(); odkStorageSurveySubmission.setODKSubmissionId(lastODKSubmissionId); ArrayList<byte[]> facilityPictures = getZNZSurveyPictures(); for (int count = 0; count < facilityPictures.size(); count++) { try { Method picMethod = odkStorageSurveySubmission.getClass().getMethod(pictureSetMethods[count], new Class[] { byte[].class }); picMethod.invoke(odkStorageSurveySubmission, facilityPictures.get(count)); } catch (IllegalAccessException ex) { // TO DO handle } catch (NoSuchMethodException ex) { // TO DO handle } catch (SecurityException ex) { // TO DO handle } catch (IllegalArgumentException ex) { // TO DO handle } catch (InvocationTargetException ex) { // TO DO handle } } odkStorageSurveySubmission.setTotalPercentage(getTotalPercentage(odkStorageSurveySubmission, 15)); odkStorageSurveySubmissionRepository.insert(odkStorageSurveySubmission); // save lmis part odkLmisSurveySubmission = handler.getOdkLmisSurveySubmission(); odkLmisSurveySubmission.setODKSubmissionId(lastODKSubmissionId); odkLmisSurveySubmission.setTotalPercentage(getTotalPercentage(odkLmisSurveySubmission, 11)); odklmisSurveySubmissionRepository.insert(odkLmisSurveySubmission); // save r and r part odkRandRSubmission = handler.getOdkRandRSubmission(); odkRandRSubmission.setODKSubmissionId(lastODKSubmissionId); odkRandRSubmission.setTotalPercentage(getTotalPercentage(odkRandRSubmission, 8)); odkRandRSubmissionRepository.insert(odkRandRSubmission); }
From source file:org.openlmis.odkapi.service.ODKSubmissionService.java
public void saveProofOfDeliverySurveyData(MultipartFile XMLSubmissionFile) throws ODKAccountNotFoundException, FacilityPictureNotFoundException, ODKCollectXMLSubmissionFileNotFoundException, ODKCollectXMLSubmissionParserConfigurationException, ODKCollectXMLSubmissionSAXException, ODKXFormNotFoundException { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); ODKProofOfDeliverySubmissionSAXHandler handler; handler = new ODKProofOfDeliverySubmissionSAXHandler(); String filePath = "/public/odk-collect-submissions/"; InputStream inputStream;// w w w . j a v a2s.co m FileOutputStream outputStream; // first save the xml submission file try { inputStream = XMLSubmissionFile.getInputStream(); File newFile = new File(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); outputStream = new FileOutputStream(newFile); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } inputStream.close(); outputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } // read the submission file into a string String xmlString = ""; try { File savedSubmissionFile = new File( servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); xmlString = readFile(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename()), Charset.defaultCharset()); } catch (Exception ex) { ex.printStackTrace(); } // use XML Formatter XmlFormatter formatter = new XmlFormatter(); String formattedXmlSubmission = formatter.format(xmlString); // get input stream from the document object of the formatted xml Document xmlSubmissionDocument; try { xmlSubmissionDocument = stringToDom(formattedXmlSubmission); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } ByteArrayOutputStream outputStreamForDocument = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(xmlSubmissionDocument); Result outputTarget = new StreamResult(outputStreamForDocument); try { TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); } catch (TransformerConfigurationException transformerConfigurationException) { transformerConfigurationException.printStackTrace(); } catch (TransformerException transformerException) { transformerException.printStackTrace(); } InputStream inputStreamForDocument = new ByteArrayInputStream(outputStreamForDocument.toByteArray()); try { SAXParser saxParser = saxParserFactory.newSAXParser(); saxParser.parse(inputStreamForDocument, handler); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } odkSubmission = handler.getOdkSubmission(); odkAccount = handler.getOdkAccount(); ODKAccount tempAccount; tempAccount = odkAccountService.authenticate(odkAccount); odkSubmission.setOdkAccountId(tempAccount.getId()); odkSubmission.setActive(true); this.insertODKSubmission(odkSubmission); Long lastODKSubmissionId = odkSubmissionRepository.getLastSubmissionId(); odkProofOfDeliverySubmissionData = handler.getOdkProofOfDeliverySubmissionData(); this.insertODKProofOfDeliverySubmissionData(odkProofOfDeliverySubmissionData); }
From source file:org.openlmis.odkapi.service.ODKSubmissionService.java
public void saveStockStatusSurveyData(MultipartFile XMLSubmissionFile) throws ODKAccountNotFoundException, FacilityPictureNotFoundException, ODKCollectXMLSubmissionFileNotFoundException, ODKCollectXMLSubmissionParserConfigurationException, ODKCollectXMLSubmissionSAXException { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); ODKStockStatusSubmissionSAXHandler handler; handler = new ODKStockStatusSubmissionSAXHandler(); // To Do : the xml is not well formatted when stock status submission is done , find out why // this is a temporary solution String filePath = "/public/odk-collect-submissions/"; InputStream inputStream;/*from w ww .j av a 2 s .c o m*/ FileOutputStream outputStream; // first save the xml submission file try { inputStream = XMLSubmissionFile.getInputStream(); File newFile = new File(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); outputStream = new FileOutputStream(newFile); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } inputStream.close(); outputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } // read the submission file into a string String xmlString = ""; try { File savedSubmissionFile = new File( servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); xmlString = readFile(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename()), Charset.defaultCharset()); } catch (Exception ex) { ex.printStackTrace(); } // use XML Formatter XmlFormatter formatter = new XmlFormatter(); String formattedXmlSubmission = formatter.format(xmlString); // get input stream from the document object of the formatted xml Document xmlSubmissionDocument; try { xmlSubmissionDocument = stringToDom(formattedXmlSubmission); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } ByteArrayOutputStream outputStreamForDocument = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(xmlSubmissionDocument); Result outputTarget = new StreamResult(outputStreamForDocument); try { TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); } catch (TransformerConfigurationException transformerConfigurationException) { transformerConfigurationException.printStackTrace(); } catch (TransformerException transformerException) { transformerException.printStackTrace(); } InputStream inputStreamForDocument = new ByteArrayInputStream(outputStreamForDocument.toByteArray()); try { SAXParser saxParser = saxParserFactory.newSAXParser(); saxParser.parse(inputStreamForDocument, handler); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } odkSubmission = handler.getOdkSubmission(); odkAccount = handler.getOdkAccount(); ODKAccount tempAccount; tempAccount = odkAccountService.authenticate(odkAccount); odkSubmission.setOdkAccountId(tempAccount.getId()); odkSubmission.setActive(true); this.insertODKSubmission(odkSubmission); Long lastODKSubmissionId = odkSubmissionRepository.getLastSubmissionId(); listODKStockStatusSubmissions = handler.getListODKStockStatusSubmissions(); for (ODKStockStatusSubmission odkStockStatusSubmission : listODKStockStatusSubmissions) { odkStockStatusSubmission.setODKSubmissionId(lastODKSubmissionId); odkStockStatusSubmission.setActive(true); this.insertStockStatus(odkStockStatusSubmission); } }