List of usage examples for org.jdom2 Element toString
@Override
public String toString()
This returns a String
representation of the Element
, suitable for debugging.
From source file:ch.unifr.diuf.diva.did.commands.AbstractCommand.java
License:Open Source License
private String getParameter(Element e, String childName) { Element child = e.getChild(childName); if (child == null) { illegalArgument("cannot find parameter " + childName + "\n" + e.toString()); }//from w ww .j a va 2 s . com if (child.getText() == null) { illegalArgument(childName + " cannot be empty"); } return script.preprocess(child.getText()); }
From source file:com.thoughtworks.go.config.ConfigCipherUpdater.java
License:Apache License
public void migrate() { File cipherFile = systemEnvironment.getDESCipherFile(); String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(timeProvider.currentTime()); File backupCipherFile = new File(systemEnvironment.getConfigDir(), "cipher.original." + timestamp); File configFile = new File(systemEnvironment.getCruiseConfigFile()); File backupConfigFile = new File(configFile.getParentFile(), configFile.getName() + ".original." + timestamp); try {//from www. j a va2 s.co m if (!cipherFile.exists() || !FileUtils.readFileToString(cipherFile, UTF_8).equals(FLAWED_VALUE)) { return; } LOGGER.info("Found unsafe cipher {} on server, Go will make an attempt to rekey", FLAWED_VALUE); FileUtils.copyFile(cipherFile, backupCipherFile); LOGGER.info("Old cipher was successfully backed up to {}", backupCipherFile.getAbsoluteFile()); FileUtils.copyFile(configFile, backupConfigFile); LOGGER.info("Old config was successfully backed up to {}", backupConfigFile.getAbsoluteFile()); String oldCipher = FileUtils.readFileToString(backupCipherFile, UTF_8); new DESCipherProvider(systemEnvironment).resetCipher(); String newCipher = FileUtils.readFileToString(cipherFile, UTF_8); if (newCipher.equals(oldCipher)) { LOGGER.warn("Unable to generate a new safe cipher. Your cipher is unsafe."); FileUtils.deleteQuietly(backupCipherFile); FileUtils.deleteQuietly(backupConfigFile); return; } Document document = new SAXBuilder().build(configFile); List<String> encryptedAttributes = Arrays.asList("encryptedPassword", "encryptedManagerPassword"); List<String> encryptedNodes = Arrays.asList("encryptedValue"); XPathFactory xPathFactory = XPathFactory.instance(); for (String attributeName : encryptedAttributes) { XPathExpression<Element> xpathExpression = xPathFactory .compile(String.format("//*[@%s]", attributeName), Filters.element()); List<Element> encryptedPasswordElements = xpathExpression.evaluate(document); for (Element element : encryptedPasswordElements) { Attribute encryptedPassword = element.getAttribute(attributeName); encryptedPassword.setValue(reEncryptUsingNewKey(decodeHex(oldCipher), decodeHex(newCipher), encryptedPassword.getValue())); LOGGER.debug("Replaced encrypted value at {}", element.toString()); } } for (String nodeName : encryptedNodes) { XPathExpression<Element> xpathExpression = xPathFactory.compile(String.format("//%s", nodeName), Filters.element()); List<Element> encryptedNode = xpathExpression.evaluate(document); for (Element element : encryptedNode) { element.setText( reEncryptUsingNewKey(decodeHex(oldCipher), decodeHex(newCipher), element.getValue())); LOGGER.debug("Replaced encrypted value at {}", element.toString()); } } try (FileOutputStream fileOutputStream = new FileOutputStream(configFile)) { XmlUtils.writeXml(document, fileOutputStream); } LOGGER.info("Successfully re-encrypted config"); } catch (Exception e) { LOGGER.error("Re-keying of cipher failed with error: [{}]", e.getMessage(), e); if (backupCipherFile.exists()) { try { FileUtils.copyFile(backupCipherFile, cipherFile); } catch (IOException e1) { LOGGER.error( "Could not replace the cipher file [{}] with original one [{}], please do so manually. Error: [{}]", cipherFile.getAbsolutePath(), backupCipherFile.getAbsolutePath(), e.getMessage(), e); bomb(e1); } } } }
From source file:helpers.XMLParser.java
public static LinkedList<ParsedSingleXML> parseMultipleDoc(String xml) { LinkedList<ParsedSingleXML> output = new LinkedList<>(); ParsedSingleXML doc = null;/* ww w. j av a 2s .com*/ try { SAXBuilder saxBuilder = new SAXBuilder(); Document document = saxBuilder.build(new StringReader(xml)); Element root = document.getRootElement(); List<Element> rootChildren = root.getChildren(); for (Element child : rootChildren) { System.out.println(child.toString()); output.addLast(parseSingleDoc(child.toString())); } } catch (Exception ex) { //return new LinkedList<>(); } return output; }
From source file:jmri.jmrit.vsdecoder.LoadXmlVSDecoderAction.java
License:Open Source License
/** * Parse the XML file and create ThrottleFrames. Returns true if throttle * loaded successfully.//ww w. j a va2 s. c o m * * @param f The XML file containing throttles. */ public boolean loadVSDecoderProfile(java.io.File f) throws java.io.IOException { try { VSDecoderPrefs prefs = new VSDecoderPrefs(); Element root = prefs.rootFromFile(f); // WARNING: This may be out of sync with the Store... the root element is <VSDecoderConfig> // not sure, must investigate. See what XmlFile.rootFromFile(f) does... List<Element> profiles = root.getChildren("VSDecoder"); if ((profiles != null) && (profiles.size() > 0)) { // Create a new VSDecoder object for each Profile in the XML file. for (java.util.Iterator<Element> i = profiles.iterator(); i.hasNext();) { Element e = i.next(); log.debug(e.toString()); //VSDecoder vsd = VSDecoderManager.instance().getVSDecoder(e.getAttribute("name").getValue(), f.getPath()); } } } catch (org.jdom2.JDOMException ex) { log.warn("Loading VSDecoder Profile exception", ex); return false; } return true; }
From source file:jmri.jmrit.vsdecoder.SteamSound.java
License:Open Source License
@Override public void setXml(Element e, VSDFile vf) { Element el;/* w w w . j av a 2 s. com*/ //int num_rpms; String fn; SoundBite sb; super.setXml(e, vf); log.debug("Steam EngineSound: " + e.getAttribute("name").getValue()); String n = e.getChild("top-speed").getValue(); if (n != null) { top_speed = Integer.parseInt(n); //log.debug("Top speed: " + top_speed + " MPH"); } n = e.getChildText("driver-diameter"); if (n != null) { driver_diameter = Integer.parseInt(n); //log.debug("Driver diameter: " + driver_diameter + " inches"); } n = e.getChildText("cylinders"); if (n != null) { num_cylinders = Integer.parseInt(n); //log.debug("Num Cylinders: " + num_cylinders); } // For now, num_rpms is not used. /* n = e.getChild("rpm-steps").getValue(); if (n != null) { num_rpms = Integer.parseInt(n); //log.debug("Number of rpm steps: " + num_rpms); } */ rpm_sounds = new ArrayList<RPMSound>(); // Get the RPM steps Iterator<Element> itr = (e.getChildren("rpm-step")).iterator(); int i = 0; while (itr.hasNext()) { el = itr.next(); fn = el.getChildText("file"); int min_r = Integer.parseInt(el.getChildText("min-rpm")); int max_r = Integer.parseInt(el.getChildText("max-rpm")); //log.debug("Notch: " + nn + " File: " + fn); sb = new SoundBite(vf, fn, "Steam_n" + i, "Steam_" + i); sb.setLooped(true); sb.setFadeTimes(100, 100); sb.setGain(setXMLGain(el)); // Store in the list. boolean chuff = false; Element c; if ((c = el.getChild("use-chuff-gen")) != null) { log.debug("Use Chuff Generator " + c.toString()); chuff = true; } rpm_sounds.add(new RPMSound(sb, min_r, max_r, chuff)); i++; } /* // Get the start and stop sounds el = e.getChild("start-sound"); if (el != null) { fn = el.getChild("file").getValue(); log.debug("Start sound: " + fn); start_sound = new SoundBite(vf, fn, "Engine_start", "Engine_Start"); // Handle gain start_sound.setGain(setXMLGain(el)); start_sound.setLooped(false); } */ }
From source file:jmri.jmrit.vsdecoder.VSDecoder.java
License:Open Source License
/** * public void setXML(VSDFile vf, String pn) * * Build this VSDecoder from an XML representation * * @param vf (VSDFile) : VSD File to pull the XML from * @param pn (String) : Parameter Name to find within the VSD File. *//*from w w w. j av a 2 s . c o m*/ @SuppressWarnings({ "cast" }) public void setXml(VSDFile vf, String pn) { Iterator<Element> itr; Element e = null; Element el = null; SoundEvent se; if (vf == null) { log.debug("Null VSD File Name"); return; } log.debug("VSD File Name = " + vf.getName()); // need to choose one. this.setVSDFilePath(vf.getName()); // Find the <profile/> element that matches the name pn // List<Element> profiles = vf.getRoot().getChildren("profile"); // java.util.Iterator i = profiles.iterator(); java.util.Iterator<Element> i = vf.getRoot().getChildren("profile").iterator(); while (i.hasNext()) { e = i.next(); if (e.getAttributeValue("name").equals(pn)) { break; } } // E is now the first <profile/> in vsdfile that matches pn. if (e == null) { // No matching profile name found. return; } // Set this decoder's name. this.setProfileName(e.getAttributeValue("name")); log.debug("Decoder Name = " + e.getAttributeValue("name")); // Read and create all of its components. // Check for default element. if (e.getChild("default") != null) { log.debug("" + getProfileName() + "is default."); is_default = true; } else { is_default = false; } // +++ DEBUG // Log and print all of the child elements. itr = (e.getChildren()).iterator(); while (itr.hasNext()) { // Pull each element from the XML file. el = itr.next(); log.debug("Element: " + el.toString()); if (el.getAttribute("name") != null) { log.debug(" Name: " + el.getAttributeValue("name")); log.debug(" type: " + el.getAttributeValue("type")); } } // --- DEBUG // First, the sounds. String prefix = "" + this.getID() + ":"; log.debug("VSDecoder " + this.getID() + " prefix = " + prefix); itr = (e.getChildren("sound")).iterator(); while (itr.hasNext()) { el = (Element) itr.next(); if (el.getAttributeValue("type") == null) { // Empty sound. Skip. log.debug("Skipping empty Sound."); continue; } else if (el.getAttributeValue("type").equals("configurable")) { // Handle configurable sounds. ConfigurableSound cs = new ConfigurableSound(prefix + el.getAttributeValue("name")); cs.setXml(el, vf); sound_list.put(el.getAttributeValue("name"), cs); } else if (el.getAttributeValue("type").equals("diesel")) { // Handle a Diesel Engine sound DieselSound es = new DieselSound(prefix + el.getAttributeValue("name")); es.setXml(el, vf); sound_list.put(el.getAttributeValue("name"), es); } else if (el.getAttributeValue("type").equals("diesel3")) { // Handle a Diesel Engine sound Diesel3Sound es = new Diesel3Sound(prefix + el.getAttributeValue("name")); es.setXml(el, vf); sound_list.put(el.getAttributeValue("name"), es); } else if (el.getAttributeValue("type").equals("steam")) { // Handle a Diesel Engine sound SteamSound es = new SteamSound(prefix + el.getAttributeValue("name")); es.setXml(el, vf); sound_list.put(el.getAttributeValue("name"), es); } else { // TODO: Some type other than configurable sound. Handle appropriately } } // Next, grab all of the SoundEvents // Have to do the sounds first because the SoundEvent's setXml() will // expect to be able to look it up. itr = (e.getChildren("sound-event")).iterator(); while (itr.hasNext()) { el = (Element) itr.next(); switch (SoundEvent.ButtonType.valueOf(el.getAttributeValue("buttontype").toUpperCase())) { case MOMENTARY: se = new MomentarySoundEvent(el.getAttributeValue("name")); break; case TOGGLE: se = new ToggleSoundEvent(el.getAttributeValue("name")); break; case ENGINE: se = new EngineSoundEvent(el.getAttributeValue("name")); break; case NONE: default: se = new SoundEvent(el.getAttributeValue("name")); } se.setParent(this); se.setXml(el, vf); event_list.put(se.getName(), se); } // Handle other types of children similarly here. // Check for an existing throttle and update speed if it exists. Float s = (Float) InstanceManager.throttleManagerInstance().getThrottleInfo(config.getDccAddress(), "SpeedSetting"); if (s != null) { // Mimic a throttlePropertyChange to propagate the current (init) speed setting of the throttle. log.debug("Existing Throttle found. Speed = " + s); this.throttlePropertyChange(new PropertyChangeEvent(this, "SpeedSetting", null, s)); } else { log.debug("No existing throttle found."); } }
From source file:jmri.jmrit.vsdecoder.VSDecoderManager.java
License:Open Source License
public void loadProfiles(VSDFile vf) { Element root;/* w w w. j a v a 2 s.co m*/ String pname; if ((root = vf.getRoot()) == null) { return; } ArrayList<String> new_entries = new ArrayList<String>(); java.util.Iterator<Element> i = root.getChildren("profile").iterator(); // NOI18N while (i.hasNext()) { Element e = i.next(); log.debug(e.toString()); if ((pname = e.getAttributeValue("name")) != null) { // NOI18N profiletable.put(pname, vf.getName()); new_entries.add(pname); } } // debug /* for (String s : new_entries) { log.debug("New entry: " + s); } */ // /debug fireMyEvent(new VSDManagerEvent(this, VSDManagerEvent.EventType.PROFILE_LIST_CHANGE, new_entries)); }
From source file:model.data.manager.Manager.java
License:Open Source License
@Override public void recovery(String path) { if (path == null || path.isEmpty()) path = VARIABLES.ManagerFilePath; SAXBuilder builder = new SAXBuilder(); File xmlFile = new File(path); boolean recovered = true; try {//from www . j ava2s .c o m Document document = (Document) builder.build(xmlFile); Element root = document.getRootElement(); Element usersElement = root.getChild("users"); for (Element e : usersElement.getChildren()) { userManager.addUser(new User(e)); } Element itemsElement = root.getChild("items"); for (Element e : itemsElement.getChildren()) { itemManager.addItem(new Item(e)); } Element messagesElement = root.getChild("messages"); for (Element e : messagesElement.getChildren()) { messageManager.addMessage(new UserMessage(e)); } Element conversationsElement = root.getChild("ReceivedMessages"); for (Element e : conversationsElement.getChildren()) { messageManager.addConversations(new Conversations(e)); } Element favoritesElement = root.getChild("favorites"); for (Element e : favoritesElement.getChildren()) { favoriteManager.addFavorites(new Favorites(e), false); } Element dealsElement = root.getChild("deals"); for (Element e : dealsElement.getChildren()) { String owner = e.getChild("owner").getText(); if (!contratManager.containsUser(owner) && userManager.userExists(owner)) contratManager.getDeals().put(owner, new ArrayList<Contrat>()); if (e.getChild(Contrat.class.getName()) != null) contratManager.addDeal(owner, new Contrat(e.getChild(Contrat.class.getName()))); } } catch (FileNotFoundException e) { recovered = Printer.printError(this, "recovery", "File \"" + path + "\" doesn't exist"); } catch (IOException e) { recovered = Printer.printError(this, "recovery", "IOException\n\t" + e.toString()); } catch (JDOMException e) { recovered = Printer.printError(this, "recovery", "JDOMException\n\tFile \"" + path + "\" is empty"); xmlFile.delete(); } finally { if (recovered) Printer.printInfo(this, "recovery", "Local data recovered"); } }
From source file:net.instantcom.mm7.MM7Message.java
License:Open Source License
@Override public void load(Element element) { Element body = element.getChild("Body", element.getNamespace()); // Extract MM7 namespace from SOAP body Iterator<?> i = body.getDescendants(new ElementFilter()); while (i.hasNext()) { Element e = (Element) i.next(); Namespace ns = e.getNamespace(); if (ns != null && ns.getURI().contains("MM7")) { this.namespace = ns; break; }/*from w w w.ja va2 s. c o m*/ } if (this.namespace == null) { throw new IllegalStateException("can't autodetect MM7 namespace: " + body.toString()); } Element header = element.getChild("Header", element.getNamespace()); setTransactionId(header.getChildTextTrim("TransactionID", namespace)); }
From source file:open.dolphin.impl.mml.MMLHelper23.java
/** * soaSpec ?? pSpec? xhtml ?????//w ww. j av a2s .c o m */ private void parse(String spec) { try { BufferedReader reader = new BufferedReader(new StringReader(spec)); SAXBuilder docBuilder = new SAXBuilder(); Document doc = docBuilder.build(reader); Element root = doc.getRootElement(); debug(root.toString()); parseChildren(root); reader.close(); } catch (JDOMException | IOException e) { e.printStackTrace(System.err); } }