作業部屋の使い方を試しています。
HTML属性の取得方法を考える。
| @@ -16,6 +16,9 @@ | ||
| 16 | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 17 | 17 | * MA 02110-1301 USA |
| 18 | 18 | */ |
| 19 | +/* | |
| 20 | + * $Id$ | |
| 21 | + */ | |
| 19 | 22 | |
| 20 | 23 | package Lib; |
| 21 | 24 |
| @@ -32,6 +35,7 @@ | ||
| 32 | 35 | |
| 33 | 36 | public AttributeData() { |
| 34 | 37 | AttrList = new ArrayList(); |
| 38 | + size = 0; | |
| 35 | 39 | } |
| 36 | 40 | |
| 37 | 41 | public void add(HTML.Tag tag, MutableAttributeSet attr) { |
| @@ -47,9 +51,10 @@ | ||
| 47 | 51 | a.tag = tag; |
| 48 | 52 | a.count = tagcount; |
| 49 | 53 | a.attrname = obj.toString(); |
| 50 | - a.attrvalue = (String)attr.getAttribute(obj); | |
| 54 | + a.attrvalue = attr.getAttribute(obj).toString(); | |
| 51 | 55 | |
| 52 | 56 | AttrList.add(a); |
| 57 | + size = AttrList.size(); | |
| 53 | 58 | } |
| 54 | 59 | |
| 55 | 60 | } |
| @@ -112,6 +117,27 @@ | ||
| 112 | 117 | return wkcnt; |
| 113 | 118 | } |
| 114 | 119 | |
| 120 | + // AttrList の内容を返すメソッド | |
| 121 | + public HTML.Tag gettag(int i) { | |
| 122 | + AttrData a = (AttrData)AttrList.get(i); | |
| 123 | + return a.tag; | |
| 124 | + } | |
| 125 | + | |
| 126 | + public int getcount(int i) { | |
| 127 | + AttrData a = (AttrData)AttrList.get(i); | |
| 128 | + return a.count; | |
| 129 | + } | |
| 130 | + | |
| 131 | + public String getattrname(int i) { | |
| 132 | + AttrData a = (AttrData)AttrList.get(i); | |
| 133 | + return a.attrname; | |
| 134 | + } | |
| 135 | + | |
| 136 | + public String getattrvalue(int i) { | |
| 137 | + AttrData a = (AttrData)AttrList.get(i); | |
| 138 | + return a.attrvalue; | |
| 139 | + } | |
| 140 | + | |
| 115 | 141 | // フィールド変数 |
| 116 | 142 | public class AttrData { |
| 117 | 143 | public HTML.Tag tag; |
| @@ -120,5 +146,6 @@ | ||
| 120 | 146 | public String attrvalue; |
| 121 | 147 | } |
| 122 | 148 | public ArrayList AttrList; |
| 149 | + public int size; // AttrListのサイズ | |
| 123 | 150 | |
| 124 | 151 | } |
| @@ -23,7 +23,6 @@ | ||
| 23 | 23 | package Lib; |
| 24 | 24 | |
| 25 | 25 | import java.util.ArrayList; |
| 26 | -import java.util.Enumeration; | |
| 27 | 26 | import java.util.HashMap; |
| 28 | 27 | import javax.swing.text.MutableAttributeSet; |
| 29 | 28 | import javax.swing.text.html.HTML; |
| @@ -36,7 +35,8 @@ | ||
| 36 | 35 | public class HtmlParserCallback extends HTMLEditorKit.ParserCallback { |
| 37 | 36 | |
| 38 | 37 | // デバック情報表示フラグ |
| 39 | - final boolean DEBUG = false; | |
| 38 | + //final boolean DEBUG = false; | |
| 39 | + final boolean DEBUG = true; | |
| 40 | 40 | |
| 41 | 41 | // Tag毎の階層 |
| 42 | 42 | HashMap<HTML.Tag,Integer> tagMap = new HashMap<>(); |
| @@ -56,6 +56,9 @@ | ||
| 56 | 56 | // serach key と一致時のデータ一覧 |
| 57 | 57 | ArrayList sData; |
| 58 | 58 | |
| 59 | + // 属性データ | |
| 60 | + AttributeData attrdata; | |
| 61 | + | |
| 59 | 62 | public HtmlParserCallback(SearchData skey) { |
| 60 | 63 | |
| 61 | 64 | // キー情報展開 |
| @@ -80,24 +83,21 @@ | ||
| 80 | 83 | } |
| 81 | 84 | tagMap.put(tag, count); |
| 82 | 85 | |
| 86 | + // 属性解析 | |
| 87 | + AttributeData handleStartattrdata = new AttributeData(); | |
| 88 | + handleStartattrdata.add(tag, attr); | |
| 89 | + | |
| 83 | 90 | //--- DEBUG OUT ---- start --- |
| 84 | 91 | if(DEBUG) { |
| 85 | 92 | StringBuffer strBuf = new StringBuffer(); |
| 86 | - String ret; | |
| 87 | - | |
| 93 | + // tag情報 | |
| 88 | 94 | strBuf.append(count).append(" : F : ").append(tag.toString()); |
| 89 | - ret = (String)attr.getAttribute(HTML.Attribute.ID); | |
| 90 | - if(ret != null) { | |
| 91 | - strBuf.append(" [ID] ").append(ret); | |
| 95 | + // 属性情報 | |
| 96 | + for(int i = 0; i < handleStartattrdata.size; i++) { | |
| 97 | + strBuf.append(" [").append(handleStartattrdata.getattrname(i)).append("] ") | |
| 98 | + .append(handleStartattrdata.getattrvalue(i)); | |
| 92 | 99 | } |
| 93 | - ret = (String)attr.getAttribute(HTML.Attribute.CLASS); | |
| 94 | - if(ret != null) { | |
| 95 | - strBuf.append(" [CLASS] ").append(ret); | |
| 96 | - } | |
| 97 | - ret = (String)attr.getAttribute(HTML.Attribute.VALUE); | |
| 98 | - if(ret != null) { | |
| 99 | - strBuf.append(" [VALUE] ").append(ret); | |
| 100 | - } | |
| 100 | + // 表示 | |
| 101 | 101 | System.out.println(strBuf); |
| 102 | 102 | } |
| 103 | 103 | //--- DEBUG OUT ---- end --- |
| @@ -109,9 +109,14 @@ | ||
| 109 | 109 | bufTag = tag; |
| 110 | 110 | bufAttr = attr; |
| 111 | 111 | bufText = new StringBuilder(); |
| 112 | + | |
| 113 | + attrdata = new AttributeData(); | |
| 112 | 114 | } |
| 113 | 115 | } |
| 114 | 116 | } |
| 117 | + if(bufCount > 0) { | |
| 118 | + attrdata.add(tag, attr); | |
| 119 | + } | |
| 115 | 120 | } |
| 116 | 121 | |
| 117 | 122 | @Override |
| @@ -124,7 +129,21 @@ | ||
| 124 | 129 | |
| 125 | 130 | //--- DEBUG OUT ---- start --- |
| 126 | 131 | if(DEBUG) { |
| 127 | - System.out.println(count + " : E : " + tag.toString()); | |
| 132 | + if(tag.equals(bufTag) && count <= bufCount) { | |
| 133 | + for(int i = 0; i < attrdata.size; i++) { | |
| 134 | + StringBuffer strBuf = new StringBuffer(); | |
| 135 | + strBuf.append(" Tag-attr : "); | |
| 136 | + strBuf.append(attrdata.gettag(i)).append(" [ "); | |
| 137 | + strBuf.append(attrdata.getcount(i)).append(" ] "); | |
| 138 | + strBuf.append(attrdata.getattrname(i)).append(" = "); | |
| 139 | + strBuf.append(attrdata.getattrvalue(i)); | |
| 140 | + System.out.println(strBuf); | |
| 141 | + } | |
| 142 | + } | |
| 143 | + StringBuffer strBuf = new StringBuffer(); | |
| 144 | + // tag情報 | |
| 145 | + strBuf.append(count).append(" : E : ").append(tag.toString()); | |
| 146 | + System.out.println(strBuf); | |
| 128 | 147 | } |
| 129 | 148 | //--- DEBUG OUT ---- end --- |
| 130 | 149 |
| @@ -174,21 +193,26 @@ | ||
| 174 | 193 | |
| 175 | 194 | @Override |
| 176 | 195 | public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet attr, int pos){ |
| 196 | + | |
| 197 | + if(bufCount > 0) { | |
| 198 | + attrdata.add(tag, attr); | |
| 199 | + } | |
| 200 | + | |
| 177 | 201 | //--- DEBUG OUT ---- start --- |
| 178 | 202 | if(DEBUG) { |
| 203 | + AttributeData simpleattrdata = new AttributeData(); | |
| 204 | + simpleattrdata.add(tag, attr); | |
| 179 | 205 | StringBuffer strBuf = new StringBuffer(); |
| 180 | - String ret; | |
| 206 | + // tag情報 | |
| 181 | 207 | strBuf.append("x : S : ").append(tag.toString()); |
| 182 | - ret = (String)attr.getAttribute(HTML.Attribute.VALUE); | |
| 183 | - if(ret != null) { | |
| 184 | - strBuf.append(" [VALUE] ").append(ret); | |
| 208 | + // 属性情報 | |
| 209 | + for(int i = 0; i < simpleattrdata.size; i++) { | |
| 210 | + strBuf.append(" [").append(simpleattrdata.getattrname(i)).append("] ").append(simpleattrdata.getcount(i)) | |
| 211 | + .append(" = ").append(simpleattrdata.getattrvalue(i)); | |
| 185 | 212 | } |
| 186 | 213 | System.out.println(strBuf); |
| 187 | 214 | } |
| 188 | 215 | //--- DEBUG OUT ---- end --- |
| 189 | - | |
| 190 | - //ParseMutableAttributeSet(attr); | |
| 191 | - | |
| 192 | 216 | } |
| 193 | 217 | |
| 194 | 218 | /** |
| @@ -220,19 +244,4 @@ | ||
| 220 | 244 | |
| 221 | 245 | return false; |
| 222 | 246 | } |
| 223 | - | |
| 224 | - public void ParseMutableAttributeSet(MutableAttributeSet attr) { | |
| 225 | - Enumeration e = attr.getAttributeNames(); | |
| 226 | - while(e.hasMoreElements()) { | |
| 227 | - Object obj = e.nextElement(); | |
| 228 | - String attributename = obj.toString(); | |
| 229 | - System.out.print(attributename + "\t"); | |
| 230 | - | |
| 231 | - String ret = (String)attr.getAttribute(obj); | |
| 232 | - System.out.println(ret); | |
| 233 | - | |
| 234 | - } | |
| 235 | - | |
| 236 | - } | |
| 237 | - | |
| 238 | 247 | } |