List of usage examples for org.jdom2 Element getChild
public Element getChild(final String cname)
From source file:at.newmedialab.lmf.search.services.cores.SolrCoreServiceImpl.java
License:Apache License
/** * Create/update the schema.xml file for the given core according to the core definition. * * @param engine the engine configuration *//*from w w w .jav a 2 s . c o m*/ private void createSchemaXml(SolrCoreConfiguration engine) throws MarmottaException { log.info("generating schema.xml for search program {}", engine.getName()); SAXBuilder parser = new SAXBuilder(XMLReaders.NONVALIDATING); File schemaTemplate = new File(getCoreDirectory(engine.getName()), "conf" + File.separator + "schema-template.xml"); File schemaFile = new File(getCoreDirectory(engine.getName()), "conf" + File.separator + "schema.xml"); try { Document doc = parser.build(schemaTemplate); Element schemaNode = doc.getRootElement(); Element fieldsNode = schemaNode.getChild("fields"); if (!schemaNode.getName().equals("schema") || fieldsNode == null) throw new MarmottaException(schemaTemplate + " is an invalid SOLR schema file"); schemaNode.setAttribute("name", engine.getName()); Program<Value> program = engine.getProgram(); for (FieldMapping<?, Value> fieldMapping : program.getFields()) { String fieldName = fieldMapping.getFieldName(); String solrType = null; try { solrType = solrProgramService.getSolrFieldType(fieldMapping.getFieldType().toString()); } catch (MarmottaException e) { solrType = null; } if (solrType == null) { log.error("field {} has an invalid field type; ignoring field definition", fieldName); continue; } Element fieldElement = new Element("field"); fieldElement.setAttribute("name", fieldName); fieldElement.setAttribute("type", solrType); // Set the default properties fieldElement.setAttribute("stored", "true"); fieldElement.setAttribute("indexed", "true"); fieldElement.setAttribute("multiValued", "true"); // FIXME: Hardcoded Stuff! if (solrType.equals("location")) { fieldElement.setAttribute("indexed", "true"); fieldElement.setAttribute("multiValued", "false"); } // Handle extra field configuration final Map<String, String> fieldConfig = fieldMapping.getFieldConfig(); if (fieldConfig != null) { for (Map.Entry<String, String> attr : fieldConfig.entrySet()) { if (SOLR_FIELD_OPTIONS.contains(attr.getKey())) { fieldElement.setAttribute(attr.getKey(), attr.getValue()); } } } fieldsNode.addContent(fieldElement); if (fieldConfig != null && fieldConfig.keySet().contains(SOLR_COPY_FIELD_OPTION)) { String[] copyFields = fieldConfig.get(SOLR_COPY_FIELD_OPTION).split("\\s*,\\s*"); for (String copyField : copyFields) { if (copyField.trim().length() > 0) { // ignore 'empty' fields Element copyElement = new Element("copyField"); copyElement.setAttribute("source", fieldName); copyElement.setAttribute("dest", copyField.trim()); schemaNode.addContent(copyElement); } } } else { Element copyElement = new Element("copyField"); copyElement.setAttribute("source", fieldName); copyElement.setAttribute("dest", "lmf.text_all"); schemaNode.addContent(copyElement); } //for suggestions, copy all fields to lmf.spellcheck (used for spellcheck and querying); //only facet is a supported type at the moment if (fieldConfig != null && fieldConfig.keySet().contains(SOLR_SUGGESTION_FIELD_OPTION)) { String suggestionType = fieldConfig.get(SOLR_SUGGESTION_FIELD_OPTION); if (suggestionType.equals("facet")) { Element copyElement = new Element("copyField"); copyElement.setAttribute("source", fieldName); copyElement.setAttribute("dest", "lmf.spellcheck"); schemaNode.addContent(copyElement); } else { log.error("suggestionType " + suggestionType + " not supported"); } } } if (!schemaFile.exists() || schemaFile.canWrite()) { FileOutputStream out = new FileOutputStream(schemaFile); XMLOutputter xo = new XMLOutputter(Format.getPrettyFormat().setIndent(" ")); xo.output(doc, out); out.close(); } else { log.error("schema file {} is not writable", schemaFile); } } catch (JDOMException e) { throw new MarmottaException("parse error while parsing SOLR schema template file " + schemaTemplate, e); } catch (IOException e) { throw new MarmottaException("I/O error while parsing SOLR schema template file " + schemaTemplate, e); } }
From source file:at.newmedialab.lmf.util.geonames.builder.GeoLookupImpl.java
License:Apache License
/** * Query the geonames-api for a place with the provided name. * @param name the name of the place to lookup at geonames * @return the URI of the resolved place, or null if no such place exists. *//*from ww w .j a v a 2 s . c om*/ private String queryPlace(String name) { if (StringUtils.isBlank(name)) return null; StringBuilder querySB = new StringBuilder(); queryStringAppend(querySB, "q", name); queryStringAppend(querySB, "countryBias", countryBias); for (char fc : featureClasses) { queryStringAppend(querySB, "featureClass", String.valueOf(fc)); } for (String fc : featureCodes) { queryStringAppend(querySB, "featureCode", fc); } for (String c : countries) { queryStringAppend(querySB, "country", c); } for (String cc : continentCodes) { queryStringAppend(querySB, "continentCode", cc); } if (fuzzy < 1) { queryStringAppend(querySB, "fuzzy", String.valueOf(fuzzy)); } queryStringAppend(querySB, "maxRows", "1"); queryStringAppend(querySB, "type", "xml"); queryStringAppend(querySB, "isNameRequired", "true"); queryStringAppend(querySB, "style", "short"); if (StringUtils.isNotBlank(geoNamesUser)) queryStringAppend(querySB, "username", geoNamesUser); if (StringUtils.isNotBlank(geoNamesPasswd)) queryStringAppend(querySB, "password", geoNamesPasswd); final String url = geoNamesUrl + "search?" + querySB.toString(); HttpGet get = new HttpGet(url); try { return http.execute(get, new ResponseHandler<String>() { /** * Parses the xml-response from the geonames webservice and build the uri for the result * @return the URI of the resolved place, or null if no place was found. */ @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { final int statusCode = response.getStatusLine().getStatusCode(); if (!(statusCode >= 200 && statusCode < 300)) { return null; } try { SAXBuilder builder = new SAXBuilder(); final HttpEntity entity = response.getEntity(); if (entity == null) throw new ClientProtocolException("Body Required"); final Document doc = builder.build(entity.getContent()); final Element root = doc.getRootElement(); final Element status = root.getChild("status"); if (status != null) { final int errCode = Integer.parseInt(status.getAttributeValue("value")); if (errCode == 15) { // NO RESULT should not be an exception return null; } throw new GeoNamesException(errCode, status.getAttributeValue("message")); } final Element gName = root.getChild("geoname"); if (gName == null) return null; final String geoId = gName.getChildTextTrim("geonameId"); if (geoId == null) return null; return String.format(GEONAMES_URI_PATTERN, geoId); } catch (NumberFormatException e) { throw new ClientProtocolException(e); } catch (IllegalStateException e) { throw new ClientProtocolException(e); } catch (JDOMException e) { throw new IOException(e); } } }); } catch (GeoNamesException e) { log.debug("Lookup at GeoNames failed: {} ({})", e.getMessage(), e.getErrCode()); } catch (ClientProtocolException e) { log.error("Could not query geoNames: " + e.getLocalizedMessage(), e); } catch (IOException e) { log.error("Could not query geoNames: " + e.getLocalizedMessage(), e); } return null; }
From source file:bg.LanguageManager.java
private HashMap<String, String> readLanguageFile(Language lan) { SAXBuilder builder = new SAXBuilder(); HashMap<String, String> lanTextList = new HashMap<String, String>(); Document document = new Document(); try {/*w w w . j a v a 2 s .c o m*/ switch (lan) { case EN: document = (Document) builder.build(new File("src\\resource\\language\\en.xml")); break; case ZH_TW: document = (Document) builder.build(new File("src\\resource\\language\\zh-tw.xml")); break; } } catch (IOException | JDOMException ex) { Logger.getLogger(TwTextSpliterGUI.class.getName()).log(Level.SEVERE, null, ex); UIMessager.showError("Open language resource file failed"); return null; } Element root_node = document.getRootElement(); Element textfield_node = root_node.getChild(NodeCategory.TEXTFIELD.getText()); Element msg_node = root_node.getChild(NodeCategory.MESSAGE.getText()); Element button_node = root_node.getChild(NodeCategory.BUTTONTEXT.getText()); addTextFieldChildrenText(lanTextList, textfield_node); addMsgChildrenText(lanTextList, msg_node); addButtonChildrenText(lanTextList, button_node); return lanTextList; }
From source file:bg.LanguageManager.java
private HashMap<String, String> readLanguageFile() { SAXBuilder builder = new SAXBuilder(); HashMap<String, String> lanTextList = new HashMap<String, String>(); Document document = new Document(); try {// w ww .jav a 2 s .c om switch (this.lanSet) { case EN: document = (Document) builder.build(new File("src\\resource\\language\\en.xml")); break; case ZH_TW: document = (Document) builder.build(new File("src\\resource\\language\\zh-tw.xml")); break; } } catch (IOException | JDOMException ex) { Logger.getLogger(TwTextSpliterGUI.class.getName()).log(Level.SEVERE, null, ex); UIMessager.showError("Open language resource file failed"); return null; } Element root_node = document.getRootElement(); Element textfield_node = root_node.getChild(NodeCategory.TEXTFIELD.getText()); Element msg_node = root_node.getChild(NodeCategory.MESSAGE.getText()); Element button_node = root_node.getChild(NodeCategory.BUTTONTEXT.getText()); addTextFieldChildrenText(lanTextList, textfield_node); addMsgChildrenText(lanTextList, msg_node); addButtonChildrenText(lanTextList, button_node); return lanTextList; }
From source file:bg.LanguageManager.java
/** * @brief Read text from node msg in language xml file * @param lanTextList store values of leaf node in language xml file * @param msg_node internal node of language xml file * @marker If add any new element in xml, please check this function *///from w w w .jav a 2s . c o m private void addMsgChildrenText(HashMap<String, String> lanTextList, Element msg_node) { Element valid_node = msg_node.getChild(NodeCategory.VALID.getText()); lanTextList.put(LeafNodeLabel.SUCCESS.getText(), valid_node.getChildText(LeafNodeLabel.SUCCESS.getText())); Element warning_node = msg_node.getChild(NodeCategory.WARNING.getText()); lanTextList.put(LeafNodeLabel.LINES_NULL.getText(), warning_node.getChildText(LeafNodeLabel.LINES_NULL.getText())); lanTextList.put(LeafNodeLabel.SIZE_NULL.getText(), warning_node.getChildText(LeafNodeLabel.SIZE_NULL.getText())); lanTextList.put(LeafNodeLabel.DELIMITER_NULL.getText(), warning_node.getChildText(LeafNodeLabel.DELIMITER_NULL.getText())); lanTextList.put(LeafNodeLabel.SPLIT_METHOD_NULL.getText(), warning_node.getChildText(LeafNodeLabel.SPLIT_METHOD_NULL.getText())); Element error_node = msg_node.getChild(NodeCategory.ERROR.getText()); lanTextList.put(LeafNodeLabel.LINES_ERROR.getText(), error_node.getChildText(LeafNodeLabel.LINES_ERROR.getText())); lanTextList.put(LeafNodeLabel.SIZE_ERROR.getText(), error_node.getChildText(LeafNodeLabel.SIZE_ERROR.getText())); lanTextList.put(LeafNodeLabel.DELIMITER_ERROR.getText(), error_node.getChildText(LeafNodeLabel.DELIMITER_ERROR.getText())); lanTextList.put(LeafNodeLabel.FILE_WRITTING_FAILED.getText(), error_node.getChildText(LeafNodeLabel.FILE_WRITTING_FAILED.getText())); lanTextList.put(LeafNodeLabel.FILE_NOT_FOUND.getText(), error_node.getChildText(LeafNodeLabel.FILE_NOT_FOUND.getText())); lanTextList.put(LeafNodeLabel.UNKNOWN_ERROR.getText(), error_node.getChildText(LeafNodeLabel.UNKNOWN_ERROR.getText())); }
From source file:br.com.sicoob.cro.cop.util.JobXMLLoader.java
/** * Le o XML e cria o objeto de Step./*from ww w.j av a 2 s .com*/ * * @param rootNode Nodo princiapl * @throws Exception para algum erro. */ private void loadSteps(Element rootNode) throws Exception { List steps = rootNode.getChildren(STEP); List<Step> jobSteps = new ArrayList(); for (Object obj : steps) { Element stepNode = (Element) obj; StepParameters stepParameters = parseParameters(); Element taskletNode = stepNode.getChild(TASKLET); Element chunkNode = stepNode.getChild(CHUNK); if (Validation.notNull(taskletNode)) { Step step = new Step(getTasklet(taskletNode), Step.Type.TASKLET, stepParameters); step.setId(stepNode.getAttributeValue(ID)); getListener(stepNode, step); jobSteps.add(step); } else if (Validation.notNull(chunkNode)) { Step step = new Step(getReader(chunkNode), getProcessor(chunkNode), getWriter(chunkNode), Step.Type.CHUNK, stepParameters, getCommitInterval(chunkNode)); step.setId(stepNode.getAttributeValue(ID)); getListener(stepNode, step); jobSteps.add(step); } } this.job.setSteps(jobSteps); }
From source file:br.com.sicoob.cro.cop.util.JobXMLLoader.java
private void getListener(Element stepNode, Step step) throws Exception { Element listenerNode = stepNode.getChild(LISTENER); if (Validation.notNull(listenerNode)) { step.setListener((BatchStepListener) Class.forName(listenerNode.getAttributeValue(REF)).newInstance()); }// ww w . j a va2 s . com }
From source file:bureau.Services.java
public void getAdmissionsFromFile() { SAXBuilder builder = new SAXBuilder(); try {//from w w w. j ava2 s. com //on liste et on parcours les fichiers pour trouver les .xml qui sont en ajout et en edit File repertoire = new File("\\\\172.18.8.70\\temp\\PP_PAT_IN");// \\\\178.18.8.70\\temp System.out.println("new file"); String[] listefichiers; int i; System.out.println("int i string liste fichier"); listefichiers = repertoire.list(); System.out.println("repertoire.list()"); for (i = 0; i < listefichiers.length; i++) { System.out.println("dans la boucle"); //si le fichier est en ajout if (listefichiers[i].startsWith("new")) { System.out.println("fichier new trouv"); Document doc = builder.build("" + repertoire + "\\" + listefichiers[i]); Element root = doc.getRootElement(); Element admissionXML = root; int type = 1; int ipp; int iep; ipp = parseInt(admissionXML.getChild("patient").getAttributeValue("ipp")); iep = parseInt(admissionXML.getAttributeValue("iep")); if (admissionXML.getChild("type").getText().startsWith("c")) type = 3; if (admissionXML.getChild("type").getText().startsWith("h")) type = 1; if (admissionXML.getChild("type").getText().startsWith("u")) type = 2; newAdmission(ipp, type, iep); System.out.println("nouvelle admission ajoute"); } if (listefichiers[i].startsWith("update")) { System.out.println("fichier en update trouv"); Document doc = builder.build("" + repertoire + "\\" + listefichiers[i]); Element root = doc.getRootElement(); Element admissionXML = root; int type = 1; int iep = parseInt(admissionXML.getAttributeValue("iep")); if (admissionXML.getChild("type").getText().startsWith("c")) type = 3; if (admissionXML.getChild("type").getText().startsWith("h")) type = 1; if (admissionXML.getChild("type").getText().startsWith("u")) type = 2; if (getAdmissionByIep(iep) == null) { System.out.println("Il n'y a pas d'admission avec cet IEP"); } else { Admission ad = getAdmissionByIep(iep); ad.setType(type); editAdmission(ad); System.out.println("admission modifie"); } String date_sortie = admissionXML.getChild("date_sortie").getValue(); DateFormat format = new SimpleDateFormat("YYYY-MM-DD", Locale.ENGLISH); Date date_s = format.parse(date_sortie); String date_entree = admissionXML.getChild("date_sortie").getValue(); Date date_e = format.parse(date_entree); List<Mouvement> listemouv = getMouvementByIep(iep); if (listemouv != null) { Mouvement mouv = listemouv.get(listemouv.size()); updateMouvement(mouv, mouv.getAdmission(), mouv.getLit(), mouv.getUf(), date_e, date_s); } else { System.out.println("pas de mouvement pour cette admission"); } } if (listefichiers[i].startsWith("mouv")) { System.out.println("fichier new trouv"); Document doc = builder.build("" + repertoire + "\\" + listefichiers[i]); Element root = doc.getRootElement(); Element mouvementXML = root; String service = "Cardiologie"; int ipp; int iep; Lit lit = new Lit(); String date_entree = mouvementXML.getChild("date_entree").getValue(); DateFormat format = new SimpleDateFormat("YYYY-MM-DD", Locale.ENGLISH); Date date = format.parse(date_entree); ipp = parseInt(mouvementXML.getChild("ipp").getValue()); iep = parseInt(mouvementXML.getChild("iep").getValue()); if (mouvementXML.getChild("service").getText().endsWith("2")) service = "Radio 2"; if (mouvementXML.getChild("service").getText().endsWith("1")) service = "Radio 1"; List<Lit> liste_lits = getLitByUF(service); for (Lit buff_lit : liste_lits) { if (!buff_lit.getOccupe()) { lit = buff_lit; } } newMouvement(getAdmissionByIep(iep), lit, getUniteFontionnelleByNom(service), date); System.out.println("nouvelle admission ajoute"); } } } catch (Exception e) { System.out.println("Erreur de lecture du fichier d'admission"); } }
From source file:ca.mcgill.cs.swevo.jayfx.model.FlyweightElementFactory.java
License:Open Source License
/** * @param elementXML//from www . java2 s . com * @return */ @SuppressWarnings("unchecked") public static <E> E getElement(Element elementXML) { // extract the category and the ID and delegate the behavior. String identifierString = elementXML.getAttribute(IElement.ID).getValue(); Category category = Category.valueOf(elementXML.getChild(Category.class.getSimpleName())); return (E) getElement(category, identifierString); }
From source file:ca.nrc.cadc.ac.xml.AbstractReaderWriter.java
License:Open Source License
/** * Get a User object from a JDOM element. * * @param element The User JDOM element. * @return A User object./*from w w w . j a v a2 s . c om*/ * @throws ReaderException */ protected final User getUser(Element element) throws ReaderException { User user = new User(); // id Element internalIDElement = element.getChild(INTERNAL_ID); if (internalIDElement != null) { setInternalID(user, internalIDElement); } // identities Element identitiesElement = element.getChild(IDENTITIES); if (identitiesElement != null) { List<Element> identityElements = identitiesElement.getChildren(IDENTITY); for (Element identityElement : identityElements) { user.getIdentities().add(getPrincipal(identityElement)); } } // personalDetails Element personalDetailsElement = element.getChild(PERSONAL_DETAILS); if (personalDetailsElement != null) { user.personalDetails = getPersonalDetails(personalDetailsElement); } // posixDetails Element posixDetailsElement = element.getChild(POSIX_DETAILS); if (posixDetailsElement != null) { user.posixDetails = getPosixDetails(posixDetailsElement); } return user; }