?? resource.java
字號(hào):
List values = getAll(key, (d != null) ? d.toString() : null); return Conversion.toClasses(values); } /** * Specifies a named resource to the null value. * * @param key specified resource name */ public void set(String key) { set(key, null); } /** * Specifies a named resource to the provided value. * * @param key specified resource name * @param value specified resource value */ public void set(String key, String value) { synchronized (this.root) { String k = key; Object o = getValue(k); if ((o = getValue(k)) == null) { String l = k; int i = 0; while ((o = getValue(l)) == null && i > -1) { i = l.lastIndexOf(ELEMENT_DELIMITER); l = l.substring(0, i); } if (o == null) { o = this.root; } int j = 0; while (j > -1) { j = k.indexOf(ELEMENT_DELIMITER, i + ELEMENT_DELIMITER.length()); String m = j > -1 ? k.substring(i + ELEMENT_DELIMITER.length(), i) : k.substring(i + ELEMENT_DELIMITER.length()); if (m.startsWith(ATTRIBUTE_DELIMITER)) { ((Element)o).setAttribute(m.substring(ATTRIBUTE_DELIMITER.length()), ""); } else { ((Element)o).appendChild(((Element)o).getOwnerDocument().createElement(m)); } o = getValue(j > -1 ? k.substring(0, j) + m : k); if (j > -1) { i = j; } } } if (o != null) { if (o instanceof Attr) { Attr a = (Attr)o; Element p = a.getOwnerElement(); if (value != null) { try { p.setAttribute(a.getName(), value); } catch (DOMException de) { } } else { try { p.removeAttribute(a.getName()); } catch (DOMException de) { } } } else if (o instanceof Element) { Element e = (Element)o; if (value != null) { try { e.setTextContent(value); } catch (DOMException de) { } } else { try { e.getParentNode().removeChild(e); } catch (DOMException de) { } } } } } } /** * Accessor to a named resource. * * @param key specified resource name * @return resource value as a {@link java.util.List} */ public List getAll(String key) { return getAll(key, null); } /** * Accessor to a named resource. * * @param key specified resource name * @param d specified default value * @return resource value as a {@link java.util.List} or the * specified default value if the named resource value * is not resolvible. */ public List getAll(String key, String d) { return getValues(key, d); } /** * Resource containment validator. * * @param key specified resource name * @return containment test results */ public boolean contains(String key) { return get(key) != null; } /** * Accessor to the {@link java.lang.String} representation of this instance. * * @return {@link java.lang.String} representation of this instance */ public String toString() { StringBuffer sb = new StringBuffer(); if (this.root.hasChildNodes()) { Node n = this.root.getFirstChild(); while (n != null) { if (n instanceof Element) { if (sb.length() > 0) { sb.append(NEW_LINE); } sb.append(toString((Element)n)); } n = n.getNextSibling(); } } return sb.toString(); } private void process(Object resource) throws ResourceNotFoundException { process(resource, null); } private void process(Object resource, Class clazz) throws ResourceNotFoundException { if (!this.resources.contains(resource)) { Element el = null; Exception ex = null; if (resource instanceof URL) { try { el = getRootElement((URL)resource); } catch (SAXException se) { ex = se; } catch (IOException ioe) { ex = ioe; } } else if (resource instanceof String) { if (clazz == null) { clazz = Resource.class; } try { el = getRootElement(clazz.getResourceAsStream((String)resource)); } catch (SAXException se) { ex = se; } catch (IOException ioe) { ex = ioe; } } else if (resource instanceof InputStream) { try { el = getRootElement((InputStream)resource); } catch (SAXException se) { ex = se; } catch (IOException ioe) { ex = ioe; } resource = new String("InputStream." + System.currentTimeMillis()); } if (el != null && ex == null) { load(resource, el); } else { throw new ResourceNotFoundException(EXCEPTION_PREFIX + ": " + resource.toString(), ex); } } } private Element getRootElement(URL resource) throws SAXException, IOException { return getRootElement(resource.openStream()); } private Element getRootElement(InputStream is) throws SAXException, IOException { return getDocumentBuilder().parse(is).getDocumentElement(); } private synchronized DocumentBuilder getDocumentBuilder() { if (this.db == null) { try { this.db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException pce) { } } return this.db; } private void load(Object resource, Element element) { if (resources != null && element != null) { synchronized (lock) { Document d = this.root.getOwnerDocument(); boolean isValid = false; try { this.root.appendChild(d.importNode(element, true)); isValid = true; } catch (DOMException de) { } if (isValid) { this.resources.add(resource); } } } } private String getValue(String key, String d) { String v = null; Object o = getValue(key); if (o != null) { if (o instanceof Attr) { v = ((Attr)o).getValue(); } else if (o instanceof Element) { v = textToString((Element)o); } } return Util.expand(v != null ? v : d); } private Object getValue(String key) { Object o = null; // xxx: key munging, may not work with non-default root elements key = DOT + key; try { o = getXPath().evaluate(key, this.root, XPathConstants.NODE); } catch (XPathExpressionException xpee) { } return o; } private List getValues(String key, String d) { List v = new ArrayList(); Object o = null; for (Iterator vi = getValues(key).iterator(); vi.hasNext(); ) { o = vi.next(); if (o != null) { String nv = null; if (o instanceof Attr) { nv = ((Attr)o).getValue(); } else if (o instanceof Element) { Node n = ((Element)o).getFirstChild(); if (n != null) { try { nv = n.getNodeValue(); } catch (DOMException de) { } } } if (nv != null) { v.add(Util.expand(nv)); } } } if (v.size() == 0 && d != null) { v.add(Util.expand(d)); } return v; } private List getValues(String key) { List v = new ArrayList(); NodeList nl = null; // xxx: key munging, may not work with non-default root elements key = DOT + key; try { nl = (NodeList)getXPath().evaluate(key, this.root, XPathConstants.NODESET); } catch (XPathExpressionException xee) { } for (int i = 0; nl != null && i < nl.getLength(); i++) { v.add((Node)nl.item(i)); } return v; } private synchronized XPath getXPath() { return (this.xp != null ? this.xp : (this.xp = XPathFactory.newInstance().newXPath())); } private Document getRootDocument() { return getRootDocument(true); } private Document getRootDocument(boolean isClone) { Document d = null; if (isClone) { d = getDocumentBuilder().newDocument(); try { d.appendChild(this.root.cloneNode(true)); } catch (DOMException de) { } } else if ((d = this.root.getOwnerDocument()) == null) { d = getDocumentBuilder().newDocument(); try { d.appendChild(this.root); } catch (DOMException de) { } } return d; } private String toString(Element e) { String s = null; Transformer t = null; try { t = TransformerFactory.newInstance().newTransformer(); } catch (TransformerFactoryConfigurationError tfce) { } catch (TransformerConfigurationException tce) { } if (t != null) { StringWriter w = new StringWriter(); try { t.transform(new DOMSource(e), new StreamResult(w)); } catch (TransformerException te) { } s = w.toString(); } return s; } private String textToString(Element e) { StringBuffer sb = new StringBuffer(); NodeList cnl = e.getChildNodes(); for (int i = 0; i < cnl.getLength(); i++) { Node cn = (Node)cnl.item(i); if (cn.getNodeType() == Node.TEXT_NODE || cn.getNodeType() == Node.CDATA_SECTION_NODE) { sb.append(cn.getTextContent().trim()); } } return sb.toString().trim(); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -