List of usage examples for org.dom4j Element attributeValue
String attributeValue(QName qName);
From source file:com.globalsight.smartbox.bussiness.process.Usecase01PreProcess.java
License:Apache License
private void getGSInfo() { //1. Get GlobalSight File Profile Map<FPName, FP>. gsFPMap = new HashMap<String, FileProfile>(); try {/*from ww w. ja va2s . c o m*/ List<FileProfile> fps = WebClientHelper.getFileProfileInfoFromGS(); for (FileProfile fp : fps) { gsFPMap.put(fp.getName(), fp); } } catch (Exception e) { String message = "Get file profile info failed, Web Service Exception."; LogUtil.fail(message, e); return; } //2. Get Alias File Profile Name Map<AliasFPName, GSFPName> try { SAXReader saxReader = new SAXReader(); String path = System.getProperty("user.dir") + File.separator + CONFIG_NAME; Document doc = saxReader.read(path); List<Element> nodes = doc.selectNodes("//fileProfileNameMappings/fileProfileNameMapping"); configFPMap = new HashMap<String, FileProfile>(); for (Element el : nodes) { String gsFPName = el.attributeValue("gs_xml"); String aliasFPName = el.attributeValue("aliasFPName"); String gsUnExtractedFPName = el.attributeValue("gs_unextracted"); FileProfile fp = new FileProfile(gsFPName, aliasFPName, gsUnExtractedFPName); configFPMap.put(aliasFPName, fp); } } catch (Exception e) { String message = "Parse " + CONFIG_NAME + " Error."; LogUtil.fail(message, e); return; } }
From source file:com.globalsight.smartbox.bussiness.process.Usecase01PreProcess.java
License:Apache License
private void parseFile(Vector<String> p_sourceFiles) { // Parse Bookmark XML File to get fileProfileName and job name. Map<String, String> srcMap = new HashMap<String, String>(); String fileName = null;/*from w w w . j a v a2 s. c o m*/ for (String path : p_sourceFiles) { String temp = path.substring(path.lastIndexOf(File.separator) + 1); srcMap.put(temp, path); if (temp.endsWith(".pdf")) { fileName = temp.replace(".pdf", ".xml"); } } String path = srcMap.get(fileName); try { SAXReader saxReader = new SAXReader(); saxReader.setValidation(false); saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); Document doc = saxReader.read(path); Element node = (Element) doc.selectSingleNode("//bookmeta/data[@datatype='GSDATA']"); String gsDataValue = node.attributeValue("value"); String jobName = gsDataValue.substring(0, gsDataValue.indexOf("~")); String customerFPName = gsDataValue.substring(gsDataValue.indexOf("~") + 1); FileProfile configFP = getConfigFP(customerFPName); if (configFP == null) { String message = "Can't find the fileProfileNameMapping for " + customerFPName; LogUtil.info(message); return; } fp = gsFPMap.get(configFP.getName()); if (fp == null) { String message = "Can't find the file profile."; LogUtil.info(message); return; } // Get Target Locale. node = (Element) doc.selectSingleNode("/bookmap"); String lang = node.attributeValue("lang"); for (String locale : fp.getTargetLocale()) { if (locale.startsWith(lang)) { trgLocale = locale; break; } } if (trgLocale == null || trgLocale.trim().length() == 0) { String message = "Can't find the correct Target Locale in File Profile."; LogUtil.info(message); return; } unExtractedFP = gsFPMap.get(configFP.getGsUnExtractedFPName()); if (unExtractedFP == null) { String message = "Can't find the UnExtracted file profile: " + fp.getGsUnExtractedFPName(); LogUtil.info(message); return; } basicJobName = jobName + "_" + customerFPName + "_" + lang; } catch (Exception e) { String message = "Read XML error: " + path; LogUtil.fail(message, e); return; } }
From source file:com.globalsight.smartbox.bussiness.process.Usecase02PostProcess.java
License:Apache License
/** * Convert xml to csv or txt file/*from w w w .j ava 2 s . c om*/ * * @param format * @param originFile * @return * @throws Exception */ private File convertXMLToCSVTXT(String format, String targetFile, String outputFilePath) throws Exception { File xmlFile = new File(targetFile); File outputFile = new File(outputFilePath); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(xmlFile); Element aElement = document.getRootElement(); String encoding = aElement.attributeValue("BomInfo"); FileOutputStream fos = new FileOutputStream(outputFilePath); FileUtil.writeBom(fos, encoding); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, encoding)); List<Element> rows = aElement.elements("row"); for (Element row : rows) { List<String> rowStr = new ArrayList<String>(); rowStr.add(row.elementText("sid")); rowStr.add(row.elementText("sourceLocaleName")); rowStr.add(row.elementText("sourceLocaleCode")); rowStr.add(row.elementText("unknown")); rowStr.add(row.elementText("translationSource")); rowStr.add(row.elementText("targetLocale")); rowStr.add(row.elementText("creationDate")); List<Element> segments = row.elements("segment"); for (Element element : segments) { rowStr.add(element.getText()); } StringBuffer sb = new StringBuffer(); if ("csv".equals(format)) { for (String str : rowStr) { sb.append("\"").append(str).append("\"").append(","); } } else { for (String str : rowStr) { sb.append(str).append("|"); } } sb.deleteCharAt(sb.length() - 1); bw.write(sb.toString()); bw.newLine(); } bw.close(); fos.close(); return outputFile; }
From source file:com.globalsight.smartbox.bussiness.process.Usecase03PreProcess.java
License:Apache License
private void loadFileProfileInfo() { //1. Get GlobalSight File Profiles from Server. serverFPMap = new HashMap<String, FileProfile>(); try {//w w w .j a v a 2s . c o m List<FileProfile> fps = WebClientHelper.getFileProfileInfoFromGS(); for (FileProfile fp : fps) { serverFPMap.put(fp.getName(), fp); } } catch (Exception e) { String message = "Get file profile info failed, Web Service Exception."; LogUtil.fail(message, e); return; } //2. Get Locale File Profiles from Local Configure File. try { SAXReader saxReader = new SAXReader(); String path = System.getProperty("user.dir") + File.separator + CONFIG_NAME; Document doc = saxReader.read(path); List<Element> nodes = doc.selectNodes("//fileProfileNameMappings/fileProfileNameMapping"); localeFPMap = new HashMap<String, FileProfile>(); for (Element el : nodes) { String gsFPName = el.attributeValue("gs_xml"); String aliasFPName = el.attributeValue("aliasFPName"); String gsUnExtractedFPName = el.attributeValue("gs_unextracted"); FileProfile fp = new FileProfile(gsFPName, aliasFPName, gsUnExtractedFPName); localeFPMap.put(aliasFPName, fp); } } catch (Exception e) { String message = "Parse " + CONFIG_NAME + " Error."; LogUtil.fail(message, e); return; } }
From source file:com.globalsight.smartbox.bussiness.process.Usecase04PreProcess.java
License:Apache License
private void parseFile(Vector<String> p_sourceFiles) { String path = null;/*ww w. ja v a2s . co m*/ try { // Parse Bookmark XML File to get fileProfileName and job name. path = getBookMapFile(p_sourceFiles); SAXReader saxReader = new SAXReader(); saxReader.setValidation(false); saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); Document doc = saxReader.read(path); Element node = (Element) doc.selectSingleNode("//bookmeta/data[@datatype='GSDATA']"); String gsDataValue = node.attributeValue("value"); String jobName = gsDataValue.substring(0, gsDataValue.indexOf("~")); String customerFPName = gsDataValue.substring(gsDataValue.indexOf("~") + 1); FileProfile configFP = getConfigFP(customerFPName); if (configFP == null) { String message = "Can't find the fileProfileNameMapping for " + customerFPName; LogUtil.info(message); return; } fp = gsFPMap.get(configFP.getName()); if (fp == null) { String message = "Can't find the file profile."; LogUtil.info(message); return; } // Get Target Locale. node = (Element) doc.selectSingleNode("/bookmap"); String lang = node.attributeValue("lang"); for (String locale : fp.getTargetLocale()) { if (locale.startsWith(lang)) { trgLocale = locale; break; } } if (trgLocale == null || trgLocale.trim().length() == 0) { String message = "Can't find the correct Target Locale in File Profile."; LogUtil.info(message); return; } unExtractedFP = gsFPMap.get(configFP.getGsUnExtractedFPName()); if (unExtractedFP == null) { String message = "Can't find the UnExtracted file profile: " + fp.getGsUnExtractedFPName(); LogUtil.info(message); return; } basicJobName = jobName + "_" + customerFPName + "_" + lang; } catch (FileNotFoundException e) { LogUtil.fail("", e); return; } catch (Exception e) { String message = "Read XML error: " + path; LogUtil.fail(message, e); return; } }
From source file:com.globalsight.terminology.EntryUtils.java
License:Apache License
static private void mergeDescripGrp(Element p_one, Element p_two, Element p_descripGrp, NodeComparator p_comp) { Element p_descrip = p_descripGrp.element("descrip"); if (p_descrip == null || !p_descrip.hasContent()) { return;/* w w w.j a va 2s . com*/ } String type = p_descrip.attributeValue("type"); // some descrips can occur only once per entry. boolean occursOnce = fieldOccursOnce(type); // Find nodes that match the descrip. List matches = p_one.selectNodes("descripGrp[descrip/@type='" + type + "']"); if (matches == null || matches.size() == 0) { // descripGrp does not exist in entry 1, add. int index = findDescripInsertionPoint(p_one, type); p_one.content().add(index, p_descripGrp); return; } // Check if one of the matches contains the same descrip for (int i = 0, max = matches.size(); i < max; i++) { Element descripGrp = (Element) matches.get(i); Element descrip = descripGrp.element("descrip"); if (fieldEquals(descrip, p_descrip, p_comp)) { // could be a case/formatting-insensitive match descrip.detach(); descripGrp.content().add(0, p_descrip); mergeInnerGroups(descripGrp, p_descripGrp, p_comp); return; } } // DescripGrp was not found, add new descripGrp to old. if (occursOnce) { // DescripGrp can occur only once, merge by overwriting. // (Single descrips also contain just text, no HTML.) Element descGrp = (Element) matches.get(0); Element desc = descGrp.element("descrip"); desc.setText(getInnerText(p_descrip)); mergeInnerGroups(descGrp, p_descripGrp, p_comp); } else { // Multiple descrips may exist (for e.g., definition), // so add the new one after the last. Element last = (Element) matches.get(matches.size() - 1); p_one.content().add(p_one.indexOf(last) + 1, p_descripGrp); } }
From source file:com.globalsight.terminology.EntryUtils.java
License:Apache License
static private void mergeLanguageGrp(Element p_one, Element p_two, Element p_languageGrp, NodeComparator p_comp) {/*from w w w . jav a2s . com*/ Element p_language = p_languageGrp.element("language"); // language element is an empty element. if (p_language == null) { return; } String name = p_language.attributeValue("name"); // Find language in entry 1. Element languageGrp = (Element) p_one.selectSingleNode("languageGrp[language/@name='" + name + "']"); if (languageGrp == null) { // languageGrp does not exist in entry 1, add to the end. p_one.add(p_languageGrp); return; } mergeInnerGroups(languageGrp, p_languageGrp, p_comp); }
From source file:com.globalsight.terminology.exporter.ExportOptions.java
License:Apache License
/** * Reads and validates a ExportOptions XML string. *///from w w w . j a v a 2 s. c om protected void initOther(Element p_root) throws ExporterException { try { Node node = p_root.selectSingleNode("/exportOptions/selectOptions"); m_selectOptions.m_selectMode = node.valueOf("selectMode"); m_selectOptions.m_selectLanguage = node.valueOf("selectLanguage"); m_selectOptions.m_duplicateHandling = node.valueOf("duplicateHandling"); node = p_root.selectSingleNode("/exportOptions/filterOptions"); m_filterOptions.m_createdBy = node.valueOf("createdby"); m_filterOptions.m_modifiedBy = node.valueOf("modifiedby"); m_filterOptions.m_createdAfter = node.valueOf("createdafter"); m_filterOptions.m_createdBefore = node.valueOf("createdbefore"); m_filterOptions.m_modifiedAfter = node.valueOf("modifiedafter"); m_filterOptions.m_modifiedBefore = node.valueOf("modifiedbefore"); m_filterOptions.m_status = node.valueOf("status"); m_filterOptions.m_domain = node.valueOf("domain"); m_filterOptions.m_project = node.valueOf("project"); m_filterOptions.m_conditions.clear(); List filters = p_root.selectNodes("/exportOptions/filterOptions/conditions/condition"); for (int i = 0; i < filters.size(); ++i) { Element filter = (Element) filters.get(i); FilterCondition condition = new FilterCondition(); condition.m_level = filter.valueOf("level"); condition.m_field = filter.valueOf("field"); condition.m_operator = filter.valueOf("operator"); condition.m_value = filter.valueOf("value"); condition.m_matchcase = filter.valueOf("matchcase"); m_filterOptions.m_conditions.add(condition); } node = p_root.selectSingleNode("/exportOptions/outputOptions"); m_outputOptions.m_systemFields = node.valueOf("systemFields"); m_outputOptions.m_separator = node.valueOf("separator"); m_outputOptions.m_writeHeader = node.valueOf("writeHeader"); m_columns.clear(); List columns = p_root.selectNodes("/exportOptions/columnOptions/column"); for (int i = 0; i < columns.size(); ++i) { Element col = (Element) columns.get(i); ColumnDescriptor desc = new ColumnDescriptor(); desc.m_position = Integer.parseInt(col.attributeValue("id")); desc.m_name = col.valueOf("name"); desc.m_type = col.valueOf("type"); desc.m_termLanguage = col.valueOf("termLanguage"); desc.m_encoding = col.valueOf("encoding"); m_columns.add(desc); } } catch (Exception e) { // cast exception and throw error(e.getMessage(), e); } }
From source file:com.globalsight.terminology.importer.ImportOptions.java
License:Apache License
/** * Reads and validates a ImportOptions XML string. * * Xml Format:/*from ww w.j a va2 s . co m*/ */ protected void initOther(Element p_root) throws ImporterException { try { Element elem = (Element) p_root.selectSingleNode("//syncOptions"); m_syncOptions.m_syncMode = elem.elementText("syncMode"); m_syncOptions.m_syncLanguage = elem.elementText("syncLanguage"); m_syncOptions.m_syncAction = elem.elementText("syncAction"); m_syncOptions.m_nosyncAction = elem.elementText("nosyncAction"); m_columns.clear(); List columns = p_root.selectNodes("/importOptions/columnOptions/column"); for (int i = 0, max = columns.size(); i < max; ++i) { Element col = (Element) columns.get(i); ColumnDescriptor desc = new ColumnDescriptor(); desc.m_position = Integer.parseInt(col.attributeValue("id")); desc.m_name = col.elementText("name"); desc.m_example = col.elementText("example"); desc.m_type = col.elementText("type"); desc.m_termLanguage = col.elementText("termLanguage"); desc.m_encoding = col.elementText("encoding"); desc.m_associatedColumn = col.elementText("associatedColumn"); m_columns.add(desc); } } catch (Exception e) { // cast exception and throw error(e.getMessage(), e); } }
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 ww. j ava 2s .co m * * - <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; }