From 8219f898fbeea733d4c9f8673fe77444ed86c2d2 Mon Sep 17 00:00:00 2001 From: Martin Wiesner Date: Sat, 4 Nov 2023 09:47:01 +0100 Subject: [PATCH] #269 Modernize handling of java.sql resources towards try-with-resources - removes java.sql boilerplate code from `WikipediaTemplateInfo` and `RevisionApi` - cures some JavaDoc issues / typos along the path --- .../java/org/dkpro/jwpl/api/Wikipedia.java | 2 +- .../jwpl/revisionmachine/api/RevisionApi.java | 598 +++++------------- .../common/util/WikipediaXMLKeys.java | 2 +- .../config/gui/control/ConfigSettings.java | 2 +- .../util/templates/WikipediaTemplateInfo.java | 524 ++++++--------- .../decompression/UniversalDecompressor.java | 2 +- 6 files changed, 357 insertions(+), 773 deletions(-) diff --git a/dkpro-jwpl-api/src/main/java/org/dkpro/jwpl/api/Wikipedia.java b/dkpro-jwpl-api/src/main/java/org/dkpro/jwpl/api/Wikipedia.java index e3e313d6..2eae0d96 100644 --- a/dkpro-jwpl-api/src/main/java/org/dkpro/jwpl/api/Wikipedia.java +++ b/dkpro-jwpl-api/src/main/java/org/dkpro/jwpl/api/Wikipedia.java @@ -373,7 +373,7 @@ public Page getDiscussionPage(Page articlePage) throws WikiApiException * {@link #getDiscussionPage(Page)}. * * @param articlePageId - * The id of the page for which to the the discussion archives + * The id of the page for which to fetch the discussion archives * @return The page object for the discussion page. * @throws WikiApiException * If no page or redirect with this title exists or title could not be properly diff --git a/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/api/RevisionApi.java b/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/api/RevisionApi.java index 9dc952b3..87086870 100644 --- a/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/api/RevisionApi.java +++ b/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/api/RevisionApi.java @@ -141,7 +141,6 @@ public Set getArticleIDsWithNumberOfRevisions(final int minNumberRevisi statement.execute(); } - ResultSet result = null; HashSet articles = new HashSet<>(); // make query @@ -159,7 +158,7 @@ public Set getArticleIDsWithNumberOfRevisions(final int minNumberRevisi statement.setInt(1, minNumberRevisions); statement.setInt(2, maxNumberRevisions); } - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); while (result.next()) { articles.add(result.getInt(1)); @@ -169,9 +168,6 @@ public Set getArticleIDsWithNumberOfRevisions(final int minNumberRevisi if (statement != null) { statement.close(); } - if (result != null) { - result.close(); - } } return articles; } @@ -199,35 +195,22 @@ public int getFirstRevisionPK(final int articleID) throws WikiApiException throw new IllegalArgumentException(); } - PreparedStatement statement = null; - ResultSet result = null; String firstRevPK; + // Retrieve the fullRevisionPK and calculate the limit + final String sql = "SELECT PrimaryKey FROM revisions WHERE ArticleID=? AND RevisionCounter =1 LIMIT 1"; - try { - // Retrieve the fullRevisionPK and calculate the limit - statement = this.connection.prepareStatement("SELECT PrimaryKey " - + "FROM revisions " + "WHERE ArticleID=? AND RevisionCounter =1 LIMIT 1"); + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { statement.setInt(1, articleID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { - firstRevPK = result.getString(1); - } else { throw new WikiPageNotFoundException( "The article with the ID " + articleID + " was not found."); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return Integer.parseInt(firstRevPK); @@ -257,35 +240,22 @@ public int getNumberOfRevisions(final int articleID) throws WikiApiException throw new IllegalArgumentException(); } - PreparedStatement statement = null; - ResultSet result = null; String revCounters; + // Retrieve the fullRevisionPK and calculate the limit + final String sql = "SELECT RevisionCounter FROM index_articleID_rc_ts WHERE ArticleID=? LIMIT 1"; - try { - // Retrieve the fullRevisionPK and calculate the limit - statement = this.connection.prepareStatement("SELECT RevisionCounter " - + "FROM index_articleID_rc_ts " + "WHERE ArticleID=? LIMIT 1"); + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { statement.setInt(1, articleID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { - revCounters = result.getString(1); - } else { throw new WikiPageNotFoundException( "The article with the ID " + articleID + " was not found."); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } int index = revCounters.lastIndexOf(' '); if (index == -1) { @@ -319,22 +289,18 @@ public List getRevisionTimestampsBetweenTimestamps(int articleID, List timestamps = new LinkedList<>(); try { - PreparedStatement statement = null; - ResultSet result = null; - - try { - // Check if necessary index exists - if (!indexExists("revisions")) { - throw new WikiInitializationException( - "Please create an index on revisions(ArticleID) in order to make this query feasible."); - } + // Check if necessary index exists + if (!indexExists("revisions")) { + throw new WikiInitializationException( + "Please create an index on revisions(ArticleID) in order to make this query feasible."); + } + final String sql = "SELECT Timestamp FROM revisions WHERE ArticleID=? AND Timestamp >= ? AND Timestamp <= ?"; + try (PreparedStatement statement = connection.prepareStatement(sql)) { - statement = connection.prepareStatement( - "SELECT Timestamp FROM revisions WHERE ArticleID=? AND Timestamp >= ? AND Timestamp <= ?"); statement.setInt(1, articleID); statement.setLong(2, from.getTime()); statement.setLong(3, to.getTime()); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); // Make the query if (result == null) { @@ -345,14 +311,6 @@ public List getRevisionTimestampsBetweenTimestamps(int articleID, timestamps.add(new Timestamp(result.getLong(1))); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return timestamps; @@ -383,21 +341,17 @@ public List getRevisionTimestampsBeforeRevision(final int revisionId) Timestamp ts = getRevision(revisionId).getTimeStamp(); // TODO do this in the SQL query try { - PreparedStatement statement = null; - ResultSet result = null; - - try { - // Check if necessary index exists - if (!indexExists("revisions")) { - throw new WikiInitializationException( - "Please create an index on revisions(ArticleID) in order to make this query feasible."); - } + // Check if necessary index exists + if (!indexExists("revisions")) { + throw new WikiInitializationException( + "Please create an index on revisions(ArticleID) in order to make this query feasible."); + } + final String sql = "SELECT Timestamp FROM revisions WHERE ArticleID=? AND Timestamp < ?"; + try (PreparedStatement statement = connection.prepareStatement(sql)) { - statement = connection.prepareStatement( - "SELECT Timestamp FROM revisions WHERE ArticleID=? AND Timestamp < ?"); statement.setInt(1, articleID); statement.setLong(2, ts.getTime()); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); // Make the query if (result == null) { @@ -408,14 +362,6 @@ public List getRevisionTimestampsBeforeRevision(final int revisionId) timestamps.add(new Timestamp(result.getLong(1))); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return timestamps; @@ -436,7 +382,7 @@ public List getRevisionTimestampsBeforeRevision(final int revisionId) * * @param articleID * ID of the article - * @return collection of timestampp of all revisions + * @return collection of timestamp of all revisions * @throws WikiApiException * if an error occurs */ @@ -449,21 +395,16 @@ public List getRevisionTimestamps(final int articleID) throws WikiApi if (articleID < 1) { throw new IllegalArgumentException(); } + // Check if necessary index exists + if (!indexExists("revisions")) { + throw new WikiInitializationException( + "Please create an index on revisions(ArticleID) in order to make this query feasible."); + } + final String sql = "SELECT Timestamp " + "FROM revisions WHERE ArticleID=?"; + try (PreparedStatement statement = connection.prepareStatement(sql)) { - PreparedStatement statement = null; - ResultSet result = null; - - try { - // Check if necessary index exists - if (!indexExists("revisions")) { - throw new WikiInitializationException( - "Please create an index on revisions(ArticleID) in order to make this query feasible."); - } - - statement = connection - .prepareStatement("SELECT Timestamp " + "FROM revisions WHERE ArticleID=?"); statement.setInt(1, articleID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); // Make the query if (result == null) { @@ -476,14 +417,6 @@ public List getRevisionTimestamps(final int articleID) throws WikiApi } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return timestamps; @@ -538,29 +471,24 @@ public int getNumberOfUniqueContributors(final int articleID, boolean onlyRegist if (articleID < 1) { throw new IllegalArgumentException(); } + // Check if necessary index exists + if (!indexExists("revisions")) { + throw new WikiInitializationException( + "Please create an index on revisions(ArticleID) in order to make this query feasible."); + } int contrCount = 0; - PreparedStatement statement = null; - ResultSet result = null; - - try { - // Check if necessary index exists - if (!indexExists("revisions")) { - throw new WikiInitializationException( - "Please create an index on revisions(ArticleID) in order to make this query feasible."); - } - StringBuffer sqlString = new StringBuffer(); - sqlString.append( - "SELECT COUNT(DISTINCT ContributorName) FROM revisions WHERE ArticleID=?"); - if (onlyRegistered) { - sqlString.append(" AND ContributorIsRegistered=1"); - } + StringBuilder sqlString = new StringBuilder(); + sqlString.append("SELECT COUNT(DISTINCT ContributorName) FROM revisions WHERE ArticleID=?"); + if (onlyRegistered) { + sqlString.append(" AND ContributorIsRegistered=1"); + } - statement = connection.prepareStatement(sqlString.toString()); + try (PreparedStatement statement = connection.prepareStatement(sqlString.toString())) { statement.setInt(1, articleID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); // Make the query if (result == null) { @@ -572,14 +500,6 @@ public int getNumberOfUniqueContributors(final int articleID, boolean onlyRegist contrCount = result.getInt(1); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return contrCount; @@ -635,33 +555,28 @@ public int getNumberOfUniqueContributorsBeforeRevision(final int revisionID, if (revisionID < 1) { throw new IllegalArgumentException(); } + // Check if necessary index exists + if (!indexExists("revisions")) { + throw new WikiInitializationException( + "Please create an index on revisions(ArticleID) in order to make this query feasible."); + } int articleID = getPageIdForRevisionId(revisionID); Timestamp ts = getRevision(revisionID).getTimeStamp(); int contrCount = 0; - PreparedStatement statement = null; - ResultSet result = null; - - try { - // Check if necessary index exists - if (!indexExists("revisions")) { - throw new WikiInitializationException( - "Please create an index on revisions(ArticleID) in order to make this query feasible."); - } - - StringBuffer sqlString = new StringBuffer(); - sqlString.append( - "SELECT COUNT(DISTINCT ContributorName) FROM revisions WHERE ArticleID=? AND Timestamp getUserContributionMap(final int articleID) * * @param articleID * ID of the article - * @param groupfilter + * @param groupFilter * a list of unwanted user groups * @return map of Timestamp-DiffPart-Collection pairs * @throws WikiApiException * if an error occurs */ - public Map getUserContributionMap(final int articleID, String[] groupfilter) + public Map getUserContributionMap(final int articleID, String[] groupFilter) throws WikiApiException { - return getUserContributionMap(articleID, groupfilter, false); + return getUserContributionMap(articleID, groupFilter, false); } /** @@ -751,7 +658,7 @@ public Map getUserContributionMap(final int articleID, String * * @param articleID * ID of the article - * @param groupfilter + * @param groupFilter * a list of unwanted user groups * @param onlyRegistered * {@code true} if result should only contain registered users. {@code false} @@ -760,7 +667,7 @@ public Map getUserContributionMap(final int articleID, String * @throws WikiApiException * if an error occurs */ - public Map getUserContributionMap(final int articleID, String[] groupfilter, + public Map getUserContributionMap(final int articleID, String[] groupFilter, boolean onlyRegistered) throws WikiApiException { @@ -771,21 +678,17 @@ public Map getUserContributionMap(final int articleID, String if (articleID < 1) { throw new IllegalArgumentException(); } - + // Check if necessary index exists + if (!indexExists("revisions")) { + throw new WikiInitializationException( + "Please create an index on revisions(ArticleID) in order to make this query feasible."); + } PreparedStatement statement = null; - ResultSet result = null; try { - - // Check if necessary index exists - if (!indexExists("revisions")) { - throw new WikiInitializationException( - "Please create an index on revisions(ArticleID) in order to make this query feasible."); - } - StringBuilder statementStr = new StringBuilder(); - if (groupfilter == null || groupfilter.length < 1 || !tableExists("user_groups")) { + if (groupFilter == null || groupFilter.length < 1 || !tableExists("user_groups")) { // create statement WITHOUT filter statementStr.append( "SELECT ContributorName, Timestamp FROM revisions WHERE ArticleID=?"); @@ -797,7 +700,7 @@ public Map getUserContributionMap(final int articleID, String statementStr.append( "SELECT ContributorName, Timestamp FROM revisions AS rev, user_groups AS ug WHERE ArticleID=?"); statementStr.append(" AND rev.ContributorId=ug.ug_user"); - for (String element : groupfilter) { + for (String element : groupFilter) { statementStr.append(" AND NOT ug.ug_group=?"); } // and combine with results from unregistered users @@ -812,7 +715,7 @@ public Map getUserContributionMap(final int articleID, String // insert filtered groups in prepared statement int curPrepStatValueIdx = 2; - for (String group : groupfilter) { + for (String group : groupFilter) { statement.setString(curPrepStatValueIdx++, group); } if (!onlyRegistered) { @@ -822,7 +725,7 @@ public Map getUserContributionMap(final int articleID, String } - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result == null) { throw new WikiPageNotFoundException( @@ -837,9 +740,6 @@ public Map getUserContributionMap(final int articleID, String if (statement != null) { statement.close(); } - if (result != null) { - result.close(); - } } return authorTSMap; @@ -874,17 +774,13 @@ public List getUserGroups(final int userID) throws WikiApiException if (!tableExists("user_groups")) { throw new WikiInitializationException( - "User group assignment data is missing. Please download user_groups.sql for this Wikipedia from http://dumps.wikimedia.org and import the data into this database."); + "User group assignment data is missing. Please download user_groups.sql for this Wikipedia from https://dumps.wikimedia.org and import the data into this database."); } - PreparedStatement statement = null; - ResultSet result = null; - - try { - statement = connection - .prepareStatement("SELECT ug_group FROM user_groups WHERE ug_user=?"); + final String sql = "SELECT ug_group FROM user_groups WHERE ug_user=?"; + try (PreparedStatement statement = connection.prepareStatement(sql)) { statement.setInt(1, userID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); // Make the query if (result == null) { @@ -892,17 +788,7 @@ public List getUserGroups(final int userID) throws WikiApiException "The user with the ID " + userID + " was not found."); } while (result.next()) { - groups.add(result.getString(1)); - - } - } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); } } @@ -918,10 +804,10 @@ public List getUserGroups(final int userID) throws WikiApiException } /** - * Returns the revisionIds of all revisions created by given user + * Returns the revisionIds of all revisions created by given user. * * @param userid - * id of the user (NOT USER NAME) + * id of the user (NOT username) * @return Map of revision ids * @throws WikiApiException * if an error occurs @@ -937,18 +823,14 @@ public Map> getUserRevisionIds(int userid) throws WikiApi } if (!indexExists("revisions", "userids")) { - System.err.println( + throw new WikiInitializationException( "You should create and index for the field ContributorID: create index userids ON revisions(ContributorId(15));"); } - PreparedStatement statement = null; - ResultSet result = null; - - try { - statement = connection.prepareStatement( - "SELECT ArticleID, RevisionID " + "FROM revisions WHERE ContributorId=?"); + final String sql = "SELECT ArticleID, RevisionID FROM revisions WHERE ContributorId=?"; + try (PreparedStatement statement = connection.prepareStatement(sql)) { statement.setInt(1, userid); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); // Make the query if (result == null) { @@ -969,14 +851,6 @@ public Map> getUserRevisionIds(int userid) throws WikiApi } } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return revIds; @@ -1010,18 +884,14 @@ public Map> getUserRevisionIds(String username, int limit } if (!indexExists("revisions", "usernames")) { - System.err.println( + throw new WikiInitializationException( "You should create and index for the field ContributorName: create index usernames ON revisions(ContributorName(50));"); } - PreparedStatement statement = null; - ResultSet result = null; - - try { - statement = connection.prepareStatement("SELECT ArticleID, RevisionID " - + "FROM revisions WHERE ContributorName=? LIMIT " + limit); + final String sql = "SELECT ArticleID, RevisionID FROM revisions WHERE ContributorName=? LIMIT " + limit; + try (PreparedStatement statement = connection.prepareStatement(sql)) { statement.setString(1, username); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); // Make the query if (result == null) { @@ -1043,15 +913,7 @@ public Map> getUserRevisionIds(String username, int limit } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } - + return revIds; } @@ -1086,23 +948,18 @@ public Map> getTimestampToRevisionMap(final int if (articleID < 1) { throw new IllegalArgumentException(); } + // Check if necessary index exists + if (!indexExists("revisions")) { + throw new WikiInitializationException( + "Please create an index on revisions(ArticleID) in order to make this query feasible."); + } - PreparedStatement statement = null; - ResultSet result = null; RevisionDecoder decoder = new RevisionDecoder(config.getCharacterSet()); - - try { - - // Check if necessary index exists - if (!indexExists("revisions")) { - throw new WikiInitializationException( - "Please create an index on revisions(ArticleID) in order to make this query feasible."); - } - - statement = connection.prepareStatement( - "SELECT Timestamp, Revision " + "FROM revisions WHERE ArticleID=?"); + + final String sql = "SELECT Timestamp, Revision " + "FROM revisions WHERE ArticleID=?"; + try (PreparedStatement statement = connection.prepareStatement(sql)) { statement.setInt(1, articleID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result == null) { throw new WikiPageNotFoundException( @@ -1133,14 +990,6 @@ public Map> getTimestampToRevisionMap(final int } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return tsDiffPartsMap; @@ -1202,15 +1051,12 @@ private Timestamp getDateOfAppearance(final int articleID, final String firstOrL throw new IllegalArgumentException(); } - PreparedStatement statement = null; - ResultSet result = null; long time; - try { - statement = this.connection.prepareStatement("SELECT " + firstOrLast - + " FROM index_articleID_rc_ts " + "WHERE ArticleID=? LIMIT 1"); + final String sql = "SELECT " + firstOrLast + " FROM index_articleID_rc_ts WHERE ArticleID=? LIMIT 1"; + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { statement.setInt(1, articleID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { @@ -1222,14 +1068,6 @@ private Timestamp getDateOfAppearance(final int articleID, final String firstOrL "The article with the ID " + articleID + " was not found."); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return new Timestamp(time); @@ -1249,7 +1087,7 @@ private Timestamp getDateOfAppearance(final int articleID, final String firstOrL * ID of the revision * @return Revision * @throws WikiApiException - * if an error occurs or the revision does not exists. + * if an error occurs or the revision does not exist. */ public Revision getRevision(final int revisionID) throws WikiApiException { @@ -1262,14 +1100,10 @@ public Revision getRevision(final int revisionID) throws WikiApiException int fullRevPK; int limit; - PreparedStatement statement = null; - ResultSet result = null; - - try { - statement = this.connection.prepareStatement("SELECT FullRevisionPK, RevisionPK " - + "FROM index_revisionID " + "WHERE revisionID=? LIMIT 1"); + final String sql = "SELECT FullRevisionPK, RevisionPK FROM index_revisionID WHERE revisionID=? LIMIT 1"; + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { statement.setInt(1, revisionID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { fullRevPK = result.getInt(1); @@ -1282,15 +1116,7 @@ public Revision getRevision(final int revisionID) throws WikiApiException } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } - + return buildRevisionMetaData(fullRevPK, limit); } @@ -1309,7 +1135,7 @@ public Revision getRevision(final int revisionID) throws WikiApiException * ID of the revision * @return the page if for the given revision * @throws WikiApiException - * if an error occurs or the revision does not exists. + * if an error occurs or the revision does not exist. */ public int getPageIdForRevisionId(final int revisionID) throws WikiApiException { @@ -1321,15 +1147,11 @@ public int getPageIdForRevisionId(final int revisionID) throws WikiApiException int pageId; - PreparedStatement statement = null; - ResultSet result = null; - - try { - statement = this.connection.prepareStatement( - "SELECT r.ArticleID " + "FROM revisions as r, index_revisionID as idx " - + "WHERE idx.RevisionID=? AND idx.RevisionPK=r.PrimaryKey LIMIT 1"); + final String sql = "SELECT r.ArticleID FROM revisions as r, index_revisionID as idx " + + "WHERE idx.RevisionID=? AND idx.RevisionPK=r.PrimaryKey LIMIT 1"; + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { statement.setInt(1, revisionID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { pageId = result.getInt(1); @@ -1340,14 +1162,6 @@ public int getPageIdForRevisionId(final int revisionID) throws WikiApiException } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return pageId; @@ -1370,7 +1184,7 @@ public int getPageIdForRevisionId(final int revisionID) throws WikiApiException * number of revision * @return Revision * @throws WikiApiException - * if an error occurs or the revision does not exists. + * if an error occurs or the revision does not exist. */ public Revision getRevision(final int articleID, final int revisionCounter) throws WikiApiException @@ -1384,14 +1198,10 @@ public Revision getRevision(final int articleID, final int revisionCounter) int revisionIndex = checkMapping(articleID, revisionCounter); String fullRevisions, revCounters; - PreparedStatement statement = null; - ResultSet result = null; - - try { - statement = this.connection.prepareStatement( - "SELECT FullRevisionPKs, RevisionCounter FROM index_articleID_rc_ts WHERE ArticleID=? LIMIT 1"); + final String sql = "SELECT FullRevisionPKs, RevisionCounter FROM index_articleID_rc_ts WHERE ArticleID=? LIMIT 1"; + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { statement.setInt(1, articleID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { @@ -1404,14 +1214,6 @@ public Revision getRevision(final int articleID, final int revisionCounter) "The article with the ID " + articleID + " was not found."); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return getReferencedRevision(articleID, revisionIndex, fullRevisions, revCounters); @@ -1426,7 +1228,7 @@ public Revision getRevision(final int articleID, final int revisionCounter) /** * Returns the by the article ID and timestamp specified revision. Note that the timestamp is - * not an unique identifier of a revision related to an article. The returned revision should be + * not a unique identifier of a revision related to an article. The returned revision should be * the first revision that can be found inside the database. * * @param articleID @@ -1435,15 +1237,13 @@ public Revision getRevision(final int articleID, final int revisionCounter) * Timestamp * @return Revision * @throws WikiApiException - * if an error occurs or the revision does not exists. + * if an error occurs or the revision does not exist. */ public Revision getRevision(final int articleID, final Timestamp time) throws WikiApiException { try { - PreparedStatement statement = null; - ResultSet result = null; String fullRevisions; String revisionCounters; @@ -1452,12 +1252,11 @@ public Revision getRevision(final int articleID, final Timestamp time) throws Wi } int firstPK, lastPK; - try { - statement = this.connection.prepareStatement( - "SELECT FullRevisionPKs, RevisionCounter," + " FirstAppearance " - + "FROM index_articleID_rc_ts " + "WHERE ArticleID=? LIMIT 1"); + final String sql = "SELECT FullRevisionPKs, RevisionCounter, FirstAppearance " + + "FROM index_articleID_rc_ts WHERE ArticleID=? LIMIT 1"; + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { statement.setInt(1, articleID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { @@ -1488,21 +1287,14 @@ public Revision getRevision(final int articleID, final Timestamp time) throws Wi "The article with the ID " + articleID + " was not found."); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } - try { - statement = this.connection.prepareStatement( - "SELECT RevisionCounter FROM revisions WHERE PrimaryKey >= ? AND PrimaryKey < ? AND Timestamp <= ? ORDER BY Timestamp DESC LIMIT 1"); + + final String query = "SELECT RevisionCounter FROM revisions WHERE PrimaryKey >= ? AND PrimaryKey < ? " + + "AND Timestamp <= ? ORDER BY Timestamp DESC LIMIT 1"; + try (PreparedStatement statement = this.connection.prepareStatement(query)) { statement.setInt(1, firstPK); statement.setInt(2, lastPK); statement.setLong(3, time.getTime()); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { int revisionCount = result.getInt(1); @@ -1514,14 +1306,6 @@ public Revision getRevision(final int articleID, final Timestamp time) throws Wi "The revision with the specified timestamp was not found."); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } } catch (WikiPageNotFoundException e) { @@ -1550,15 +1334,11 @@ public Revision getRevision(final int articleID, final Timestamp time) throws Wi protected int checkMapping(final int articleID, final int revisionCounter) throws SQLException { - PreparedStatement statement = null; - ResultSet result = null; - // Check for the correct revisionCounter mapping - try { - statement = this.connection.prepareStatement( - "SELECT Mapping " + "FROM index_chronological " + "WHERE ArticleID=? LIMIT 1"); + final String sql = "SELECT Mapping FROM index_chronological WHERE ArticleID=? LIMIT 1"; + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { statement.setInt(1, articleID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { @@ -1567,14 +1347,6 @@ protected int checkMapping(final int articleID, final int revisionCounter) throw } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return revisionCounter; } @@ -1596,15 +1368,11 @@ public int checkReverseMapping(final int articleID, final int revisionCounter) throws SQLException { - PreparedStatement statement = null; - ResultSet result = null; - // Check for the correct revisionCounter mapping - try { - statement = this.connection.prepareStatement( - "SELECT ReverseMapping FROM index_chronological WHERE ArticleID=? LIMIT 1"); + final String sql = "SELECT ReverseMapping FROM index_chronological WHERE ArticleID=? LIMIT 1"; + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { statement.setInt(1, articleID); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { @@ -1613,14 +1381,6 @@ public int checkReverseMapping(final int articleID, final int revisionCounter) } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return revisionCounter; } @@ -1758,18 +1518,14 @@ private Revision getReferencedRevision(final int articleID, final int revisionIn public void setRevisionTextAndParts(Revision revision) { - try { + int fullRevPK; + int limit; - PreparedStatement statement = null; - ResultSet result = null; - - int fullRevPK; - int limit; - try { - statement = this.connection.prepareStatement("SELECT FullRevisionPK, RevisionPK " - + "FROM index_revisionID " + "WHERE revisionID=? LIMIT 1"); + try { + final String sql = "SELECT FullRevisionPK, RevisionPK FROM index_revisionID WHERE revisionID=? LIMIT 1"; + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { statement.setInt(1, revision.getRevisionID()); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); if (result.next()) { fullRevPK = result.getInt(1); @@ -1781,21 +1537,12 @@ public void setRevisionTextAndParts(Revision revision) "The revision with ID " + revision.getRevisionID() + " was not found."); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } - - try { - statement = this.connection.prepareStatement( - "SELECT Revision, PrimaryKey, RevisionCounter, RevisionID, ArticleID, Timestamp, Comment, Minor, ContributorName, ContributorId, ContributorIsRegistered " - + "FROM revisions " + "WHERE PrimaryKey >= ? LIMIT " + limit); + + final String query = "SELECT Revision, PrimaryKey, RevisionCounter, RevisionID, ArticleID, Timestamp, Comment, Minor, ContributorName, ContributorId, ContributorIsRegistered " + + "FROM revisions " + "WHERE PrimaryKey >= ? LIMIT " + limit; + try (PreparedStatement statement = this.connection.prepareStatement(query)) { statement.setInt(1, fullRevPK); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); String previousRevision = null, currentRevision = null; @@ -1831,14 +1578,6 @@ public void setRevisionTextAndParts(Revision revision) revision.setRevisionText(currentRevision); } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } } catch (WikiPageNotFoundException | DecodingException | SQLException | IOException e) { throw new RuntimeException(e); @@ -1863,25 +1602,21 @@ public void setRevisionTextAndParts(Revision revision) private Revision buildRevisionMetaData(final int fullRevPK, final int limit) throws SQLException { - PreparedStatement statement = null; - ResultSet result = null; - - try { - String query = "SELECT Revision, PrimaryKey, RevisionCounter, RevisionID, ArticleID, Timestamp, Comment, Minor, ContributorName, ContributorId, ContributorIsRegistered " - + "FROM revisions " + "WHERE PrimaryKey >= ? LIMIT " + limit; + final String query = "SELECT Revision, PrimaryKey, RevisionCounter, RevisionID, ArticleID, Timestamp, Comment, Minor, ContributorName, ContributorId, ContributorIsRegistered " + + "FROM revisions " + "WHERE PrimaryKey >= ? LIMIT " + limit; + /* + * As HSQL does not support ResultSet.last() per default, we have to specify these extra + * parameters here. + * + * With these parameters in place, the 'last()' call works as expected. + * + * See also: https://stackoverflow.com/q/19533991 + */ + try (PreparedStatement statement = this.connection.prepareStatement(query, + ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) { - /* - * As HSQL does not support ResultSet.last() per default, we have to specify these extra - * parameters here. - * - * With these parameters in place, the 'last()' call works as expected. - * - * See also: https://stackoverflow.com/q/19533991 - */ - statement = this.connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, - ResultSet.CONCUR_READ_ONLY); statement.setInt(1, fullRevPK); - result = statement.executeQuery(); + ResultSet result = statement.executeQuery(); Revision revision = null; @@ -1907,14 +1642,6 @@ private Revision buildRevisionMetaData(final int fullRevPK, final int limit) thr return revision; } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } } @@ -1938,7 +1665,7 @@ private boolean indexExists(String table) throws SQLException * @param table * the table to check * @param indexName - * the name of the index (may be null) + * the name of the index (might be {@code null}) * @return {@code true} if index exists, false else * @throws SQLException * if an error occurs connecting to or querying the db @@ -1946,11 +1673,11 @@ private boolean indexExists(String table) throws SQLException private boolean indexExists(String table, String indexName) throws SQLException { - try (PreparedStatement statement = this.connection - .prepareStatement("SHOW INDEX FROM " + table + " WHERE Key_name!= 'PRIMARY'"); - ResultSet result = statement.executeQuery()) { + final String sql = "SHOW INDEX FROM " + table + " WHERE Key_name!= 'PRIMARY'"; + try (PreparedStatement statement = this.connection.prepareStatement(sql)) { + ResultSet result = statement.executeQuery(); - // Check if an index exists (because otherwise the query would + // Check if an index exists because otherwise the query would // be awfully slow. Note that the existence of ANY index will // suffice - we might want to check for a specific index. if (result == null || !result.next()) { @@ -1996,9 +1723,8 @@ private boolean indexExists(String table, String indexName) throws SQLException private boolean tableExists(String table) throws SQLException { - try (PreparedStatement statement = this.connection.prepareStatement("SHOW TABLES;"); - ResultSet result = statement.executeQuery()) { - + try (PreparedStatement statement = this.connection.prepareStatement("SHOW TABLES;")) { + ResultSet result = statement.executeQuery(); if (result == null) { return false; } diff --git a/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/common/util/WikipediaXMLKeys.java b/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/common/util/WikipediaXMLKeys.java index ca130d96..6147b957 100644 --- a/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/common/util/WikipediaXMLKeys.java +++ b/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/common/util/WikipediaXMLKeys.java @@ -84,7 +84,7 @@ public enum WikipediaXMLKeys KEY_END_IP(""), /** - * Indicates the start of the the contributor username + * Indicates the start of the contributor username */ KEY_START_USERNAME(""), diff --git a/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/difftool/config/gui/control/ConfigSettings.java b/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/difftool/config/gui/control/ConfigSettings.java index 9e35b70c..ff8287cc 100644 --- a/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/difftool/config/gui/control/ConfigSettings.java +++ b/dkpro-jwpl-revisionmachine/src/main/java/org/dkpro/jwpl/revisionmachine/difftool/config/gui/control/ConfigSettings.java @@ -132,7 +132,7 @@ public Iterator archiveIterator() } /** - * Assigns the given value to the the given key. + * Assigns the given value to the given key. * * @param key * configuration key diff --git a/dkpro-jwpl-util/src/main/java/org/dkpro/jwpl/util/templates/WikipediaTemplateInfo.java b/dkpro-jwpl-util/src/main/java/org/dkpro/jwpl/util/templates/WikipediaTemplateInfo.java index fdcf53a1..db34b566 100644 --- a/dkpro-jwpl-util/src/main/java/org/dkpro/jwpl/util/templates/WikipediaTemplateInfo.java +++ b/dkpro-jwpl-util/src/main/java/org/dkpro/jwpl/util/templates/WikipediaTemplateInfo.java @@ -63,9 +63,6 @@ public class WikipediaTemplateInfo private Connection connection; - /** - * - */ public WikipediaTemplateInfo(Wikipedia pWiki) throws SQLException, WikiApiException { this.wiki = pWiki; @@ -79,7 +76,7 @@ public WikipediaTemplateInfo(Wikipedia pWiki) throws SQLException, WikiApiExcept /** * Returns the number of all pages that contain a template the name of which starts with any of - * the the given Strings. + * the given Strings. * * @param templateFragments * a list Strings containing the beginnings of the desired templates @@ -96,29 +93,23 @@ private Integer countFragmentFilteredPages(List templateFragments, boole { try { int count = 0; - PreparedStatement statement = null; - ResultSet result = null; - - try { - StringBuffer sqlString = new StringBuffer(); - StringBuffer subconditions = new StringBuffer(); - sqlString.append( - "SELECT distinct(count(*)) FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " as tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID - + " AS p WHERE tpl.templateId = p.templateId " - + (whitelist ? "AND" : "AND NOT") + " ("); - for (@SuppressWarnings("unused") - String fragment : templateFragments) { - if (subconditions.length() != 0) { - subconditions.append("OR "); - } - subconditions.append("tpl.templateName LIKE ?"); - } - sqlString.append(subconditions); - sqlString.append(")"); - - statement = connection.prepareStatement(sqlString.toString()); + StringBuilder sqlString = new StringBuilder(); + StringBuilder subconditions = new StringBuilder(); + sqlString.append("SELECT distinct(count(*)) FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " as tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID + + " AS p WHERE tpl.templateId = p.templateId " + + (whitelist ? "AND" : "AND NOT") + " ("); + for (@SuppressWarnings("unused") + String fragment : templateFragments) { + if (!subconditions.isEmpty()) { + subconditions.append("OR "); + } + subconditions.append("tpl.templateName LIKE ?"); + } + sqlString.append(subconditions); + sqlString.append(")"); + try (PreparedStatement statement = connection.prepareStatement(sqlString.toString())) { int curIdx = 1; for (String fragment : templateFragments) { fragment = fragment.toLowerCase(); @@ -127,7 +118,7 @@ private Integer countFragmentFilteredPages(List templateFragments, boole statement.setString(curIdx++, fragment + "%"); } - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { return 0; @@ -137,14 +128,6 @@ private Integer countFragmentFilteredPages(List templateFragments, boole count = result.getInt(1); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return count; } @@ -155,7 +138,7 @@ private Integer countFragmentFilteredPages(List templateFragments, boole /** * Returns the number of all pages that contain a template the name of which starts with any of - * the the given Strings. + * the given Strings. * * @param templateFragments * a list Strings containing the beginnings of the desired templates @@ -172,7 +155,7 @@ public Integer countPagesContainingTemplateFragments(List templateFragme /** * Returns the number of all pages that contain a template the name of which starts with any of - * the the given Strings. + * the given Strings. * * @param templateFragments * a list Strings containing the beginnings of the desired templates @@ -206,32 +189,27 @@ private Integer countFilteredPages(List templateNames, boolean whitelist throws WikiApiException { + int count = 0; try { - int count = 0; - PreparedStatement statement = null; - ResultSet result = null; - - try { - StringBuffer sqlString = new StringBuffer(); - StringBuffer subconditions = new StringBuffer(); - sqlString.append( - "SELECT distinct(count(*)) FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " as tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID - + " AS p WHERE tpl.templateId = p.templateId " - + (whitelist ? "AND" : "AND NOT") + " ("); - - for (@SuppressWarnings("unused") - String name : templateNames) { - if (subconditions.length() != 0) { - subconditions.append("OR "); - } - subconditions.append("tpl.templateName = ?"); - } - sqlString.append(subconditions); - sqlString.append(")"); - - statement = connection.prepareStatement(sqlString.toString()); + StringBuilder sqlString = new StringBuilder(); + StringBuilder subconditions = new StringBuilder(); + sqlString.append( + "SELECT distinct(count(*)) FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " as tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID + + " AS p WHERE tpl.templateId = p.templateId " + + (whitelist ? "AND" : "AND NOT") + " ("); + + for (@SuppressWarnings("unused") + String name : templateNames) { + if (!subconditions.isEmpty()) { + subconditions.append("OR "); + } + subconditions.append("tpl.templateName = ?"); + } + sqlString.append(subconditions); + sqlString.append(")"); + try (PreparedStatement statement = connection.prepareStatement(sqlString.toString())) { int curIdx = 1; for (String name : templateNames) { name = name.toLowerCase().trim(); @@ -239,7 +217,7 @@ private Integer countFilteredPages(List templateNames, boolean whitelist statement.setString(curIdx++, name); } - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { return 0; @@ -249,14 +227,6 @@ private Integer countFilteredPages(List templateNames, boolean whitelist count = result.getInt(1); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return count; } @@ -321,31 +291,26 @@ private Iterable getFragmentFilteredPages(List templateFragments, throws WikiApiException { + List matchedPages = new LinkedList<>(); try { - PreparedStatement statement = null; - ResultSet result = null; - List matchedPages = new LinkedList<>(); - - try { - StringBuffer sqlString = new StringBuffer(); - StringBuffer subconditions = new StringBuffer(); - sqlString.append("SELECT p.pageId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " AS tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID - + " AS p WHERE tpl.templateId = p.templateId " - + (whitelist ? "AND" : "AND NOT") + " ("); - - for (@SuppressWarnings("unused") - String fragment : templateFragments) { - if (subconditions.length() != 0) { - subconditions.append("OR "); - } - subconditions.append("tpl.templateName LIKE ?"); - } - sqlString.append(subconditions); - sqlString.append(")"); - - statement = connection.prepareStatement(sqlString.toString()); + StringBuilder sqlString = new StringBuilder(); + StringBuilder subconditions = new StringBuilder(); + sqlString.append("SELECT p.pageId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " AS tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID + + " AS p WHERE tpl.templateId = p.templateId " + + (whitelist ? "AND" : "AND NOT") + " ("); + + for (@SuppressWarnings("unused") + String fragment : templateFragments) { + if (!subconditions.isEmpty()) { + subconditions.append("OR "); + } + subconditions.append("tpl.templateName LIKE ?"); + } + sqlString.append(subconditions); + sqlString.append(")"); + try (PreparedStatement statement = connection.prepareStatement(sqlString.toString())) { int curIdx = 1; for (String fragment : templateFragments) { fragment = fragment.toLowerCase().trim(); @@ -353,7 +318,7 @@ private Iterable getFragmentFilteredPages(List templateFragments, statement.setString(curIdx++, fragment + "%"); } - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { throw new WikiPageNotFoundException("Nothing was found"); @@ -364,14 +329,6 @@ private Iterable getFragmentFilteredPages(List templateFragments, matchedPages.add(wiki.getPage(pageID)); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return matchedPages; } @@ -383,20 +340,15 @@ private Iterable getFragmentFilteredPages(List templateFragments, public int checkTemplateId(String templateName) throws WikiApiException { try { - PreparedStatement statement = null; - ResultSet result = null; - - try { - StringBuffer sqlString = new StringBuffer(); - - sqlString.append( - "SELECT tpl.templateId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " AS tpl WHERE tpl.templateName='" - + templateName.trim().replaceAll(" ", "_") + "'"); + StringBuilder sqlString = new StringBuilder(); + sqlString.append( + "SELECT tpl.templateId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " AS tpl WHERE tpl.templateName='" + + templateName.trim().replaceAll(" ", "_") + "'"); - statement = connection.prepareStatement(sqlString.toString()); + try (PreparedStatement statement = connection.prepareStatement(sqlString.toString())) { - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { return -1; @@ -406,15 +358,6 @@ public int checkTemplateId(String templateName) throws WikiApiException return result.getInt(1); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - - } return -1; } @@ -468,7 +411,7 @@ public Iterable getPagesNotContainingTemplateFragments(List templa * @param whitelist * whether to return pages containing these templates (true) or return pages NOT * containing these templates (false) - * @return An iterable with the page objects that contain any of the the specified templates + * @return An iterable with the page objects that contain any of the specified templates * @throws WikiApiException * If there was any error retrieving the page object (most likely if the templates * are corrupted) @@ -476,30 +419,26 @@ public Iterable getPagesNotContainingTemplateFragments(List templa private Iterable getFilteredPages(List templateNames, boolean whitelist) throws WikiApiException { + List matchedPages = new LinkedList<>(); try { - PreparedStatement statement = null; - ResultSet result = null; - List matchedPages = new LinkedList<>(); - - try { - StringBuffer sqlString = new StringBuffer(); - StringBuffer subconditions = new StringBuffer(); - sqlString.append("SELECT p.pageId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " AS tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID - + " AS p WHERE tpl.templateId = p.templateId " - + (whitelist ? "AND" : "AND NOT") + " ("); - - for (@SuppressWarnings("unused") - String name : templateNames) { - if (subconditions.length() != 0) { - subconditions.append("OR "); - } - subconditions.append("tpl.templateName = ?"); - } - sqlString.append(subconditions); - sqlString.append(")"); + StringBuilder sqlString = new StringBuilder(); + StringBuilder subconditions = new StringBuilder(); + sqlString.append("SELECT p.pageId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " AS tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID + + " AS p WHERE tpl.templateId = p.templateId " + + (whitelist ? "AND" : "AND NOT") + " ("); + + for (@SuppressWarnings("unused") + String name : templateNames) { + if (!subconditions.isEmpty()) { + subconditions.append("OR "); + } + subconditions.append("tpl.templateName = ?"); + } + sqlString.append(subconditions); + sqlString.append(")"); - statement = connection.prepareStatement(sqlString.toString()); + try (PreparedStatement statement = connection.prepareStatement(sqlString.toString())) { int curIdx = 1; for (String name : templateNames) { @@ -508,7 +447,7 @@ private Iterable getFilteredPages(List templateNames, boolean whit statement.setString(curIdx++, name); } - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { throw new WikiPageNotFoundException("Nothing was found"); @@ -519,14 +458,6 @@ private Iterable getFilteredPages(List templateNames, boolean whit matchedPages.add(wiki.getPage(pageID)); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return matchedPages; } @@ -541,7 +472,7 @@ private Iterable getFilteredPages(List templateNames, boolean whit * * @param templateNames * the names of the template that we want to match - * @return An iterable with the page objects that contain any of the the specified templates + * @return An iterable with the page objects that contain any of the specified templates * @throws WikiApiException * If there was any error retrieving the page object (most likely if the templates * are corrupted) @@ -558,7 +489,7 @@ public Iterable getPagesContainingTemplateNames(List templateNames * * @param templateNames * the names of the template that we want to match - * @return An iterable with the page objects that do NOT contain any of the the specified + * @return An iterable with the page objects that do NOT contain any of the specified * templates * @throws WikiApiException * If there was any error retrieving the page object (most likely if the templates @@ -577,7 +508,7 @@ public Iterable getPagesNotContainingTemplateNames(List templateNa * * @param templateName * the template names that have to be matched - * @return An list with the revision ids of the first appearance of the template + * @return A list with the revision ids of the first appearance of the template * @throws WikiApiException * If there was any error retrieving the page object (most likely if the templates * are corrupted) @@ -599,7 +530,7 @@ public List getRevisionsWithFirstTemplateAppearance(String templateName List revisionIds = new LinkedList<>(); List pageIds = getPageIdsContainingTemplateNames( Arrays.asList(new String[] { templateName })); - if (pageIds.size() == 0) { + if (pageIds.isEmpty()) { return revisionIds; } if (revApi == null) { @@ -664,7 +595,7 @@ public List getRevisionsWithFirstTemplateAppearance(String templateName * @param whitelist * whether to return pages containing these templates (true) or return pages NOT * containing these templates (false) - * @return An list with the ids of the pages that contain templates beginning with any String in + * @return A list with the ids of the pages that contain templates beginning with any String in * templateFragments * @throws WikiApiException * If there was any error retrieving the page object (most likely if the templates @@ -674,29 +605,25 @@ private List getFragmentFilteredPageIds(List templateFragments, boolean whitelist) throws WikiApiException { + List matchedPages = new LinkedList<>(); try { - PreparedStatement statement = null; - ResultSet result = null; - List matchedPages = new LinkedList<>(); - - try { - StringBuffer sqlString = new StringBuffer(); - StringBuffer subconditions = new StringBuffer(); - sqlString.append("SELECT p.pageId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " AS tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID - + " AS p WHERE tpl.templateId = p.templateId " - + (whitelist ? "AND" : "AND NOT") + " ("); - for (@SuppressWarnings("unused") - String fragment : templateFragments) { - if (subconditions.length() != 0) { - subconditions.append("OR "); - } - subconditions.append("tpl.templateName LIKE ?"); - } - sqlString.append(subconditions); - sqlString.append(")"); + StringBuilder sqlString = new StringBuilder(); + StringBuilder subconditions = new StringBuilder(); + sqlString.append("SELECT p.pageId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " AS tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID + + " AS p WHERE tpl.templateId = p.templateId " + + (whitelist ? "AND" : "AND NOT") + " ("); + for (@SuppressWarnings("unused") + String fragment : templateFragments) { + if (!subconditions.isEmpty()) { + subconditions.append("OR "); + } + subconditions.append("tpl.templateName LIKE ?"); + } + sqlString.append(subconditions); + sqlString.append(")"); - statement = connection.prepareStatement(sqlString.toString()); + try (PreparedStatement statement = connection.prepareStatement(sqlString.toString())) { int curIdx = 1; for (String fragment : templateFragments) { @@ -705,7 +632,7 @@ private List getFragmentFilteredPageIds(List templateFragments, statement.setString(curIdx++, fragment + "%"); } - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { throw new WikiPageNotFoundException("Nothing was found"); @@ -715,14 +642,6 @@ private List getFragmentFilteredPageIds(List templateFragments, matchedPages.add(result.getInt(1)); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return matchedPages; } @@ -737,7 +656,7 @@ private List getFragmentFilteredPageIds(List templateFragments, * * @param templateFragments * the beginning of the templates that have to be matched - * @return An list with the ids of the pages that contain templates beginning with any String in + * @return A list with the ids of the pages that contain templates beginning with any String in * templateFragments * @throws WikiApiException * If there was any error retrieving the page object (most likely if the template @@ -755,7 +674,7 @@ public List getPageIdsContainingTemplateFragments(List template * * @param templateFragments * the beginning of the templates that have to be matched - * @return An list with the ids of the pages that do not contain templates beginning with any + * @return A list with the ids of the pages that do not contain templates beginning with any * String in templateFragments * @throws WikiApiException * If there was any error retrieving the page object (most likely if the template @@ -778,7 +697,7 @@ public List getPageIdsNotContainingTemplateFragments(List templ * @param whitelist * whether to return pages containing these templates (true) or return pages NOT * containing these templates (false) - * @return An list with the ids of the revisions that contain templates beginning with any + * @return A list with the ids of the revisions that contain templates beginning with any * String in templateFragments * @throws WikiApiException * If there was any error retrieving the page object (most likely if the templates @@ -789,30 +708,26 @@ private List getFragmentFilteredRevisionIds(List templateFragme throws WikiApiException { + List matchedPages = new LinkedList<>(); try { - PreparedStatement statement = null; - ResultSet result = null; - List matchedPages = new LinkedList<>(); - - try { - StringBuffer sqlString = new StringBuffer(); - StringBuffer subconditions = new StringBuffer(); - sqlString - .append("SELECT r.revisionId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " AS tpl, " + GeneratorConstants.TABLE_TPLID_REVISIONID - + " AS r WHERE tpl.templateId = r.templateId " - + (whitelist ? "AND" : "AND NOT") + " ("); - for (@SuppressWarnings("unused") - String fragment : templateFragments) { - if (subconditions.length() != 0) { - subconditions.append("OR "); - } - subconditions.append("tpl.templateName LIKE ?"); - } - sqlString.append(subconditions); - sqlString.append(")"); + StringBuilder sqlString = new StringBuilder(); + StringBuilder subconditions = new StringBuilder(); + sqlString + .append("SELECT r.revisionId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " AS tpl, " + GeneratorConstants.TABLE_TPLID_REVISIONID + + " AS r WHERE tpl.templateId = r.templateId " + + (whitelist ? "AND" : "AND NOT") + " ("); + for (@SuppressWarnings("unused") + String fragment : templateFragments) { + if (!subconditions.isEmpty()) { + subconditions.append("OR "); + } + subconditions.append("tpl.templateName LIKE ?"); + } + sqlString.append(subconditions); + sqlString.append(")"); - statement = connection.prepareStatement(sqlString.toString()); + try (PreparedStatement statement = connection.prepareStatement(sqlString.toString())) { int curIdx = 1; for (String fragment : templateFragments) { @@ -821,7 +736,7 @@ private List getFragmentFilteredRevisionIds(List templateFragme statement.setString(curIdx++, fragment + "%"); } - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { throw new WikiPageNotFoundException("Nothing was found"); @@ -831,14 +746,6 @@ private List getFragmentFilteredRevisionIds(List templateFragme matchedPages.add(result.getInt(1)); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return matchedPages; } @@ -853,7 +760,7 @@ private List getFragmentFilteredRevisionIds(List templateFragme * * @param templateFragments * the beginning of the templates that have to be matched - * @return An list with the ids of the revisions that contain templates beginning with any + * @return A list with the ids of the revisions that contain templates beginning with any * String in templateFragments * @throws WikiApiException * If there was any error retrieving the page object (most likely if the template @@ -871,7 +778,7 @@ public List getRevisionIdsContainingTemplateFragments(List temp * * @param templateFragments * the beginning of the templates that have to be matched - * @return An list with the ids of the revisions that do not contain templates beginning with + * @return A list with the ids of the revisions that do not contain templates beginning with * any String in templateFragments * @throws WikiApiException * If there was any error retrieving the page object (most likely if the template @@ -944,10 +851,7 @@ public List getIdsOfPagesThatEverContainedTemplateFragments( pageIdSet.add(revApi.getPageIdForRevisionId(revId)); } - List pageIds = new LinkedList<>(); - pageIds.addAll(pageIdSet); - - return pageIds; + return new LinkedList<>(pageIdSet); } /////////////////// @@ -961,7 +865,7 @@ public List getIdsOfPagesThatEverContainedTemplateFragments( * @param whitelist * whether to return pages containing these templates (true) or return pages NOT * containing these templates (false) - * @return A list with the ids of all pages that contain any of the the specified templates + * @return A list with the ids of all pages that contain any of the specified templates * @throws WikiApiException * If there was any error retrieving the page object (most likely if the templates * are corrupted) @@ -969,30 +873,26 @@ public List getIdsOfPagesThatEverContainedTemplateFragments( private List getFilteredPageIds(List templateNames, boolean whitelist) throws WikiApiException { + List matchedPages = new LinkedList<>(); try { - PreparedStatement statement = null; - ResultSet result = null; - List matchedPages = new LinkedList<>(); - - try { - StringBuffer sqlString = new StringBuffer(); - StringBuffer subconditions = new StringBuffer(); - sqlString.append("SELECT p.pageId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " AS tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID - + " AS p WHERE tpl.templateId = p.templateId " - + (whitelist ? "AND" : "AND NOT") + " ("); - - for (@SuppressWarnings("unused") - String name : templateNames) { - if (subconditions.length() != 0) { - subconditions.append("OR "); - } - subconditions.append("tpl.templateName = ?"); - } - sqlString.append(subconditions); - sqlString.append(")"); + StringBuilder sqlString = new StringBuilder(); + StringBuilder subconditions = new StringBuilder(); + sqlString.append("SELECT p.pageId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " AS tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID + + " AS p WHERE tpl.templateId = p.templateId " + + (whitelist ? "AND" : "AND NOT") + " ("); + + for (@SuppressWarnings("unused") + String name : templateNames) { + if (!subconditions.isEmpty()) { + subconditions.append("OR "); + } + subconditions.append("tpl.templateName = ?"); + } + sqlString.append(subconditions); + sqlString.append(")"); - statement = connection.prepareStatement(sqlString.toString()); + try (PreparedStatement statement = connection.prepareStatement(sqlString.toString())) { int curIdx = 1; for (String name : templateNames) { @@ -1001,7 +901,7 @@ private List getFilteredPageIds(List templateNames, boolean whi statement.setString(curIdx++, name); } - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { throw new WikiPageNotFoundException("Nothing was found"); @@ -1011,14 +911,6 @@ private List getFilteredPageIds(List templateNames, boolean whi matchedPages.add(result.getInt(1)); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return matchedPages; } @@ -1033,7 +925,7 @@ private List getFilteredPageIds(List templateNames, boolean whi * * @param templateNames * the names of the template that we want to match - * @return A list with the ids of all pages that contain any of the the specified templates + * @return A list with the ids of all pages that contain any of the specified templates * @throws WikiApiException * If there was any error retrieving the page object (most likely if the templates * are corrupted) @@ -1050,7 +942,7 @@ public List getPageIdsContainingTemplateNames(List templateName * * @param templateNames * the names of the template that we want to match - * @return A list with the ids of all pages that do not contain any of the the specified + * @return A list with the ids of all pages that do not contain any of the specified * templates * @throws WikiApiException * If there was any error retrieving the page object (most likely if the templates @@ -1071,7 +963,7 @@ public List getPageIdsNotContainingTemplateNames(List templateN * @param whitelist * whether to return pages containing these templates (true) or return pages NOT * containing these templates (false) - * @return A list with the ids of all revisions that contain any of the the specified templates + * @return A list with the ids of all revisions that contain any of the specified templates * @throws WikiApiException * If there was any error retrieving the page object (most likely if the templates * are corrupted) @@ -1079,31 +971,27 @@ public List getPageIdsNotContainingTemplateNames(List templateN private List getFilteredRevisionIds(List templateNames, boolean whitelist) throws WikiApiException { + List matchedPages = new LinkedList<>(); try { - PreparedStatement statement = null; - ResultSet result = null; - List matchedPages = new LinkedList<>(); - - try { - StringBuffer sqlString = new StringBuffer(); - StringBuffer subconditions = new StringBuffer(); - sqlString - .append("SELECT r.revisionId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " AS tpl, " + GeneratorConstants.TABLE_TPLID_REVISIONID - + " AS r WHERE tpl.templateId = r.templateId " - + (whitelist ? "AND" : "AND NOT") + " ("); - - for (@SuppressWarnings("unused") - String name : templateNames) { - if (subconditions.length() != 0) { - subconditions.append("OR "); - } - subconditions.append("tpl.templateName = ?"); - } - sqlString.append(subconditions); - sqlString.append(")"); + StringBuilder sqlString = new StringBuilder(); + StringBuilder subconditions = new StringBuilder(); + sqlString + .append("SELECT r.revisionId FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " AS tpl, " + GeneratorConstants.TABLE_TPLID_REVISIONID + + " AS r WHERE tpl.templateId = r.templateId " + + (whitelist ? "AND" : "AND NOT") + " ("); + + for (@SuppressWarnings("unused") + String name : templateNames) { + if (!subconditions.isEmpty()) { + subconditions.append("OR "); + } + subconditions.append("tpl.templateName = ?"); + } + sqlString.append(subconditions); + sqlString.append(")"); - statement = connection.prepareStatement(sqlString.toString()); + try (PreparedStatement statement = connection.prepareStatement(sqlString.toString())) { int curIdx = 1; for (String name : templateNames) { @@ -1112,7 +1000,7 @@ private List getFilteredRevisionIds(List templateNames, boolean statement.setString(curIdx++, name); } - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { throw new WikiPageNotFoundException("Nothing was found"); @@ -1122,14 +1010,6 @@ private List getFilteredRevisionIds(List templateNames, boolean matchedPages.add(result.getInt(1)); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return matchedPages; } @@ -1225,19 +1105,16 @@ public List getTemplateNamesFromPage(int pageId) throws WikiApiException if (pageId < 1) { throw new WikiApiException("Page ID must be > 0"); } + List templateNames = new LinkedList<>(); try { - PreparedStatement statement = null; - ResultSet result = null; - List templateNames = new LinkedList<>(); + final String sql = "SELECT tpl.templateName FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " AS tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID + + " AS p WHERE tpl.templateId = p.templateId AND p.pageId = ?"; - try { - statement = connection.prepareStatement( - "SELECT tpl.templateName FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " AS tpl, " + GeneratorConstants.TABLE_TPLID_PAGEID - + " AS p WHERE tpl.templateId = p.templateId AND p.pageId = ?"); + try (PreparedStatement statement = connection.prepareStatement(sql)) { statement.setInt(1, pageId); - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { return templateNames; @@ -1247,14 +1124,6 @@ public List getTemplateNamesFromPage(int pageId) throws WikiApiException templateNames.add(result.getString(1).toLowerCase()); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return templateNames; } @@ -1278,19 +1147,16 @@ public List getTemplateNamesFromRevision(int revid) throws WikiApiExcept if (revid < 1) { throw new WikiApiException("Revision ID must be > 0"); } + List templateNames = new LinkedList<>(); try { - PreparedStatement statement = null; - ResultSet result = null; - List templateNames = new LinkedList<>(); - - try { - statement = connection.prepareStatement( - "SELECT tpl.templateName FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME - + " AS tpl, " + GeneratorConstants.TABLE_TPLID_REVISIONID - + " AS p WHERE tpl.templateId = p.templateId AND p.revisionId = ?"); + final String sql = "SELECT tpl.templateName FROM " + GeneratorConstants.TABLE_TPLID_TPLNAME + + " AS tpl, " + GeneratorConstants.TABLE_TPLID_REVISIONID + + " AS p WHERE tpl.templateId = p.templateId AND p.revisionId = ?"; + + try (PreparedStatement statement = connection.prepareStatement(sql)) { statement.setInt(1, revid); - result = execute(statement); + ResultSet result = execute(statement); if (result == null) { return templateNames; @@ -1300,14 +1166,6 @@ public List getTemplateNamesFromRevision(int revid) throws WikiApiExcept templateNames.add(result.getString(1).toLowerCase()); } } - finally { - if (statement != null) { - statement.close(); - } - if (result != null) { - result.close(); - } - } return templateNames; } @@ -1410,7 +1268,7 @@ public boolean revisionContainsTemplateNameWithoutIndex(int revId, String templa } /** - * Does the same as {@link #revisionContainsTemplateFragmentWithoutIndex(int, String)} without + * Does the same as {@link #revisionContainsTemplateNameWithoutIndex(int, String)} without * using a template index * * @param revId diff --git a/dkpro-jwpl-wikimachine/src/main/java/org/dkpro/jwpl/wikimachine/decompression/UniversalDecompressor.java b/dkpro-jwpl-wikimachine/src/main/java/org/dkpro/jwpl/wikimachine/decompression/UniversalDecompressor.java index 8a4ee763..04deb284 100644 --- a/dkpro-jwpl-wikimachine/src/main/java/org/dkpro/jwpl/wikimachine/decompression/UniversalDecompressor.java +++ b/dkpro-jwpl-wikimachine/src/main/java/org/dkpro/jwpl/wikimachine/decompression/UniversalDecompressor.java @@ -155,7 +155,7 @@ public boolean isSupported(String fileName) } /** - * Start an external utility to unpack the the archive + * Start an external utility to unpack the archive. * * @param fileName * @return InputStream to read the decompressed data