作業部屋の使い方を試しています。
HtmlParser仮付
| @@ -0,0 +1,179 @@ | ||
| 1 | +/* | |
| 2 | + * To change this license header, choose License Headers in Project Properties. | |
| 3 | + * To change this template file, choose Tools | Templates | |
| 4 | + * and open the template in the editor. | |
| 5 | + */ | |
| 6 | + | |
| 7 | +package test1; | |
| 8 | + | |
| 9 | +import java.util.ArrayList; | |
| 10 | +import java.util.HashMap; | |
| 11 | +import javax.swing.text.MutableAttributeSet; | |
| 12 | +import javax.swing.text.html.HTML; | |
| 13 | +import javax.swing.text.html.HTMLEditorKit; | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * | |
| 17 | + * @author kgto | |
| 18 | + */ | |
| 19 | +public class HtmlParserCallback extends HTMLEditorKit.ParserCallback { | |
| 20 | + | |
| 21 | + // Tag毎の階層 | |
| 22 | + HashMap<HTML.Tag,Integer> tagMap = new HashMap<>(); | |
| 23 | + | |
| 24 | + // serach key 情報 | |
| 25 | + String keytag; | |
| 26 | + String keyid; | |
| 27 | + String keyclass; | |
| 28 | + | |
| 29 | + // serach key と一致時の情報退避 | |
| 30 | + int bufCount = 0; | |
| 31 | + HTML.Tag bufTag = null; | |
| 32 | + MutableAttributeSet bufAttr = null; | |
| 33 | + // serach key と一致時の情報格納ワーク | |
| 34 | + StringBuilder bufText; | |
| 35 | + | |
| 36 | + // serach key と一致時のデータ一覧 | |
| 37 | + ArrayList sData; | |
| 38 | + | |
| 39 | + public HtmlParserCallback(serachData skey) { | |
| 40 | + | |
| 41 | + // キー情報展開 | |
| 42 | + keytag = skey.getHtmltag(); | |
| 43 | + keyid = skey.getHtmlid(); | |
| 44 | + keyclass = skey.getHtmlclass(); | |
| 45 | + | |
| 46 | + sData = new ArrayList(); | |
| 47 | + } | |
| 48 | + | |
| 49 | + public ArrayList getrtnData() { | |
| 50 | + return this.sData; | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Override | |
| 54 | + public void handleStartTag(HTML.Tag tag, MutableAttributeSet attr, int pos){ | |
| 55 | + // Tag毎の階層を保持 | |
| 56 | + int count = 1; | |
| 57 | + if(tagMap.containsKey(tag)) { | |
| 58 | + count = tagMap.get(tag); | |
| 59 | + count++; | |
| 60 | + } | |
| 61 | + tagMap.put(tag, count); | |
| 62 | + | |
| 63 | + //--- DEBUG OUT ---- start --- | |
| 64 | + StringBuffer strBuf = new StringBuffer(); | |
| 65 | + String ret; | |
| 66 | + | |
| 67 | + strBuf.append(count).append(" : F : ").append(tag.toString()); | |
| 68 | + ret = (String)attr.getAttribute(HTML.Attribute.ID); | |
| 69 | + if(ret != null) { | |
| 70 | + strBuf.append(" [ID] ").append(ret); | |
| 71 | + } | |
| 72 | + ret = (String)attr.getAttribute(HTML.Attribute.CLASS); | |
| 73 | + if(ret != null) { | |
| 74 | + strBuf.append(" [CLASS] ").append(ret); | |
| 75 | + } | |
| 76 | + ret = (String)attr.getAttribute(HTML.Attribute.VALUE); | |
| 77 | + if(ret != null) { | |
| 78 | + strBuf.append(" [VALUE] ").append(ret); | |
| 79 | + } | |
| 80 | + System.out.println(strBuf); | |
| 81 | + //--- DEBUG OUT ---- end --- | |
| 82 | + | |
| 83 | + if(bufCount == 0) { | |
| 84 | + if(tag.toString().equals(keytag)) { | |
| 85 | + if(serachAttribute(attr)) { | |
| 86 | + bufCount = count; | |
| 87 | + bufTag = tag; | |
| 88 | + bufAttr = attr; | |
| 89 | + bufText = new StringBuilder(); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + @Override | |
| 96 | + public void handleEndTag(HTML.Tag tag, int pos){ | |
| 97 | + // Tag毎の階層を取得 | |
| 98 | + int count = 0; | |
| 99 | + if(tagMap.containsKey(tag)) { | |
| 100 | + count = tagMap.get(tag); | |
| 101 | + } | |
| 102 | + | |
| 103 | + //--- DEBUG OUT ---- start --- | |
| 104 | + System.out.println(count + " : E : " + tag.toString()); | |
| 105 | + //--- DEBUG OUT ---- end --- | |
| 106 | + | |
| 107 | + //if(tag.equals(bufSerchTag) && bufSerchCount >= count) { | |
| 108 | + | |
| 109 | + if(tag.equals(bufTag) && count <= bufCount) { | |
| 110 | + | |
| 111 | + // 溜め込んだ一致情報をリストへ格納 | |
| 112 | + sData.add(bufText.toString()); | |
| 113 | + | |
| 114 | + // 退避したserach keyとの一致情報クリア | |
| 115 | + bufCount = 0; | |
| 116 | + bufTag = null; | |
| 117 | + bufAttr = null; | |
| 118 | + bufText = null; | |
| 119 | + } | |
| 120 | + | |
| 121 | + // Tag毎の階層減算 | |
| 122 | + tagMap.put(tag, --count); | |
| 123 | + } | |
| 124 | + | |
| 125 | + @Override | |
| 126 | + public void handleText(char[] data, int pos){ | |
| 127 | + | |
| 128 | + //--- DEBUG OUT ---- start --- | |
| 129 | + String dat = new String(data); | |
| 130 | + System.out.println(dat); | |
| 131 | + //--- DEBUG OUT ---- end --- | |
| 132 | + | |
| 133 | + if(bufCount > 0) { | |
| 134 | + bufText.append(new String(data)).append(" | "); | |
| 135 | + } | |
| 136 | + | |
| 137 | + } | |
| 138 | + | |
| 139 | + @Override | |
| 140 | + public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet attr, int pos){ | |
| 141 | + //--- DEBUG OUT ---- start --- | |
| 142 | + StringBuffer strBuf = new StringBuffer(); | |
| 143 | + String ret; | |
| 144 | + | |
| 145 | + strBuf.append("x : S : ").append(tag.toString()); | |
| 146 | + ret = (String)attr.getAttribute(HTML.Attribute.VALUE); | |
| 147 | + if(ret != null) { | |
| 148 | + strBuf.append(" [VALUE] ").append(ret); | |
| 149 | + } | |
| 150 | + System.out.println(strBuf); | |
| 151 | + //--- DEBUG OUT ---- end --- | |
| 152 | + } | |
| 153 | + | |
| 154 | + public boolean serachAttribute(MutableAttributeSet attr) { | |
| 155 | + String currentID = (String)attr.getAttribute(HTML.Attribute.ID); | |
| 156 | + String currentClass = (String)attr.getAttribute(HTML.Attribute.CLASS); | |
| 157 | + | |
| 158 | + if(keyid.isEmpty() == false && keyclass.isEmpty() == false) { | |
| 159 | + if(keyid.equals(currentID) && keyclass.equals(currentClass)) { | |
| 160 | + return true; | |
| 161 | + } | |
| 162 | + } | |
| 163 | + | |
| 164 | + if(keyid.isEmpty() == false) { | |
| 165 | + if(keyid.equals(currentID)) { | |
| 166 | + return true; | |
| 167 | + } | |
| 168 | + } | |
| 169 | + | |
| 170 | + if(keyclass.isEmpty() == false) { | |
| 171 | + if(keyclass.equals(currentClass)) { | |
| 172 | + return true; | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 176 | + return false; | |
| 177 | + } | |
| 178 | + | |
| 179 | +} |
| @@ -0,0 +1,92 @@ | ||
| 1 | +/* | |
| 2 | + * To change this license header, choose License Headers in Project Properties. | |
| 3 | + * To change this template file, choose Tools | Templates | |
| 4 | + * and open the template in the editor. | |
| 5 | + */ | |
| 6 | + | |
| 7 | +package test1; | |
| 8 | + | |
| 9 | +import java.io.*; | |
| 10 | +import java.net.*; | |
| 11 | +import java.util.ArrayList; | |
| 12 | +import javax.swing.text.html.parser.ParserDelegator; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * | |
| 16 | + * @author kgto | |
| 17 | + */ | |
| 18 | +public class HtmlParser { | |
| 19 | + | |
| 20 | + String UrlAdress; | |
| 21 | + String pageData; | |
| 22 | + | |
| 23 | + ArrayList sData; | |
| 24 | + | |
| 25 | + public HtmlParser() { | |
| 26 | + UrlAdress = null; | |
| 27 | + } | |
| 28 | + | |
| 29 | + public HtmlParser(String UrlAdress) { | |
| 30 | + this.UrlAdress = UrlAdress; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public void seturl(String UrlAdress) { | |
| 34 | + this.UrlAdress = UrlAdress; | |
| 35 | + } | |
| 36 | + | |
| 37 | + public String search(serachData skey) { | |
| 38 | + | |
| 39 | + String htmltag = skey.getHtmltag(); | |
| 40 | + String htmlid = skey.getHtmlid(); | |
| 41 | + String htmlclass = skey.getHtmlclass(); | |
| 42 | + | |
| 43 | + getpageData(); | |
| 44 | + serchpageData(skey); | |
| 45 | + | |
| 46 | + if(sData.size() > 0) { | |
| 47 | + String rtn = (String)sData.get(0); | |
| 48 | + return rtn; | |
| 49 | + } | |
| 50 | + | |
| 51 | + return null; | |
| 52 | + } | |
| 53 | + | |
| 54 | + | |
| 55 | + public void getpageData() { | |
| 56 | + try { | |
| 57 | + URL url = new URL(UrlAdress); | |
| 58 | + HttpURLConnection con = (HttpURLConnection)url.openConnection(); | |
| 59 | + con.setRequestMethod("GET"); | |
| 60 | + BufferedReader reader = new BufferedReader( | |
| 61 | + new InputStreamReader(con.getInputStream(), "utf-8")); | |
| 62 | + String wkline; | |
| 63 | + StringBuilder sb = new StringBuilder(); | |
| 64 | + while((wkline = reader.readLine()) != null) { | |
| 65 | + sb.append(wkline).append("\n"); | |
| 66 | + } | |
| 67 | + pageData = sb.toString(); | |
| 68 | + | |
| 69 | + con.disconnect(); | |
| 70 | + } | |
| 71 | + catch(IOException e) { | |
| 72 | + System.err.println(e); | |
| 73 | + } | |
| 74 | + } | |
| 75 | + | |
| 76 | + public void serchpageData(serachData skey){ | |
| 77 | + Reader reader; | |
| 78 | + try { | |
| 79 | + reader = new BufferedReader(new StringReader(pageData)); | |
| 80 | + HtmlParserCallback cb = new HtmlParserCallback(skey); | |
| 81 | + ParserDelegator pd = new ParserDelegator(); | |
| 82 | + pd.parse(reader, cb, true); | |
| 83 | + reader.close(); | |
| 84 | + | |
| 85 | + sData = cb.getrtnData(); | |
| 86 | + | |
| 87 | + } catch (IOException e) { | |
| 88 | + System.err.println(e); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + | |
| 92 | +} |
| @@ -53,6 +53,11 @@ | ||
| 53 | 53 | jTable1 = new javax.swing.JTable(); |
| 54 | 54 | jBtnRowIns = new javax.swing.JButton(); |
| 55 | 55 | jBtnRowDel = new javax.swing.JButton(); |
| 56 | + jLabel1 = new javax.swing.JLabel(); | |
| 57 | + jTxtUrl = new javax.swing.JTextField(); | |
| 58 | + jPanel2 = new javax.swing.JPanel(); | |
| 59 | + jScrollPane2 = new javax.swing.JScrollPane(); | |
| 60 | + jTextArea1 = new javax.swing.JTextArea(); | |
| 56 | 61 | jMenuBar1 = new javax.swing.JMenuBar(); |
| 57 | 62 | jMenu1 = new javax.swing.JMenu(); |
| 58 | 63 | jMenuLoad = new javax.swing.JMenuItem(); |
| @@ -65,6 +70,8 @@ | ||
| 65 | 70 | |
| 66 | 71 | setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); |
| 67 | 72 | |
| 73 | + jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("検索情報")); | |
| 74 | + | |
| 68 | 75 | jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); |
| 69 | 76 | jTable1.getTableHeader().setReorderingAllowed(false); |
| 70 | 77 |
| @@ -130,6 +137,25 @@ | ||
| 130 | 137 | .addComponent(jBtnRowIns))) |
| 131 | 138 | ); |
| 132 | 139 | |
| 140 | + jLabel1.setText("URL:"); | |
| 141 | + | |
| 142 | + jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("検索結果")); | |
| 143 | + | |
| 144 | + jTextArea1.setColumns(20); | |
| 145 | + jTextArea1.setRows(5); | |
| 146 | + jScrollPane2.setViewportView(jTextArea1); | |
| 147 | + | |
| 148 | + javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); | |
| 149 | + jPanel2.setLayout(jPanel2Layout); | |
| 150 | + jPanel2Layout.setHorizontalGroup( | |
| 151 | + jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| 152 | + .addComponent(jScrollPane2) | |
| 153 | + ); | |
| 154 | + jPanel2Layout.setVerticalGroup( | |
| 155 | + jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| 156 | + .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE) | |
| 157 | + ); | |
| 158 | + | |
| 133 | 159 | jMenu1.setText("ファイル"); |
| 134 | 160 | |
| 135 | 161 | jMenuLoad.setText("LOAD"); |
| @@ -150,7 +176,17 @@ | ||
| 150 | 176 | |
| 151 | 177 | jMenuBar1.add(jMenu1); |
| 152 | 178 | |
| 153 | - jMenu2.setText("Edit"); | |
| 179 | + jMenu2.setText("検索"); | |
| 180 | + jMenu2.addMouseListener(new java.awt.event.MouseAdapter() { | |
| 181 | + public void mouseClicked(java.awt.event.MouseEvent evt) { | |
| 182 | + jMenu2MouseClicked(evt); | |
| 183 | + } | |
| 184 | + }); | |
| 185 | + jMenu2.addActionListener(new java.awt.event.ActionListener() { | |
| 186 | + public void actionPerformed(java.awt.event.ActionEvent evt) { | |
| 187 | + jMenu2ActionPerformed(evt); | |
| 188 | + } | |
| 189 | + }); | |
| 154 | 190 | jMenuBar1.add(jMenu2); |
| 155 | 191 | |
| 156 | 192 | setJMenuBar(jMenuBar1); |
| @@ -159,15 +195,25 @@ | ||
| 159 | 195 | getContentPane().setLayout(layout); |
| 160 | 196 | layout.setHorizontalGroup( |
| 161 | 197 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 198 | + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
| 199 | + .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
| 200 | + .addGap(0, 0, Short.MAX_VALUE)) | |
| 162 | 201 | .addGroup(layout.createSequentialGroup() |
| 163 | - .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
| 164 | - .addGap(0, 10, Short.MAX_VALUE)) | |
| 202 | + .addComponent(jLabel1) | |
| 203 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
| 204 | + .addComponent(jTxtUrl)) | |
| 205 | + .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
| 165 | 206 | ); |
| 166 | 207 | layout.setVerticalGroup( |
| 167 | 208 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 168 | 209 | .addGroup(layout.createSequentialGroup() |
| 210 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
| 211 | + .addComponent(jLabel1) | |
| 212 | + .addComponent(jTxtUrl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
| 213 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
| 169 | 214 | .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) |
| 170 | - .addGap(0, 30, Short.MAX_VALUE)) | |
| 215 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
| 216 | + .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
| 171 | 217 | ); |
| 172 | 218 | |
| 173 | 219 | bindingGroup.bind(); |
| @@ -211,6 +257,32 @@ | ||
| 211 | 257 | } |
| 212 | 258 | }//GEN-LAST:event_jMenuSaveActionPerformed |
| 213 | 259 | |
| 260 | + private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu2ActionPerformed | |
| 261 | + // TODO add your handling code here: | |
| 262 | + | |
| 263 | + System.out.println("jMenu2ActionPerformed"); | |
| 264 | + | |
| 265 | + serachData sdat = (serachData)slist.get(0); | |
| 266 | + | |
| 267 | + HtmlParser par = new HtmlParser(jTxtUrl.getText()); | |
| 268 | + String rtn = par.search(sdat); | |
| 269 | +// par.seturl(url); | |
| 270 | +// par.setserchkey(key); | |
| 271 | + | |
| 272 | + }//GEN-LAST:event_jMenu2ActionPerformed | |
| 273 | + | |
| 274 | + private void jMenu2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu2MouseClicked | |
| 275 | + // TODO add your handling code here: | |
| 276 | + System.out.println("jMenu2MouseClicked"); | |
| 277 | + | |
| 278 | + serachData sdat = (serachData)slist.get(0); | |
| 279 | + | |
| 280 | + HtmlParser par = new HtmlParser(jTxtUrl.getText()); | |
| 281 | + String rtn = par.search(sdat); | |
| 282 | + | |
| 283 | + jTextArea1.setText(rtn); | |
| 284 | + }//GEN-LAST:event_jMenu2MouseClicked | |
| 285 | + | |
| 214 | 286 | /** |
| 215 | 287 | * @param args the command line arguments |
| 216 | 288 | */ |
| @@ -250,6 +322,7 @@ | ||
| 250 | 322 | private javax.swing.JButton jBtnRowDel; |
| 251 | 323 | private javax.swing.JButton jBtnRowIns; |
| 252 | 324 | private javax.swing.JFileChooser jFileChooser1; |
| 325 | + private javax.swing.JLabel jLabel1; | |
| 253 | 326 | private javax.swing.JMenu jMenu1; |
| 254 | 327 | private javax.swing.JMenu jMenu2; |
| 255 | 328 | private javax.swing.JMenuBar jMenuBar1; |
| @@ -256,8 +329,12 @@ | ||
| 256 | 329 | private javax.swing.JMenuItem jMenuLoad; |
| 257 | 330 | private javax.swing.JMenuItem jMenuSave; |
| 258 | 331 | private javax.swing.JPanel jPanel1; |
| 332 | + private javax.swing.JPanel jPanel2; | |
| 259 | 333 | private javax.swing.JScrollPane jScrollPane1; |
| 334 | + private javax.swing.JScrollPane jScrollPane2; | |
| 260 | 335 | private javax.swing.JTable jTable1; |
| 336 | + private javax.swing.JTextArea jTextArea1; | |
| 337 | + private javax.swing.JTextField jTxtUrl; | |
| 261 | 338 | private org.jdesktop.beansbinding.BindingGroup bindingGroup; |
| 262 | 339 | // End of variables declaration//GEN-END:variables |
| 263 | 340 | } |