Java編の続きである。
どうせならGUIをしてみよう、という事でeclipseを使いJava+SWINGでRSS取り込みをやってみた。もちろんXMLライブラリも使っている。
使い方は実行するだけ。アドレス欄にURLをいれて「GO」をぽちればいい。
//import javax.swing.SwingUtilities; import javax.swing.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import java.awt.*; import java.io.*; import java.net.*; import java.util.regex.*; public class swingtest extends JFrame { private static Pattern pattern; private static Matcher matcher; private static final long serialVersionUID = 1L; private static final int FRAME_WIDTH = 640; private static final int FRAME_HEIGHT = 560; private static final int P1_WIDTH = 634; private static final int P1_HEIGHT = 25; private static final int P2_WIDTH = 634; private static final int P2_HEIGHT = 440; private static final int P3_WIDTH = 634; private static final int P3_HEIGHT = 25; private JPanel panel1; private JPanel panel2; private JPanel panel3; private JButton button1; private JTextField inputtext; //private JTextArea textArea; private JTextPane textArea; private JRadioButton radio1; private JRadioButton radio2; private JRadioButton radio3; private JLabel report1; private ButtonGroup group; // @jve:decl-index=0: private static String tmpfile = "tempfile.69"; /** * @param args */ public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ SwingUtilities.invokeLater(new Runnable() { public void run() { swingtest thisClass = new swingtest(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); } /** * This is the default constructor */ public swingtest() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(FRAME_WIDTH,FRAME_HEIGHT); this.setTitle("SWINGテスト"); this.setVisible(true); // make Container Container container = getContentPane(); container.setLayout(null); container.setBounds(0, 50, FRAME_WIDTH, FRAME_HEIGHT); // Panelを生成(送信ボタンとURL入力フィールド) panel1 = new JPanel(null); //panel1 = new JPanel(new GridLayout(1,1)); panel1.setBounds(0, 0, P1_WIDTH, P1_HEIGHT); // make Button; button1 = new JButton("GO"); //button1.setSize(100,25); button1.setBounds(0,0,100,20); button1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent e) { String r = inputtext.getText(); if((r.equals("") == false)&&(r.equals("http://") == false)){ report1.setText("取得:"+r); String x = getWeb(r,"UTF-8"); //String x = getWeb(r,"JISAutoDetect"); fileWrite(tmpfile, x); x = getXml(tmpfile); textArea.setText(x); //System.out.println(x); }else{ report1.setText("URLを打ち込んで[GO]を押すべし"); } } }); panel1.add(button1); // make textfield inputtext = new JTextField("http://www.pheedo.jp/f/slashdot_japan"); inputtext.setBounds(105, 0, 520, 22); panel1.add(inputtext); // Containerにpanel1を仕込む container.add(panel1); // Panel2を生成(リターン結果を表示する) panel2 = new JPanel(null); panel2.setBounds(0, P1_HEIGHT+2, P2_WIDTH, P2_HEIGHT); // make TextArea; // private JTextArea textArea; //textArea = new JTextArea("(`・ω・´) シャキーン"); //textArea.setVisible(true); //textArea.setLineWrap(true); // make TextArea; // private JTextArea textArea; textArea = new JTextPane(); textArea.setText("(`・ω・´) シャキーン"); textArea.setVisible(true); // make textArea (JEditorpane) //textArea = new JEditorPane(); //textArea.setFont((Font)UIManager.get("inputtext.font")); //textArea.setContentType("text/html"); //textArea.setText("<p>:D</p>"); //textArea.setEditable(true); // textAreaをスクロールバーつきでパネル2に張り込む。 JScrollPane scrollview = new JScrollPane(textArea); scrollview.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollview.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollview.setVisible(true); scrollview.setBounds(panel2.getVisibleRect()); //panel2.add(textArea); panel2.add(scrollview); // Containerにpanel2を仕込む container.add(panel2); // Panel3を生成 panel3 = new JPanel(null); //panel3.setBounds(P1_WIDTH+2, 0, P3_WIDTH, P3_HEIGHT); panel3.setBounds(0, P1_HEIGHT+2+P2_HEIGHT+2, P3_WIDTH, P3_HEIGHT); // Radio Button group = new ButtonGroup(); radio1 = new JRadioButton("UTF-8"); radio1.setSelected(true); radio2 = new JRadioButton("EUC_JP"); radio3 = new JRadioButton("SJIS"); report1 = new JLabel("¥e"); report1.setBackground(Color.white); report1.setForeground(Color.red); radio1.setBounds(0,0,70,20); radio2.setBounds(70,0,70,20); radio3.setBounds(140,0,70,20); report1.setBounds(210,0,420,20); group.add(radio1); group.add(radio2); group.add(radio3); panel3.add(radio1); panel3.add(radio2); panel3.add(radio3); panel3.add(report1); radio1.setVisible(false); radio2.setVisible(false); radio3.setVisible(false); report1.setVisible(true); panel3.setVisible(true); container.add(panel3); container.setVisible(true); } // マッチングルーチン static Boolean existStr(String seq,String k){ pattern = Pattern.compile(k); matcher = pattern.matcher(seq); return(matcher.matches()); } // マッチングルーチン(Replace) static String replaceStr(String seq,String k,String m){ String r = seq; pattern = Pattern.compile(k); matcher = pattern.matcher(seq); r = matcher.replaceAll(m); return(r); } static void fileWrite(String fn,String seq){ try { FileOutputStream fos = new FileOutputStream(fn); OutputStreamWriter osw = new OutputStreamWriter(fos , "UTF-8"); BufferedWriter bw = new BufferedWriter(osw); String msg = seq; bw.write(msg); bw.close(); osw.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } static String getXml(String filename){ String r = ""; try { // ドキュメントビルダーファクトリを生成 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); // ドキュメントビルダーを生成 DocumentBuilder builder = dbfactory.newDocumentBuilder(); // パースを実行してDocumentオブジェクトを取得 Document doc = builder.parse(new File(filename)); //Document doc = builder.parse(xml2); //StringReader sr = new StringReader(xml2); //Document doc = builder.parse(new InputString(sr)); // ルート要素を取得(タグ名:site) Element root = doc.getDocumentElement(); System.out.println("ルート要素のタグ名:" + root.getTagName()); // page要素のリストを取得 NodeList list = root.getElementsByTagName("item"); System.out.println("要素数: "+list.getLength()); // page要素の数だけループ int j=1; for (int i=0; i < list.getLength() ; i++) { // page要素を取得 Element element = (Element)list.item(i); // id属性の値を取得 //System.out.println(element.getAttribute("title")); //String id = element.getAttribute("id"); // title要素のリストを取得 NodeList titleList = element.getElementsByTagName("title"); // title要素を取得 Element titleElement = (Element)titleList.item(0); // title要素の最初の子ノード(テキストノード)の値を取得 String title = titleElement.getFirstChild().getNodeValue(); if(existStr(title,".*AD:.*") == false){ // link要素のリストを取得 NodeList linkList = element.getElementsByTagName("link"); Element linkElement = (Element)linkList.item(0); String link = linkElement.getFirstChild().getNodeValue(); // description要素のリストを取得 NodeList descList = element.getElementsByTagName("description"); Element descElement = (Element)descList.item(0); String description = descElement.getFirstChild().getNodeValue(); // file要素を取得 //Element fileElement = (Element)fileList.item(0); // file要素の最初の子ノード(テキストノード)の値を取得 //String file = fileElement.getFirstChild().getNodeValue(); description = replaceStr(description,"\\<.*",""); description = replaceStr(description,"[\r\n]",""); if(description.length()>128){ description = description.substring(0,120)+"…(つづく)"; } r = r + j + ":"; r = r + "『" + title + "』\n\t" + link; if(description.equals("") == false){ r = r + "\n" + description + "\n\n"; } //r += "<dl>"; //r = r + "<dt>"+j+"</dt>"; //r = r + // "<dd>タイトル:" + title + "</dd>" + // "<dd>URL: " + link + "</dd>" + // "<dd>記事:" + description + "</dd></dl>\n"; j++; } } } catch (Exception e) { e.printStackTrace(); } return(r); } static String getWeb(String url,String ucode){ String r=""; try { URL TestURL = new URL(url); URLConnection con = TestURL.openConnection(); InputStreamReader ir1 = new InputStreamReader(con.getInputStream(),ucode); BufferedReader br1 = new BufferedReader(ir1); String line; while((line=br1.readLine()) != null){ r = r + line + "\n"; } br1.close(); ir1.close(); } catch(Exception e) { e.printStackTrace(); r = ""; } return(r); } } // @jve:decl-index=0:visual-constraint="10,10"