/** * Return the mesaurements * * @return */ public void readMesasurementFilesFromSafe() { XPathExpression<Element> expr = xFactory .compile( "/xfdu:XFDU/dataObjectSection/dataObject[@repID='s1Level1ProductSchema']/byteStream/fileLocation", Filters.element(), null, xfdu); List<Element> values = expr.evaluate(safe); this.measurements=new File[values.size()]; File safefile = new File(safePath); String measurementPath = safefile.getParent() + "/measurement"; for (int i = 0; i < values.size(); i++) { Element e = values.get(i); String href = e.getAttributeValue("href"); if (href.startsWith("./")) href = href.substring(2); measurements[i] = new File(measurementPath + "/" + href); System.out.println(measurements[i]); } }
/** * Read the generalProductInformation: * productType,instrumentConfigurationID, * missionDataTakeID,transmitterReceiverPolarisation * ,productTimelinessCategory,sliceProductFlag * * @throws JAXBException * @throws SAXException */ public void readGeneralProductInformation() throws JAXBException, SAXException { String xPathGenProdInfo = "/xfdu:XFDU/metadataSection/metadataObject/metadataWrap/xmlData/*[name()='generalProductInformation']"; String xPathStandAloneInfo = "/xfdu:XFDU/metadataSection/metadataObject/metadataWrap/xmlData/s1sarl1:standAloneProductInformation']"; XPathExpression<Element> expr = xFactory.compile(xPathGenProdInfo, Filters.element(), null, xfdu); List<Element> value = expr.evaluate(safe); if (value == null || value.isEmpty()) { expr = xFactory.compile(xPathStandAloneInfo, Filters.element(), null, s1sarl1, xfdu); value = expr.evaluate(safe); } List<Element> informationsNode = value.get(0).getChildren(); productInformation = new ProductInformation(); for (Element e : informationsNode) { String name = e.getName(); String val = e.getValue(); productInformation.putValueInfo(name, val); } }
/** * Read the acquisition period * * @throws JAXBException * @throws SAXException */ public void readAcquisitionPeriod() throws JAXBException, SAXException { acquPeriod = new AcquisitionPeriod(); String xPathStartTime = "/xfdu:XFDU/metadataSection/metadataObject/metadataWrap/xmlData//*[name()='safe:startTime']"; String xPathStopTime = "/xfdu:XFDU/metadataSection/metadataObject/metadataWrap/xmlData//*[name()='safe:stopTime']"; XPathExpression<Element> expr = xFactory.compile(xPathStartTime, Filters.element(), null, xfdu, safeNs); Element e = expr.evaluateFirst(safe); if (e != null) acquPeriod.setStartTime(e.getValue()); expr = xFactory.compile(xPathStopTime, Filters.element(), null, xfdu, safeNs); e = expr.evaluateFirst(safe); if (e != null) acquPeriod.setStopTime(e.getValue()); }
public void readMesasurementFilesFromSafe() { XPathExpression<Element> expr = xFactory .compile( "/xfdu:XFDU/dataObjectSection/dataObject[@repID='s1Level1ProductSchema']/byteStream/fileLocation", Filters.element(), null, xfdu); List<Element> values = expr.evaluate(safe); this.measurements=new File[values.size()]; String measurementPath = safefile.getParent() + "/measurement"; for (int i = 0; i < values.size(); i++) { Element e = values.get(i); String href = e.getAttributeValue("href"); if (href.startsWith("./")) href = href.substring(2); measurements[i] = new File(measurementPath + "/" + href); System.out.println(measurements[i]); } }
public void readGeneralProductInformation() throws JAXBException, SAXException { String xPathGenProdInfo = "/xfdu:XFDU/metadataSection/metadataObject/metadataWrap/xmlData/*[name()='generalProductInformation']"; String xPathStandAloneInfo = "/xfdu:XFDU/metadataSection/metadataObject/metadataWrap/xmlData/s1sarl1:standAloneProductInformation"; XPathExpression<Element> expr = xFactory.compile(xPathGenProdInfo, Filters.element(), null, xfdu); List<Element> value = expr.evaluate(safe); if (value == null || value.isEmpty()) { expr = xFactory.compile(xPathStandAloneInfo, Filters.element(), null, s1sarl1, xfdu); value = expr.evaluate(safe); } List<Element> informationsNode = value.get(0).getChildren(); productInformation = new ProductInformation(); for (Element e : informationsNode) { String name = e.getName(); String val = e.getValue(); productInformation.putValueInfo(name, val); } }
public void readAcquisitionPeriod() throws JAXBException, SAXException { acquPeriod = new AcquisitionPeriod(); String xPathStartTime = "/xfdu:XFDU/metadataSection/metadataObject/metadataWrap/xmlData//*[name()='safe:startTime']"; String xPathStopTime = "/xfdu:XFDU/metadataSection/metadataObject/metadataWrap/xmlData//*[name()='safe:stopTime']"; XPathExpression<Element> expr = xFactory.compile(xPathStartTime, Filters.element(), null, xfdu, safeNs); Element e = expr.evaluateFirst(safe); if (e != null) acquPeriod.setStartTime(e.getValue()); expr = xFactory.compile(xPathStopTime, Filters.element(), null, xfdu, safeNs); e = expr.evaluateFirst(safe); if (e != null) acquPeriod.setStopTime(e.getValue()); }
public Set<Element> getPublicationsForAuthor(PublicationAuthor author) throws IOException, JDOMException, SAXException { if (!author.getScopusAuthorID().isEmpty()) { Set<Element> publications = new HashSet<>(); String queryURL = API_URL + "/author/AUTHOR_ID:" + author.getScopusAuthorID() + "?start=0&count=200&view=DOCUMENTS&apikey=" + API_KEY; XPathExpression<Element> xPath = XPathFactory.instance().compile(pathToDocumentIdentifier, Filters.element()); List<Element> identifierElements = xPath .evaluate(getResponse(queryURL).asXML().detachRootElement().clone()); for (Element idElement : identifierElements) { publications.add(getPublicationByID(idElement.getValue())); } return publications; } else return null; }
public Element getCitationInformation(String scopusID) throws IOException, JDOMException, SAXException { // build API URL String queryURL = API_URL + "/abstract/citation-count?scopus_id=" + scopusID + "&apikey=" + API_KEY + "&httpAccept=application%2Fxml"; XPathExpression<Element> xPathCount = XPathFactory.instance().compile(pathToCitationCount, Filters.element()); XPathExpression<Element> xPathLink = XPathFactory.instance().compile(pathToCitationLink, Filters.element()); XPathExpression<Element> xPathArticleLink = XPathFactory.instance().compile(pathToArticleLink, Filters.element()); Element response = getResponse(queryURL).asXML().detachRootElement().clone(); String citationCount = xPathCount.evaluateFirst(response).getValue(); String citationLink = xPathLink.evaluateFirst(response).getAttributeValue("href"); String articleLink = xPathArticleLink.evaluateFirst(response).getAttributeValue("href"); Element citationInformation = new Element("citationInformation"); citationInformation.addContent(new Element("count").setText(citationCount)); citationInformation.addContent(new Element("citationLink").setText(citationLink)); citationInformation.addContent(new Element("articleLink").setText(articleLink)); return citationInformation; }
private void bind(String xPath, boolean buildIfNotExists, String initialValue) throws JaxenException { this.xPath = xPath; Map<String, Object> variables = buildXPathVariables(); XPathExpression<Object> xPathExpr = XPathFactory.instance().compile(xPath, Filters.fpassthrough(), variables, MCRConstants.getStandardNamespaces()); boundNodes.addAll(xPathExpr.evaluate(parent.getBoundNodes())); for (Object boundNode : boundNodes) if (!(boundNode instanceof Element || boundNode instanceof Attribute || boundNode instanceof Document)) throw new RuntimeException( "XPath MUST only bind either element, attribute or document nodes: " + xPath); LOGGER.debug("Bind to {} selected {} node(s)", xPath, boundNodes.size()); if (boundNodes.isEmpty() && buildIfNotExists) { MCRNodeBuilder builder = new MCRNodeBuilder(variables); Object built = builder.buildNode(xPath, initialValue, (Parent) (parent.getBoundNode())); LOGGER.debug("Bind to {} generated node {}", xPath, MCRXPathBuilder.buildXPath(built)); boundNodes.add(built); trackNodeCreated(builder.getFirstNodeBuilt()); } }
@Test public void testUpdate() throws IOException, URISyntaxException, MCRPersistenceException, MCRActiveLinkException, JDOMException, SAXException, MCRAccessException { MCRObject seriesNew = new MCRObject(getResourceAsURL(seriesID + "-updated.xml").toURI()); MCRMetadataManager.update(seriesNew); Document bookNew = MCRXMLMetadataManager.instance().retrieveXML(bookID); XPathBuilder<Element> builder = new XPathBuilder<>( "/mycoreobject/metadata/def.modsContainer/modsContainer/mods:mods/mods:relatedItem/mods:titleInfo/mods:title", Filters.element()); builder.setNamespace(MCRConstants.MODS_NAMESPACE); XPathExpression<Element> seriesTitlePath = builder.compileWith(XPathFactory.instance()); Element titleElement = seriesTitlePath.evaluateFirst(bookNew); Assert.assertNotNull( "No title element in related item: " + new XMLOutputter(Format.getPrettyFormat()).outputString(bookNew), titleElement); Assert.assertEquals("Title update from series was not promoted to book of series.", "Updated series title", titleElement.getText()); }
@Override public List<MCRIIIFMetadata> extractModsMetadata(Element xmlData) { Map<String, String> elementLabelMap = new HashMap<>(); elementLabelMap.put("title", "mods:mods/mods:titleInfo/mods:title/text()"); elementLabelMap.put("genre", "mods:mods/mods:genre/text()"); // TODO: add some more metadata return elementLabelMap.entrySet().stream().map(entry -> { XPathExpression<Text> pathExpression = XPathFactory.instance().compile(entry.getValue(), Filters.text(), null, MCRConstants.MODS_NAMESPACE); List<Text> texts = pathExpression.evaluate(xmlData); if (texts.size() == 0) { return null; } return new MCRIIIFMetadata(entry.getKey(), texts.stream().map(Text::getText).collect(Collectors.joining(", "))); }).filter(Objects::nonNull) .collect(Collectors.toList()); }
/** * Searches a file in a group, which matches a filename. * * @param mets the mets file to search * @param path the path to the alto file (e.g. "alto/alto_file.xml" when searching in DEFAULT_FILE_GROUP_USE or * "image_file.jpg" when searchin in ALTO_FILE_GROUP_USE) * @param searchFileGroup * @return the id of the matching file or null if there is no matching file */ private static String searchFileInGroup(Document mets, String path, String searchFileGroup) { XPathExpression<Element> xpath;// first check all files in default file group String relatedFileExistPathString = String.format(Locale.ROOT, "mets:mets/mets:fileSec/mets:fileGrp[@USE='%s']/mets:file/mets:FLocat", searchFileGroup); xpath = XPathFactory.instance().compile(relatedFileExistPathString, Filters.element(), null, MCRConstants.METS_NAMESPACE, MCRConstants.XLINK_NAMESPACE); List<Element> fileLocList = xpath.evaluate(mets); String matchId = null; // iterate over all files path = getCleanPath(path); for (Element fileLoc : fileLocList) { Attribute hrefAttribute = fileLoc.getAttribute("href", MCRConstants.XLINK_NAMESPACE); String hrefAttributeValue = hrefAttribute.getValue(); String hrefPath = getCleanPath(removeExtension(hrefAttributeValue)); if (hrefPath.equals(removeExtension(path))) { matchId = ((Element) fileLoc.getParent()).getAttributeValue("ID"); break; } } return matchId; }
@Override public Optional<MCRPersistentIdentifier> getIdentifier(MCRBase obj, String additional) throws MCRPersistentIdentifierException { String xpath = getProperties().get("Xpath"); Document xml = obj.createXML(); XPathFactory xpfac = XPathFactory.instance(); XPathExpression<Text> xp = xpfac.compile(xpath, Filters.text()); List<Text> evaluate = xp.evaluate(xml); if (evaluate.size() > 1) { throw new MCRPersistentIdentifierException( "Got " + evaluate.size() + " matches for " + obj.getId() + " with xpath " + xpath + ""); } if (evaluate.size() == 0) { return Optional.empty(); } Text identifierText = evaluate.listIterator().next(); String identifierString = identifierText.getTextNormalize(); Optional<MCRDNBURN> parsedIdentifierOptional = PARSER.parse(identifierString); return parsedIdentifierOptional.map(MCRPersistentIdentifier.class::cast); }
/** * Output xml * @param eRoot - the root element * @param lang - the language which should be filtered or null for no filter * @return a string representation of the XML * @throws IOException */ private static String writeXML(Element eRoot, String lang) throws IOException { StringWriter sw = new StringWriter(); if (lang != null) { // <label xml:lang="en" text="part" /> XPathExpression<Element> xpE = XPathFactory.instance().compile("//label[@xml:lang!='" + lang + "']", Filters.element(), null, Namespace.XML_NAMESPACE); for (Element e : xpE.evaluate(eRoot)) { e.getParentElement().removeContent(e); } } XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat()); Document docOut = new Document(eRoot.detach()); xout.output(docOut, sw); return sw.toString(); }
private static void createXMLForSubdirectories(MCRPath mcrPath, Element currentElement, int currentDepth, int maxDepth) { if (currentDepth < maxDepth) { XPathExpression<Element> xp = XPathFactory.instance().compile("./children/child[@type='directory']", Filters.element()); for (Element e : xp.evaluate(currentElement)) { String name = e.getChildTextNormalize("name"); try { MCRPath pChild = (MCRPath) mcrPath.resolve(name); Document doc = MCRPathXML.getDirectoryXML(pChild); Element eChildren = doc.getRootElement().getChild("children"); if (eChildren != null) { e.addContent(eChildren.detach()); createXMLForSubdirectories(pChild, e, currentDepth + 1, maxDepth); } } catch (IOException ex) { //ignore } } } }
protected InputStream transform(String xmlFile) throws Exception { InputStream guiXML = getClass().getResourceAsStream(xmlFile); if (guiXML == null) { throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).build()); } SAXBuilder saxBuilder = new SAXBuilder(); Document webPage = saxBuilder.build(guiXML); XPathExpression<Object> xpath = XPathFactory.instance().compile( "/MyCoReWebPage/section/div[@id='mycore-acl-editor2']"); Object node = xpath.evaluateFirst(webPage); MCRSession mcrSession = MCRSessionMgr.getCurrentSession(); String lang = mcrSession.getCurrentLanguage(); if (node != null) { Element mainDiv = (Element) node; mainDiv.setAttribute("lang", lang); String bsPath = CONFIG.getString("MCR.bootstrap.path", ""); if (!bsPath.equals("")) { bsPath = MCRFrontendUtil.getBaseURL() + bsPath; Element item = new Element("link").setAttribute("href", bsPath).setAttribute("rel", "stylesheet") .setAttribute("type", "text/css"); mainDiv.addContent(0, item); } } MCRContent content = MCRJerseyUtil.transform(webPage, request); return content.getInputStream(); }
/** * Returns all labels of the ancestor axis for the given item within * navigation.xml * * @param item a navigation item * @return Label as String, like "labelRoot > labelChild > * labelChildOfChild" */ public static String getAncestorLabels(Element item) { StringBuilder label = new StringBuilder(); String lang = MCRSessionMgr.getCurrentSession().getCurrentLanguage().trim(); XPathExpression<Element> xpath; Element ic = null; xpath = XPATH_FACTORY.compile("//.[@href='" + getWebpageID(item) + "']", Filters.element()); ic = xpath.evaluateFirst(getNavi()); while (ic.getName().equals("item")) { ic = ic.getParentElement(); String webpageID = getWebpageID(ic); Element labelEl = null; xpath = XPATH_FACTORY.compile("//.[@href='" + webpageID + "']/label[@xml:lang='" + lang + "']", Filters.element()); labelEl = xpath.evaluateFirst(getNavi()); if (labelEl != null) { if (label.length() == 0) { label = new StringBuilder(labelEl.getTextTrim()); } else { label.insert(0, labelEl.getTextTrim() + " > "); } } } return label.toString(); }
private static String getName(Document doc) { XPathFactory xFactory = XPathFactory.instance(); XPathExpression<Attribute> exprAttribute = xFactory.compile( "/osm/relation" + "[member]" + "[tag/@k='name' and tag/@v]" + "[tag/@k='type' and tag/@v='site']" + "[tag/@k='site' and tag/@v='parking']" + "/tag[@k='name']/@v", Filters.attribute()); Attribute nameAttribute = exprAttribute.evaluateFirst(doc); if (nameAttribute == null) { exprAttribute = xFactory .compile( "/osm/way[tag/@k='amenity' and tag/@v='parking']/tag[@k='name']/@v", Filters.attribute()); nameAttribute = exprAttribute.evaluateFirst(doc); } String name = nameAttribute.getValue(); return name; }
private static Map<Integer, GeoNode> getNodes(Document doc) { Map<Integer, GeoNode> nList = new HashMap<Integer, GeoNode>(); XPathFactory xFactory = XPathFactory.instance(); XPathExpression<Element> exprElement = xFactory.compile( "/osm/node[@id and @lat and @lon]", Filters.element()); List<Element> nodesEle = exprElement.evaluate(doc); for (Element nele : nodesEle) { GeoNode n = null; try { int id = nele.getAttribute("id").getIntValue(); double lat = nele.getAttribute("lat").getDoubleValue(); double lon = nele.getAttribute("lon").getDoubleValue(); n = new GeoNode(id, lat, lon, 0); } catch (DataConversionException e) { e.printStackTrace(); } nList.put(n.id, n); } return nList; }
public static Map<Integer, GeoNode> getNodes(Document doc) { Map<Integer, GeoNode> nList = new HashMap<Integer, GeoNode>(); XPathFactory xFactory = XPathFactory.instance(); XPathExpression<Element> exprElement = xFactory.compile( "/osm/node[@id and @lat and @lon]", Filters.element()); List<Element> nodesEle = exprElement.evaluate(doc); for (Element nele : nodesEle) { GeoNode n = null; try { int id = nele.getAttribute("id").getIntValue(); double lat = nele.getAttribute("lat").getDoubleValue(); double lon = nele.getAttribute("lon").getDoubleValue(); n = new GeoNode(id, lat, lon, 0); } catch (DataConversionException e) { e.printStackTrace(); } nList.put(n.id, n); } return nList; }
public Reader(File file, String name) { SAXBuilder builder = new SAXBuilder(); try { doc = (Document) builder.build(file); Element root = doc.getRootElement(); // use the default implementation XPathFactory xFactory = XPathFactory.instance(); // System.out.println(xFactory.getClass()); // select all data for motion sensor XPathExpression<Element> expr = xFactory.compile( "/SensorData/data[@" + Data.NAMETAG + "='" + name + "']", Filters.element()); it = expr.evaluate(doc).iterator(); } catch (IOException io) { System.out.println(io.getMessage()); } catch (JDOMException jdomex) { System.out.println(jdomex.getMessage()); } }
public Reader(File file) { SAXBuilder builder = new SAXBuilder(); try { doc = (Document) builder.build(file); Element root = doc.getRootElement(); // use the default implementation XPathFactory xFactory = XPathFactory.instance(); // System.out.println(xFactory.getClass()); // select all data for motion sensor XPathExpression<Element> expr = xFactory.compile( "/SensorData/data", Filters.element()); it = expr.evaluate(doc).iterator(); } catch (IOException io) { System.out.println(io.getMessage()); } catch (JDOMException jdomex) { System.out.println(jdomex.getMessage()); } }
/** * List all datapoints of a functionality * * @param p_functionality * The name of the group to perform the action on. For example in * the url kitchen_light.ground.home.com the functionality would * be kitchen_light * @param p_location * The middle dns name of the url without the top level domain. * For example in the url kitchen_light.ground.home.com the * location would be ground.home * @return An ArrayList of datapoints descriptions */ public ArrayList<DatapointDescription> listDatapoints( String p_functionality, String p_location) { ArrayList<DatapointDescription> l_dps = new ArrayList<DatapointDescription>(); // Create the xpath expression to find the datapoint XPathExpression<Element> l_xpath = XPathFactory .instance() .compile( "/datapoints/datapoint[lower-case(@location)='" + p_location.toLowerCase() + "' and translate(lower-case(@name), 'àäâéèêëüûùôöò', 'aaaeeeeuuuooo')='" + p_functionality.toLowerCase() + "']", Filters.element()); List<Element> l_elements = l_xpath.evaluate(m_Document); for (int i = 0; i < l_elements.size(); i++) { l_dps.add(new DatapointDescription(l_elements.get(i) .getAttributeValue("actionName"), l_elements.get(i) .getAttributeValue("dptID"), l_elements.get(i) .getAttributeValue("actionDesc"), l_elements.get(i) .getAttributeValue("dptDesc"), Integer.parseInt(l_elements .get(i).getAttributeValue("dptBitsSize")))); } return l_dps; }
@Override public List<String> extractList(String data) { List<String> stringList = new LinkedList<>(); try { Document doc = createDom(data); XPathExpression xp = createXpathExpression(); List<Object> texts = xp.evaluate(doc); for (Object text : texts) { String result = wrap(text); stringList.add(result); } } catch (Exception e) { throw new ExtractException(e); } return stringList; }
private LinkedList<String> doFilter(String in, Set<String> xpaths) throws IOException { LinkedList<String> result = new LinkedList<String>(); try { Document doc = new SAXBuilder(XMLReaders.NONVALIDATING).build(new StringReader(in)); XMLOutputter out = new XMLOutputter(); for (String xp : xpaths) { XPathExpression<Content> xpath = XPathFactory.instance().compile(xp, Filters.content()); for (Content node : xpath.evaluate(doc)) { if(node instanceof Element) { result.add(out.outputString((Element) node)); } else if(node instanceof Text) { result.add(out.outputString((Text) node)); } } } return result; } catch (JDOMException xpe) { throw new IllegalArgumentException("error while processing xpath expressions: '" + xpaths + "'", xpe); } }
@Override public Object convert(ConvertContext cxt, Rule rule) throws ConvertException { String xpath = (String ) rule.getParams()[0]; logger.debug("xpath: {}",xpath); XPathExpression<Object> objs = factory.compile(xpath); List<Object> objList = objs.diagnose(cxt.getSourceDocument(), false).getResult(); logger.debug(">> {}",objList); if(objList==null){ return null; } for(Object obj :objList){ if(obj instanceof Element){ return ((Element) obj).getValue(); }else if(obj instanceof Content){ return ((Content) obj).getValue(); } } return "null"; }
@Override public Object convert(ConvertContext cxt, Rule rule) throws ConvertException { String slotName = (String ) rule.getParams()[0]; String xpath = "/SubmitSoapRequest/objective/slot[slotName='"+slotName+"']/slotValue/text()"; logger.trace("xpath: {}",xpath); XPathExpression<Object> objs = factory.compile(xpath); List<Object> objList = objs.diagnose(cxt.getSourceDocument(), false).getResult(); logger.trace(">> {}",objList); if(objList==null){ return null; } for(Object obj :objList){ if(obj instanceof Element){ return ((Element) obj).getValue(); }else if(obj instanceof Content){ return ((Content) obj).getValue(); } } return "XXXX"; }
@Override public Object convert(ConvertContext cxt, Rule rule) throws ConvertException { String groupName = (String ) rule.getParams()[0]; String itemName = (String ) rule.getParams()[1]; String xpath = "/CdpeRequest/phyExamInfo/examGroup[groupName='"+groupName+"']/groupItem[itemName='"+itemName+"']/itemValue/text()"; logger.debug("xpath: {}",xpath); XPathExpression<Object> objs = factory.compile(xpath); List<Object> objList = objs.diagnose(cxt.getSourceDocument(), false).getResult(); logger.trace(">> {}",objList); if(objList==null){ return null; } for(Object obj :objList){ if(obj instanceof Element){ return ((Element) obj).getValue(); }else if(obj instanceof Content){ return ((Content) obj).getValue(); } } return "XXXX"; }
/** * Obtains the measure GUID. * * @param element XML element that represents the Quality Measure Identifier * @return The measure GUID in the Quality Measure Identifier */ private List<String> getMeasureGuid(final Element element) { String expressionStr = getXpath(MEASURE_ID); XPathExpression<Attribute> expression = XPathFactory.instance() .compile(expressionStr, Filters.attribute(), null, xpathNs); return expression.evaluate(element).stream() .map(Attribute::getValue) .collect(Collectors.toList()); }
/** * Executes an Xpath for an element and executes the consumer * * @param element Element the xpath is executed against * @param expressionStr Xpath * @param consumer Consumer to execute if the xpath matches * @param filter Filter to apply for the xpath * @param selectOne Whether to execute for the first match or multiple matches */ @SuppressWarnings({ "rawtypes", "unchecked" }) void setOnNode(Element element, String expressionStr, Consumer consumer, Filter<?> filter, boolean selectOne) { XPathExpression<?> expression = XPathFactory.instance().compile(expressionStr, filter, null, xpathNs); if (selectOne) { Optional.ofNullable(expression.evaluateFirst(element)).ifPresent(consumer); } else { List<?> elems = expression.evaluate(element); Optional.ofNullable(elems) .ifPresent(notNullElems -> notNullElems.forEach(consumer)); } }
private static void addDataFromScopusProfile(Element profile, String type, Institution institution) { XPathExpression<Element> xPathInstitution; XPathExpression<Element> xPathDepartment; XPathExpression<Element> xPathAdress; if (type.equals("short")) { xPathInstitution = XPathFactory.instance().compile(pathToInstitutionInAuthorProfile, Filters.element()); xPathDepartment = XPathFactory.instance().compile(pathToDepartmentInAuthorProfile, Filters.element()); xPathAdress = XPathFactory.instance().compile(pathToAddressInAuthorProfile, Filters.element()); } else { xPathInstitution = XPathFactory.instance().compile(pathToInstitutionInAffilResponse, Filters.element()); xPathDepartment = XPathFactory.instance().compile(pathToDepartmentnAffilResponse, Filters.element()); xPathAdress = XPathFactory.instance().compile(pathToAddressnAffilResponse, Filters.element()); } Element institutionElement = xPathInstitution.evaluateFirst(profile); Element departmentElement = xPathDepartment.evaluateFirst(profile); if (institutionElement != null) { institution.setDepartment(departmentElement.getText()); institution.setInstitution(institutionElement.getText()); } else if (departmentElement != null) { institution.setInstitution(departmentElement.getText()); } Element addressElement = xPathAdress.evaluateFirst(profile); institution.setCity(addressElement.getChildText("city")); institution.setCountry(addressElement.getChildText("country")); if (profile.getChild("geoCoordinates") != null) { XPathExpression<Element> xPathLatitude = XPathFactory.instance().compile(pathToLatitude, Filters.element()); XPathExpression<Element> xPathLongitude = XPathFactory.instance().compile(pathToLongitude, Filters.element()); String textLatitude = xPathLatitude.evaluateFirst(profile).getText(); String textLongitude = xPathLongitude.evaluateFirst(profile).getText(); institution.setLatitude(Double.parseDouble(textLatitude)); institution.setLongitude(Double.parseDouble(textLongitude)); } }
public CitationStatistics(Set<Element> mcrEntries, String source) throws HttpException, IOException, JDOMException, SAXException { this.source = source; totalCitations = 0; uncited = 0; numberOfDocumentsWithCitationData = 0; hIndex = 0; List<Namespace> namespaces = MCRConstants.getStandardNamespaces(); namespaces.add(DC_Namespace); namespaces.add(ELSEVIER_Namespace); List<Integer> citationCounts = new ArrayList<>(); for (Element mcrEntry : mcrEntries) { LOGGER.info(mcrEntry); XPathExpression<Element> xPath = XPathFactory.instance().compile(sourceIDScopus, Filters.element(),null, namespaces); Element idenitiferElement = xPath.evaluateFirst(mcrEntry); if (idenitiferElement != null) { String scopusID = xPath.evaluateFirst(mcrEntry).getValue(); if (scopusID.contains("SCOPUS_ID:")) scopusID = scopusID.replace("SCOPUS_ID:", ""); CitationGetter getter = new ScopusConnector(); Element citationInformation = getter.getCitationInformation(scopusID); if (citationInformation != null) { mcrEntry.addContent(citationInformation); int numberOfCitations = Integer.parseInt(citationInformation.getChildText("count")); citationCounts.add(numberOfCitations); LOGGER.info("retrieved citation count " + numberOfCitations + " for document with scopus id " + scopusID); if (numberOfCitations == 0) uncited++; totalCitations += numberOfCitations; numberOfDocumentsWithCitationData++; } } } hIndex = calculateHIndex(citationCounts); }
public Element getPublicationByID(String id) throws IOException, JDOMException, SAXException { //build API URL String queryURL = API_URL + "objects/" + id; String pathToMODS = "metadata/def.modsContainer/modsContainer/mods:mods"; XPathExpression<Element> xPath = XPathFactory.instance().compile(pathToMODS, Filters.element(),null,MCRConstants.getStandardNamespaces()); return xPath.evaluateFirst(getResponse(queryURL).asXML().detachRootElement().clone()); }
private List<String> getMCRIDs(PublicationAuthor author) throws IOException, JDOMException, SAXException { //prepare list of results blocks String query = prepareAuthorQuery(author); //divide API request in blocks a 100 documents int numberOfPublications = getNumberOfPublications(query); String pathToID = "result/doc/str[@name='id']"; // String pathToID = "lst[@name='responseHeader']"; XPathExpression<Element> xPath = XPathFactory.instance().compile(pathToID, Filters.element()); List<String> foundMCRIds = new ArrayList<>(); //at the moment only testing, two blocks a 10 documents for (int i = 0; i < ( (numberOfPublications / 100) + 1); i++) { int start = 100 * i; //build API URL String queryURL = query + "&rows=100&start=" + start; Element response = getResponse(queryURL).asXML().detachRootElement().clone(); List<Element> foundMCRIDElements = xPath.evaluate(response); LOGGER.info("found " + foundMCRIDElements.size() + " mods elements"); for (Element mcrIDElement : foundMCRIDElements) { String mcrID = mcrIDElement.getValue(); if (!mcrID.contains("-")) foundMCRIds.add(mcrID); } } LOGGER.info("retrieved " + foundMCRIds.size() + " entries from MyCoRe repository."); //return API-response return foundMCRIds; }
public Element getGeoData(String location) throws JDOMException, HttpException, IOException, SAXException { //build API URL String queryURL = API_URL + "?address="+ urlEncode(location.replace(" ", "+")) + "&key="+ API_KEY; LOGGER.info("Retrieving coordinates for " + location); Element response = getResponse(queryURL).asXML().detachRootElement().clone(); XPathExpression<Element> xPathGeoData = XPathFactory.instance().compile(pathToGeoData, Filters.element()); Element geodata = xPathGeoData.evaluateFirst(response); return geodata; }
public int getNumberOfPublications(PublicationAuthor author) throws JDOMException, IOException, SAXException { if (!author.getScopusAuthorID().isEmpty()) { String queryURL = API_URL + "/author/AUTHOR_ID:" + author.getScopusAuthorID() + "?start=0&count=200&view=DOCUMENTS&apikey=" + API_KEY; XPathExpression<Element> xPath = XPathFactory.instance().compile(pathToNumber, Filters.element()); Element numberElement = xPath.evaluateFirst(getResponse(queryURL).asXML().detachRootElement().clone()); // read and return total number of publications from the Element return Integer.parseInt(numberElement.getValue()); } else return 0; }
public int getCitationCount(String scopusID) throws IOException, JDOMException, SAXException { // build API URL String queryURL = API_URL + "/abstract/citation-count?scopus_id=" + scopusID + "&apikey=" + API_KEY + "&httpAccept=application%2Fxml"; XPathExpression<Element> xPath = XPathFactory.instance().compile(pathToCitationCount, Filters.element()); Element citationCountElement = xPath.evaluateFirst(getResponse(queryURL).asXML()); return Integer.parseInt(citationCountElement.getValue()); }
@SuppressWarnings({ "rawtypes", "unchecked" }) public static List<Object> evaluate(String expr, Object parent, Filter filter) throws FatalIndexerException { XPathBuilder<Object> builder = new XPathBuilder<>(expr.trim().replace("\n", ""), filter); // Add all namespaces for (String key : Configuration.getInstance().getNamespaces().keySet()) { Namespace value = Configuration.getInstance().getNamespaces().get(key); builder.setNamespace(key, value.getURI()); } XPathExpression<Object> xpath = builder.compileWith(XPathFactory.instance()); return xpath.evaluate(parent); }
private void createMapping(MCRObject obj) { MCRMetaElement mappings = obj.getMetadata().getMetadataElement("mappings"); if (mappings != null) { oldMappings = mappings.clone(); obj.getMetadata().removeMetadataElement("mappings"); } Element currentClassElement = null; try { Document doc = new Document(obj.getMetadata().createXML().detach()); XPathExpression<Element> classElementPath = XPathFactory.instance().compile("//*[@categid]", Filters.element()); List<Element> classList = classElementPath.evaluate(doc); if (classList.size() > 0) { mappings = new MCRMetaElement(); mappings.setTag("mappings"); mappings.setClass(MCRMetaClassification.class); mappings.setHeritable(false); mappings.setNotInherit(true); obj.getMetadata().setMetadataElement(mappings); } for (Element classElement : classList) { currentClassElement = classElement; MCRCategory categ = DAO.getCategory(new MCRCategoryID(classElement.getAttributeValue("classid"), classElement.getAttributeValue("categid")), 0); addMappings(mappings, categ); } } catch (Exception je) { if (currentClassElement == null) { LOGGER.error("Error while finding classification elements", je); } else { LOGGER.error("Error while finding classification elements for {}", new XMLOutputter().outputString(currentClassElement), je); } } finally { if (mappings == null || mappings.size() == 0) { obj.getMetadata().removeMetadataElement("mappings"); } } }
@Override public <T> XPathExpression<T> compile(String expression, Filter<T> filter, Map<String, Object> variables, Namespace... namespaces) { XPathExpression<T> jaxenCompiled = super.compile(expression, filter, variables, namespaces); if (functions.stream().anyMatch(function -> function.isCalledIn(expression))) { addExtensionFunctions(jaxenCompiled, namespaces); } return jaxenCompiled; }