List of usage examples for org.jdom2 Element getTextNormalize
public String getTextNormalize()
From source file:org.rhwlab.dispim.datasource.MicroCluster4DDataSource.java
final public void openFromClusteredDataSourceFile(String xml) throws Exception { SAXBuilder saxBuilder = new SAXBuilder(); Document doc = saxBuilder.build(new File(xml)); Element root = doc.getRootElement(); K = Integer.valueOf(root.getAttributeValue("NumberOfClusters")); D = Integer.valueOf(root.getAttributeValue("Dimensions")); micros = new MicroCluster[K]; N = Integer.valueOf(root.getAttributeValue("NumberOfPoints")); List<Element> clusterElements = root.getChildren("Cluster"); int k = 0;//w w w .jav a2 s .c o m for (Element clusterElement : clusterElements) { String[] tokens = clusterElement.getAttributeValue("Center").split(" "); double[] v = new double[tokens.length]; for (int i = 0; i < v.length; ++i) { v[i] = Double.valueOf(tokens[i]); } double prob = Double.valueOf(clusterElement.getAttributeValue("AvgAdjusted")); List<Element> pointElements = clusterElement.getChildren("Point"); short[][] points = new short[pointElements.size()][]; int[] intensities = new int[pointElements.size()]; int n = 0; for (Element pointElement : pointElements) { intensities[n] = Integer.valueOf(pointElement.getAttributeValue("Intensity")); tokens = pointElement.getTextNormalize().split(" "); points[n] = new short[tokens.length]; for (int i = 0; i < v.length; ++i) { points[n][i] = Double.valueOf(tokens[i]).shortValue(); } ++n; } micros[k] = new MicroCluster4D(v, points, intensities, prob); ++k; } }
From source file:org.rhwlab.dispim.datasource.MicroClusterDataSource.java
public void openFromClusteredDataSourceFile(String xml) throws Exception { SAXBuilder saxBuilder = new SAXBuilder(); Document doc = saxBuilder.build(new File(xml)); Element root = doc.getRootElement(); K = Integer.valueOf(root.getAttributeValue("NumberOfClusters")); D = Integer.valueOf(root.getAttributeValue("Dimensions")); micros = new MicroCluster[K]; N = Integer.valueOf(root.getAttributeValue("NumberOfPoints")); List<Element> clusterElements = root.getChildren("Cluster"); int k = 0;// ww w. ja va2s .c o m for (Element clusterElement : clusterElements) { String[] tokens = clusterElement.getAttributeValue("Center").split(" "); double[] v = new double[tokens.length]; for (int i = 0; i < v.length; ++i) { v[i] = Double.valueOf(tokens[i]); } double prob = Double.valueOf(clusterElement.getAttributeValue("AvgAdjusted")); List<Element> pointElements = clusterElement.getChildren("Point"); short[][] points = new short[pointElements.size()][]; int[] intensities = new int[pointElements.size()]; int n = 0; for (Element pointElement : pointElements) { intensities[n] = Integer.valueOf(pointElement.getAttributeValue("Intensity")); tokens = pointElement.getTextNormalize().split(" "); points[n] = new short[tokens.length]; for (int i = 0; i < v.length; ++i) { points[n][i] = Double.valueOf(tokens[i]).shortValue(); } ++n; } micros[k] = new MicroCluster(v, points, intensities, prob); ++k; } }
From source file:org.tenje.jtrain.runnable.JTrainRPiTrain.java
License:Open Source License
private static TrainFunction getVolatileSoundFunction(Element functionElem) throws LineUnavailableException, IOException, UnsupportedAudioFileException { List<Element> soundElems = functionElem.getChildren("sound"); List<Clip> clips = new ArrayList<>(soundElems.size()); Attribute attribute = functionElem.getAttribute("order"); Order order;/*from w ww. j a v a 2 s. c o m*/ if (attribute != null && soundElems.size() > 1) { order = Order.valueOf(attribute.getValue().toUpperCase()); } else { // Not defined or only one element order = Order.RANDOM; } for (Element soundElem : soundElems) { Line.Info linfo = new Line.Info(Clip.class); Line line = AudioSystem.getLine(linfo); Clip clip = (Clip) line; clip.open(AudioSystem.getAudioInputStream(new File(soundElem.getTextNormalize()))); clips.add(clip); } return new MultipleVolatileSoundFunction(clips, order); }
From source file:org.tenje.jtrain.runnable.JTrainRPiTrain.java
License:Open Source License
private static TrainFunction getPermanentSoundFunction(Element functionElem) throws LineUnavailableException, IOException, UnsupportedAudioFileException { Element child; Clip enableClip, loopClip, disableClip; if ((child = functionElem.getChild("enableSound")) != null) { Line.Info linfo = new Line.Info(Clip.class); Line line = AudioSystem.getLine(linfo); enableClip = (Clip) line; enableClip.open(AudioSystem.getAudioInputStream(new File(child.getTextNormalize()))); } else {/* w w w.j a v a 2 s .c o m*/ enableClip = null; } if ((child = functionElem.getChild("loopSound")) != null) { Line.Info linfo = new Line.Info(Clip.class); Line line = AudioSystem.getLine(linfo); loopClip = (Clip) line; loopClip.open(AudioSystem.getAudioInputStream(new File(child.getTextNormalize()))); } else { throw new XmlReadException("no loop sound defined: " + functionElem); } if ((child = functionElem.getChild("enableSound")) != null) { Line.Info linfo = new Line.Info(Clip.class); Line line = AudioSystem.getLine(linfo); disableClip = (Clip) line; disableClip.open(AudioSystem.getAudioInputStream(new File(child.getTextNormalize()))); } else { disableClip = null; } return new PermanentSoundTrainFunction(enableClip, loopClip, disableClip); }
From source file:org.tenje.jtrain.runnable.JTrainXmlReader.java
License:Open Source License
static TrainFunction getVolatileSoundFunction(Element functionElem) throws LineUnavailableException, IOException, UnsupportedAudioFileException { List<Element> soundElems = functionElem.getChildren("sound"); List<Clip> clips = new ArrayList<>(soundElems.size()); Attribute attribute = functionElem.getAttribute("order"); Order order;/*from w ww .java 2 s. c om*/ if (attribute != null && soundElems.size() > 1) { order = Order.valueOf(attribute.getValue().toUpperCase()); } else { // Not defined or only one element order = Order.RANDOM; } for (Element soundElem : soundElems) { Line.Info linfo = new Line.Info(Clip.class); Line line = AudioSystem.getLine(linfo); Clip clip = (Clip) line; clip.open(AudioSystem.getAudioInputStream(new File(soundElem.getTextNormalize()))); clips.add(clip); } return new MultipleVolatileSoundFunction(clips, order); }
From source file:org.tenje.jtrain.runnable.JTrainXmlReader.java
License:Open Source License
static TrainFunction getPermanentSoundFunction(Element functionElem) throws LineUnavailableException, IOException, UnsupportedAudioFileException { Element child; Clip enableClip, loopClip, disableClip; if ((child = functionElem.getChild("enableSound")) != null) { Line.Info linfo = new Line.Info(Clip.class); Line line = AudioSystem.getLine(linfo); enableClip = (Clip) line; enableClip.open(AudioSystem.getAudioInputStream(new File(child.getTextNormalize()))); } else {/*from w w w .j a v a 2s.c o m*/ enableClip = null; } if ((child = functionElem.getChild("loopSound")) != null) { Line.Info linfo = new Line.Info(Clip.class); Line line = AudioSystem.getLine(linfo); loopClip = (Clip) line; loopClip.open(AudioSystem.getAudioInputStream(new File(child.getTextNormalize()))); } else { throw new XmlReadException("no loop sound defined: " + functionElem); } if ((child = functionElem.getChild("enableSound")) != null) { Line.Info linfo = new Line.Info(Clip.class); Line line = AudioSystem.getLine(linfo); disableClip = (Clip) line; disableClip.open(AudioSystem.getAudioInputStream(new File(child.getTextNormalize()))); } else { disableClip = null; } return new PermanentSoundTrainFunction(enableClip, loopClip, disableClip); }
From source file:org.transitime.avl.NextBusAvlModule.java
License:Open Source License
/** * Extracts the AVL data from the XML document. * Uses JDOM to parse the XML because it makes the Java code much simpler. * @param doc// www .j a v a2 s . co m * @throws NumberFormatException */ @Override protected void extractAvlData(Document doc) throws NumberFormatException { logger.info("Extracting data from xml file"); // Get root of doc Element rootNode = doc.getRootElement(); // Handle any error message Element error = rootNode.getChild("Error"); if (error != null) { String errorStr = error.getTextNormalize(); logger.error("While processing AVL data in NextBusAvlModule: " + errorStr); return; } // Handle getting last time. This is the system time of the server. // This means it can be used to along with secsSinceReport to determine // the epoch time when the GPS report was generated. Element lastTime = rootNode.getChild("lastTime"); if (lastTime != null) { String lastTimeStr = lastTime.getAttributeValue("time"); // Store previous time so that it can be used in the URL // the next time the feed is polled. previousTime = Long.parseLong(lastTimeStr); logger.debug("PreviousTime={}", Time.dateTimeStr(previousTime)); } // Handle getting vehicle location data List<Element> vehicles = rootNode.getChildren("vehicle"); for (Element vehicle : vehicles) { String vehicleId = vehicle.getAttributeValue("id"); float lat = Float.parseFloat(vehicle.getAttributeValue("lat")); float lon = Float.parseFloat(vehicle.getAttributeValue("lon")); // Determine GPS time. Use the previousTime read from the feed // because it indicates what the secsSinceReport is relative to int secsSinceReport = Integer.parseInt(vehicle.getAttributeValue("secsSinceReport")); long gpsEpochTime = previousTime - secsSinceReport * 1000; // Handle the speed float speed = Float.NaN; String speedStr = vehicle.getAttributeValue("speedKmHr"); if (speedStr != null) speed = Geo.converKmPerHrToMetersPerSecond(Float.parseFloat(speedStr)); // Handle heading float heading = Float.NaN; String headingStr = vehicle.getAttributeValue("heading"); if (headingStr != null) { heading = Float.parseFloat(headingStr); // Heading less than 0 means it is invalid if (heading < 0) heading = Float.NaN; } // Get block ID. Since for some feeds the block ID from the feed // doesn't match the GTFS data need to process the block ID. String blockId = processBlockId(vehicle.getAttributeValue("block")); String tripId = vehicle.getAttributeValue("tripTag"); // Determine if part of consist String leadingVehicleId = vehicle.getAttributeValue("leadingVehicleId"); // Get driver ID. Be consistent about using null if not set // instead of empty string String driverId = vehicle.getAttributeValue("driverId"); if (driverId != null && driverId.length() == 0) driverId = null; // Get passenger count Integer passengerCount = null; String passengerCountStr = vehicle.getAttributeValue("passengerCount"); if (passengerCountStr != null) { passengerCount = Integer.parseInt(passengerCountStr); if (passengerCount < 0) passengerCount = 0; } // Log raw info for debugging logger.debug( "vehicleId={} time={} lat={} lon={} spd={} head={} " + "blk={} leadVeh={} drvr={} psngCnt={}", vehicleId, Time.timeStrMsec(gpsEpochTime), lat, lon, speed, heading, blockId, leadingVehicleId, driverId, passengerCount); // Create the AVL object and send it to the JMS topic. // The NextBus feed provides silly amount of precision so // round to just 5 decimal places. AvlReport avlReport = new AvlReport(vehicleId, gpsEpochTime, MathUtils.round(lat, 5), MathUtils.round(lon, 5), speed, heading, "NextBus", leadingVehicleId, driverId, null, // license plate passengerCount, Float.NaN); // passengerFullness // Record the assignment for the vehicle if it is available if (blockId != null && !useTripShortNameForAssignment.getValue()) avlReport.setAssignment(blockId, AssignmentType.BLOCK_ID); else if (tripId != null && useTripShortNameForAssignment.getValue()) avlReport.setAssignment(tripId, AssignmentType.TRIP_SHORT_NAME); else avlReport.setAssignment(null, AssignmentType.UNSET); processAvlReport(avlReport); } }
From source file:org.transitime.custom.missionBay.GtfsFromNextBus.java
License:Open Source License
/** * Opens up InputStream for the routeConfig command for the NextBus API. * //from w w w . ja v a2s . co m * @param command * The NextBus API command * @param options * Query string options to be appended to request to NextBus API * @return XML Document to be parsed * @throws IOException * @throws JDOMException */ private static Document getNextBusApiInputStream(String command, String options) throws IOException, JDOMException { String fullUrl = getNextBusUrl(command, options); // Create the connection URL url = new URL(fullUrl); URLConnection con = url.openConnection(); // Request compressed data to reduce bandwidth used con.setRequestProperty("Accept-Encoding", "gzip,deflate"); // Create appropriate input stream depending on whether content is // compressed or not InputStream in = con.getInputStream(); if ("gzip".equals(con.getContentEncoding())) { in = new GZIPInputStream(in); logger.debug("Returned XML data is compressed"); } else { logger.debug("Returned XML data is NOT compressed"); } SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(in); // Handle any error message Element rootNode = doc.getRootElement(); Element error = rootNode.getChild("Error"); if (error != null) { String errorStr = error.getTextNormalize(); logger.error("While processing data in GtfsFromNextBus: " + errorStr); return null; } return doc; }
From source file:org.transitime.custom.nyc.BusTimeSiriAvlModule.java
License:Open Source License
/** * Extracts the AVL data from the XML document. * Uses JDOM to parse the XML because it makes the Java code much simpler. * @param doc//w w w . j ava2 s. c o m * @throws NumberFormatException */ @Override protected void extractAvlData(Document doc) throws NumberFormatException { logger.info("Extracting data from xml file"); // Get root of doc Element rootNode = doc.getRootElement(); // Get the Namespace. Found that if don't use the namespace when calling // getChild() then it returns null! Namespace ns = rootNode.getNamespace(); Element serviceDelivery = rootNode.getChild("ServiceDelivery", ns); if (serviceDelivery == null) { logger.error("ServiceDelivery element not found in XML data"); return; } Element vehicleMonitoringDelivery = serviceDelivery.getChild("VehicleMonitoringDelivery", ns); if (vehicleMonitoringDelivery == null) { logger.error("VehicleMonitoringDelivery element not found in XML data"); return; } // Handle any error message Element errorCondition = vehicleMonitoringDelivery.getChild("ErrorCondition", ns); if (errorCondition != null) { Element description = errorCondition.getChild("Description", ns); String errorStr = description.getTextNormalize(); logger.error("While processing AVL data in BusTimeSiriAvlModule: " + errorStr); return; } // Handle getting vehicle location data List<Element> vehicles = vehicleMonitoringDelivery.getChildren("VehicleActivity", ns); for (Element vehicle : vehicles) { Element monitoredVehicleJourney = vehicle.getChild("MonitoredVehicleJourney", ns); if (monitoredVehicleJourney != null) { // Get vehicle id Element vehicleRef = monitoredVehicleJourney.getChild("VehicleRef", ns); String vehicleRefStr = vehicleRef.getTextNormalize(); String vehicleId = vehicleRefStr.substring(vehicleRefStr.lastIndexOf('_') + 1); // Get the timestamp of the GPS report. // Note: this probably isn't the actually GPS timestamp but instead // when the GPS report was "recorded", but it is the best we have. Element recordedAtTime = monitoredVehicleJourney.getChild("RecordedAtTime", ns); String timestampStr = recordedAtTime.getTextNormalize(); // The timestamp in the XML has the TimeZone specified as "-04:00" instead of // what SimpleDateFormat can handle, which is "0400". Therefore need to remove // that one semicolon. String fixedStr = timestampStr.substring(0, timestampStr.lastIndexOf(':')) + "00"; long gpsEpochTime = 0; try { gpsEpochTime = dateFormatter.parse(fixedStr).getTime(); } catch (ParseException e) { logger.error("Could not parse <RecordedAtTime> of \"" + timestampStr + "\""); } // Get lat & lon Element vehicleLocation = monitoredVehicleJourney.getChild("VehicleLocation", ns); Element latitude = vehicleLocation.getChild("Latitude", ns); float lat = Float.parseFloat(latitude.getTextNormalize()); Element longitude = vehicleLocation.getChild("Longitude", ns); float lon = Float.parseFloat(longitude.getTextNormalize()); // Block assignment. This one is hard to figure out. While there is a <BlockRef> // element it appears that it is not the right one. Instead need to use // <FramedVehicleJourneyRef><DatedVehicleJourneyRef> and then just use // the last two chunks of something like "MTA NYCT_GH_C3-Weekday-SDon-118800_BX44A_115". // And at NextBus we definitely saw lots of problems where the block assignment // info wasn't accurate. Element framedVehicleJourneyRef = monitoredVehicleJourney.getChild("FramedVehicleJourneyRef", ns); Element datedVehicleJourneyRef = framedVehicleJourneyRef.getChild("DatedVehicleJourneyRef", ns); String fullBlockName = datedVehicleJourneyRef.getTextNormalize(); String blockName2 = fullBlockName.substring(fullBlockName.lastIndexOf('-') + 1); String block = blockName2.substring(blockName2.indexOf('_') + 1); // Heading float heading = Float.NaN; Element bearingElement = monitoredVehicleJourney.getChild("Bearing", ns); if (bearingElement != null) { float bearing = Float.parseFloat(bearingElement.getTextNormalize()); // For bearing: 0 is East, increments counter-clockwise. // But GPS heading: 0 is North, increments clockwise. So // need to convert. heading = 90.0f - bearing; if (heading < 0.0f) heading += 360.0f; } // Speed is not available float speed = Float.NaN; logger.debug("vehicle={} time={} lat={} lon={} spd={} head={} blk={}", vehicleId, Time.timeStr(gpsEpochTime), lat, lon, speed, heading, block); // Create the AVL object and send it to the JMS topic AvlReport avlReport = new AvlReport(vehicleId, gpsEpochTime, lat, lon, speed, heading, "SIRI"); avlReport.setAssignment(block, AssignmentType.BLOCK_ID); processAvlReport(avlReport); } } }
From source file:TVShowTimelineMaker.character.NamedCharacter.java
private NamedCharacter(Element root) { super(root, "NamedCharacter"); EventImp.EventIDXMLWriter appEventIDXMLWriter = EventImp.EventIDXMLWriter.instance; Element nameElement = root.getChild("name"); this.name = nameElement.getTextNormalize(); if (this.getLeastSignificantIDPart() >= count) { count = this.getLeastSignificantIDPart() + 1; }//from w w w .j a v a2 s . c o m Element birthdayElement = root.getChild("birthday"); this.birthday = (OnceDayEvent) appEventIDXMLWriter.readElements(birthdayElement); }