List of usage examples for org.jdom2 Element getName
public String getName()
From source file:cz.pecina.retro.trec.XML.java
License:Open Source License
/** * Reads the tape from an XML file./*from w ww . j ava 2 s. c o m*/ * * @param file input file */ public void read(final File file) { log.fine("Reading tape data from an XML file, file: " + file); try { SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new StreamSource( getClass().getResourceAsStream("tape-" + TAPE_XML_FILE_VERSION + ".xsd"))) .newValidator().validate(new StreamSource(file)); } catch (final Exception exception) { log.fine("Error, validation failed, exception: " + exception.getMessage()); throw Application.createError(this, "validation"); } Document doc; try { doc = new SAXBuilder().build(file); } catch (final JDOMException exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(this, "parsing"); } catch (final Exception exception) { log.fine("Error, reading failed, exception: " + exception.getMessage()); throw Application.createError(this, "XMLRead"); } Element tag; try { tag = doc.getRootElement(); } catch (final Exception exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(this, "parsing"); } if (!tag.getName().equals("tape")) { log.fine("Error, parsing failed, no <tape> tag"); throw Application.createError(this, "noTape"); } if (!TAPE_XML_FILE_VERSION.equals(tag.getAttributeValue("version"))) { log.fine("Version mismatch"); throw Application.createError(this, "version"); } if (!"per sec".equals(tag.getAttributeValue("unit"))) { log.fine("Unsupported sample rate"); throw Application.createError(this, "XMLSampleRate"); } tape.clear(); try { long currPos = -1; for (Element pulse : tag.getChildren("pulse")) { final long start = Long.parseLong(pulse.getAttributeValue("start")); final long duration = Long.parseLong(pulse.getAttributeValue("duration")); if ((start <= currPos) || (duration <= 0)) { log.fine("Error in XML file"); throw Application.createError(this, "XML"); } tape.put(start, duration); log.finest(String.format("Read: (%d, %d)", start, duration)); currPos = start; } } catch (final Exception exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(this, "parsing"); } log.fine("Reading completed"); }
From source file:ddf.catalog.source.opensearch.impl.OpenSearchSource.java
License:Open Source License
/** * @param is// ww w . j av a 2 s .c om * @param queryRequest * @return * @throws ddf.catalog.source.UnsupportedQueryException */ private SourceResponseImpl processResponse(InputStream is, QueryRequest queryRequest) throws UnsupportedQueryException { List<Result> resultQueue = new ArrayList<>(); SyndFeedInput syndFeedInput = new SyndFeedInput(); SyndFeed syndFeed = null; try { syndFeed = syndFeedInput.build(new InputStreamReader(is, StandardCharsets.UTF_8)); } catch (FeedException e) { LOGGER.debug("Unable to read RSS/Atom feed.", e); } List<SyndEntry> entries; long totalResults = 0; List<Element> foreignMarkup = null; if (syndFeed != null) { entries = syndFeed.getEntries(); for (SyndEntry entry : entries) { resultQueue.addAll(createResponseFromEntry(entry)); } totalResults = entries.size(); foreignMarkup = syndFeed.getForeignMarkup(); for (Element element : foreignMarkup) { if (element.getName().equals("totalResults")) { try { totalResults = Long.parseLong(element.getContent(0).getValue()); } catch (NumberFormatException | IndexOutOfBoundsException e) { // totalResults is already initialized to the correct value, so don't change it here. LOGGER.debug("Received invalid number of results.", e); } } } } SourceResponseImpl response = new SourceResponseImpl(queryRequest, resultQueue); response.setHits(totalResults); if (foreignMarkup != null) { this.foreignMarkupBiConsumer.accept(Collections.unmodifiableList(foreignMarkup), response); } return response; }
From source file:ddf.catalog.source.opensearch.impl.OpenSearchSource.java
License:Open Source License
/** * Creates a single response from input parameters. Performs XPath operations on the document to * retrieve data not passed in.// w w w .j av a 2 s .c o m * * @param entry a single Atom entry * @return single response * @throws ddf.catalog.source.UnsupportedQueryException */ private List<Result> createResponseFromEntry(SyndEntry entry) throws UnsupportedQueryException { String id = entry.getUri(); if (id != null && !id.isEmpty()) { id = id.substring(id.lastIndexOf(':') + 1); } List<SyndContent> contents = entry.getContents(); List<SyndCategory> categories = entry.getCategories(); List<Metacard> metacards = new ArrayList<>(); List<Element> foreignMarkup = entry.getForeignMarkup(); String relevance = ""; for (Element element : foreignMarkup) { if (element.getName().equals("score")) { relevance = element.getContent(0).getValue(); } metacards.addAll(processAdditionalForeignMarkups(element, id)); } // we currently do not support downloading content via an RSS enclosure, this support can be // added at a later date if we decide to include it for (SyndContent content : contents) { Metacard metacard = parseContent(content.getValue(), id); if (metacard != null) { metacard.setSourceId(this.shortname); String title = metacard.getTitle(); if (StringUtils.isEmpty(title)) { metacard.setAttribute(new AttributeImpl(Core.TITLE, entry.getTitle())); } metacards.add(metacard); } } for (int i = 0; i < categories.size() && i < metacards.size(); i++) { SyndCategory category = categories.get(i); Metacard metacard = metacards.get(i); if (StringUtils.isBlank(metacard.getContentTypeName())) { metacard.setAttribute(new AttributeImpl(Metacard.CONTENT_TYPE, category.getName())); } } List<Result> results = new ArrayList<>(); for (Metacard metacard : metacards) { ResultImpl result = new ResultImpl(metacard); if (relevance == null || relevance.isEmpty()) { LOGGER.debug("couldn't find valid relevance. Setting relevance to 0"); relevance = "0"; } result.setRelevanceScore(new Double(relevance)); results.add(result); } return results; }
From source file:ddf.catalog.source.opensearch.impl.OpenSearchSource.java
License:Open Source License
private List<Metacard> processAdditionalForeignMarkups(Element element, String id) { List<Metacard> metacards = new ArrayList<>(); if (CollectionUtils.isNotEmpty(markUpSet) && markUpSet.contains(element.getName())) { XMLOutputter xmlOutputter = new XMLOutputter(); Metacard metacard = parseContent(xmlOutputter.outputString(element), id); metacards.add(metacard);//from w ww.j a v a2s.com } return metacards; }
From source file:ddf.catalog.source.opensearch.OpenSearchSource.java
License:Open Source License
/** * @param is//from w w w. j a va 2 s . c o m * @param queryRequest * @return * @throws ddf.catalog.source.UnsupportedQueryException */ private SourceResponseImpl processResponse(InputStream is, QueryRequest queryRequest) throws UnsupportedQueryException { List<Result> resultQueue = new ArrayList<>(); SyndFeedInput syndFeedInput = new SyndFeedInput(); SyndFeed syndFeed = null; try { syndFeed = syndFeedInput.build(new InputStreamReader(is)); } catch (FeedException e) { LOGGER.error("Unable to read RSS/Atom feed.", e); } List<SyndEntry> entries = null; long totalResults = 0; if (syndFeed != null) { entries = syndFeed.getEntries(); for (SyndEntry entry : entries) { resultQueue.addAll(createResponseFromEntry(entry)); } totalResults = entries.size(); List<Element> foreignMarkup = syndFeed.getForeignMarkup(); for (Element element : foreignMarkup) { if (element.getName().equals("totalResults")) { try { totalResults = Long.parseLong(element.getContent(0).getValue()); } catch (NumberFormatException | IndexOutOfBoundsException e) { // totalResults is already initialized to the correct value, so don't change it here. LOGGER.debug("Received invalid number of results.", e); } } } } SourceResponseImpl response = new SourceResponseImpl(queryRequest, resultQueue); response.setHits(totalResults); return response; }
From source file:ddf.catalog.source.opensearch.OpenSearchSource.java
License:Open Source License
/** * Creates a single response from input parameters. Performs XPath operations on the document to * retrieve data not passed in./*from www. j av a2 s. c om*/ * * @param entry * a single Atom entry * @return single response * @throws ddf.catalog.source.UnsupportedQueryException */ private List<Result> createResponseFromEntry(SyndEntry entry) throws UnsupportedQueryException { String id = entry.getUri(); if (id != null && !id.isEmpty()) { id = id.substring(id.lastIndexOf(':') + 1); } List<SyndContent> contents = entry.getContents(); List<SyndCategory> categories = entry.getCategories(); List<Metacard> metacards = new ArrayList<>(); List<Element> foreignMarkup = entry.getForeignMarkup(); String relevance = ""; String source = ""; for (Element element : foreignMarkup) { if (element.getName().equals("score")) { relevance = element.getContent(0).getValue(); } } //we currently do not support downloading content via an RSS enclosure, this support can be added at a later date if we decide to include it for (SyndContent content : contents) { MetacardImpl metacard = getMetacardImpl(parseContent(content.getValue(), id)); metacard.setSourceId(this.shortname); String title = metacard.getTitle(); if (StringUtils.isEmpty(title)) { metacard.setTitle(entry.getTitle()); } if (!source.isEmpty()) { metacard.setSourceId(source); } metacards.add(metacard); } for (int i = 0; i < categories.size() && i < metacards.size(); i++) { SyndCategory category = categories.get(i); Metacard metacard = metacards.get(i); if (StringUtils.isBlank(metacard.getContentTypeName())) { ((MetacardImpl) metacard).setContentTypeName(category.getName()); } } List<Result> results = new ArrayList<>(); for (Metacard metacard : metacards) { ResultImpl result = new ResultImpl(metacard); if (relevance == null || relevance.isEmpty()) { LOGGER.debug("couldn't find valid relevance. Setting relevance to 0"); relevance = "0"; } result.setRelevanceScore(new Double(relevance)); results.add(result); } return results; }
From source file:de.andrena.tools.macker.rule.RuleSetBuilder.java
License:Open Source License
public RuleSet buildRuleSet(final Element ruleSetElem, final RuleSet parent) throws RulesException { final RuleSet ruleSet = new RuleSet(parent); final String name = ruleSetElem.getAttributeValue("name"); if (name != null) { ruleSet.setName(name);//from ww w.ja v a 2s .c om } buildSeverity(ruleSet, ruleSetElem); for (final Element subElem : getChildren(ruleSetElem)) { final String subElemName = subElem.getName(); if (subElemName.equals("pattern")) { final String patternName = subElem.getAttributeValue("name"); if (ruleSet.declaresPattern(patternName)) { throw new RulesDocumentException(subElem, "Pattern named \"" + patternName + "\" is already defined in this context"); } ruleSet.setPattern(patternName, buildPattern(subElem, ruleSet)); } else if (subElemName.equals("subset")) { if (ruleSet.getSubsetPattern() != null) { throw new RulesDocumentException(subElem, "<ruleset> may only contain a single <subset> element"); } ruleSet.setSubsetPattern(buildPattern(subElem, ruleSet)); } else if (subElemName.equals("access-rule")) { ruleSet.addRule(buildAccessRule(subElem, ruleSet)); } else if (subElemName.equals("var")) { ruleSet.addRule(buildVariable(subElem, ruleSet)); } else if (subElemName.equals("foreach")) { ruleSet.addRule(buildForEach(subElem, ruleSet)); } else if (subElemName.equals("ruleset")) { ruleSet.addRule(buildRuleSet(subElem, ruleSet)); } else if (subElemName.equals("message")) { ruleSet.addRule(buildMessage(subElem, ruleSet)); } } return ruleSet; }
From source file:de.andrena.tools.macker.rule.RuleSetBuilder.java
License:Open Source License
public Pattern buildPattern(final Element patternElem, final RuleSet ruleSet, final boolean isTopElem, final Pattern nextPat) throws RulesException { // handle options final String otherPatName = patternElem.getAttributeValue("pattern"); final String className = getClassNameAttributeValue(patternElem); final String filterName = patternElem.getAttributeValue("filter"); CompositePatternType patType;//from ww w .j a v a2 s . c om if (patternElem.getName().equals("include")) { patType = CompositePatternType.INCLUDE; } else if (patternElem.getName().equals("exclude")) { patType = filterName == null ? CompositePatternType.EXCLUDE : CompositePatternType.INCLUDE; } else if (isTopElem) { patType = CompositePatternType.INCLUDE; } else { throw new RulesDocumentException(patternElem, "Invalid element <" + patternElem.getName() + "> --" + " expected <include> or <exclude>"); } if (otherPatName != null && className != null) { throw new RulesDocumentException(patternElem, "patterns cannot have both a \"pattern\" and a \"class\" attribute"); } // do the head thing Pattern head = null; if (className != null) { head = new RegexPattern(className); } else if (otherPatName != null) { head = ruleSet.getPattern(otherPatName); if (head == null) { throw new UndeclaredPatternException(otherPatName); } } // build up children Pattern childrenPat = null; final List<Element> children = new ArrayList<Element>(getChildren(patternElem)); // ! // workaround // for // bug // in // JUnit // List children = patternElem.getChildren(); // this should work // instead when JUnit bug is fixed for (final ListIterator<Element> childIter = children.listIterator(children.size()); childIter .hasPrevious();) { final Element subElem = childIter.previous(); if (subElem.getName().equals("message")) { continue; } childrenPat = buildPattern(subElem, ruleSet, false, childrenPat); } // wrap head in a filter if necessary if (filterName != null) { final Map<String, String> options = new HashMap<String, String>(); for (final Attribute attr : getAttributes(patternElem)) { options.put(attr.getName(), attr.getValue()); } options.remove("name"); options.remove("pattern"); options.remove("class"); options.remove("regex"); final Filter filter = FilterFinder.findFilter(filterName); head = filter.createPattern(ruleSet, head == null ? new ArrayList<Pattern>() : Collections.singletonList(head), options); if (patternElem.getName().equals("exclude")) { head = CompositePattern.create(CompositePatternType.EXCLUDE, head, null, null); } } // pull together composite return CompositePattern.create(patType, head, childrenPat, nextPat); }
From source file:de.andrena.tools.macker.rule.RuleSetBuilder.java
License:Open Source License
public AccessRule buildAccessRule(final Element ruleElem, final RuleSet ruleSet) throws RulesException { AccessRule prevRule = null, topRule = null; for (final Element subElem : getChildren(ruleElem)) { final AccessRule accRule = new AccessRule(ruleSet); if (subElem.getName().equals("allow")) { accRule.setType(AccessRuleType.ALLOW); } else if (subElem.getName().equals("deny")) { accRule.setType(AccessRuleType.DENY); } else if (subElem.getName().equals("from") || subElem.getName().equals("to") || subElem.getName().equals("message")) { continue; } else {/* w ww . j a va2 s .com*/ throw new RulesDocumentException(subElem, "Invalid element <" + subElem.getName() + "> --" + " expected an access rule (<deny> or <allow>)"); } final Element fromElem = subElem.getChild("from"); if (fromElem != null) { accRule.setFrom(buildPattern(fromElem, ruleSet)); } final Element toElem = subElem.getChild("to"); if (toElem != null) { accRule.setTo(buildPattern(toElem, ruleSet)); } if (!subElem.getChildren().isEmpty()) { accRule.setChild(buildAccessRule(subElem, ruleSet)); } if (topRule == null) { topRule = accRule; } else { prevRule.setNext(accRule); } prevRule = accRule; } if (topRule != null) { topRule.setMessage(ruleElem.getChildText("message")); buildSeverity(topRule, ruleElem); } return topRule; }
From source file:de.bund.bfr.knime.pmm.common.ParametricModel.java
License:Open Source License
public ParametricModel(final Element modelElement) { this();//from w w w. j av a 2 s . c om m_dbuuid = modelElement.getAttributeValue(ATT_MDBUUID); em_dbuuid = modelElement.getAttributeValue(ATT_EMDBUUID); modelName = modelElement.getAttributeValue(ATT_MODELNAME); if (modelElement.getAttributeValue("FittedModelName") != null && !modelElement.getAttributeValue("FittedModelName").isEmpty()) fittedModelName = modelElement.getAttributeValue("FittedModelName"); modelClass = XmlHelper.getInt(modelElement, ATT_MODELCLASS); level = Integer.valueOf(modelElement.getAttributeValue(ATT_LEVEL)); modelId = Integer.valueOf(modelElement.getAttributeValue(ATT_MODELID)); estModelId = Integer.valueOf(modelElement.getAttributeValue(ATT_ESTMODELID)); if (modelElement.getAttributeValue(ATT_GLOBALMODELID) != null) globalModelId = Integer.valueOf(modelElement.getAttributeValue(ATT_GLOBALMODELID)); if (modelElement.getAttributeValue(ATT_CHECKED) != null && !modelElement.getAttributeValue(ATT_CHECKED).isEmpty()) isChecked = Boolean.valueOf(modelElement.getAttributeValue(ATT_CHECKED)); if (modelElement.getAttributeValue(ATT_COMMENT) != null && !modelElement.getAttributeValue(ATT_COMMENT).isEmpty()) comment = modelElement.getAttributeValue(ATT_COMMENT); if (modelElement.getAttributeValue(ATT_QSCORE) != null && !modelElement.getAttributeValue(ATT_QSCORE).isEmpty()) qualityScore = Integer.valueOf(modelElement.getAttributeValue(ATT_QSCORE)); if (modelElement.getAttributeValue(ATT_RSS) != null) rss = Double.valueOf(modelElement.getAttributeValue(ATT_RSS)); if (modelElement.getAttributeValue(ATT_RMS) != null) rms = Double.valueOf(modelElement.getAttributeValue(ATT_RMS)); if (modelElement.getAttributeValue(ATT_AIC) != null) aic = Double.valueOf(modelElement.getAttributeValue(ATT_AIC)); if (modelElement.getAttributeValue(ATT_BIC) != null) bic = Double.valueOf(modelElement.getAttributeValue(ATT_BIC)); rsquared = Double.valueOf(modelElement.getAttributeValue(ATT_RSQUARED)); condId = Integer.valueOf(modelElement.getAttributeValue(ATT_CONDID)); HashMap<String, String> rMap = new HashMap<>(); for (Element el : modelElement.getChildren()) { if (el.getName().equals(ATT_FORMULA)) { formula = el.getText(); } else if (el.getName().equals(ELEMENT_PARAM)) { boolean minNull = el.getAttributeValue(ATT_MINVALUE) == null || el.getAttributeValue(ATT_MINVALUE).equals("null"); boolean maxNull = el.getAttributeValue(ATT_MAXVALUE) == null || el.getAttributeValue(ATT_MAXVALUE).equals("null"); boolean valNull = el.getAttributeValue(ATT_VALUE) == null || el.getAttributeValue(ATT_VALUE).equals("null"); boolean errNull = el.getAttributeValue(ATT_PARAMERR) == null || el.getAttributeValue(ATT_PARAMERR).equals("null"); boolean categoryNull = el.getAttributeValue("Category") == null || el.getAttributeValue("Category").equals("null"); boolean unitNull = el.getAttributeValue("Unit") == null || el.getAttributeValue("Unit").equals("null"); Double min = minNull ? Double.NaN : Double.valueOf(el.getAttributeValue(ATT_MINVALUE)); Double max = maxNull ? Double.NaN : Double.valueOf(el.getAttributeValue(ATT_MAXVALUE)); Double val = valNull ? Double.NaN : Double.valueOf(el.getAttributeValue(ATT_VALUE)); Double err = errNull ? Double.NaN : Double.valueOf(el.getAttributeValue(ATT_PARAMERR)); String category = categoryNull ? null : el.getAttributeValue("Category"); String unit = unitNull ? null : el.getAttributeValue("Unit"); ParamXml px = new ParamXml(el.getAttributeValue(ATT_PARAMNAME), XmlHelper.getBoolean(el, ATT_IS_START), val, err, min, max, null, null, category, unit); parameter.add(px); } else if (el.getName().equals(ATT_INDEPVAR)) { boolean minNull = el.getAttributeValue(ATT_MININDEP) == null || el.getAttributeValue(ATT_MININDEP).equals("null"); boolean maxNull = el.getAttributeValue(ATT_MAXINDEP) == null || el.getAttributeValue(ATT_MAXINDEP).equals("null"); boolean categoryNull = el.getAttributeValue("Category") == null || el.getAttributeValue("Category").equals("null"); boolean unitNull = el.getAttributeValue("Unit") == null || el.getAttributeValue("Unit").equals("null"); Double min = minNull ? Double.NaN : Double.valueOf(el.getAttributeValue(ATT_MININDEP)); Double max = maxNull ? Double.NaN : Double.valueOf(el.getAttributeValue(ATT_MAXINDEP)); String category = categoryNull ? null : el.getAttributeValue("Category"); String unit = unitNull ? null : el.getAttributeValue("Unit"); IndepXml ix = new IndepXml(el.getAttributeValue(ATT_PARAMNAME), min, max, category, unit); independent.add(ix); } else if (el.getName().equals(ATT_RMAP)) { rMap.put(el.getAttributeValue("NEW"), el.getAttributeValue("OLD")); } else if (el.getName().equals(ATT_DEPVAR)) { depXml = new DepXml(el.getAttributeValue(ATT_PARAMNAME), el.getAttributeValue("Category"), el.getAttributeValue("Unit")); } else if (el.getName().equals(ATT_MLIT)) { try { modelLit = new PmmXmlDoc(el.getText()); } catch (Exception e) { e.printStackTrace(); } } else if (el.getName().equals(ATT_EMLIT)) { try { estLit = new PmmXmlDoc(el.getText()); } catch (Exception e) { e.printStackTrace(); } } else if (el.getName().equals(ATT_INDEP)) { try { independent = new PmmXmlDoc(el.getText()); } catch (Exception e) { e.printStackTrace(); } } else if (el.getName().equals(ATT_DEP)) { try { PmmXmlDoc dep = new PmmXmlDoc(el.getText()); if (dep.size() > 0) depXml = (DepXml) dep.get(0); } catch (Exception e) { e.printStackTrace(); } } else if (el.getName().equals(ATT_PARAM)) { try { parameter = new PmmXmlDoc(el.getText()); } catch (Exception e) { e.printStackTrace(); } } else { assert false; } } for (String name : rMap.keySet()) { String origName = rMap.get(name); if (depXml.getOrigName().equals(origName)) { depXml.setName(name); } else { for (PmmXmlElementConvertable el : parameter.getElementSet()) { if (el instanceof ParamXml) { ParamXml px = (ParamXml) el; if (px.getOrigName().equals(origName)) { px.setName(name); break; } } } for (PmmXmlElementConvertable el : independent.getElementSet()) { if (el instanceof IndepXml) { IndepXml ix = (IndepXml) el; if (ix.getOrigName().equals(origName)) { ix.setName(name); break; } } } } } }