Java Code to Detect and Write Sitemap Manually
Here the simple Java code to make a mini application to create sitemap in my static web. I build with JFrame and FileOutputStream for write result to file.
package sitemap.maker;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class SiteMapMaker extends JFrame
{
private JTextField jnama=new JTextField(25);
private JLabel jl = new JLabel();
private String checkIndexPHP(String path,int id,File folder)
{
File[] files = folder.listFiles();
String hasil="";
for (File file : files)
{
if(file.isDirectory()){
hasil+=checkIndexPHP(path+"/"+file.getName(),id++,file);
}else{
if(file.getName().equals("index.php")){
hasil+="<url>\n" +
" <loc>"+path+"</loc>\n" +
"</url>\n";
}
}
}
return hasil;
}
public void makeSitemap()
{
String content="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<urlset\n" +
" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9\n" +
" http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\n";
File folder = new File(System.getProperty("user.dir"));
String namasite="http://www.example.com";
if(!jnama.getText().isEmpty())namasite=jnama.getText();
if(folder.isDirectory()){
content+=checkIndexPHP(namasite,0,folder);
}
try {
//Whatever the file path is.
File statText = new File("sitemap.xml");
FileOutputStream is = new FileOutputStream(statText);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
w.write(content);
w.close();
setFinish();
} catch (IOException e){
System.err.println("Problem writing to the file sitemap.xml");
}
}
public void callThread()
{
jl.setText("Creating sitemap, please wait...");
Thread th=new Thread(new Runnable(){
public void run(){
makeSitemap();
}
});
th.start();
}
public void setFinish()
{
jl.setText("Sitemap created!");
this.pack();
}
public void showFrame()
{
this.setSize(200,80);
this.setLocation(200,200);
JPanel panel=new JPanel();
BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
panel.setLayout(boxLayout);
panel.add(new JLabel("Name Website"));
panel.add(Box.createRigidArea(new Dimension(0,5)));
jnama.setText("http://example.com");
panel.add(jnama);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
JButton b=new JButton("Click Here");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
callThread();
}
});
panel.add(b);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
jl.setText("");
panel.add(jl);
this.add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args){
SiteMapMaker sitemap=new SiteMapMaker();
sitemap.showFrame();
}
}




Comments
Post a Comment