List of usage examples for org.dom4j Element addAttribute
Element addAttribute(QName qName, String value);
From source file:com.globalsight.ling.docproc.worldserver.WsSkeletonDispose.java
License:Apache License
@Override public String dealSkeleton(String skeleton, String localizedBy) { boolean localizedByUser = false; boolean localizedByMT = false; boolean localizedByLocalTM = false; if (localizedBy != null && localizedBy.equals(PageTemplate.byUser)) { localizedByUser = true;/*from w w w .j av a 2s .c o m*/ } else if (localizedBy != null && localizedBy.equals(PageTemplate.byMT)) { localizedByMT = true; } else if (localizedBy != null && localizedBy.equals(PageTemplate.byLocalTM)) { localizedByLocalTM = true; } int begin = skeleton.indexOf("<" + IWS_SEGMENT_DATA); int end = skeleton.indexOf("</" + IWS_SEGMENT_DATA) + ("</" + IWS_SEGMENT_DATA + ">").length(); String iwsStr = skeleton.substring(begin, end); iwsStr = "<segmentdata " + "xmlns:iws=\"http://www.idiominc.com/ws/asset\">" + iwsStr + "</segmentdata>"; Document dom = getDom(iwsStr); Element root = dom.getRootElement(); List iwsStatusList = root.selectNodes("//iws:status"); for (int x = 0; x < iwsStatusList.size(); x++) { Element status = (Element) iwsStatusList.get(x); if (localizedByUser || localizedByLocalTM || localizedByMT) { /* * if (status.attribute("match-quality") != null) * status.remove(status.attribute("match-quality")); * * if (status.attribute("source_content") != null) * status.remove(status.attribute("source_content")); * * if (status.attribute("target_content") != null) * status.remove(status.attribute("target_content")); */ List<Attribute> attrList = new ArrayList(); attrList.addAll(status.attributes()); for (int i = 0; i < attrList.size(); i++) { String name = attrList.get(i).getName(); if (!name.equals(IWS_TRANSLATION_STATUS) && !name.equals(IWS_TRANSLATION_TYPE) && !name.equals(IWS_SOURCE_CONTENT)) { status.remove(attrList.get(i)); } } } if (status.attribute(IWS_TRANSLATION_STATUS) != null) { if (localizedByUser) { status.attribute(IWS_TRANSLATION_STATUS).setValue("finished"); } else if (localizedByLocalTM) { status.attribute(IWS_TRANSLATION_STATUS).setValue("pending"); } } else { if (localizedByUser) { status.addAttribute(IWS_TRANSLATION_STATUS, "finished"); } else if (localizedByLocalTM) { status.addAttribute(IWS_TRANSLATION_STATUS, "pending"); } } if (status.attribute(IWS_TRANSLATION_TYPE) != null) { if (localizedByMT) { status.attribute(IWS_TRANSLATION_TYPE).setValue("machine_translation_mt"); } else if (localizedByUser || localizedByLocalTM) { status.attribute(IWS_TRANSLATION_TYPE).setValue("manual_translation"); } } else { if (localizedBy != null) { if (localizedByMT) { status.addAttribute(IWS_TRANSLATION_TYPE, "machine_translation_mt"); } else if (localizedByUser || localizedByLocalTM) { status.addAttribute(IWS_TRANSLATION_TYPE, "manual_translation"); } } } } iwsStr = dom.selectSingleNode("//iws:segment-metadata").asXML(); String str = "xmlns:iws=\"http://www.idiominc.com/ws/asset\""; iwsStr = iwsStr.replace(str, ""); skeleton = skeleton.substring(0, begin) + iwsStr + skeleton.substring(end); return skeleton; }
From source file:com.globalsight.smartbox.bussiness.process.Usecase02PreProcess.java
License:Apache License
/** * Convert csv/txt file to xml file/*www . j a v a2 s. c o m*/ * * @param format * @param originFile * @return */ private String convertToXML(String format, File originFile) { String fileName = originFile.getName(); // Save the converted file to temp directory String xmlFilePath = jobInfo.getTempFile() + File.separator + fileName.substring(0, fileName.lastIndexOf(".")) + ".xml"; File xmlFile = new File(xmlFilePath); FileReader fr = null; FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; XMLWriter output = null; try { String encoding = FileUtil.guessEncoding(originFile); Document document = DocumentHelper.createDocument(); Element aElement = document.addElement("root"); aElement.addAttribute("BomInfo", encoding == null ? "" : encoding); if (encoding == null) { fr = new FileReader(originFile); br = new BufferedReader(fr); } else { fis = new FileInputStream(originFile); isr = new InputStreamReader(fis, encoding); br = new BufferedReader(isr); } String str; if ("csv".equals(format)) { while ((str = br.readLine()) != null) { String[] values = str.split("\",\""); values[0] = values[0].substring(1); values[10] = values[10].substring(0, values[10].length() - 1); writeRow(aElement, values); } } else { while ((str = br.readLine()) != null) { str = str + "*"; String[] values = str.split("\\|"); values[10] = values[10].substring(0, values[10].lastIndexOf("*")); writeRow(aElement, values); } } output = new XMLWriter(new FileOutputStream(xmlFile)); output.write(document); } catch (Exception e) { String message = "Failed to convert to XML, File Name: " + originFile.getName(); LogUtil.fail(message, e); return null; } finally { try { if (output != null) { output.close(); } if (br != null) { br.close(); } if (isr != null) { isr.close(); } if (fis != null) { fis.close(); } if (fr != null) { fr.close(); } } catch (IOException e) { e.printStackTrace(); } } return xmlFile.getPath(); }
From source file:com.globalsight.terminology.exporter.MtfWriter.java
License:Apache License
/** * Converts a GlobalSight concept group to a MultiTerm iX concept * group. Differences://from www . j a v a 2s . c o m * * - concept level <descrip type="entryClass|status"> --> * <system type="entryClass|status"> * * - <language name="English" locale="en_US" /> --> * <language type="English" lang="EN" /> * * - <noteGrp><note> --> * <descripGrp><descrip type="note"></descripGrp> * * - <note> --> (should not be produced but could be in old data) * <descripGrp><descrip type="note"></descripGrp> * * - <sourceGrp><source></sourceGrp> --> * <descripGrp><descrip type="source"></descripGrp> * * - descripGrp is not recursive */ private Document convertToMtf(Document p_elem) { List nodes; Node node; Element root = p_elem.getRootElement(); Element elem; Iterator it; ListIterator lit; if (false && CATEGORY.isDebugEnabled()) { CATEGORY.debug("gt2mtf init: " + p_elem.asXML()); } // rewrite <descrip type=entryClass> (only one on concept level) nodes = root.selectNodes("descrip[@type='entryClass']"); for (it = nodes.iterator(); it.hasNext();) { elem = (Element) it.next(); Element parent = elem.getParent(); parent.remove(elem); parent.addElement("system").addAttribute("type", "entryClass").addText(elem.getText()); } // rewrite <descrip type=status> (?? used in MTF?) nodes = root.selectNodes("descrip[@type='status']"); for (it = nodes.iterator(); it.hasNext();) { elem = (Element) it.next(); Element parent = elem.getParent(); parent.remove(elem); parent.addElement("system").addAttribute("type", "status").addText(elem.getText()); } // rewrite <noteGrp> while (true) { // refresh the node list, we're rewriting the structure node = root.selectSingleNode("//noteGrp"); if (node == null) { break; } elem = (Element) node; Element parent = elem.getParent(); parent.remove(elem); Element newNote = parent.addElement("descripGrp"); Element note = null; // copy all child nodes but remember the <note> for (lit = elem.elements().listIterator(); lit.hasNext();) { Element child = (Element) lit.next(); if (child.getName().equals("note")) { note = child; } else { lit.remove(); newNote.add(child); } } // create new <descrip type="note"> with note's value newNote.addElement("descrip").addAttribute("type", "note").addText(note.getText()); } // rewrite single <note>, if any are left in the entry while (true) { // refresh the node list, we're rewriting the structure node = root.selectSingleNode("//note"); if (node == null) { break; } Element note = (Element) node; Element parent = note.getParent(); parent.remove(note); Element newNote = parent.addElement("descripGrp"); newNote.addElement("descrip").addAttribute("type", "note").addText(note.getText()); } // rewrite <sourceGrp> while (true) { // refresh the node list, we're rewriting the structure node = root.selectSingleNode("//sourceGrp"); if (node == null) { break; } elem = (Element) node; Element parent = elem.getParent(); parent.remove(elem); Element newSource = parent.addElement("descripGrp"); Element source = null; // copy all child nodes but remember the <source> for (lit = elem.elements().listIterator(); lit.hasNext();) { Element child = (Element) lit.next(); if (child.getName().equals("source")) { source = child; } else { lit.remove(); newSource.add(child); } } // create new <descrip type="source"> with source's value newSource.addElement("descrip").addAttribute("type", "source").addText(source.getText()); } // rewrite <language> nodes = root.selectNodes("//languageGrp/language"); for (it = nodes.iterator(); it.hasNext();) { elem = (Element) it.next(); Attribute nameAttr = elem.attribute("name"); Attribute langAttr = elem.attribute("locale"); String langName = nameAttr.getValue(); String langLocale = langAttr.getValue(); // locales in MTF consist of 2 letter codes (uppercase). langLocale = langLocale.substring(0, 2).toUpperCase(); elem.remove(nameAttr); elem.remove(langAttr); elem.addAttribute("type", langName); elem.addAttribute("lang", langLocale); } if (false && CATEGORY.isDebugEnabled()) { CATEGORY.debug("gt2mtf done: " + p_elem.asXML()); } return p_elem; }
From source file:com.globalsight.terminology.importer.CsvReaderThread.java
License:Apache License
private Element buildDescripGrp(String p_value, String p_type) { Element grp = m_factory.createElement("descripGrp"); Element node = grp.addElement("descrip").addText(p_value); node.addAttribute("type", p_type); return grp;//w w w .j a va2 s.c o m }
From source file:com.globalsight.terminology.importer.CsvReaderThread.java
License:Apache License
private Element buildLanguageGrp(String p_language) throws TermbaseException { String locale = m_termbase.getLocaleByLanguage(p_language); Element grp = m_factory.createElement("languageGrp"); Element node = grp.addElement("language"); node.addAttribute("name", p_language); node.addAttribute("locale", locale); return grp;//from w w w.ja v a 2s .c o m }
From source file:com.globalsight.terminology.importer.MtfReaderThread.java
License:Apache License
/** * Converts a MultiTerm MTF concept group to a GlobalSight concept * group. Differences://from w w w . j a v a2 s.com * * - <system type="entryClass|status"> --> * <descrip type="entryClass|status"> * * - <language type="English" lang="EN" /> --> * <language name="English" locale="en_US" /> * * - <descripGrp><descrip type="note"> --> * <noteGrp><note> * * - <descripGrp><descrip type="source"> --> * <sourceGrp><source> * * - descripGrp is recursive, must map to noteGrps or delete. * * - remove empty descripGrp <descrip type="Graphic"/> */ private Element convertMtf(Element p_elem) { List nodes; Node node; Element elem; Iterator it; ListIterator lit; // fix <system> nodes = p_elem.elements("system"); for (it = nodes.iterator(); it.hasNext();) { elem = (Element) it.next(); p_elem.remove(elem); p_elem.addElement("descrip").addAttribute("type", elem.attributeValue("type")).addText(elem.getText()); } // fix Graphic; we cannot handle them, so remove them nodes = p_elem.selectNodes("descripGrp[descrip/@type='Graphic']"); for (it = nodes.iterator(); it.hasNext();) { elem = (Element) it.next(); p_elem.remove(elem); } // convert <descripGrp><descrip type="note"> to noteGrp nodes = p_elem.selectNodes(".//descripGrp[descrip/@type='note']"); for (it = nodes.iterator(); it.hasNext();) { elem = (Element) it.next(); convertToNoteGrp(elem); } // convert <descripGrp><descrip type="source"> to sourceGrp nodes = p_elem.selectNodes(".//descripGrp[descrip/@type='source']"); for (it = nodes.iterator(); it.hasNext();) { elem = (Element) it.next(); convertToSourceGrp(elem); } // Convert recursive descripGrps to noteGrps if possible. convertRecursiveDescripGrps(p_elem, ".//conceptGrp/descripGrp"); convertRecursiveDescripGrps(p_elem, ".//languageGrp/descripGrp"); convertRecursiveDescripGrps(p_elem, ".//termGrp/descripGrp"); // Remove the recursive descripGrps that are left over. // (In case there are doubly recursive descrips and stuff.) removeRecursiveDescripGrps(p_elem); // fix <language> nodes = p_elem.selectNodes("languageGrp/language"); for (it = nodes.iterator(); it.hasNext();) { elem = (Element) it.next(); Attribute nameAttr = elem.attribute("type"); Attribute langAttr = elem.attribute("lang"); String langName = nameAttr.getValue(); String langLocale = langAttr.getValue(); // locales in entries consist of 2 letter codes. langLocale = langLocale.substring(0, 2).toLowerCase(); elem.remove(nameAttr); elem.remove(langAttr); elem.addAttribute("name", langName); elem.addAttribute("locale", langLocale); } return p_elem; }
From source file:com.globalsight.webservices.Ambassador.java
License:Apache License
/** * Validates the segment, and try to repair it if the format is wrong. * /*from w w w.j a va 2s. com*/ * <p> * Will throw out a exception if the format is wrong and can not be * repaired. * * @param p_seg * The segment string to validate * @param p_x_count * The value of x * @return Repaired segment * @throws Exception */ private Element validateSegment(Element p_seg, IntHolder p_x_count) throws Exception { String attr; List elems = p_seg.elements(); for (Iterator it = elems.iterator(); it.hasNext();) { Element elem = (Element) it.next(); String name = elem.getName(); if (name.equals("bpt")) { attr = elem.attributeValue("x"); // mandatory only in 1.4 if (attr == null || attr.length() == 0) { elem.addAttribute("x", String.valueOf(p_x_count.inc())); } attr = elem.attributeValue("i"); // mandatory if (attr == null || attr.length() == 0) { throw new Exception("A <bpt> tag is lacking the mandatory i attribute."); } attr = elem.attributeValue("type"); if (attr == null || attr.length() == 0) { elem.addAttribute("type", DEFAULT_TYPE); } } else if (name.equals("ept")) { attr = elem.attributeValue("i"); // mandatory if (attr == null || attr.length() == 0) { throw new Exception("A <ept> tag is lacking the mandatory i attribute."); } } else if (name.equals("it")) { attr = elem.attributeValue("x"); // mandatory only in 1.4 if (attr == null || attr.length() == 0) { elem.addAttribute("x", String.valueOf(p_x_count.inc())); } attr = elem.attributeValue("pos"); // mandatory if (attr == null || attr.length() == 0) { throw new Exception("A <it> tag is lacking the mandatory pos attribute."); } attr = elem.attributeValue("type"); if (attr == null || attr.length() == 0) { elem.addAttribute("type", DEFAULT_TYPE); } } else if (name.equals("ph")) { attr = elem.attributeValue("x"); // mandatory only in 1.4 if (attr == null || attr.length() == 0) { elem.addAttribute("x", String.valueOf(p_x_count.inc())); } attr = elem.attributeValue("type"); if (attr == null || attr.length() == 0) { elem.addAttribute("type", DEFAULT_TYPE); } // GXML doesn't care about assoc, just preserve it. // attr = elem.attributeValue("assoc"); } else if (name.equals("ut")) { // TMX level 2 does not allow UT. We can either remove // it, or look inside and guess what it may be. it.remove(); continue; } // Recurse into any subs. validateSubs(elem, p_x_count); } return p_seg; }
From source file:com.globalsight.webservices.Ambassador.java
License:Apache License
/** * Normalizes the spelling of the "lang" elements. * /*from www . j a va 2s .co m*/ * @param p_tu * Element * @throws Exception */ private void normalizeTu(Element p_tu) throws Exception { // Header default source lang normalized when header is read. // Locales read from m_options were normalized by TmxReader. String lang = p_tu.attributeValue(Tmx.SRCLANG); if (lang != null) { lang = ImportUtil.normalizeLocale(lang); p_tu.addAttribute(Tmx.SRCLANG, lang); } // can't use xpath here because xml:lang won't be matched List nodes = p_tu.selectNodes("./tuv"); for (int i = 0, max = nodes.size(); i < max; i++) { Element elem = (Element) nodes.get(i); lang = elem.attributeValue(Tmx.LANG); lang = ImportUtil.normalizeLocale(lang); elem.addAttribute(Tmx.LANG, lang); } }
From source file:com.gote.importexport.ExportTournamentForOpenGotha.java
License:Apache License
/** * Update a document according to Tournament state * //from ww w . j av a 2 s .c om * @param pDocument Document * @param pTournament Tournament */ private void updateDocument(Document pDocument, Tournament pTournament) { Element pElementTournament = pDocument.getRootElement(); Element elementGames = pElementTournament.element(TournamentOpenGothaUtil.TAG_GAMES); @SuppressWarnings("unchecked") List<Element> listOfGames = (List<Element>) elementGames.elements(TournamentOpenGothaUtil.TAG_GAME); for (Element game : listOfGames) { // Check if there is no result for the game before checking games if (game.attribute(TournamentOpenGothaUtil.ATTRIBUTE_GAME_RESULT) != null && !TournamentOpenGothaUtil.VALUE_GAME_RESULT_UNKNOWN .equals(game.attribute(TournamentOpenGothaUtil.ATTRIBUTE_GAME_RESULT).getValue())) { if (game.attribute(TournamentOpenGothaUtil.ATTRIBUTE_GAME_ROUND) != null && game.attribute(TournamentOpenGothaUtil.ATTRIBUTE_GAME_BLACK_PLAYER) != null && game.attribute(TournamentOpenGothaUtil.ATTRIBUTE_GAME_WHITE_PLAYER) != null) { game.addAttribute(TournamentOpenGothaUtil.ATTRIBUTE_GAME_RESULT, pTournament.getGameResultWithCompleteName( Integer.parseInt(game.attribute(TournamentOpenGothaUtil.ATTRIBUTE_GAME_ROUND) .getValue()), game.attribute(TournamentOpenGothaUtil.ATTRIBUTE_GAME_BLACK_PLAYER).getValue(), game.attribute(TournamentOpenGothaUtil.ATTRIBUTE_GAME_WHITE_PLAYER) .getValue())); } else { LOGGER.log(Level.SEVERE, "Either the round number, black player name or white player name has not been found for game : " + game + ". Update impossible"); } } } }
From source file:com.gote.pojo.Game.java
License:Apache License
/** * Transform Game to XML/*w w w . j a va2 s . c om*/ * * @param pRoot "Games" element * @see TournamentGOTEUtil */ public void toXML(Element pRoot) { // Create game element Element game = pRoot.addElement(TournamentGOTEUtil.TAG_GAME); // Add its attributes game.addAttribute(TournamentGOTEUtil.ATT_GAME_WHITEPLAYER, getWhite().getPseudo()); game.addAttribute(TournamentGOTEUtil.ATT_GAME_BLACKPLAYER, getBlack().getPseudo()); game.addAttribute(TournamentGOTEUtil.ATT_GAME_URL, getGameUrl()); game.addAttribute(TournamentGOTEUtil.ATT_GAME_KOMI, getKomi()); game.addAttribute(TournamentGOTEUtil.ATT_GAME_HANDICAP, getHandicap()); game.addAttribute(TournamentGOTEUtil.ATT_GAME_RESULT, getResult()); }