List of usage examples for org.dom4j Node getName
String getName();
getName
returns the name of this node.
From source file:pt.webdetails.cdv.notifications.EmailOutlet.java
License:Open Source License
private Session buildSession() { final Properties props = new Properties(); try {/*www . j av a 2s . c o m*/ final Document configDocument = PentahoSystem.getSystemSettings() .getSystemSettingsDocument("smtp-email/email_config.xml"); //$NON-NLS-1$ final List<Node> properties = configDocument.selectNodes("/email-smtp/properties/*"); //$NON-NLS-1$ for (Node propertyNode : properties) { final String propertyName = propertyNode.getName(); final String propertyValue = propertyNode.getText(); props.put(propertyName, propertyValue); } } catch (Exception e) { logger.error("Failed to build session: " + e.getMessage()); } final boolean authenticate = "true".equals(props.getProperty("mail.smtp.auth")); //$NON-NLS-1$//$NON-NLS-2$ // Get a Session object final Session session; if (authenticate) { final Authenticator authenticator = new EmailAuthenticator(); session = Session.getInstance(props, authenticator); } else { session = Session.getInstance(props); } // if debugging is not set in the email config file, match the // component debug setting if (!props.containsKey("mail.debug")) { //$NON-NLS-1$ session.setDebug(true); } return session; }
From source file:revaligner.service.FileAligner.java
public void fixTxlfTrgTags(org.dom4j.Document doc) throws Exception { org.dom4j.Element root = doc.getRootElement(); List<org.dom4j.Element> list_transunit = root.selectNodes("//*[name() = 'trans-unit']"); for (org.dom4j.Element e : list_transunit) { org.dom4j.Element source = e.element("source"); org.dom4j.Element target = e.element("target"); if ((target != null) && (!target.getText().equals(""))) { HashMap<String, List<Object>> src_map = new HashMap(); boolean tagnodeappear = false; boolean textnodeappear = false; boolean sourcetagsurroundtext = true; int maxid = 1; for (int i = 0; i < source.content().size(); i++) { org.dom4j.Node node = (org.dom4j.Node) source.content().get(i); if (node.getNodeType() == 1) { org.dom4j.Element tag = (org.dom4j.Element) node; maxid = Math.max(maxid, Integer.parseInt(tag.attributeValue("id"))); }// ww w.ja va 2 s . c o m } maxid++; for (int i = 0; i < source.content().size(); i++) { org.dom4j.Node node = (org.dom4j.Node) source.content().get(i); if (node.getNodeType() == 1) { if (textnodeappear) { tagnodeappear = true; } org.dom4j.Element tag = (org.dom4j.Element) node; String key = ""; if ((tag.getName().equals("ex")) || (tag.getName().equals("ept"))) { key = tag.getName() + "#" + tag.attributeValue("rid"); } else if (tag.getName().equals("x")) { key = tag.getName() + "#" + tag.attributeValue("ctype"); } else { key = tag.getName(); } if (src_map.containsKey(key)) { ((List) src_map.get(key)).add(node.clone()); } else { List<Object> list = new ArrayList(); list.add(node.clone()); src_map.put(key, list); } } else { if (tagnodeappear) { sourcetagsurroundtext = false; } textnodeappear = true; } } HashMap<String, String> rid_map = new HashMap(); List trg_contents = new ArrayList(); boolean targetnotag = true; for (int i = 0; i < target.content().size(); i++) { org.dom4j.Node node = (org.dom4j.Node) target.content().get(i); if (node.getNodeType() == 1) { org.dom4j.Element tag = (org.dom4j.Element) node; String rid = tag.attributeValue("rid"); String key = ""; if ((tag.getName().equals("ph")) || (tag.getName().equals("bpt")) || (tag.getName().equals("bx"))) { key = tag.getName(); if (src_map.containsKey(key)) { List list = (List) src_map.get(key); trg_contents.add(list.get(0)); targetnotag = false; rid_map.put(rid, ((org.dom4j.Element) list.get(0)).attributeValue("rid")); list.remove(0); if (list.size() == 0) { src_map.remove(key); } } } else if (tag.getName().equals("x")) { key = tag.getName() + "#" + tag.attributeValue("ctype"); if (src_map.containsKey(key)) { List list = (List) src_map.get(key); trg_contents.add(list.get(0)); targetnotag = false; list.remove(0); if (list.size() == 0) { src_map.remove(key); } } else if ((tag.getName().equals("x")) && (tag.attributeValue("ctype").equals("x-tab"))) { org.dom4j.Element tab = DocumentHelper.createElement("x"); tab.addAttribute("ctype", "x-tab"); tab.addAttribute("id", Integer.toString(maxid)); maxid++; tab.addAttribute("equiv-text", " "); trg_contents.add(tab); } else if ((tag.getName().equals("x")) && (tag.attributeValue("ctype").equals("lb"))) { org.dom4j.Element lb = DocumentHelper.createElement("x"); lb.addAttribute("ctype", "lb"); lb.addAttribute("id", Integer.toString(maxid)); maxid++; lb.addAttribute("equiv-text", " "); trg_contents.add(lb); } } else if ((tag.getName().equals("ex")) || (tag.getName().equals("ept"))) { String mapped_rid = (String) rid_map.get(tag.attributeValue("rid")); key = tag.getName() + "#" + mapped_rid; if (src_map.containsKey(key)) { List list = (List) src_map.get(key); trg_contents.add(list.get(0)); rid_map.put(rid, ((org.dom4j.Element) list.get(0)).attributeValue("rid")); list.remove(0); if (list.size() == 0) { src_map.remove(key); } } } } else if (node.getNodeType() == 3) { String text = node.getText(); if (text.contains("<br> ")) { String key = "x#lb"; String[] ss = text.split("<br> "); for (int s = 0; s < ss.length; s++) { trg_contents.add(DocumentHelper.createText(ss[s])); if (s < ss.length - 1) { if (src_map.containsKey(key)) { List list = (List) src_map.get(key); trg_contents.add(list.get(0)); targetnotag = false; list.remove(0); if (list.size() == 0) { src_map.remove(key); } } else { org.dom4j.Element lb = DocumentHelper.createElement("x"); lb.addAttribute("ctype", "lb"); lb.addAttribute("id", Integer.toString(maxid)); maxid++; lb.addAttribute("equiv-text", " "); trg_contents.add(lb); } } } } else if (text.contains("<br>")) { String key = "x#lb"; String[] ss = text.split("<br> "); for (int s = 0; s < ss.length; s++) { trg_contents.add(DocumentHelper.createText(ss[s])); if (s < ss.length - 1) { if (src_map.containsKey(key)) { List list = (List) src_map.get(key); trg_contents.add(list.get(0)); targetnotag = false; list.remove(0); if (list.size() == 0) { src_map.remove(key); } } else { org.dom4j.Element lb = DocumentHelper.createElement("x"); lb.addAttribute("ctype", "lb"); lb.addAttribute("id", Integer.toString(maxid)); maxid++; lb.addAttribute("equiv-text", " "); trg_contents.add(lb); } } } } else { trg_contents.add(node); } } else { trg_contents.add(node); } } while (trg_contents.size() > 0) { org.dom4j.Node nd = (org.dom4j.Node) trg_contents.get(0); if (nd.getNodeType() == 3) { if (nd.getText().trim().equals("")) { trg_contents.remove(0); } else { nd.setText(nd.getText().replaceAll("^(\\s)+", "")); break; } } else if ((nd.getNodeType() == 1) && (nd.getName().equals("x")) && (((org.dom4j.Element) nd).attributeValue("ctype").equals("lb"))) { trg_contents.remove(0); } else { if ((nd.getNodeType() != 1) || (!nd.getName().equals("x")) || (!((org.dom4j.Element) nd).attributeValue("ctype").equals("x-tab"))) { break; } trg_contents.remove(0); } } while (trg_contents.size() > 0) { org.dom4j.Node nd = (org.dom4j.Node) trg_contents.get(trg_contents.size() - 1); if (nd.getNodeType() == 3) { if (nd.getText().trim().equals("")) { trg_contents.remove(trg_contents.size() - 1); } else { nd.setText(nd.getText().replaceAll("(\\s)+$", "")); break; } } else if ((nd.getNodeType() == 1) && (nd.getName().equals("x")) && (((org.dom4j.Element) nd).attributeValue("ctype").equals("lb"))) { trg_contents.remove(trg_contents.size() - 1); } else { if ((nd.getNodeType() != 1) || (!nd.getName().equals("x")) || (!((org.dom4j.Element) nd).attributeValue("ctype").equals("x-tab"))) { break; } trg_contents.remove(trg_contents.size() - 1); } } boolean isnotignoredsegment = true; if (trg_contents.size() == 1) { org.dom4j.Node node = (org.dom4j.Node) trg_contents.get(0); if ((node.getNodeType() == 3) && (node.getText().replaceFirst("", "").equals(""))) { isnotignoredsegment = false; } } if ((targetnotag) && (sourcetagsurroundtext) && (isnotignoredsegment)) { boolean isleadingtag = true; int index = 0; for (int i = 0; i < source.content().size(); i++) { org.dom4j.Node node = (org.dom4j.Node) source.content().get(i); if (node.getNodeType() == 1) { if (isleadingtag) { trg_contents.add(index, node); index++; } else { trg_contents.add(node); } } else { isleadingtag = false; index++; } } } target.setContent(trg_contents); } } }
From source file:treesim.TreeSim.java
private static void preParentOrderFirstDOM(Node n) { if (n != null) { if (n.getParent() == null) { System.out.println("ROOT -- \"" + n.getName() + "\""); elementNodes.add(n.getName()); } else {// w w w. j ava 2 s . c om System.out.println("\"" + n.getParent().getName() + "\" -- \"" + n.getName() + "\""); elementNodes.add(n.getName()); } for (Object a : ((Element) n).attributes()) { System.out.println("\"" + n.getName() + "\" -- \"" + ((Attribute) a).getName() + " " + ((Attribute) a).getValue() + "\""); attributeParentNodes.add(n.getName()); attributeNodes.add(((Attribute) a).getName()); } if (!n.getText().trim().equals("")) { System.out.println("\"" + n.getName() + "\" -- \"" + n.getText().trim() + "\""); textNodeConnections.add(n.getName()); } for (Object o : n.selectNodes("child::*")) { if (n.getNodeType() == Node.ELEMENT_NODE) { preParentOrderFirstDOM((Node) o); } } } }
From source file:treesim.TreeSim.java
private static void preParentOrderSecondDOM(Node n) { if (n != null) { if (n.getParent() == null) { System.out.println("ROOT -- \"" + n.getName() + "\""); elementNodes2.add(n.getName()); } else {/*from www .j a va 2 s .co m*/ System.out.println("\"" + n.getParent().getName() + "\" -- \"" + n.getName() + "\""); elementNodes2.add(n.getName()); } for (Object a : ((Element) n).attributes()) { System.out.println("\"" + n.getName() + "\" -- \"" + ((Attribute) a).getName() + " " + ((Attribute) a).getValue() + "\""); attributeParentNodes2.add(n.getName()); attributeNodes2.add(((Attribute) a).getName()); } if (!n.getText().trim().equals("")) { System.out.println("\"" + n.getName() + "\" -- \"" + n.getText().trim() + "\""); textNodeConnections2.add(n.getName()); } for (Object o : n.selectNodes("child::*")) { if (n.getNodeType() == Node.ELEMENT_NODE) { preParentOrderSecondDOM((Node) o); } } } }