Java tutorial
/* * Copyright (C) 2016 normal * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package channellistmaker.dataextractor; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.lang.invoke.MethodHandles; import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.collections4.keyvalue.MultiKey; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.NamedNodeMap; /** * ??EPG XML????? * * @author dosdiaopfhj * @param <T> EPGData?? */ public abstract class AbstractEPGFileExtractor<T extends EpgData> { protected static final Log LOG; static { final Class<?> myClass = MethodHandles.lookup().lookupClass(); LOG = LogFactory.getLog(myClass); } private final Document doc; private final String nodeName; /** * * @param doc ??EPG?XML * @param nodeName ???????? */ public AbstractEPGFileExtractor(Document doc, String nodeName) { this.doc = doc; this.nodeName = nodeName; } /** * trace?????????????? * @param node * @return trace?????????????????????????? */ protected final String dumpNode(final Node node) { if (LOG.isTraceEnabled()) { if (node == null) { //???? return ""; } else { final StringBuilder sb = new StringBuilder(); final String nodeName_t; if (node.getNodeName() != null && !"".equals(node.getNodeName())) { nodeName_t = node.getNodeName(); } else { nodeName_t = "no_Name"; } sb.append(nodeName_t).append(" [ "); /* ? */ sb.append("Node_type = ").append(node.getNodeType()).append(" "); sb.append(" "); /*(??)*/ NamedNodeMap attrs = node.getAttributes(); // NamedNodeMap?? if (attrs != null) { sb.append("Attribute [ "); for (int index = 0; index < attrs.getLength(); index++) { Node attr = attrs.item(index); // sb.append("( "); sb.append("Attribute_name = ").append(attr.getNodeName()).append(" "); // ???? sb.append("Attribute_value = ").append(attr.getNodeValue()).append(" "); // ? sb.append(")"); } sb.append(" ] "); } /* ? sb.append("] "); */ sb.append("Node_value = ").append(node.getNodeValue()).append(" "); /*(??)?? */ if (node.hasChildNodes()) { sb.append(" \n"); NodeList Children = node.getChildNodes(); int Nodes = Children.getLength(); for (int i = 0; i < Nodes; i++) { Node child = Children.item(i); sb.append(dumpNode(child)); } } sb.append("] "); return sb.toString(); } } else { return ""; } } /** * ?????????1?????dump()?????? * * @author dosdiaopfhj * @return ??? * */ public final synchronized Map<MultiKey<Integer>, T> makeMap() { Map<MultiKey<Integer>, T> records = new ConcurrentHashMap<>(); Element root; root = this.doc.getDocumentElement(); NodeList nl = doc.getElementsByTagName(this.nodeName); int Nodes = nl.getLength(); for (int i = 0; i < Nodes; i++) { Node N = nl.item(i); try { T record_val = dump(N); T ret = records.put(record_val.getKeyfields().getMuiltiKey(), record_val); if (ret != null) { LOG.info("????????\n" + ret); } } catch (IllegalArgumentException ex) { LOG.warn("??????????", ex); } } return Collections.unmodifiableMap(records); } /** * ????????1?makeMap()???????????????????? * * @author dosdiaopfhj * @param N ???? * @return 1? */ protected abstract T dump(final Node N) throws IllegalArgumentException; // protected final synchronized void printNode(Node node) { // // System.out.print("?? = " + node.getNodeName() + " "); // ?? // System.out.print(" = " + node.getNodeType() + " "); // // System.out.println(" = " + node.getNodeValue()); // // // } }