List of usage examples for org.apache.commons.io.input BOMInputStream getBOM
public ByteOrderMark getBOM() throws IOException
From source file:org.languagetool.gui.Main.java
private void loadFile(File file) { try (FileInputStream inputStream = new FileInputStream(file)) { BOMInputStream bomIn = new BOMInputStream(inputStream, false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE); String charsetName;/*from ww w .j av a 2 s . c om*/ if (bomIn.hasBOM()) { bom = bomIn.getBOM(); charsetName = bom.getCharsetName(); } else { // No BOM found bom = null; charsetName = null; } String fileContents = StringTools.readStream(bomIn, charsetName); textArea.setText(fileContents); currentFile = file; updateTitle(); if (recentFiles.contains(file.getAbsolutePath())) { recentFiles.remove(file.getAbsolutePath()); } recentFiles.add(file.getAbsolutePath()); localStorage.saveProperty("recentFiles", recentFiles); updateRecentFilesMenu(); } catch (IOException e) { Tools.showError(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.//from w ww .j av a 2 s . com * * @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: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 w w .j a v a2 s . 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; }