List of usage examples for org.jdom2 Element getTextTrim
public String getTextTrim()
From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.OperatorNormalizer.java
License:Apache License
private void replaceOperators(final Element element, final Map<String, String> replacements) { assert element != null && replacements != null; List<Element> operatorsToReplace = new ArrayList<Element>(); for (Element operator : element.getDescendants(new ElementFilter(OPERATOR))) { if (replacements.containsKey(operator.getTextTrim())) { operatorsToReplace.add(operator); }//from w w w.j av a 2 s .c o m } for (Element operator : operatorsToReplace) { final String oldOperator = operator.getTextTrim(); final String newOperator = replacements.get(oldOperator); operator.setText(newOperator); LOGGER.log(Level.FINE, "Operator ''{0}'' was replaced by ''{1}''", new Object[] { oldOperator, newOperator }); } }
From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.OperatorNormalizer.java
License:Apache License
private void replaceIdentifiers(final Element ancestor, final Set<String> operators) { assert ancestor != null && operators != null; final List<Element> toReplace = new ArrayList<Element>(); for (Element element : ancestor.getDescendants(new ElementFilter(IDENTIFIER))) { // TODO: control whole ranges of symbols rather than listed ones if (operators.contains(element.getTextTrim())) { toReplace.add(element);//from ww w . j a v a2 s.c om } } for (Element element : toReplace) { LOGGER.log(Level.FINE, "Creating an operator from {0}", element.getText()); replaceElement(element, OPERATOR); } }
From source file:cz.pecina.retro.cpu.Register.java
License:Open Source License
@Override public void unmarshal(final Element descriptor) { assert descriptor.getName().equals(tagName); final String value = descriptor.getTextTrim(); processValue(value);// w ww.j a va2s.co m log.fine("Register '" + name + "' unmarshalled to value '" + value + "'"); }
From source file:cz.pecina.retro.memory.Snapshot.java
License:Open Source License
/** * Processes a block tag./*from www. j a v a 2 s.c o m*/ * * @param memory memory array * @param tag tag to process * @param destinationAddress destination address ({@code -1} = none) * @return info info record */ public static Info processBlockElement(final byte[] memory, final Element tag, final int destinationAddress) { log.finer( String.format("Method processMemoryElement called: destination address: %04x", destinationAddress)); assert (destinationAddress >= -1) && (destinationAddress <= 0xffff); final int size = memory.length; final Info info = new Info(); int startAddress = 0; if (destinationAddress == -1) { try { startAddress = Integer.parseInt(tag.getAttributeValue("start"), 16); } catch (final Exception exception) { log.fine("Error in starting address, exception: " + exception.getMessage()); throw Application.createError(Snapshot.class, "parsing"); } log.finer(String.format("Starting address: %04x", startAddress)); } else { startAddress = destinationAddress; log.finer(String.format("Destination address used instead: %04x", startAddress)); } for (Element bytes : tag.getChildren(subtagName)) { int count; String string; if ((string = bytes.getAttributeValue("count")) != null) { try { count = Integer.parseInt(string); } catch (final Exception exception) { log.fine("Error in count, exception: " + exception.getMessage()); throw Application.createError(Snapshot.class, "parsing"); } } else { count = 1; } string = bytes.getTextTrim(); try { for (; count > 0; count--) { for (int i = 0; i < (string.length() / 2); i++) { startAddress &= 0xffff; if (startAddress < info.minAddress) { info.minAddress = startAddress; } if (startAddress > info.maxAddress) { info.maxAddress = startAddress; } final int dataByte = Integer.parseInt(string.substring(i * 2, (i + 1) * 2), 16); memory[startAddress % size] = (byte) dataByte; log.finest(String.format("Read: %02x -> (%04x)", dataByte, startAddress)); startAddress++; info.number++; } } } catch (final Exception exception) { log.fine("Error, parsing failed, exception: " + exception.getMessage()); throw Application.createError(Snapshot.class, "parsing"); } } log.finer("Method processMemoryElement finished"); return info; }
From source file:de.altimos.util.translator.XmlTranslation.java
License:Apache License
@Override public String getString(Translator translator, String key, Locale locale, String domain, Object[] args) { Element e = getElement(translator, key.toLowerCase(), locale, domain); if (e != null) { return e.getTextTrim().replace("\\n", "\n"); }// w w w .jav a2 s .c o m return ""; }
From source file:de.herm_detlef.java.application.io.Import.java
License:Apache License
private static void createNode(Element child) { List<Element> children = child.getChildren(); if (children.isEmpty()) { switch (TAG.getValueOf(child.getName())) { case ID:/*from w ww . j a v a 2 s . c o m*/ exerciseItem.setItemId(Integer.parseInt(child.getTextTrim())); break; case TEXT: { final String str = child.getTextTrim(); if (isQuestionPart) { exerciseItem.addQuestionText(str); } else if (isAnswerPart) { Attribute mark = child .getAttribute(ApplicationConstants.NAME_OF_XML_ATTRIBUTE_ANSWER_TEXT_MARK); if (mark != null) { try { exerciseItem.addAnswerText(str, mark.getBooleanValue()); } catch (DataConversionException e) { Utilities.showErrorMessage(e.getClass().getSimpleName(), e.getMessage()); assert false : String.format("DataConversionException: %s", mark.toString()); // TODO exerciseItem.addAnswerText(str, false); } } else { exerciseItem.addAnswerText(str, false); } } else if (isSolutionPart) { exerciseItem.addSolutionText(str); } break; } case CODE: if (isQuestionPart) { exerciseItem.addQuestionCode(child.getTextTrim()); } break; case TEXT2: if (isQuestionPart) { exerciseItem.addQuestionText2(child.getTextTrim()); } break; case CATALOG: // TODO empty catalog file break; default: assert false : String.format("%s", TAG.getValueOf(child.getName()).name()); // TODO } return; } for (Element aChild : children) { switch (TAG.getValueOf(aChild.getName())) { case ITEM: exerciseItem = new ExerciseItem(); exerciseItemList.add(exerciseItem); break; case QUESTION: signalQuestion(); break; case SINGLE_CHOICE_ANSWER: signalSingleChoiceAnswer(); exerciseItem.createSingleChoiceModel(); break; case MULTIPLE_CHOICE_ANSWER: signalMultipleChoiceAnswer(); exerciseItem.createMultipleChoiceModel(); break; case SOLUTION: signalSolution(); break; case ID: case TEXT: case CODE: case TEXT2: break; default: assert false : String.format("%s", TAG.getValueOf(aChild.getName()).name()); // TODO } createNode(aChild); } }
From source file:de.knewcleus.openradar.view.stdroutes.StdRouteReader.java
License:Open Source License
private void readRouteXml() { SAXBuilder builder = new SAXBuilder(); InputStream xmlInputStream = null; File dir = new File("data/routes/" + data.getAirportCode()); if (!dir.exists() || !dir.isDirectory()) { return;//from w ww. ja v a 2 s .co m } List<File> files = new ArrayList<File>(Arrays.asList(dir.listFiles())); data.getNavaidDB().clearAddPoints(); while (files.size() > 0) { File file = files.remove(0); try { if (!file.getName().endsWith(".xml")) continue; // todo read all files xmlInputStream = new FileInputStream(file); Document document = (Document) builder.build(xmlInputStream); Element rootNode = document.getRootElement(); // String orFilename = "data/routes/" + data.getAirportCode() + "/" + file.getName().substring(0, file.getName().indexOf(".xml")) + ".or.xml"; if ("ProceduresDB".equalsIgnoreCase(rootNode.getName())) {// && !(new File(orFilename).exists())) { // if converted file does not exist, convert it now. // deactivated for now convertProcedureDbFile(orFilename, rootNode); } else { // read or file List<Element> list = rootNode.getChildren("addPoint"); for (Element eAddPoint : list) { String code = eAddPoint.getAttributeValue("code"); String sPoint = eAddPoint.getAttributeValue("point"); try { Point2D point = StdRoute.getPoint(data, mapViewAdapter, sPoint, null); data.getNavaidDB().addPoint(code, point); } catch (Exception e) { log.error("Problem to parse file " + file.getAbsolutePath() + ", addPoint: " + code + ": " + sPoint + ", Error:" + e.getMessage()); } } List<Element> includeList = rootNode.getChildren("include"); for (Element eInclude : includeList) { String fileName = eInclude.getAttributeValue("file"); files.add(new File(file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(File.separator) + 1) + fileName)); } // routes list = rootNode.getChildren("route"); for (Element eRoute : list) { String name = eRoute.getAttributeValue("name"); String displayMode = eRoute.getAttributeValue("displayMode"); String zoomMin = eRoute.getAttributeValue("zoomMin"); String zoomMax = eRoute.getAttributeValue("zoomMax"); String stroke = eRoute.getAttributeValue("stroke"); String lineWidth = eRoute.getAttributeValue("lineWidth"); String color = eRoute.getAttributeValue("color"); List<Element> sublist = eRoute.getChildren(); AStdRouteElement previous = null; StdRoute route = new StdRoute(data, mapViewAdapter, name, displayMode, zoomMin, zoomMax, stroke, lineWidth, color); for (Element element : sublist) { try { if (element.getName().equalsIgnoreCase("activeLandingRunways")) { route.setActiveLandingRunways(element.getText()); } else if (element.getName().equalsIgnoreCase("activeStartRunways")) { route.setActiveStartingRunways(element.getText()); } else if (element.getName().equalsIgnoreCase("navaids")) { color = element.getAttributeValue("color"); route.setNavaids(element.getText(), color); } else if (element.getName().equalsIgnoreCase("include")) { String routeName = element.getAttributeValue("routeName"); route.includeRoute(stdRoutes, routeName); } else if (element.getName().equalsIgnoreCase("line")) { String start = element.getAttributeValue("start"); String end = element.getAttributeValue("end"); String angle = element.getAttributeValue("angle"); String length = element.getAttributeValue("length"); String startOffset = element.getAttributeValue("startOffset"); String endOffset = element.getAttributeValue("endOffset"); stroke = element.getAttributeValue("stroke"); lineWidth = element.getAttributeValue("lineWidth"); String arrows = element.getAttributeValue("arrows"); color = element.getAttributeValue("color"); String text = element.getAttributeValue("text"); StdRouteLine line = new StdRouteLine(route, mapViewAdapter, previous, start, end, angle, length, startOffset, endOffset, stroke, lineWidth, arrows, color, text); previous = line; route.addElement(line); } else if (element.getName().equalsIgnoreCase("bow")) { String center = element.getAttributeValue("center"); String radius = element.getAttributeValue("radius"); String startAngle = element.getAttributeValue("startAngle"); String extentAngle = element.getAttributeValue("extentAngle"); stroke = element.getAttributeValue("stroke"); lineWidth = element.getAttributeValue("lineWidth"); String arrows = element.getAttributeValue("arrows"); color = element.getAttributeValue("color"); String text = element.getAttributeValue("text"); StdRouteBow bow = new StdRouteBow(route, mapViewAdapter, previous, center, radius, startAngle, extentAngle, stroke, lineWidth, color, arrows, text); previous = bow; route.addElement(bow); } else if (element.getName().equalsIgnoreCase("curve")) { String start = element.getAttributeValue("start"); String end = element.getAttributeValue("end"); String controlPoint = element.getAttributeValue("controlPoint"); stroke = element.getAttributeValue("stroke"); lineWidth = element.getAttributeValue("lineWidth"); String arrows = element.getAttributeValue("arrows"); color = element.getAttributeValue("color"); StdRouteCurve bow = new StdRouteCurve(route, mapViewAdapter, previous, start, end, controlPoint, stroke, lineWidth, color, arrows); previous = bow; route.addElement(bow); } else if (element.getName().equalsIgnoreCase("intercept")) { String start = element.getAttributeValue("start"); String startOffset = element.getAttributeValue("startOffset"); String startHeading = element.getAttributeValue("startHeading"); String startTurn = element.getAttributeValue("startTurn"); String radius = element.getAttributeValue("radius"); String speed = element.getAttributeValue("speed"); String end = element.getAttributeValue("end"); String radial = element.getAttributeValue("radial"); String endHeading = element.getAttributeValue("endHeading"); String direction = element.getAttributeValue("direction"); String endOffset = element.getAttributeValue("endOffset"); String text = element.getAttributeValue("text"); stroke = element.getAttributeValue("stroke"); lineWidth = element.getAttributeValue("lineWidth"); String arrows = element.getAttributeValue("arrows"); color = element.getAttributeValue("color"); StdRouteIntercept intercept = new StdRouteIntercept(route, mapViewAdapter, previous, start, startOffset, startHeading, startTurn, radius, speed, end, radial, endHeading, direction, endOffset, stroke, lineWidth, arrows, color, text); previous = intercept; route.addElement(intercept); } else if (element.getName().equalsIgnoreCase("loop")) { String navpoint = element.getAttributeValue("navpoint"); String inboundHeading = element.getAttributeValue("inboundHeading"); String length = element.getAttributeValue("length"); String width = element.getAttributeValue("width"); String right = element.getAttributeValue("right"); String arrows = element.getAttributeValue("arrows"); String minHeight = element.getAttributeValue("minHeight"); String maxHeight = element.getAttributeValue("maxHeight"); String misapHeight = element.getAttributeValue("misapHeight"); stroke = element.getAttributeValue("stroke"); lineWidth = element.getAttributeValue("lineWidth"); color = element.getAttributeValue("color"); StdRouteLoop ellipse = new StdRouteLoop(route, mapViewAdapter, previous, navpoint, inboundHeading, length, width, right, arrows, minHeight, maxHeight, misapHeight, stroke, lineWidth, color); previous = ellipse; route.addElement(ellipse); } else if (element.getName().equalsIgnoreCase("multiPointLine")) { String close = element.getAttributeValue("close"); stroke = element.getAttributeValue("stroke"); lineWidth = element.getAttributeValue("lineWidth"); color = element.getAttributeValue("color"); List<String> points = new ArrayList<String>(); List<Element> pointList = element.getChildren("point"); for (Element ePoint : pointList) { points.add(ePoint.getTextTrim()); } StdRouteMultipointLine line = new StdRouteMultipointLine(route, mapViewAdapter, previous, points, close, stroke, lineWidth, color); previous = line; route.addElement(line); } else if (element.getName().equalsIgnoreCase("text")) { String position = element.getAttributeValue("position"); String angle = element.getAttributeValue("angle"); String alignHeading = element.getAttributeValue("alignHeading"); String font = element.getAttributeValue("font"); String fontSize = element.getAttributeValue("fontSize"); color = element.getAttributeValue("color"); boolean clickable = "true".equals(element.getAttributeValue("clickable")); String sText = element.getAttributeValue("text"); StdRouteText text = new StdRouteText(route, mapViewAdapter, previous, position, angle, alignHeading, font, fontSize, color, clickable, sText); previous = text; route.addElement(text); } else if (element.getName().equalsIgnoreCase("screenText")) { String position = element.getAttributeValue("screenPos"); String angle = element.getAttributeValue("angle"); String font = element.getAttributeValue("font"); String fontSize = element.getAttributeValue("fontSize"); color = element.getAttributeValue("color"); String sText = element.getAttributeValue("text"); StdRouteScreenText text = new StdRouteScreenText(route, mapViewAdapter, previous, position, angle, font, fontSize, color, sText); previous = text; route.addElement(text); } else if (element.getName().equalsIgnoreCase("minAlt")) { String position = element.getAttributeValue("position"); String value = element.getAttributeValue("value"); String font = element.getAttributeValue("font"); String fontSize = element.getAttributeValue("fontSize"); color = element.getAttributeValue("color"); StdRouteMinAltitude minAlt = new StdRouteMinAltitude(route, mapViewAdapter, previous, position, value, font, fontSize, color); previous = minAlt; route.addElement(minAlt); } } catch (Exception e) { log.error("Problem to parse file " + file.getAbsolutePath() + ", Route: " + route.getName() + ", Error:" + e.getMessage(), e); break; } } stdRoutes.add(route); } } } catch (Exception e) { log.error("Problem to parse file " + file.getAbsolutePath() + ", Error:" + e.getMessage()); } finally { if (xmlInputStream != null) { try { xmlInputStream.close(); } catch (IOException e) { } } } } data.getNavaidDB().setStdRoutes(stdRoutes); }
From source file:de.nava.informa.parsers.Atom_0_3_Parser.java
License:Open Source License
/** * @see de.nava.informa.core.ChannelParserIF#parse(de.nava.informa.core.ChannelBuilderIF, org.jdom2.Element) */// www. j a v a 2 s.com public ChannelIF parse(ChannelBuilderIF cBuilder, Element channel) throws ParseException { if (cBuilder == null) { throw new RuntimeException("Without builder no channel can " + "be created."); } Date dateParsed = new Date(); Namespace defNS = ParserUtils.getDefaultNS(channel); if (defNS == null) { defNS = Namespace.NO_NAMESPACE; LOGGER.info("No default namespace found."); } // RSS 1.0 Dublin Core Module namespace Namespace dcNS = ParserUtils.getNamespace(channel, "dc"); if (dcNS == null) { LOGGER.debug("No namespace for dublin core found"); dcNS = defNS; } LOGGER.debug("start parsing."); // get version attribute String formatVersion = "0.3"; if (channel.getAttribute("version") != null) { formatVersion = channel.getAttribute("version").getValue().trim(); LOGGER.debug("Atom version " + formatVersion + " specified in document."); } else { LOGGER.info("No format version specified, using default."); } // --- read in channel information // Lower the case of these tags to simulate case-insensitive parsing ParserUtils.matchCaseOfChildren(channel, new String[] { "title", "description", "tagline", "ttl", "modified", "author", "generator", "copyright", "link", "entry" }); // title element ChannelIF chnl = cBuilder.createChannel(channel, channel.getChildTextTrim("title", defNS)); // TODO: support attributes: type, mode chnl.setFormat(ChannelFormat.ATOM_0_3); // language String language = channel.getAttributeValue("lang", Namespace.XML_NAMESPACE); if (language != null) { chnl.setLanguage(language); } // description element if (channel.getChild("description") != null) { chnl.setDescription(channel.getChildTextTrim("description", defNS)); } else { // fallback chnl.setDescription(channel.getChildTextTrim("tagline", defNS)); } // ttl in dc namespace Element ttl = channel.getChild("ttl", dcNS); if (ttl != null) { String ttlString = ttl.getTextTrim(); if (ttlString != null) { chnl.setTtl(Integer.parseInt(ttlString)); } } // lastbuild element : modified ? Element modified = channel.getChild("modified", defNS); if (modified != null) { chnl.setPubDate(ParserUtils.getDate(modified.getTextTrim())); } // TODO : issued value /* if (modified != null) { modified = channel.getChild("issued", defNS); chnl.setLastBuildDate (ParserUtils.getDate(modified.getTextTrim())); } */ // author element Element author = channel.getChild("author", defNS); if (author != null) { ParserUtils.matchCaseOfChildren(author, "name"); chnl.setCreator(author.getChildTextTrim("name", defNS)); } // generator element Element generator = channel.getChild("generator", defNS); if (generator != null) { chnl.setGenerator(generator.getTextTrim()); } // copyright element Element copyright = channel.getChild("copyright", defNS); if (copyright != null) { chnl.setCopyright(getCopyright(copyright)); } // n link elements // TODO : type attribut of link (text, application...) List links = channel.getChildren("link", defNS); Iterator i = links.iterator(); while (i.hasNext()) { Element linkElement = (Element) i.next(); // use first 'alternate' link String rel = linkElement.getAttributeValue("rel"); String href = linkElement.getAttributeValue("href"); if ((rel != null) && (href != null) && rel.equals("alternate")) { URL linkURL = ParserUtils.getURL(href); chnl.setSite(linkURL); break; } // TODO: further extraction of link information } // 1..n entry elements List items = channel.getChildren("entry", defNS); i = items.iterator(); while (i.hasNext()) { Element item = (Element) i.next(); // Lower the case of these tags to simulate case-insensitive parsing ParserUtils.matchCaseOfChildren(item, new String[] { "title", "link", "content", "summary", "issued", "subject" }); // get title element // TODO : deal with type attribut Element elTitle = item.getChild("title", defNS); String strTitle = "<No Title>"; if (elTitle != null) { strTitle = getTitle(elTitle); LOGGER.debug("Parsing title " + elTitle.getTextTrim() + "->" + strTitle); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Entry element found (" + strTitle + ")."); } // get link element String strLink = AtomParserUtils.getItemLink(item, defNS); // get description element String strDesc = getDescription(item, defNS); // generate new news item (link to article) ItemIF curItem = cBuilder.createItem(item, chnl, strTitle, strDesc, ParserUtils.getURL(strLink)); curItem.setFound(dateParsed); // get issued element (required) Element elIssued = item.getChild("issued", defNS); if (elIssued == null) { // [adewale@gmail.com, 01-May-2005] Fix for blogs which have // 'created' dates, but not 'issued' dates -- in clear contravention // of the Atom 0.3 spec. Element elCreated = item.getChild("created", defNS); if (elCreated != null) { curItem.setDate(ParserUtils.getDate(elCreated.getTextTrim())); } } else { curItem.setDate(ParserUtils.getDate(elIssued.getTextTrim())); } // get subject element Element elSubject = item.getChild("subject", dcNS); if (elSubject != null) { // TODO: Mulitple subject elements not handled currently curItem.setSubject(elSubject.getTextTrim()); } } // set to current date chnl.setLastUpdated(dateParsed); return chnl; }
From source file:de.nava.informa.parsers.Atom_1_0_Parser.java
License:Open Source License
/** * @see de.nava.informa.core.ChannelParserIF#parse(de.nava.informa.core.ChannelBuilderIF, org.jdom2.Element) *//* ww w .ja v a 2 s . co m*/ public ChannelIF parse(ChannelBuilderIF cBuilder, Element channel) throws ParseException { if (cBuilder == null) { throw new RuntimeException("Without builder no channel can " + "be created."); } Date dateParsed = new Date(); Namespace defNS = ParserUtils.getDefaultNS(channel); if (defNS == null) { defNS = Namespace.NO_NAMESPACE; LOGGER.info("No default namespace found."); } else if ((defNS.getURI() == null) || !defNS.getURI().equals("http://www.w3.org/2005/Atom")) { LOGGER.warn("Namespace is not really supported, still trying assuming Atom 1.0 format"); } LOGGER.debug("start parsing."); // --- read in channel information // Lower the case of these tags to simulate case-insensitive parsing ParserUtils.matchCaseOfChildren(channel, new String[] { "title", "subtitle", "updated", "published", "author", "generator", "rights", "link", "entry" }); // TODO icon and logo: Feed element can have upto 1 logo and icon. // TODO id: Feed and all entries have a unique id string. This can // be the URL of the website. Supporting this will require API change. // TODO: Feed can optionally have category information // title element ChannelIF chnl = cBuilder.createChannel(channel, channel.getChildTextTrim("title", defNS)); chnl.setFormat(ChannelFormat.ATOM_1_0); // description element if (channel.getChild("subtitle") != null) { chnl.setDescription(channel.getChildTextTrim("subtitle", defNS)); } // TODO: should we use summary element? // lastbuild element : updated ? Element updated = channel.getChild("updated", defNS); if (updated != null) { chnl.setPubDate(ParserUtils.getDate(updated.getTextTrim())); } // author element List authors = channel.getChildren("author", defNS); chnl.setCreator(getAuthorString(authors, defNS)); // TODO we are ignoring contributors information // generator element Element generator = channel.getChild("generator", defNS); if (generator != null) { chnl.setGenerator(generator.getTextTrim()); } // TODO generator can have URI and version information // copyright element Element rights = channel.getChild("rights", defNS); if (rights != null) { chnl.setCopyright(AtomParserUtils.getValue(rights, getMode(rights))); } List links = channel.getChildren("link", defNS); Iterator i = links.iterator(); URL linkUrl = null; while (i.hasNext()) { Element linkElement = (Element) i.next(); // use first 'alternate' link // if rel is not present, use first link without rel String rel = linkElement.getAttributeValue("rel"); String href = linkElement.getAttributeValue("href"); // TODO we need to handle relative links also if ((rel == null) && (href != null) && (linkUrl == null)) { linkUrl = ParserUtils.getURL(href); } else if ((rel != null) && (href != null) && rel.equals("alternate")) { linkUrl = ParserUtils.getURL(href); break; } } if (linkUrl != null) { chnl.setSite(linkUrl); } List items = channel.getChildren("entry", defNS); i = items.iterator(); while (i.hasNext()) { Element item = (Element) i.next(); // Lower the case of these tags to simulate case-insensitive parsing ParserUtils.matchCaseOfChildren(item, new String[] { "title", "link", "content", "summary", "published", "author" }); // TODO entry, if copied from some other feed, may have source element // TODO each entry can have its own rights declaration // get title element Element elTitle = item.getChild("title", defNS); String strTitle = "<No Title>"; if (elTitle != null) { strTitle = AtomParserUtils.getValue(elTitle, getMode(elTitle)); LOGGER.debug("Parsing title " + elTitle.getTextTrim() + "->" + strTitle); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Entry element found (" + strTitle + ")."); } // get link element String strLink = AtomParserUtils.getItemLink(item, defNS); // get description element String strDesc = getDescription(item, defNS); // generate new news item (link to article) ItemIF curItem = cBuilder.createItem(item, chnl, strTitle, strDesc, ParserUtils.getURL(strLink)); //TODO enclosure data curItem.setFound(dateParsed); List itemAuthors = item.getChildren("author", defNS); curItem.setCreator(getAuthorString(itemAuthors, defNS)); // get published element Element elIssued = item.getChild("published", defNS); if (elIssued == null) { // published element may not be present (but updated should be) Element elUpdated = item.getChild("updated", defNS); // TODO there should be some way to determining which one are we // returning if (elUpdated != null) { curItem.setDate(ParserUtils.getDate(elUpdated.getTextTrim())); } } else { curItem.setDate(ParserUtils.getDate(elIssued.getTextTrim())); } // get list of category elements List elCategoryList = item.getChildren("category", defNS); // categories present will be stored here Collection<CategoryIF> categories = new ArrayList<>(); // multiple category elements may be present for (Object elCategoryItem : elCategoryList) { Element elCategory = (Element) elCategoryItem; // notice: atom spec. forbids to have category "term" (="subject") // set as inner text of category tags, so we have to read it from // the "term" attribute if (elCategory != null) { // TODO: what if we have more than one category element present? // subject would be overwritten each loop and therefore represent only // the last category read, so does this make any sense? // TODO: what about adding functionality for accessing "label" or "scheme" attributes? // if set, a label should be displayed instead of the value set in term // we keep this line not to break up things which // use getSubject() to read an item category curItem.setSubject(elCategory.getAttributeValue("term")); CategoryIF c = new Category(elCategory.getAttributeValue("term")); // add current category to category list categories.add(c); } } // assign categories curItem.setCategories(categories); } // set to current date chnl.setLastUpdated(dateParsed); return chnl; }
From source file:de.nava.informa.parsers.RSS_0_91_Parser.java
License:Open Source License
/** * @see de.nava.informa.core.ChannelParserIF#parse(de.nava.informa.core.ChannelBuilderIF, org.jdom2.Element) *///www . ja v a2 s. com public ChannelIF parse(ChannelBuilderIF cBuilder, Element root) throws ParseException { if (cBuilder == null) { throw new RuntimeException("Without builder no channel can " + "be created."); } Date dateParsed = new Date(); logger.debug("start parsing."); // Get the channel element (only one occurs) ParserUtils.matchCaseOfChildren(root, "channel"); Element channel = root.getChild("channel"); if (channel == null) { logger.warn("Channel element could not be retrieved from feed."); throw new ParseException("No channel element found in feed."); } // --- read in channel information ParserUtils.matchCaseOfChildren(channel, new String[] { "title", "description", "link", "language", "item", "image", "textinput", "copyright", "rating", "pubDate", "lastBuildDate", "docs", "managingEditor", "webMaster", "cloud" }); // 1 title element ChannelIF chnl = cBuilder.createChannel(channel, channel.getChildTextTrim("title")); chnl.setFormat(ChannelFormat.RSS_0_91); // 1 description element chnl.setDescription(channel.getChildTextTrim("description")); // 1 link element chnl.setSite(ParserUtils.getURL(channel.getChildTextTrim("link"))); // 1 language element chnl.setLanguage(channel.getChildTextTrim("language")); // 1..n item elements List items = channel.getChildren("item"); Iterator i = items.iterator(); while (i.hasNext()) { Element item = (Element) i.next(); ParserUtils.matchCaseOfChildren(item, new String[] { "title", "link", "description", "source", "enclosure" }); // get title element Element elTitle = item.getChild("title"); String strTitle = "<No Title>"; if (elTitle != null) { strTitle = elTitle.getTextTrim(); } if (logger.isDebugEnabled()) { logger.debug("Item element found (" + strTitle + ")."); } // get link element Element elLink = item.getChild("link"); String strLink = ""; if (elLink != null) { strLink = elLink.getTextTrim(); } // get description element Element elDesc = item.getChild("description"); String strDesc = ""; if (elDesc != null) { strDesc = elDesc.getTextTrim(); } // generate new RSS item (link to article) ItemIF rssItem = cBuilder.createItem(item, chnl, strTitle, strDesc, ParserUtils.getURL(strLink)); rssItem.setFound(dateParsed); // get source element (an RSS 0.92 element) Element source = item.getChild("source"); if (source != null) { String sourceName = source.getTextTrim(); Attribute sourceAttribute = source.getAttribute("url"); if (sourceAttribute != null) { String location = sourceAttribute.getValue().trim(); ItemSourceIF itemSource = cBuilder.createItemSource(rssItem, sourceName, location, null); rssItem.setSource(itemSource); } } // get enclosure element (an RSS 0.92 element) Element enclosure = item.getChild("enclosure"); if (enclosure != null) { URL location = null; String type = null; int length = -1; Attribute urlAttribute = enclosure.getAttribute("url"); if (urlAttribute != null) { location = ParserUtils.getURL(urlAttribute.getValue().trim()); } Attribute typeAttribute = enclosure.getAttribute("type"); if (typeAttribute != null) { type = typeAttribute.getValue().trim(); } Attribute lengthAttribute = enclosure.getAttribute("length"); if (lengthAttribute != null) { try { length = Integer.parseInt(lengthAttribute.getValue().trim()); } catch (NumberFormatException e) { logger.warn(e); } } ItemEnclosureIF itemEnclosure = cBuilder.createItemEnclosure(rssItem, location, type, length); rssItem.setEnclosure(itemEnclosure); } } // 0..1 image element Element image = channel.getChild("image"); if (image != null) { ParserUtils.matchCaseOfChildren(image, new String[] { "title", "url", "link", "width", "height", "description" }); ImageIF rssImage = cBuilder.createImage(image.getChildTextTrim("title"), ParserUtils.getURL(image.getChildTextTrim("url")), ParserUtils.getURL(image.getChildTextTrim("link"))); Element imgWidth = image.getChild("width"); if (imgWidth != null) { try { rssImage.setWidth(Integer.parseInt(imgWidth.getTextTrim())); } catch (NumberFormatException e) { logger.warn(e); } } Element imgHeight = image.getChild("height"); if (imgHeight != null) { try { rssImage.setHeight(Integer.parseInt(imgHeight.getTextTrim())); } catch (NumberFormatException e) { logger.warn(e); } } Element imgDescr = image.getChild("description"); if (imgDescr != null) { rssImage.setDescription(imgDescr.getTextTrim()); } chnl.setImage(rssImage); } // 0..1 textinput element Element txtinp = channel.getChild("textinput"); if (txtinp != null) { ParserUtils.matchCaseOfChildren(txtinp, new String[] { "title", "description", "name", "link" }); TextInputIF rssTextInput = cBuilder.createTextInput(txtinp.getChild("title").getTextTrim(), txtinp.getChild("description").getTextTrim(), txtinp.getChild("name").getTextTrim(), ParserUtils.getURL(txtinp.getChild("link").getTextTrim())); chnl.setTextInput(rssTextInput); } // 0..1 copyright element Element copyright = channel.getChild("copyright"); if (copyright != null) { chnl.setCopyright(copyright.getTextTrim()); } // 0..1 rating element Element rating = channel.getChild("rating"); if (rating != null) { chnl.setRating(rating.getTextTrim()); } // 0..1 pubDate element Element pubDate = channel.getChild("pubDate"); if (pubDate != null) { chnl.setPubDate(ParserUtils.getDate(pubDate.getTextTrim())); } // 0..1 lastBuildDate element Element lastBuildDate = channel.getChild("lastBuildDate"); if (lastBuildDate != null) { chnl.setLastBuildDate(ParserUtils.getDate(lastBuildDate.getTextTrim())); } // 0..1 docs element Element docs = channel.getChild("docs"); if (docs != null) { chnl.setDocs(docs.getTextTrim()); } // 0..1 managingEditor element Element managingEditor = channel.getChild("managingEditor"); if (managingEditor != null) { chnl.setCreator(managingEditor.getTextTrim()); } // 0..1 webMaster element Element webMaster = channel.getChild("webMaster"); if (webMaster != null) { chnl.setPublisher(webMaster.getTextTrim()); } // 0..1 cloud element Element cloud = channel.getChild("cloud"); if (cloud != null) { String _port = cloud.getAttributeValue("port"); int port = -1; if (_port != null) { try { port = Integer.parseInt(_port); } catch (NumberFormatException e) { logger.warn(e); } } chnl.setCloud( cBuilder.createCloud(cloud.getAttributeValue("domain"), port, cloud.getAttributeValue("path"), cloud.getAttributeValue("registerProcedure"), cloud.getAttributeValue("protocol"))); } chnl.setLastUpdated(dateParsed); // 0..1 skipHours element // 0..1 skipDays element return chnl; }