List of usage examples for org.apache.commons.io ByteOrderMark getCharsetName
public String getCharsetName()
From source file:com.hangum.tadpole.commons.dialogs.fileupload.SingleFileuploadDialog.java
private boolean insert() throws Exception { BOMInputStream bomInputStream;/* w ww . j a v a2s . co m*/ File[] arryFiles = receiver.getTargetFiles(); if (arryFiles.length == 0) { throw new Exception(Messages.get().SingleFileuploadDialog_5); } File userUploadFile = arryFiles[arryFiles.length - 1]; try { // bom? charset? ? ?. bomInputStream = new BOMInputStream(FileUtils.openInputStream(FileUtils.getFile(userUploadFile))); ByteOrderMark bom = bomInputStream.getBOM(); String charsetName = bom == null ? "CP949" : bom.getCharsetName(); //$NON-NLS-1$ strTxtFile = FileUtils.readFileToString(userUploadFile, charsetName); if (bom != null) { // ByteOrderMark.UTF_8.equals(strTxtFile.getBytes()[0]); //??? ?? BOM? . strTxtFile = strTxtFile.substring(1); } } catch (Exception e) { logger.error("file read error", e); //$NON-NLS-1$ throw new Exception(Messages.get().SingleFileuploadDialog_7 + e.getMessage()); } return true; }
From source file:de.iteratec.iteraplan.businesslogic.exchange.nettoExport.NettoCSVTransformerTest.java
@Test public void testCSVTransformerForOverviewPage() { NettoTransformer csvTransformer = NettoCSVTransformer.newInstance(createSimpleOverviewPageTableStructure()); assertNotNull("Can't create netto transformer for overview page table structure for csv", csvTransformer); //Create output stream ByteArrayOutputStream out = new ByteArrayOutputStream(); csvTransformer.transform(sourceList, out, TypeOfBuildingBlock.INFORMATIONSYSTEMRELEASE); //assert that output stream InputStream in = new ByteArrayInputStream(out.toByteArray()); CSVReader reader;/*from ww w . j a v a 2 s . co m*/ try { //BOMInputStream is necessary, because of leading BOM in Stream //Otherwise assertion would fail BOMInputStream bOMInputStream = new BOMInputStream(in); ByteOrderMark bom = bOMInputStream.getBOM(); String charsetName = bom == null ? "UTF-8" : bom.getCharsetName(); reader = new CSVReader(new InputStreamReader(new BufferedInputStream(bOMInputStream), charsetName), ';'); List<String[]> allLines = reader.readAll(); int index = 0; SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US); for (String[] line : allLines) { String name = line[0]; String desc = line[1]; String start = line[2]; String end = line[3]; String status = line[4]; if (index == 0) { //assert the headers of CSV file assertEquals("Name und Version", name.trim()); assertEquals("Beschreibung", desc); assertEquals("von", start); assertEquals("bis", end); assertEquals("Status", status); } else { assertEquals(isArray[index - 1].getName(), name); assertEquals(isArray[index - 1].getDescription(), desc); assertEquals(df.format(isArray[index - 1].getRuntimePeriod().getStart()), start); assertEquals(df.format(isArray[index - 1].getRuntimePeriod().getEnd()), end); assertEquals(isArray[index - 1].getTypeOfStatus().toString(), status); } index++; } reader.close(); } catch (IOException e) { e.printStackTrace(); fail("Fail due to IO Exception"); } }
From source file:de.iteratec.iteraplan.businesslogic.exchange.nettoExport.NettoCSVTransformerTest.java
@Test /**//w ww.j a v a 2 s .c om * Testcase creates 4 Informationsystemreleases with Text, Date and Numeric Attributes, which are transformed in CSV * The CSV output stream will be parsed again as CSV and the values are asserted */ public void testCSVTransformerForSpreadsheetReport() { NettoTransformer csvTransformer = NettoCSVTransformer .newInstance(createSimpleSpreadsheetReportTableStructure()); assertNotNull("Can't create netto transformer for overview page table structure for csv", csvTransformer); //Create output stream ByteArrayOutputStream out = new ByteArrayOutputStream(); csvTransformer.transform(sourceList, out, TypeOfBuildingBlock.INFORMATIONSYSTEMRELEASE); //assert that output stream InputStream in = new ByteArrayInputStream(out.toByteArray()); CSVReader reader; try { //BOMInputStream is necessary, because of leading BOM in Stream //Otherwise assertion would fail BOMInputStream bOMInputStream = new BOMInputStream(in); ByteOrderMark bom = bOMInputStream.getBOM(); String charsetName = bom == null ? "UTF-8" : bom.getCharsetName(); reader = new CSVReader(new InputStreamReader(new BufferedInputStream(bOMInputStream), charsetName), ';'); List<String[]> allLines = reader.readAll(); int index = 0; SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN); for (String[] line : allLines) { String name = line[0]; String desc = line[1]; String start = line[2]; String end = line[3]; String statusString = line[4]; String numString = line[5]; //get the status enum first //we onlny need planned and current in this test TypeOfStatus status = null; if ("Plan".equals(statusString)) { status = TypeOfStatus.PLANNED; } else if ("Ist".equals(statusString)) { status = TypeOfStatus.CURRENT; } if (index == 0) { //assert the headers of CSV file assertEquals("Name und Version", name.trim()); assertEquals("Beschreibung", desc); assertEquals("von", start); assertEquals("bis", end); assertEquals("Status", statusString); assertEquals("Complexity", numString); } else { //format the number NumberFormat nf = new DecimalFormat("0.00"); String number = nf.format(Double.valueOf(numString)); assertEquals(isArray[index - 1].getName(), name); assertEquals(isArray[index - 1].getDescription(), desc); assertEquals(df.format(isArray[index - 1].getRuntimePeriod().getStart()), start); assertEquals(df.format(isArray[index - 1].getRuntimePeriod().getEnd()), end); assertEquals(isArray[index - 1].getTypeOfStatus(), status); assertEquals(isArray[index - 1].getAttributeValue(numberAT.getName(), Locale.GERMAN), number); } index++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.hillview.storage.TextFileLoader.java
Reader getFileReader() { try {/* w w w. j ava 2 s. c om*/ HillviewLogger.instance.info("Reading file", "{0}", this.filename); this.inputStream = new FileInputStream(this.filename); this.bufferedInputStream = new BufferedInputStream(inputStream); // The buffered input stream is needed by the CompressorStream // to detect the compression method at runtime. InputStream fis = this.bufferedInputStream; if (Utilities.isCompressed(this.filename)) { this.compressedStream = new CompressorStreamFactory().createCompressorInputStream(fis); fis = this.compressedStream; } this.bomStream = new BOMInputStream(fis, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE); ByteOrderMark bom = this.bomStream.getBOM(); String charsetName = bom == null ? "UTF-8" : bom.getCharsetName(); return new InputStreamReader(this.bomStream, charsetName); } catch (IOException | CompressorException e) { throw new RuntimeException(e); } }
From source file:org.owasp.dependencycheck.xml.pom.PomParser.java
/** * Parses the given XML file and returns a Model object containing only the * fields dependency-check requires./* w w w. ja v a 2s . c o m*/ * * @param inputStream an InputStream containing suppression rues * @return a list of suppression rules * @throws PomParseException if the XML cannot be parsed */ public Model parse(InputStream inputStream) throws PomParseException { try { final PomHandler handler = new PomHandler(); final SAXParser saxParser = XmlUtils.buildSecureSaxParser(); final XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(handler); final BOMInputStream bomStream = new BOMInputStream(new XmlInputStream(inputStream)); final ByteOrderMark bom = bomStream.getBOM(); final String defaultEncoding = "UTF-8"; final String charsetName = bom == null ? defaultEncoding : bom.getCharsetName(); final Reader reader = new InputStreamReader(bomStream, charsetName); final InputSource in = new InputSource(reader); xmlReader.parse(in); return handler.getModel(); } catch (ParserConfigurationException | SAXException | FileNotFoundException ex) { LOGGER.debug("", ex); throw new PomParseException(ex); } catch (IOException ex) { LOGGER.debug("", ex); throw new PomParseException(ex); } }
From source file:org.sonar.scanner.scan.filesystem.CharsetDetector.java
private boolean detectCharset(byte[] buf) throws IOException { ByteCharsetDetector detector = new ByteCharsetDetector(new CharsetValidation(), userEncoding); ByteOrderMark bom = detector.detectBOM(buf); if (bom != null) { detectedCharset = Charset.forName(bom.getCharsetName()); stream.skip(bom.length());//from www. j a v a2s . c om return true; } detectedCharset = detector.detect(buf); return detectedCharset != null; }
From source file:org.terems.webz.internals.FileDownloaderWithBOM.java
public FileDownloaderWithBOM(WebzInputStreamDownloader downloader, String defaultEncoding) throws IOException, WebzException { this.bomIn = (BOMInputStream) new BOMInputStream(downloader.getInputStream(), false, ALL_BOMS); this.downloader = new FileDownloader(downloader.getFileSpecific(), bomIn); ByteOrderMark bom = bomIn.getBOM(); if (bom == null) { actualEncoding = defaultEncoding; actualNumberOfBytes = downloader.getFileSpecific().getNumberOfBytes(); } else {/*from w w w .ja v a2s . com*/ actualEncoding = bom.getCharsetName(); actualNumberOfBytes = downloader.getFileSpecific().getNumberOfBytes() - bom.length(); } reader = new InputStreamReader(bomIn, actualEncoding); }
From source file:translator.logic.AllVendorAnnotationTranslator.java
/** * Translates Compumedics annotation XML file to standard XML file and save it * @param annotation_file annotation file name * @param edf_file edf file name/*from w ww .ja v a 2s .c o m*/ * @param mapping_file mapping file name * @param output_file output file name * @return true if the translation is successful */ @SuppressWarnings("unchecked") public boolean convertXML(String annotation_file, String edf_file, String mapping_file, String output_file) { HashMap<String, Object>[] map = this.readMapFile(mapping_file); @SuppressWarnings("unused") ArrayList<String> events = new ArrayList<String>(map[1].keySet().size()); @SuppressWarnings("unused") double[] starttimes = new double[map[1].keySet().size()]; Document xmlRoot = new DocumentImpl(); Element root = xmlRoot.createElement("PSGAnnotation"); Element software = xmlRoot.createElement("SoftwareVersion"); software.appendChild(xmlRoot.createTextNode("Compumedics")); Element epoch = xmlRoot.createElement("EpochLength"); // System.out.println("<<<<TEST>>>>>: " + map[0].get("EpochLength")); // wei wang, test epoch.appendChild(xmlRoot.createTextNode((String) map[0].get("EpochLength"))); // System.out.println("<<<<TEST>>>>: " + epoch.hasChildNodes()); // wei wang, test root.appendChild(software); root.appendChild(epoch); Element scoredEvents = xmlRoot.createElement("ScoredEvents"); String[] timeStr = readEDF(edf_file); String[] elmts = new String[3]; elmts[0] = "Recording Start Time"; elmts[1] = "0"; elmts[2] = timeStr[1]; Element elmt = addElements(xmlRoot, elmts); Element clock = xmlRoot.createElement("ClockTime"); clock.appendChild(xmlRoot.createTextNode(timeStr[0])); elmt.appendChild(clock); scoredEvents.appendChild(elmt); boolean bTranslation = true; InputStream inputStream = null; try { // http://stackoverflow.com/questions/1772321/what-is-xml-bom-and-how-do-i-detect-it // Detect (BOM)Byte Order Mark inputStream = new FileInputStream(new File(annotation_file)); @SuppressWarnings("resource") BOMInputStream bOMInputStream = new BOMInputStream(inputStream); ByteOrderMark bom = bOMInputStream.getBOM(); String charsetName = bom == null ? "UTF-8" : bom.getCharsetName(); inputStream.close(); inputStream = new FileInputStream(new File(annotation_file)); Reader reader = new InputStreamReader(inputStream, charsetName); InputSource is = new InputSource(reader); is.setEncoding(charsetName); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(is); doc.getDocumentElement().normalize(); NodeList nodeLst = doc.getElementsByTagName("ScoredEvent"); // File file = new File(annotation_file); // DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // DocumentBuilder db = dbf.newDocumentBuilder(); // Document doc = db.parse(file); // doc.getDocumentElement().normalize(); // NodeList nodeLst = doc.getElementsByTagName("ScoredEvent"); for (int s = 0; s < nodeLst.getLength(); s++) { Element e = null; @SuppressWarnings("unused") Node n = null; Node fstNode = nodeLst.item(s); Element fstElmnt = (Element) fstNode; NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("Name"); Element fstNmElmnt = (Element) fstNmElmntLst.item(0); NodeList fstNm = fstNmElmnt.getChildNodes(); String eventname = ((Node) fstNm.item(0)).getNodeValue(); // first Name child value // map[1] contains keySet with event name if (map[1].keySet().contains(eventname)) { e = xmlRoot.createElementNS(null, "ScoredEvent"); Element name = xmlRoot.createElement("EventConcept"); Node nameNode = xmlRoot .createTextNode((String) ((ArrayList<String>) map[1].get(eventname)).get(1)); name.appendChild(nameNode); e.appendChild(name); //System.out.println("\t<EventConcept>" + map[1].get(eventname) + "</EventConcept>"); NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("Duration"); Element lstNmElmnt = (Element) lstNmElmntLst.item(0); NodeList lstNm = lstNmElmnt.getChildNodes(); Element duration = xmlRoot.createElement("Duration"); Node durationNode = xmlRoot.createTextNode((String) ((Node) lstNm.item(0)).getNodeValue()); duration.appendChild(durationNode); e.appendChild(duration); //System.out.println("\t<Duration>" + ((Node) lstNm.item(0)).getNodeValue() + "</Duration>" ); NodeList startElmntLst = fstElmnt.getElementsByTagName("Start"); Element startElmnt = (Element) startElmntLst.item(0); NodeList start = startElmnt.getChildNodes(); double starttime = Double.parseDouble(((Node) start.item(0)).getNodeValue()); Element startEt = xmlRoot.createElement("Start"); Node startNode = xmlRoot.createTextNode(Double.toString(starttime)); startEt.appendChild(startNode); e.appendChild(startEt); if (((ArrayList<String>) map[1].get(eventname)).get(0).compareTo("Desaturation") == 0) { //System.out.println("here"); NodeList otherElmntLst = fstElmnt.getElementsByTagName("LowestSpO2"); double lowestspo2 = 0; if (otherElmntLst.getLength() >= 1) { Element lowest = (Element) otherElmntLst.item(0); Element nadir = xmlRoot.createElement("SpO2Nadir"); NodeList nadirLst = lowest.getChildNodes(); nadir.appendChild(xmlRoot.createTextNode(nadirLst.item(0).getNodeValue())); lowestspo2 = Double.parseDouble(nadirLst.item(0).getNodeValue()); e.appendChild(nadir); } NodeList baseLst = fstElmnt.getElementsByTagName("Desaturation"); if (baseLst.getLength() >= 1) { Element baseElmnt = (Element) baseLst.item(0); Element baseline = xmlRoot.createElement("SpO2Baseline"); NodeList baselineLst = baseElmnt.getChildNodes(); baseline.appendChild(xmlRoot.createTextNode(Double.toString( Double.parseDouble(baselineLst.item(0).getNodeValue()) + lowestspo2))); e.appendChild(baseline); } } // Other informations depending on type of events if (((ArrayList<String>) map[1].get(eventname)).get(0).compareTo("Respiratory") == 0) { } if (((ArrayList<String>) map[1].get(eventname)).size() > 2) { Element notes = xmlRoot.createElement("Notes"); notes.appendChild( xmlRoot.createTextNode(((ArrayList<String>) map[1].get(eventname)).get(2))); e.appendChild(notes); } scoredEvents.appendChild(e); } else { // no mapping event name found Element eventNode = xmlRoot.createElement("ScoredEvent"); Element nameNode = xmlRoot.createElement("EventConcept"); Element startNode = xmlRoot.createElement("Starttime"); Element durationNode = xmlRoot.createElement("Duration"); Element notesNode = xmlRoot.createElement("Notes"); nameNode.appendChild(xmlRoot.createTextNode("Technician Notes")); notesNode.appendChild(xmlRoot.createTextNode(eventname)); NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("Duration"); Element lstNmElmnt = (Element) lstNmElmntLst.item(0); NodeList lstNm = lstNmElmnt.getChildNodes(); @SuppressWarnings("unused") Element duration = xmlRoot.createElement("Duration"); Node durationN = xmlRoot.createTextNode((String) ((Node) lstNm.item(0)).getNodeValue()); durationNode.appendChild(durationN); //e.appendChild(duration); //System.out.println("\t<Duration>" + ((Node) lstNm.item(0)).getNodeValue() + "</Duration>" ); NodeList startElmntLst = fstElmnt.getElementsByTagName("Start"); Element startElmnt = (Element) startElmntLst.item(0); NodeList start = startElmnt.getChildNodes(); double starttime = Double.parseDouble(((Node) start.item(0)).getNodeValue()); //Element startEt = xml.createElement("Start"); Node startN = xmlRoot.createTextNode(Double.toString(starttime)); startNode.appendChild(startN); eventNode.appendChild(nameNode); eventNode.appendChild(startNode); eventNode.appendChild(durationNode); eventNode.appendChild(notesNode); scoredEvents.appendChild(eventNode); String info = annotation_file + "," + eventname + "," + Double.toString(starttime); this.log(info); } } // for each sleep stages NodeList allStages = doc.getElementsByTagName("SleepStage"); Element eventNode = xmlRoot.createElement("ScoredEvent"); Element nameNode = xmlRoot.createElement("EventConcept"); Element startNode = xmlRoot.createElement("Start"); Element durationNode = xmlRoot.createElement("Duration"); String stage = ((Element) allStages.item(0)).getTextContent(); String name = ""; // map[2] <- {key(Event), value(Value)} if (map[2].keySet().contains(stage)) { name = (String) map[2].get(stage); } double start = 0; nameNode.appendChild(xmlRoot.createTextNode(name)); startNode.appendChild(xmlRoot.createTextNode(Double.toString(start))); eventNode.appendChild(nameNode); eventNode.appendChild(startNode); // eventNode.appendChild(durationNode); // eventsElmt.appendChild(eventNode); // System.out.println(name); int count = 0; for (int i = 1; i < allStages.getLength(); i++) { String nstage = ((Element) allStages.item(i)).getTextContent(); if (nstage.compareTo(stage) == 0) { count = count + 1; } else { durationNode.appendChild(xmlRoot.createTextNode(Double.toString(count * 30))); eventNode.appendChild(durationNode); scoredEvents.appendChild(eventNode); eventNode = xmlRoot.createElement("ScoredEvent"); nameNode = xmlRoot.createElement("EventConcept"); stage = nstage; if (map[2].keySet().contains(stage)) { name = (String) map[2].get(stage); } nameNode.appendChild(xmlRoot.createTextNode(name)); startNode = xmlRoot.createElement("Start"); start = count * 30 + start; startNode.appendChild(xmlRoot.createTextNode(Double.toString(start))); durationNode = xmlRoot.createElement("Duration"); //durationNode.appendChild(xml.createTextNode("abc")); //durationNode.appendChild(xml.createTextNode(Integer.toString(count*30))); eventNode.appendChild(nameNode); eventNode.appendChild(startNode); //eventNode.appendChild(durationNode); count = 1; } } durationNode.appendChild(xmlRoot.createTextNode(Double.toString(count * 30))); eventNode.appendChild(durationNode); scoredEvents.appendChild(eventNode); //root.appendChild(eventsElmt); } catch (Exception e) { e.printStackTrace(); bTranslation = false; StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); log(errors.toString()); } finally { try { if (inputStream != null) inputStream.close(); } catch (Exception e) { // ignore } } root.appendChild(scoredEvents); xmlRoot.appendChild(root); saveXML(xmlRoot, output_file); // System.out.println(outfile.get); return bTranslation; }
From source file:uk.nhs.fhir.load.FileLoader.java
/** * Return an appropriate charset which handles BOMs if necessary * @throws IOException // ww w .j a va 2s. c om * @throws FileNotFoundException */ private static String getCharset(File file) throws IOException { try (BOMInputStream bomInputStream = new BOMInputStream(new FileInputStream(file))) { // Use commons.io to deal with byte-order-marker if present ByteOrderMark bom = bomInputStream.getBOM(); if (bom != null) { return bom.getCharsetName(); } else { return DEFAULT_ENCODING; } } }