List of usage examples for org.dom4j Element clearContent
void clearContent();
Node
instances this branch may contain. From source file:org.withinsea.izayoi.cortile.template.html.grammar.attr.core.Output.java
License:Mozilla Public License
@Override @Priority(Priority.LOWER)//from w w w .j av a 2 s .c o m public void processAttr(Attribute attr) throws CortileException { Element elem = attr.getParent(); String attrname = attr.getName(); String attrvalue = attr.getValue(); if (attrname.startsWith("attr.")) { String forAttrname = attrname.substring("attr.".length()); Attribute forAttr = elem.attribute(forAttrname); String escapedValue = attrvalue .replace("<%=", "<%=" + Output.class.getCanonicalName() + ".escapeAttrValue(") .replace("%>", ")%>"); if (forAttr == null) { elem.addAttribute(forAttrname, escapedValue); } else { forAttr.setValue(escapedValue); } } else { elem.clearContent(); elem.addText(attr.getValue()); } attr.detach(); }
From source file:revaligner.service.FileAligner.java
public void update(JSONArray arr, JSONArray missings, JSONArray locks, JSONArray segaligned, JSONArray targets, JSONArray missing_targets, int cnt) throws Exception { File alignedFile = new File(this.alignedfile); if (!alignedFile.exists()) { throw new FileNotFoundException("Could not find aligned xml file"); }/*from w w w.ja va 2s. c om*/ ExtractionSupportImpl extractionSupportImpl = new ExtractionSupportImpl( Locale.makeLocale(this.sourcelanguage), Locale.makeLocale(this.targetlanguage)); Configuration config = new BaseConfiguration(); config.setProperty("extraction.tokens.extract", "all"); extractionSupportImpl.setConfiguration(config); List<JSONArray> trg_list = new ArrayList(); for (int i = 0; i < arr.length(); i++) { trg_list.add(arr.getJSONArray(i)); } List<JSONArray> missings_list = new ArrayList(); for (int i = 0; i < missings.length(); i++) { missings_list.add(missings.getJSONArray(i)); } List<String> locks_list = new ArrayList(); for (int i = 0; i < locks.length(); i++) { locks_list.add(locks.getString(i)); } List<String> segaligned_list = new ArrayList(); for (int i = 0; i < segaligned.length(); i++) { segaligned_list.add(segaligned.getString(i)); } this.nullcnt = cnt; int unitcnt = 0; org.dom4j.Document document = XmlParser.parseXmlFile(this.alignedfile); org.dom4j.Element root = document.getRootElement(); List<org.dom4j.Element> groups = document.getRootElement().element("aligned").elements("group"); for (int i = 0; i < groups.size(); i++) { org.dom4j.Element group = (org.dom4j.Element) groups.get(i); List<org.dom4j.Element> units = group.elements("unit"); for (int j = 0; j < units.size(); j++) { unitcnt++; org.dom4j.Element unit = (org.dom4j.Element) units.get(j); String seq = i + " - " + j; if (locks_list.contains(seq)) { unit.attribute("locked").setValue("true"); } else { unit.attribute("locked").setValue("false"); } if (segaligned_list.contains(seq)) { unit.attribute("alignsegs").setValue("true"); } else { unit.attribute("alignsegs").setValue("false"); } if (unitcnt <= trg_list.size()) { JSONArray segs = (JSONArray) trg_list.get(unitcnt - 1); String trg_para_text = targets.getString(unitcnt - 1); org.dom4j.Element trg_para = unit.element("trg_para"); if (trg_para != null) { trg_para.clearContent(); } else { trg_para = unit.addElement("trg_para"); } trg_para.addAttribute("id", segs.getString(0)); org.dom4j.Element trg_text = trg_para.addElement("text"); org.dom4j.Element trg_segs = trg_para.addElement("segments"); for (int s = 1; s < segs.length(); s++) { org.dom4j.Element trg_seg = trg_segs.addElement("trg_seg"); trg_seg.addAttribute("id", Integer.toString(s - 1)); trg_seg.addAttribute("isExtractable", Boolean.toString(extractionSupportImpl.isExtractable(segs.getString(s)))); trg_seg.setText(decodehtmlstring(segs.getString(s))); } trg_text.setText(decodehtmlstring(trg_para_text)); } else { units.remove(j); j--; } } if (group.elements("unit").size() == 0) { groups.remove(i); i--; } } org.dom4j.Element orphans = root.element("orphans"); orphans.clearContent(); for (int i = 0; i < missings_list.size(); i++) { JSONArray segs = (JSONArray) missings_list.get(i); String trg_para_text = missing_targets.getString(i); org.dom4j.Element unit = orphans.addElement("unit"); unit.addAttribute("id", Integer.toString(i)); org.dom4j.Element trg_para = unit.addElement("trg_para"); trg_para.addAttribute("id", segs.getString(0)); org.dom4j.Element trg_text = trg_para.addElement("text"); org.dom4j.Element trg_segs = trg_para.addElement("segments"); for (int s = 1; s < segs.length(); s++) { org.dom4j.Element trg_seg = trg_segs.addElement("trg_seg"); trg_seg.addAttribute("id", Integer.toString(s)); trg_seg.addAttribute("isExtractable", Boolean.toString(extractionSupportImpl.isExtractable(segs.getString(s)))); trg_seg.setText(decodehtmlstring(segs.getString(s))); } trg_text.setText(decodehtmlstring(trg_para_text)); } new File(this.alignedfile).delete(); OutputStreamWriter writer = new OutputStreamWriter( new BufferedOutputStream(new FileOutputStream(this.alignedfile)), "UTF8"); document.write(writer); writer.close(); }
From source file:revaligner.service.FileAligner.java
public void update_seg(JSONArray targets, JSONArray trg_seqs, JSONArray missing_targets, JSONArray missing_trg_seqs, JSONArray locks, int cnt, JSONArray edited, JSONArray review, JSONArray ignore) throws Exception { File alignedFile = new File(this.alignedfile); if (!alignedFile.exists()) { throw new FileNotFoundException("Could not find aligned xml file"); }/*from www . j a va 2s.c o m*/ ExtractionSupportImpl extractionSupportImpl = new ExtractionSupportImpl( Locale.makeLocale(this.sourcelanguage), Locale.makeLocale(this.targetlanguage)); Configuration config = new BaseConfiguration(); config.setProperty("extraction.tokens.extract", "all"); extractionSupportImpl.setConfiguration(config); List<String> trg_list = new ArrayList(); for (int i = 0; i < targets.length(); i++) { trg_list.add(targets.getString(i)); } List<String> trg_seq_list = new ArrayList(); for (int i = 0; i < trg_seqs.length(); i++) { trg_seq_list.add(trg_seqs.getString(i)); } List<String> missing_list = new ArrayList(); for (int i = 0; i < missing_targets.length(); i++) { missing_list.add(missing_targets.getString(i)); } List<String> missing_seq_list = new ArrayList(); for (int i = 0; i < missing_trg_seqs.length(); i++) { missing_seq_list.add(missing_trg_seqs.getString(i)); } List<String> locks_list = new ArrayList(); for (int i = 0; i < locks.length(); i++) { locks_list.add(locks.getString(i)); } this.nullcnt = cnt; List<String> edited_list = new ArrayList(); for (int i = 0; i < edited.length(); i++) { edited_list.add(edited.getString(i)); } List<String> review_list = new ArrayList(); for (int i = 0; i < review.length(); i++) { review_list.add(review.getString(i)); } List<String> ignore_list = new ArrayList(); for (int i = 0; i < ignore.length(); i++) { ignore_list.add(ignore.getString(i)); } int segcnt = 0; org.dom4j.Document document = XmlParser.parseXmlFile(this.alignedfile); org.dom4j.Element root = document.getRootElement(); List<org.dom4j.Element> groups = document.getRootElement().element("aligned").elements("group"); for (int i = 0; i < groups.size(); i++) { org.dom4j.Element group = (org.dom4j.Element) groups.get(i); List<org.dom4j.Element> units = group.elements("unit"); for (int j = 0; j < units.size(); j++) { org.dom4j.Element unit = (org.dom4j.Element) units.get(j); unit.addAttribute("alignsegs", "true"); org.dom4j.Element src_para = unit.element("src_para"); if (src_para != null) { List<org.dom4j.Element> src_segs = src_para.element("segments").elements("src_seg"); for (int z = 0; z < src_segs.size(); z++) { org.dom4j.Element src_seg = (org.dom4j.Element) src_segs.get(z); String seq = i + " - " + j + " - " + z; if (locks_list.contains(seq)) { src_seg.attribute("locked").setValue("true"); } else { src_seg.attribute("locked").setValue("false"); } if (review_list.contains(seq)) { src_seg.attribute("needreview").setValue("true"); } else { src_seg.attribute("needreview").setValue("false"); } if (ignore_list.contains(seq)) { src_seg.attribute("ignored").setValue("true"); } else { src_seg.attribute("ignored").setValue("false"); } } org.dom4j.Element trg_para = unit.element("trg_para"); trg_para.remove(trg_para.element("text")); List<org.dom4j.Element> trg_segs = trg_para.element("segments").elements("trg_seg"); for (int z = 0; z < trg_segs.size(); z++) { org.dom4j.Element trg_seg = (org.dom4j.Element) trg_segs.get(z); if (z >= src_segs.size()) { trg_para.element("segments").remove(trg_seg); } else if (segcnt < trg_list.size()) { trg_seg.addAttribute("id", decodehtmlstring((String) trg_seq_list.get(segcnt))); if ((edited_list.contains(decodehtmlstring((String) trg_seq_list.get(segcnt)))) || (decodehtmlstring((String) trg_seq_list.get(segcnt)).startsWith("n - "))) { trg_seg.addAttribute("edited", "true"); } String text = decodehtmlstring((String) trg_list.get(segcnt)); trg_seg.addAttribute("isExtractable", Boolean.toString(extractionSupportImpl.isExtractable(text))); trg_seg.setText(text); segcnt++; } else { trg_para.element("segments").remove(trg_seg); } } if (trg_para.element("segments").elements("trg_seg").size() == 0) { group.remove(unit); } } else { org.dom4j.Element trg_para = unit.element("trg_para"); trg_para.remove(trg_para.element("text")); List<org.dom4j.Element> trg_segs = trg_para.element("segments").elements("trg_seg"); for (int z = 0; z < trg_segs.size(); z++) { org.dom4j.Element trg_seg = (org.dom4j.Element) trg_segs.get(z); if (segcnt < trg_list.size()) { trg_seg.addAttribute("id", decodehtmlstring((String) trg_seq_list.get(segcnt))); if ((edited_list.contains(decodehtmlstring((String) trg_seq_list.get(segcnt)))) || (decodehtmlstring((String) trg_seq_list.get(segcnt)).startsWith("n - "))) { trg_seg.addAttribute("edited", "true"); } String text = decodehtmlstring((String) trg_list.get(segcnt)); trg_seg.addAttribute("isExtractable", Boolean.toString(extractionSupportImpl.isExtractable(text))); trg_seg.setText(text); segcnt++; } else { trg_para.element("segments").remove(trg_seg); } } if (trg_para.element("segments").elements("trg_seg").size() == 0) { group.remove(unit); } } } if (group.elements("unit").size() == 0) { groups.remove(i); i--; } } if (segcnt < trg_list.size()) { org.dom4j.Element group = root.element("aligned").addElement("group"); group.addAttribute("id", Integer.toString(groups.size())); org.dom4j.Element unit = group.addElement("unit"); unit.addAttribute("id", "0"); org.dom4j.Element trg_para = unit.addElement("trg_para"); trg_para.addAttribute("id", Integer.toString(groups.size()) + " - 0"); org.dom4j.Element trgsegs = trg_para.addElement("segments"); for (int x = segcnt; x < trg_list.size(); x++) { String text = decodehtmlstring((String) trg_list.get(x)); org.dom4j.Element trg_seg = trgsegs.addElement("trg_seg"); trg_seg.addAttribute("id", decodehtmlstring((String) trg_seq_list.get(x))); if ((edited_list.contains(decodehtmlstring((String) trg_seq_list.get(x)))) || (decodehtmlstring((String) trg_seq_list.get(x)).startsWith("n - "))) { trg_seg.addAttribute("edited", "true"); } trg_seg.addAttribute("isExtractable", Boolean.toString(extractionSupportImpl.isExtractable(text))); trg_seg.setText(text); } } org.dom4j.Element orphans = root.element("orphans"); orphans.clearContent(); org.dom4j.Element unit = orphans.addElement("unit"); unit.addAttribute("id", "0"); org.dom4j.Element trg_para = unit.addElement("trg_para"); trg_para.addAttribute("id", "0 - 0"); org.dom4j.Element trg_segs = trg_para.addElement("segments"); for (int i = 0; i < missing_list.size(); i++) { org.dom4j.Element trg_seg = trg_segs.addElement("trg_seg"); String text = decodehtmlstring((String) missing_list.get(i)); trg_seg.addAttribute("id", decodehtmlstring((String) missing_seq_list.get(i))); if ((edited_list.contains(decodehtmlstring((String) missing_seq_list.get(i)))) || (decodehtmlstring((String) trg_seq_list.get(i)).startsWith("n - "))) { trg_seg.addAttribute("edited", "true"); } trg_seg.addAttribute("isExtractable", Boolean.toString(extractionSupportImpl.isExtractable(text))); trg_seg.setText(text); } new File(this.alignedfile).delete(); OutputStreamWriter writer = new OutputStreamWriter( new BufferedOutputStream(new FileOutputStream(this.alignedfile)), "UTF8"); document.write(writer); writer.close(); if (new File(this.auto_saved_alignedfile).exists()) { new File(this.auto_saved_alignedfile).delete(); } }
From source file:revaligner.service.FileAligner.java
public void auto_update_seg(JSONArray targets, JSONArray trg_seqs, JSONArray missing_targets, JSONArray missing_trg_seqs, JSONArray locks, int cnt, JSONArray edited, JSONArray review, JSONArray ignore) throws Exception { File alignedFile = new File(this.alignedfile); if (!alignedFile.exists()) { throw new FileNotFoundException("Could not find aligned xml file"); }// www . j a v a 2s . c om ExtractionSupportImpl extractionSupportImpl = new ExtractionSupportImpl( Locale.makeLocale(this.sourcelanguage), Locale.makeLocale(this.targetlanguage)); Configuration config = new BaseConfiguration(); config.setProperty("extraction.tokens.extract", "all"); extractionSupportImpl.setConfiguration(config); List<String> trg_list = new ArrayList(); for (int i = 0; i < targets.length(); i++) { trg_list.add(targets.getString(i)); } List<String> trg_seq_list = new ArrayList(); for (int i = 0; i < trg_seqs.length(); i++) { trg_seq_list.add(trg_seqs.getString(i)); } List<String> missing_list = new ArrayList(); for (int i = 0; i < missing_targets.length(); i++) { missing_list.add(missing_targets.getString(i)); } List<String> missing_seq_list = new ArrayList(); for (int i = 0; i < missing_trg_seqs.length(); i++) { missing_seq_list.add(missing_trg_seqs.getString(i)); } List<String> locks_list = new ArrayList(); for (int i = 0; i < locks.length(); i++) { locks_list.add(locks.getString(i)); } this.nullcnt = cnt; List<String> edited_list = new ArrayList(); for (int i = 0; i < edited.length(); i++) { edited_list.add(edited.getString(i)); } List<String> review_list = new ArrayList(); for (int i = 0; i < review.length(); i++) { review_list.add(review.getString(i)); } List<String> ignore_list = new ArrayList(); for (int i = 0; i < ignore.length(); i++) { ignore_list.add(ignore.getString(i)); } int segcnt = 0; org.dom4j.Document document = XmlParser.parseXmlFile(this.alignedfile); org.dom4j.Element root = document.getRootElement(); List<org.dom4j.Element> groups = document.getRootElement().element("aligned").elements("group"); for (int i = 0; i < groups.size(); i++) { org.dom4j.Element group = (org.dom4j.Element) groups.get(i); List<org.dom4j.Element> units = group.elements("unit"); for (int j = 0; j < units.size(); j++) { org.dom4j.Element unit = (org.dom4j.Element) units.get(j); unit.addAttribute("alignsegs", "true"); org.dom4j.Element src_para = unit.element("src_para"); if (src_para != null) { List<org.dom4j.Element> src_segs = src_para.element("segments").elements("src_seg"); for (int z = 0; z < src_segs.size(); z++) { org.dom4j.Element src_seg = (org.dom4j.Element) src_segs.get(z); String seq = i + " - " + j + " - " + z; if (locks_list.contains(seq)) { src_seg.attribute("locked").setValue("true"); } else { src_seg.attribute("locked").setValue("false"); } if (review_list.contains(seq)) { src_seg.attribute("needreview").setValue("true"); } else { src_seg.attribute("needreview").setValue("false"); } if (ignore_list.contains(seq)) { src_seg.attribute("ignored").setValue("true"); } else { src_seg.attribute("ignored").setValue("false"); } } org.dom4j.Element trg_para = unit.element("trg_para"); trg_para.remove(trg_para.element("text")); List<org.dom4j.Element> trg_segs = trg_para.element("segments").elements("trg_seg"); for (int z = 0; z < trg_segs.size(); z++) { org.dom4j.Element trg_seg = (org.dom4j.Element) trg_segs.get(z); if (z >= src_segs.size()) { trg_para.element("segments").remove(trg_seg); } else if (segcnt < trg_list.size()) { trg_seg.addAttribute("id", decodehtmlstring((String) trg_seq_list.get(segcnt))); if ((edited_list.contains(decodehtmlstring((String) trg_seq_list.get(segcnt)))) || (decodehtmlstring((String) trg_seq_list.get(segcnt)).startsWith("n - "))) { trg_seg.addAttribute("edited", "true"); } String text = decodehtmlstring((String) trg_list.get(segcnt)); trg_seg.addAttribute("isExtractable", Boolean.toString(extractionSupportImpl.isExtractable(text))); trg_seg.setText(text); segcnt++; } else { trg_para.element("segments").remove(trg_seg); } } if (trg_para.element("segments").elements("trg_seg").size() == 0) { group.remove(unit); } } else { org.dom4j.Element trg_para = unit.element("trg_para"); trg_para.remove(trg_para.element("text")); List<org.dom4j.Element> trg_segs = trg_para.element("segments").elements("trg_seg"); for (int z = 0; z < trg_segs.size(); z++) { org.dom4j.Element trg_seg = (org.dom4j.Element) trg_segs.get(z); if (segcnt < trg_list.size()) { trg_seg.addAttribute("id", decodehtmlstring((String) trg_seq_list.get(segcnt))); if ((edited_list.contains(decodehtmlstring((String) trg_seq_list.get(segcnt)))) || (decodehtmlstring((String) trg_seq_list.get(segcnt)).startsWith("n - "))) { trg_seg.addAttribute("edited", "true"); } String text = decodehtmlstring((String) trg_list.get(segcnt)); trg_seg.addAttribute("isExtractable", Boolean.toString(extractionSupportImpl.isExtractable(text))); trg_seg.setText(text); segcnt++; } else { trg_para.element("segments").remove(trg_seg); } } if (trg_para.element("segments").elements("trg_seg").size() == 0) { group.remove(unit); } } } if (group.elements("unit").size() == 0) { groups.remove(i); i--; } } if (segcnt < trg_list.size()) { org.dom4j.Element group = root.element("aligned").addElement("group"); group.addAttribute("id", Integer.toString(groups.size())); org.dom4j.Element unit = group.addElement("unit"); unit.addAttribute("id", "0"); org.dom4j.Element trg_para = unit.addElement("trg_para"); org.dom4j.Element trgsegs = trg_para.addElement("segments"); for (int x = segcnt; x < trg_list.size(); x++) { String text = decodehtmlstring((String) trg_list.get(x)); org.dom4j.Element trg_seg = trgsegs.addElement("trg_seg"); trg_seg.addAttribute("id", decodehtmlstring((String) trg_seq_list.get(x))); if ((edited_list.contains(decodehtmlstring((String) trg_seq_list.get(x)))) || (decodehtmlstring((String) trg_seq_list.get(x)).startsWith("n - "))) { trg_seg.addAttribute("edited", "true"); } trg_seg.addAttribute("isExtractable", Boolean.toString(extractionSupportImpl.isExtractable(text))); trg_seg.setText(text); } } org.dom4j.Element orphans = root.element("orphans"); orphans.clearContent(); org.dom4j.Element unit = orphans.addElement("unit"); unit.addAttribute("id", "0"); org.dom4j.Element trg_para = unit.addElement("trg_para"); org.dom4j.Element trg_segs = trg_para.addElement("segments"); for (int i = 0; i < missing_list.size(); i++) { org.dom4j.Element trg_seg = trg_segs.addElement("trg_seg"); String text = decodehtmlstring((String) missing_list.get(i)); trg_seg.addAttribute("id", decodehtmlstring((String) missing_seq_list.get(i))); if ((edited_list.contains(decodehtmlstring((String) missing_seq_list.get(i)))) || (decodehtmlstring((String) trg_seq_list.get(i)).startsWith("n - "))) { trg_seg.addAttribute("edited", "true"); } trg_seg.addAttribute("isExtractable", Boolean.toString(extractionSupportImpl.isExtractable(text))); trg_seg.setText(text); } this.auto_saved_alignedfile = (this.alignedfile + ".temp"); if (new File(this.auto_saved_alignedfile).exists()) { new File(this.auto_saved_alignedfile).delete(); } OutputStreamWriter writer = new OutputStreamWriter( new BufferedOutputStream(new FileOutputStream(this.auto_saved_alignedfile)), "UTF8"); document.write(writer); writer.close(); }
From source file:zutil.net.ws.soap.SOAPHttpPage.java
License:Open Source License
protected Document genSOAPResponse(String xml, WSInterface obj) { Document document = DocumentHelper.createDocument(); Element envelope = document.addElement("soap:Envelope"); try {/*from w w w.j av a2 s.c o m*/ envelope.addNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/"); envelope.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); envelope.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema"); Element body = envelope.addElement("soap:Body"); try { Element request = getXMLRoot(xml); if (request == null) return document; // Header if (request.element("Header") != null) { Element header = envelope.addElement("soap:Header"); prepareInvoke(obj, request.element("Header"), header); } // Body if (request.element("Body") != null) { prepareInvoke(obj, request.element("Body"), body); } } catch (Throwable e) { body.clearContent(); Element fault = body.addElement("soap:Fault"); // The fault source if (e instanceof SOAPException || e instanceof SAXException || e instanceof DocumentException) fault.addElement("faultcode").setText("soap:Client"); else fault.addElement("faultcode").setText("soap:Server"); // The fault message if (e.getMessage() == null || e.getMessage().isEmpty()) fault.addElement("faultstring").setText("" + e.getClass().getSimpleName()); else fault.addElement("faultstring").setText("" + e.getMessage()); logger.log(Level.WARNING, "Caught exception from SOAP Class", e); } } catch (Exception e) { logger.log(Level.WARNING, "Exception in SOAP generation", e); } return document; }