?? resource.java
字號:
/* * Copyright (c) 2001 Sun Microsystems, Inc. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and thproe following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Sun Microsystems, Inc. for Project JXTA." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" * must not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", * nor may "JXTA" appear in their name, without prior written * permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA. For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. * * $Id: Resource.java,v 1.30 2006/09/22 22:48:53 gonzo Exp $ */package net.jxta.ext.config;import java.awt.Color;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.InputStream;import java.io.StringWriter;import java.io.IOException;import java.net.MalformedURLException;import java.net.URI;import java.net.URL;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.xml.namespace.QName;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactoryConfigurationError;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathExpression;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathFactory;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.DOMException;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Text;import org.xml.sax.SAXException;/** * XML resource accessor utility. * * @author james todd [gonzo at jxta dot org] */public class Resource { private final static String ROOT = "ROOT"; private final static String ELEMENT_DELIMITER = "/"; private final static String ATTRIBUTE_DELIMITER = "@"; private final static String DOT = "."; private final static String FILE_SEPERATOR = System.getProperty("file.separator", "/"); private final static char NEW_LINE = '\n'; private final static String EXCEPTION_PREFIX = "unable to find resource: "; private final static Object lock = new Object(); private List resources = null; private Element root = null; private DocumentBuilder db = null; private XPath xp = null; /** * Convenience {@link java.net.URL} conversion method. * * @param s specified {@link java.net.URL} in the form of a * {@link java.lang.String} * @return {@link java.net.URL} representaion of the specified * parameter */ public static URL toURL(String s) throws ConversionException { return Conversion.toURL(s); } /** * Convenience {@link java.net.URL} conversion method. * * @param f specified {@link java.net.URL} in the form of a * {@link java.io.File} * @return {@link java.net.URL} representation of the specified * parameter. */ public static URL toURL(File f) throws ConversionException { return Conversion.toURL(f); } /** * Default constructor. */ public Resource() { this(ROOT); } /** * Constructor which overrides the default backing document root. * * @param root root element name */ public Resource(String root) { this.resources = new ArrayList(); try { this.root = getDocumentBuilder().newDocument().createElement(root); } catch (DOMException de) { } } /** * Loads the specified resource. * * @param resource resource {@link java.net.URL} address */ public void load(URL resource) throws ResourceNotFoundException { process(resource); } /** * Loads the specified resource. * * <p>If the specified resource fails to convert to a {@link java.net.URL} * a series of stream instantiation will be attempted. * * @param resource specified resource * @throws {@link net.jxta.ext.config.ResourceNotFoundException} thrown if the specified * resource is not resolvable */ public void load(String resource) throws ResourceNotFoundException { Exception e = null; try { load(getClass().getResource(resource)); } catch (ResourceNotFoundException rnfe) { e = rnfe; } if (e != null) { try { process(resource); } catch (ResourceNotFoundException rnfe) { if (!resource.startsWith(FILE_SEPERATOR)) { process(FILE_SEPERATOR + resource); } } } } /** * Loads the specified resource. * * @param resource specified resource as a {@link java.io.File} * @throws {@link net.jxta.ext.config.ResourceNotFoundException} thrown if the specified resource * is not resolvable. * @throws {@link java.net.MalformedURLException} thrown if the specified * resource can not be converted * to a {@link java.net.URL} */ public void load(File resource) throws ResourceNotFoundException, MalformedURLException { load(resource.toURL()); } /** * Loads the specified resource relative to the provided {@link java.lang.Class} * * @param resource specified resource * @param clazz {@link java.lang.Class} used to load the resource * @throws {@link net.jxta.ext.config.ResourceNotFoundException} thrown if the specified * resource is not resolvable. */ public void load(String resource, Class clazz) throws ResourceNotFoundException { process(resource, clazz); } /** * Loads the specified resource. * * @param is resource specified as a {@link java.io.InputStream} * @throws {@link net.jxta.ext.config.ResourceNotFoundException} thrown if the specified * resource is not resolvable. */ public void load(InputStream is) throws ResourceNotFoundException { process(is); } /** * Accessor for the named resource as a {@link java.io.InputStream} * * @param resource name of the resource * @return {@link java.io.InputStream} a {@link java.io.InputStream} * for the specified resource. */ public InputStream getResourceAsStream(String resource) { return getResourceAsStream(resource, null); } /** * Accessor for the named resource as a {@link java.io.InputStream} * * @param resource name of the resource * @param clazz {@link java.lang.Class} used to load the resource. */ public InputStream getResourceAsStream(String resource, Class clazz) { if (clazz == null) { clazz = Resource.class; } return clazz.getResourceAsStream(resource); } /** * Accessor for the named resource. * * @param key specified resource name * @return resource value as a {@link java.lang.String} or null * if the named resource is not resolvable */ public String get(String key) { return get(key, null); } /** * Accessor for the named resource. * * @param key specified resource name * @param d specified default value * @return resource value as a {@link java.lang.String} or the * specified default value if the named resource is not * resolvable */ public String get(String key, String d) { return getValue(key, d); } /** * Accessor for the named resource. * * @param key specified resource name * @return resource value as an int * @throws {link ConversionException} thrown in the event the * named resource is not convertible to an integer or * is not resolvable. */ public int getInt(String key) throws ConversionException { return getInt(key, null); } /** * Accessor for the named resource. * * @param key specified resource name * @param d specified default value * @return resource value as an int or the specified default * value if the named resource is not resolvable. */ public int getInt(String key, int d) { int i = 0; try { i = getInt(key, new Integer(d)); } catch (ConversionException ce) {} return i; } /** * Accessor for the named rsource. * * @param key specified resource name * @param d specified default value * @return resource value as an int or the specified default * value if the named resource is not resolvable. * @throws {@link net.jxta.ext.config.ConverstionException} thrown in the event the named * resource is not convertible to * an int or is not resolvable. */ public int getInt(String key, Integer d) throws ConversionException { String s = get(key, (d != null) ? d.toString() : null); return Conversion.toInt(s); } /** * Accessor for the named resource. * * @param key specified resource name * @return resource value as a long * @exception {@link net.jxta.ext.config.ConverstionException} thrown in the event the named * resource is not convertible to * a long or is not resolvable. */ public long getLong(String key) throws ConversionException { return getLong(key, null); } /** * Accessor for the named resource. * * @param key specified resource name * @param d specified default value * @return resource value as a long or the specified default * value if the named resource is not covertible to a * long or is not resovlable. */ public long getLong(String key, long d) { long i = 0; try { i = getLong(key, new Long(d)); } catch (ConversionException ce) {} return i; } /** * Accessor for the named resource. * * @param key specified resource name * @param d specified default value * @return resource value as a long or the specified default * value if the named resource is not covertible to a * long or is not resovlable. */ public long getLong(String key, Long d) throws ConversionException { String s = get(key, (d != null) ? d.toString() : null); return Conversion.toLong(s); } /** * Accessor for a named resource. *
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -