Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leaks #102

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,21 @@ private Element createMetadataTEIHeader(NodeList stuffToTake, Document doc) {
private void parseOrgsAddress(Document doc, NodeList orgs) {
Node org = null;
GrobidService gs = new GrobidService();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
for (int i = orgs.getLength() - 1; i >= 0; i--) {
org = orgs.item(i);
if (org.getNodeType() == Node.ELEMENT_NODE) {
Element orgElt = (Element) orgs.item(i);
NodeList addressNodes = orgElt.getElementsByTagName("addrLine");

NodeList orgNameNodes = orgElt.getElementsByTagName("orgName");
String orgNameStr = "";
StringBuilder orgNameStr = new StringBuilder();
Node orgNameNode = null;
for (int y = orgNameNodes.getLength() - 1; y >= 0; y--) {
orgNameNode = orgNameNodes.item(y);
if (orgNameNode.getNodeType() == Node.ELEMENT_NODE) {
orgNameStr += !orgNameStr.isEmpty() ? " "+orgNameNode.getTextContent():orgNameNode.getTextContent();
orgNameStr.append((orgNameStr.length() > 0) ? " " + orgNameNode.getTextContent() : orgNameNode.getTextContent());
}
}

Expand All @@ -231,8 +233,7 @@ private void parseOrgsAddress(Document doc, NodeList orgs) {
if (addrLine != null && isNotBlank(addrLine.getTextContent())) {
grobidResponse = gs.processAffiliation(orgNameStr + " " + addrLine.getTextContent() + " " + countryCode);
try {
Element node = DocumentBuilderFactory
.newInstance()
Element node = documentBuilderFactory
.newDocumentBuilder()
.parse(new ByteArrayInputStream(grobidResponse.getBytes()))
.getDocumentElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ private void updateKeywords(Document metadata) {
private void parseAffiliationString(Document doc, NodeList affs) {
Node aff = null;
GrobidService gs = new GrobidService();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
for (int i = affs.getLength() - 1; i >= 0; i--) {
aff = affs.item(i);
if (aff.getNodeType() == Node.ELEMENT_NODE) {
Expand All @@ -125,8 +127,7 @@ private void parseAffiliationString(Document doc, NodeList affs) {
try {
// (HACK)Grobid may split affiliation string into two affiliation elements, which is considered not well-formed.
grobidResponse = "<wrap>" + grobidResponse + "</wrap>";
Element node = DocumentBuilderFactory
.newInstance()
Element node = documentBuilderFactory
.newDocumentBuilder()
.parse(new ByteArrayInputStream(grobidResponse.getBytes()))
.getDocumentElement();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package fr.inria.anhalytics.harvest.crossref;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.inria.anhalytics.commons.exceptions.ServiceException;
import fr.inria.anhalytics.commons.exceptions.SystemException;
import fr.inria.anhalytics.commons.managers.MongoFileManager;
import fr.inria.anhalytics.commons.properties.HarvestProperties;
import com.fasterxml.jackson.databind.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import java.io.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

Expand Down Expand Up @@ -65,26 +66,16 @@ public class CrossRef {

private MongoFileManager mm;

private DocumentBuilder docBuilder;

public CrossRef() {
this.mm = MongoFileManager.getInstance(false);

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setValidating(false);
//docFactory.setNamespaceAware(true);
try {
docBuilder = docFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new SystemException("Cannot instantiate CrossRef parser", e);
}
}

/**
* Try to consolidate some uncertain bibliographical data with crossref web
* service based on core metadata
*/
public void findDois() {
// XPath xPath = XPathFactory.newInstance().newXPath()
// String doi = "";
// String aut = "";
// String title = "";
Expand Down Expand Up @@ -277,7 +268,7 @@ private String getMetadataByDoi(String doi) throws Exception {
}

private HttpURLConnection openConnection(URL url) {
HttpURLConnection urlConn;
HttpURLConnection urlConn;
try {
urlConn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
Expand All @@ -293,10 +284,20 @@ private HttpURLConnection openConnection(URL url) {
/**
* Try to consolidate some uncertain bibliographical data with crossref web
* service based on title and first author.
*
*/
private String queryCrossref(String query) throws Exception {

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setValidating(false);
//docFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = null;

try {
docBuilder = docFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new SystemException("Cannot instantiate CrossRef parser", e);
}

String doi = "";
// we check if the entry is not already in the DB

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ public void initKnowledgeBase() {
initResult = mm.initObjects(null, MongoFileManager.ONLY_NOT_MINED_INIT_KB_PROCESS);
}

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setValidating(false);
//docFactory.setNamespaceAware(true);

DocumentBuilder docBuilder = null;
try {
docBuilder = docFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}

if (initResult) {
while (mm.hasMore()) {
BiblioObject biblioObject = mm.nextBiblioObject();
Expand All @@ -81,18 +92,18 @@ public void initKnowledgeBase() {
adf.openTransaction();
Document teiDoc = null;
try {
InputStream teiStream = new ByteArrayInputStream(mm.getTEICorpus(biblioObject).getBytes());
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setValidating(false);
//docFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = null;
InputStream teiStream = null;
try {
docBuilder = docFactory.newDocumentBuilder();
teiStream = new ByteArrayInputStream(mm.getTEICorpus(biblioObject).getBytes());
teiDoc = docBuilder.parse(teiStream);
} catch (Exception e) {
logger.error("Error when parsing TEI stream. ", e);
} finally {
if (teiStream != null) {
teiStream.close();
}

}
teiStream.close();

Publication pub = new Publication();

Expand Down Expand Up @@ -138,7 +149,7 @@ public void initKnowledgeBase() {
processPersons(editors, "editor", pub, teiDoc, authorsFromfulltextTeiHeader);

logger.info("#################################################################");
} catch(NumberOfCoAuthorsExceededException e) {
} catch (NumberOfCoAuthorsExceededException e) {
logger.warn("Skipping publication, number of coauthors are exceeding 30", e);
adf.rollback();
teiDoc = null;
Expand Down