作業部屋の使い方を試しています。
(empty log message)
| @@ -32,12 +32,8 @@ | ||
| 32 | 32 | * HTMLパーサ部品. |
| 33 | 33 | * @author kgto |
| 34 | 34 | */ |
| 35 | -public class HtmlParserCallback extends HTMLEditorKit.ParserCallback { | |
| 35 | +class HtmlParserCallback extends HTMLEditorKit.ParserCallback { | |
| 36 | 36 | |
| 37 | - // デバック情報表示フラグ | |
| 38 | - final boolean DEBUG = false; | |
| 39 | - //final boolean DEBUG = true; | |
| 40 | - | |
| 41 | 37 | // Tag毎の階層 |
| 42 | 38 | HashMap<HTML.Tag,Integer> tagMap = new HashMap<>(); |
| 43 | 39 |
| @@ -58,7 +54,7 @@ | ||
| 58 | 54 | // 属性データ |
| 59 | 55 | AttributeData attrdata; |
| 60 | 56 | |
| 61 | - public HtmlParserCallback(SearchData skey) { | |
| 57 | + protected HtmlParserCallback(SearchData skey) { | |
| 62 | 58 | |
| 63 | 59 | // キー情報展開 |
| 64 | 60 | keytag = skey.getHtmltag(); |
| @@ -68,7 +64,7 @@ | ||
| 68 | 64 | sData = new ArrayList(); |
| 69 | 65 | } |
| 70 | 66 | |
| 71 | - public ArrayList getrtnData() { | |
| 67 | + ArrayList getrtnData() { | |
| 72 | 68 | return this.sData; |
| 73 | 69 | } |
| 74 | 70 |
| @@ -86,9 +82,7 @@ | ||
| 86 | 82 | AttributeData handleStartattrdata = new AttributeData(); |
| 87 | 83 | handleStartattrdata.add(tag, attr); |
| 88 | 84 | |
| 89 | - //--- DEBUG OUT ---- start --- | |
| 90 | 85 | DebugProcess.htmlinfo(tag, attr, "handleStartTag", count); |
| 91 | - //--- DEBUG OUT ---- end --- | |
| 92 | 86 | |
| 93 | 87 | if(bufCount == 0) { |
| 94 | 88 | if(tag.toString().equals(keytag)) { |
| @@ -114,9 +108,7 @@ | ||
| 114 | 108 | count = tagMap.get(tag); |
| 115 | 109 | } |
| 116 | 110 | |
| 117 | - //--- DEBUG OUT ---- start --- | |
| 118 | 111 | DebugProcess.htmlinfo(tag, null, "handleEndTag", count); |
| 119 | - //--- DEBUG OUT ---- end --- | |
| 120 | 112 | |
| 121 | 113 | if(tag.equals(bufTag) && count <= bufCount) { |
| 122 | 114 |
| @@ -135,11 +127,10 @@ | ||
| 135 | 127 | |
| 136 | 128 | @Override |
| 137 | 129 | public void handleText(char[] data, int pos){ |
| 138 | - //--- DEBUG OUT ---- start --- | |
| 130 | + | |
| 139 | 131 | DebugProcess.htmlinfo(data, "handleText"); |
| 140 | - //--- DEBUG OUT ---- end --- | |
| 132 | + | |
| 141 | 133 | String splitchar = "\t"; |
| 142 | - | |
| 143 | 134 | //制御文字の削除 |
| 144 | 135 | // 0xa0 |
| 145 | 136 | StringBuilder buf = new StringBuilder(); |
| @@ -148,7 +139,6 @@ | ||
| 148 | 139 | buf.append(data[i]); |
| 149 | 140 | } |
| 150 | 141 | } |
| 151 | - | |
| 152 | 142 | if(bufCount > 0) { |
| 153 | 143 | if(bufText.length() > 0) { |
| 154 | 144 | bufText.append(splitchar); |
| @@ -155,19 +145,14 @@ | ||
| 155 | 145 | } |
| 156 | 146 | bufText.append(buf.toString()); |
| 157 | 147 | } |
| 158 | - | |
| 159 | 148 | } |
| 160 | 149 | |
| 161 | 150 | @Override |
| 162 | 151 | public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet attr, int pos){ |
| 163 | - | |
| 164 | 152 | if(bufCount > 0) { |
| 165 | 153 | attrdata.add(tag, attr); |
| 166 | 154 | } |
| 167 | - | |
| 168 | - //--- DEBUG OUT ---- start --- | |
| 169 | 155 | DebugProcess.htmlinfo(tag, attr, "handleSimpleTag", 0); |
| 170 | - //--- DEBUG OUT ---- end --- | |
| 171 | 156 | } |
| 172 | 157 | |
| 173 | 158 | /** |
| @@ -175,7 +160,7 @@ | ||
| 175 | 160 | * @param attr ページのMutableAttributeSet |
| 176 | 161 | * @return boolean 検索キーと一致の時、true |
| 177 | 162 | */ |
| 178 | - public boolean serachAttribute(MutableAttributeSet attr) { | |
| 163 | + boolean serachAttribute(MutableAttributeSet attr) { | |
| 179 | 164 | String currentID = (String)attr.getAttribute(HTML.Attribute.ID); |
| 180 | 165 | String currentClass = (String)attr.getAttribute(HTML.Attribute.CLASS); |
| 181 | 166 |
| @@ -206,7 +191,7 @@ | ||
| 206 | 191 | * @param attrdata |
| 207 | 192 | * @return boolean 検索キーと一致の時、true |
| 208 | 193 | */ |
| 209 | - public boolean serachAttribute(HTML.Tag tag, AttributeData attrdata) { | |
| 194 | + boolean serachAttribute(HTML.Tag tag, AttributeData attrdata) { | |
| 210 | 195 | // ID と CLASS の両方にキー入力有りの場合 |
| 211 | 196 | if(keyid.isEmpty() == false && keyclass.isEmpty() == false) { |
| 212 | 197 | if(attrdata.searchId(tag, keyid) && attrdata.searchClass(tag, keyclass)) { |
| @@ -34,46 +34,68 @@ | ||
| 34 | 34 | import javax.swing.text.html.HTML; |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | - * | |
| 37 | + * デバック情報. | |
| 38 | + * カレントディレクトリに設定ファイル(Debug.prop)を置くことで、デバックログの出力を制御する。 | |
| 38 | 39 | * @author kgto |
| 39 | 40 | */ |
| 40 | 41 | public class DebugProcess { |
| 41 | - | |
| 42 | + // 設定ファイル名 | |
| 42 | 43 | protected static final String configurationFilename = "Debug.prop"; |
| 44 | + // ロガー名 | |
| 43 | 45 | protected static final Logger logger = Logger.getLogger("WebScraping"); |
| 46 | + // ログ出力デフォルトレベル | |
| 47 | + protected static final Level loggerlevel = Level.FINEST; | |
| 44 | 48 | |
| 45 | 49 | |
| 50 | + /** | |
| 51 | + * ログ出力設定. | |
| 52 | + * ログ設定ファイルの存在をチェック、(最終的な)ログレベルにより、 | |
| 53 | + * ファイルハンドラの設定と出力書式の設定を行う。 | |
| 54 | + */ | |
| 46 | 55 | public static void debuglog_set() { |
| 47 | 56 | try { |
| 48 | 57 | initLogConfiguration(); |
| 49 | 58 | |
| 50 | 59 | if(Level.ALL.equals(logger.getLevel())) { |
| 51 | - logger.addHandler(new FileHandler("WebScraping%g.log", 100000, 2)); | |
| 60 | + //logger.addHandler(new FileHandler("WebScraping%g.log", 100000, 2)); | |
| 61 | + logger.addHandler(new FileHandler("WebScraping%g.log")); | |
| 52 | 62 | } |
| 53 | 63 | setFomatter(); |
| 54 | 64 | |
| 55 | - } catch (IOException ex) { | |
| 65 | + } catch (IOException | SecurityException ex) { | |
| 56 | 66 | Logger.getLogger(DebugProcess.class.getName()).log(Level.SEVERE, null, ex); |
| 57 | - } catch (SecurityException ex) { | |
| 58 | - Logger.getLogger(DebugProcess.class.getName()).log(Level.SEVERE, null, ex); | |
| 59 | 67 | } |
| 60 | - | |
| 61 | 68 | } |
| 62 | 69 | |
| 70 | + /** | |
| 71 | + * ログ出力設定解除. | |
| 72 | + */ | |
| 63 | 73 | public static void debuglog_unset() { |
| 64 | - | |
| 65 | 74 | } |
| 66 | 75 | |
| 67 | 76 | |
| 68 | - | |
| 77 | + /** | |
| 78 | + * デバック出力(HTML解析-タグ&属性). | |
| 79 | + * HTMLのタグと属性の解析状態を出力する。 | |
| 80 | + * 書式: 9 : x : タグ名 [属性名]属性数 = 属性値<br> | |
| 81 | + * 凡例: 9 = 階層レベル(count値), x = F(tagの開始)/E(tagの終了)/S(単独tag)の何れか1文字<br> | |
| 82 | + * @param tag タグ | |
| 83 | + * @param attr 属性 | |
| 84 | + * @param methodname このメソッドを呼び出した親メソッド名 | |
| 85 | + * @param count HTMLタグの階層レベル | |
| 86 | + */ | |
| 69 | 87 | public static void htmlinfo(HTML.Tag tag, MutableAttributeSet attr, |
| 70 | 88 | String methodname, int count) { |
| 71 | 89 | |
| 72 | - if(!Level.ALL.equals(logger.getLevel())) { | |
| 90 | + // ログ出力レベルチェック | |
| 91 | + if(logger.getLevel() == null) { | |
| 73 | 92 | return; |
| 74 | 93 | } |
| 94 | + if(logger.getLevel().intValue() > loggerlevel.intValue()) { | |
| 95 | + return; | |
| 96 | + } | |
| 75 | 97 | |
| 76 | - | |
| 98 | + // 編集処理 | |
| 77 | 99 | char kbn = ' '; |
| 78 | 100 | if("handleStartTag".equals(methodname)) { |
| 79 | 101 | kbn = 'F'; |
| @@ -105,27 +127,43 @@ | ||
| 105 | 127 | } |
| 106 | 128 | } |
| 107 | 129 | |
| 108 | - logger.finest(strBuf.toString()); | |
| 130 | + logger.log(loggerlevel, strBuf.toString()); | |
| 109 | 131 | } |
| 110 | 132 | |
| 133 | + /** | |
| 134 | + * デバック出力(メッセージ). | |
| 135 | + * 引数に渡された任意のメッセージを出力する。 | |
| 136 | + * @param str メッセージ | |
| 137 | + * @param methodname このメソッドを呼び出した親メソッド名 | |
| 138 | + */ | |
| 111 | 139 | public static void htmlinfo(String str, String methodname) { |
| 112 | - logger.finest(str); | |
| 140 | + logger.log(loggerlevel, str); | |
| 113 | 141 | } |
| 114 | 142 | |
| 115 | 143 | public static void htmlinfo(String str) { |
| 116 | - logger.finest(str); | |
| 144 | + logger.log(loggerlevel, str); | |
| 117 | 145 | } |
| 118 | 146 | |
| 147 | + /** | |
| 148 | + * デバック出力(HTML解析-本文). | |
| 149 | + * 本文の内容を出力する。 | |
| 150 | + * @param data 本文(HTML内の文字列) | |
| 151 | + * @param methodname このメソッドを呼び出した親メソッド名 | |
| 152 | + */ | |
| 119 | 153 | public static void htmlinfo(char[] data, String methodname) { |
| 120 | 154 | String dat = new String(data); |
| 121 | - logger.finest(dat); | |
| 155 | + logger.log(loggerlevel, dat); | |
| 122 | 156 | } |
| 123 | 157 | |
| 124 | 158 | public static void htmlinfo(char[] data) { |
| 125 | 159 | String dat = new String(data); |
| 126 | - logger.finest(dat); | |
| 160 | + logger.log(loggerlevel, dat); | |
| 127 | 161 | } |
| 128 | 162 | |
| 163 | + /** | |
| 164 | + * ログ出力設定ファイルチェック. | |
| 165 | + * 設定ファイルの存在をチェックし存在する場合、設定ファイルの内容を設定する。 | |
| 166 | + */ | |
| 129 | 167 | private static void initLogConfiguration() { |
| 130 | 168 | |
| 131 | 169 | File file = new File(configurationFilename); |
| @@ -143,6 +181,10 @@ | ||
| 143 | 181 | } |
| 144 | 182 | } |
| 145 | 183 | |
| 184 | + /** | |
| 185 | + * ログ出力フォーマッター設定. | |
| 186 | + * ファイルへログ出力時の書式を設定する。 | |
| 187 | + */ | |
| 146 | 188 | private static void setFomatter() { |
| 147 | 189 | Handler[] handlers = logger.getHandlers(); |
| 148 | 190 | for(int i = 0 ; i < handlers.length ; i++) { |
| @@ -154,12 +196,20 @@ | ||
| 154 | 196 | |
| 155 | 197 | } |
| 156 | 198 | |
| 199 | +/** | |
| 200 | + * ログ出力フォーマッター. | |
| 201 | + * @author kgto | |
| 202 | + */ | |
| 157 | 203 | class HtmlFormatter extends Formatter { |
| 158 | - | |
| 204 | + /** | |
| 205 | + * Logの出力文字列を生成する。 | |
| 206 | + * 出力書式:<br> | |
| 207 | + * YYYY-MM-DD HH:SS:MM ログレベル<メソッド名>メッセージ | |
| 208 | + */ | |
| 159 | 209 | @Override |
| 160 | 210 | public synchronized String format(final LogRecord aRecord) { |
| 161 | 211 | |
| 162 | - final StringBuffer message = new StringBuffer(131); | |
| 212 | + final StringBuffer message = new StringBuffer(100); | |
| 163 | 213 | |
| 164 | 214 | long millis = aRecord.getMillis(); |
| 165 | 215 | String time = String.format("%tF %<tT", millis); |
| @@ -176,6 +226,7 @@ | ||
| 176 | 226 | message.append(formatMessage(aRecord)); |
| 177 | 227 | message.append('\n'); |
| 178 | 228 | |
| 229 | + // 例外エラーの場合、エラー内容とスタックトレース出力 | |
| 179 | 230 | Throwable throwable = aRecord.getThrown(); |
| 180 | 231 | if (throwable != null) { |
| 181 | 232 | message.append(throwable.toString()); |
| @@ -187,7 +238,5 @@ | ||
| 187 | 238 | } |
| 188 | 239 | } |
| 189 | 240 | return message.toString(); |
| 190 | - | |
| 191 | 241 | } |
| 192 | - | |
| 193 | 242 | } |
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | |
| 22 | 22 | System.out.println("LoggerName : " + logger.getName()); |
| 23 | 23 | System.out.println("LoggerLevel : " + logger.getLevel()); |
| 24 | + System.out.println("Parent : " + logger.getParent().getName()); | |
| 24 | 25 | |
| 25 | 26 | Handler[] handlers = logger.getHandlers(); |
| 26 | 27 | for(int i = 0 ; i < handlers.length ; i++) { |