-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
147 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/net/renfei/indexing/service/ExtractSitemapService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package net.renfei.indexing.service; | ||
|
||
import net.renfei.indexing.ui.MainWindow; | ||
import net.renfei.sdk.utils.BeanUtils; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class ExtractSitemapService implements Runnable { | ||
private MainWindow mainWindow; | ||
|
||
public ExtractSitemapService(MainWindow mainWindow) { | ||
this.mainWindow = mainWindow; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
String site = mainWindow.siteUrl.getText(); | ||
if (BeanUtils.isEmpty(site)) { | ||
mainWindow.setLog("【站点URL】不能为空。"); | ||
return; | ||
} | ||
if (site.endsWith("/")) { | ||
site += "sitemap.xml"; | ||
} else { | ||
site += "/sitemap.xml"; | ||
} | ||
mainWindow.setLog("访问 " + site); | ||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
try { | ||
DocumentBuilder db = dbf.newDocumentBuilder(); | ||
Document document = db.parse(site); | ||
NodeList sitemapList = document.getElementsByTagName("sitemap"); | ||
if (sitemapList.getLength() > 0) { | ||
mainWindow.setLog("检测到站点地图集合"); | ||
for (int i = 0; i < sitemapList.getLength(); i++) { | ||
Node item = sitemapList.item(i); | ||
NodeList childNodes = item.getChildNodes(); | ||
for (int j = 0; j < childNodes.getLength(); j++) { | ||
String nodeName = childNodes.item(j).getTextContent().trim(); | ||
if (!nodeName.isEmpty() && "loc".equalsIgnoreCase(childNodes.item(j).getNodeName())) { | ||
getUrl(nodeName); | ||
} | ||
} | ||
} | ||
} else { | ||
getUrl(site); | ||
} | ||
mainWindow.setLog("执行结束"); | ||
} catch (Exception e) { | ||
mainWindow.setLog("\n[!] 发生错误:\r\n" + e.getMessage() + "\r\n如果您认为不是您的错误,请联系开发者:[email protected]。\r\n"); | ||
} | ||
} | ||
|
||
private void getUrl(String url) { | ||
mainWindow.setLog("发现网站地图:" + url); | ||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
try { | ||
DocumentBuilder db = dbf.newDocumentBuilder(); | ||
Document document = db.parse(url); | ||
NodeList sitemapList = document.getElementsByTagName("url"); | ||
for (int i = 0; i < sitemapList.getLength(); i++) { | ||
Node item = sitemapList.item(i); | ||
NodeList childNodes = item.getChildNodes(); | ||
for (int j = 0; j < childNodes.getLength(); j++) { | ||
String nodeName = childNodes.item(j).getTextContent().trim(); | ||
if ("loc".equalsIgnoreCase(childNodes.item(j).getNodeName())) { | ||
mainWindow.urls.append("\r\n" + nodeName); | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
mainWindow.setLog("\n[!] 发生错误:\r\n" + e.getMessage() + "\r\n如果您认为不是您的错误,请联系开发者:[email protected]。\r\n"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters