📄 xmlparser.java
字号:
if (pid != null) entity = (URL) _redirectMap.get(pid); if (entity == null) entity = (URL) _redirectMap.get(sid); if (entity == null) { String dtd = sid; if (dtd.lastIndexOf('/') >= 0) dtd = dtd.substring(dtd.lastIndexOf('/') + 1); if (Log.isDebugEnabled()) Log.debug("Can't exact match entity in redirect map, trying " + dtd); entity = (URL) _redirectMap.get(dtd); } if (entity != null) { try { InputStream in = entity.openStream(); if (Log.isDebugEnabled()) Log.debug("Redirected entity " + sid + " --> " + entity); InputSource is = new InputSource(in); is.setSystemId(sid); return is; } catch (IOException e) { Log.ignore(e); } } return null; } } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /** * XML Attribute. */ public static class Attribute { private String _name; private String _value; Attribute(String n, String v) { _name = n; _value = v; } public String getName() { return _name; } public String getValue() { return _value; } } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /** * XML Node. Represents an XML element with optional attributes and ordered content. */ public static class Node extends AbstractList { Node _parent; private ArrayList _list; private String _tag; private Attribute[] _attrs; private boolean _lastString = false; private String _path; /* ------------------------------------------------------------ */ Node(Node parent, String tag, Attributes attrs) { _parent = parent; _tag = tag; if (attrs != null) { _attrs = new Attribute[attrs.getLength()]; for (int i = 0; i < attrs.getLength(); i++) { String name = attrs.getLocalName(i); if (name == null || name.equals("")) name = attrs.getQName(i); _attrs[i] = new Attribute(name, attrs.getValue(i)); } } } /* ------------------------------------------------------------ */ public Node getParent() { return _parent; } /* ------------------------------------------------------------ */ public String getTag() { return _tag; } /* ------------------------------------------------------------ */ public String getPath() { if (_path == null) { if (getParent() != null && getParent().getTag() != null) _path = getParent().getPath() + "/" + _tag; else _path = "/" + _tag; } return _path; } /* ------------------------------------------------------------ */ /** * Get an array of element attributes. */ public Attribute[] getAttributes() { return _attrs; } /* ------------------------------------------------------------ */ /** * Get an element attribute. * * @return attribute or null. */ public String getAttribute(String name) { return getAttribute(name, null); } /* ------------------------------------------------------------ */ /** * Get an element attribute. * * @return attribute or null. */ public String getAttribute(String name, String dft) { if (_attrs == null || name == null) return dft; for (int i = 0; i < _attrs.length; i++) if (name.equals(_attrs[i].getName())) return _attrs[i].getValue(); return dft; } /* ------------------------------------------------------------ */ /** * Get the number of children nodes. */ public int size() { if (_list != null) return _list.size(); return 0; } /* ------------------------------------------------------------ */ /** * Get the ith child node or content. * * @return Node or String. */ public Object get(int i) { if (_list != null) return _list.get(i); return null; } /* ------------------------------------------------------------ */ /** * Get the first child node with the tag. * * @param tag * @return Node or null. */ public Node get(String tag) { if (_list != null) { for (int i = 0; i < _list.size(); i++) { Object o = _list.get(i); if (o instanceof Node) { Node n = (Node) o; if (tag.equals(n._tag)) return n; } } } return null; } /* ------------------------------------------------------------ */ public void add(int i, Object o) { if (_list == null) _list = new ArrayList(); if (o instanceof String) { if (_lastString) { int last = _list.size() - 1; _list.set(last, (String) _list.get(last) + o); } else _list.add(i, o); _lastString = true; } else { _lastString = false; _list.add(i, o); } } /* ------------------------------------------------------------ */ public void clear() { if (_list != null) _list.clear(); _list = null; } /* ------------------------------------------------------------ */ /** * Get a tag as a string. * * @param tag The tag to get * @param tags IF true, tags are included in the value. * @param trim If true, trim the value. * @return results of get(tag).toString(tags). */ public String getString(String tag, boolean tags, boolean trim) { Node node = get(tag); if (node == null) return null; String s = node.toString(tags); if (s != null && trim) s = s.trim(); return s; } /* ------------------------------------------------------------ */ public synchronized String toString() { return toString(true); } /* ------------------------------------------------------------ */ /** * Convert to a string. * * @param tag If false, only _content is shown. */ public synchronized String toString(boolean tag) { StringBuffer buf = new StringBuffer(); synchronized (buf) { toString(buf, tag); return buf.toString(); } } /* ------------------------------------------------------------ */ /** * Convert to a string. * * @param tag If false, only _content is shown. */ public synchronized String toString(boolean tag, boolean trim) { String s = toString(tag); if (s != null && trim) s = s.trim(); return s; } /* ------------------------------------------------------------ */ private synchronized void toString(StringBuffer buf, boolean tag) { if (tag) { buf.append("<"); buf.append(_tag); if (_attrs != null) { for (int i = 0; i < _attrs.length; i++) { buf.append(' '); buf.append(_attrs[i].getName()); buf.append("=\""); buf.append(_attrs[i].getValue()); buf.append("\""); } } } if (_list != null) { if (tag) buf.append(">"); for (int i = 0; i < _list.size(); i++) { Object o = _list.get(i); if (o == null) continue; if (o instanceof Node) ((Node) o).toString(buf, tag); else buf.append(o.toString()); } if (tag) { buf.append("</"); buf.append(_tag); buf.append(">"); } } else if (tag) buf.append("/>"); } /* ------------------------------------------------------------ */ /** * Iterator over named child nodes. * * @param tag The tag of the nodes. * @return Iterator over all child nodes with the specified tag. */ public Iterator iterator(final String tag) { return new Iterator() { int c = 0; Node _node; /* -------------------------------------------------- */ public boolean hasNext() { if (_node != null) return true; while (_list != null && c < _list.size()) { Object o = _list.get(c); if (o instanceof Node) { Node n = (Node) o; if (tag.equals(n._tag)) { _node = n; return true; } } c++; } return false; } /* -------------------------------------------------- */ public Object next() { try { if (hasNext()) return _node; throw new NoSuchElementException(); } finally { _node = null; c++; } } /* -------------------------------------------------- */ public void remove() { throw new UnsupportedOperationException("Not supported"); } }; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -