giacomo 2003/01/10 12:47:40 Modified: src/java/org/apache/cocoon/i18n XMLResourceBundle.java Log: some code formatting Revision Changes Path 1.14 +167 -141 xml-cocoon2/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java Index: XMLResourceBundle.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/i18n/XMLResourceBundle.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -d -b -u -r1.13 -r1.14 --- XMLResourceBundle.java 7 Jan 2003 23:56:12 -0000 1.13 +++ XMLResourceBundle.java 10 Jan 2003 20:47:40 -0000 1.14 @@ -1,36 +1,27 @@ /* - ============================================================================ The Apache Software License, Version 1.1 ============================================================================ - Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. - Redistribution and use in source and binary forms, with or without modifica- tion, 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 the 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 Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. - 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] - 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. - 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 @@ -41,13 +32,11 @@ 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 the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. - -*/ + */ package org.apache.cocoon.i18n; import java.io.IOException; @@ -63,11 +52,12 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import org.apache.avalon.excalibur.xml.xpath.XPathProcessor; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.logger.Logger; +import org.apache.avalon.excalibur.xml.xpath.XPathProcessor; + import org.apache.excalibur.source.Source; import org.apache.excalibur.source.SourceResolver; @@ -75,25 +65,26 @@ import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; + import org.xml.sax.InputSource; import org.xml.sax.SAXException; + /** - * Implementation of <code>Bundle</code> interface for XML resources. - * Represents a single XML message bundle. + * Implementation of <code>Bundle</code> interface for XML resources. Represents a single XML message bundle. * * @author <a href="mailto:[EMAIL PROTECTED]">Mike Engelhart</a> * @author <a href="mailto:[EMAIL PROTECTED]">Neeme Praks</a> * @author <a href="mailto:[EMAIL PROTECTED]">Oleg Podolsky</a> * @author <a href="mailto:[EMAIL PROTECTED]">Matthieu Sozeau</a> - * @author <a href="mailto:[EMAIL PROTECTED]">Konstantin Piroumian</a> - * $Id$ + * @author <a href="mailto:[EMAIL PROTECTED]">Konstantin Piroumian</a> $Id: XMLResourceBundle.java,v 1.13 + * 2003/01/07 23:56:12 giacomo Exp $ */ -public class XMLResourceBundle extends ResourceBundle implements Bundle { - +public class XMLResourceBundle + extends ResourceBundle + implements Bundle { /** DOM factory */ - protected static DocumentBuilderFactory docfactory = - DocumentBuilderFactory.newInstance(); + protected static DocumentBuilderFactory docfactory = DocumentBuilderFactory.newInstance(); /** Cache for storing string values for existing XPaths */ private Hashtable cache = new Hashtable(); @@ -123,9 +114,14 @@ private XPathProcessor processor = null; /** + * Compose this instance + * + * @param manager The <code>ComponentManager</code> instance + * * @throws ComponentException if XPath processor is not found */ - public void compose(ComponentManager manager) throws ComponentException { + public void compose(ComponentManager manager) + throws ComponentException { this.manager = manager; this.processor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE); } @@ -156,21 +152,22 @@ * @param locale locale * @param parent parent bundle of this bundle * @param cacheAtStartup cache all the keys when constructing? + * * @throws IOException if an IO error occurs while reading the file * @throws ParserConfigurationException if no parser is configured * @throws SAXException if an error occurs while parsing the file */ - public void init(String name, String fileName, Locale locale, - XMLResourceBundle parent, boolean cacheAtStartup) + public void init(String name, String fileName, Locale locale, XMLResourceBundle parent, boolean cacheAtStartup) throws IOException, ParserConfigurationException, SAXException { - - logger.debug("Constructing XMLResourceBundle: " - + name + ", locale: " + locale); + if (logger.isDebugEnabled()) { + logger.debug("Constructing XMLResourceBundle: " + name + ", locale: " + locale); + } this.name = name; this.doc = loadResourceBundle(fileName); this.locale = locale; this.parent = parent; + if (cacheAtStartup) { Node root = doc.getDocumentElement(); cacheAll(root, "/" + root.getNodeName()); @@ -181,33 +178,36 @@ * Load the DOM tree, based on the file name. * * @param fileName name of the XML source file + * * @return the DOM tree + * * @exception IOException if an IO error occurs while reading the file * @exception ParserConfigurationException if no parser is configured * @exception SAXException if an error occurs while parsing the file */ protected synchronized Document loadResourceBundle(String fileName) throws IOException, ParserConfigurationException, SAXException { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Source source = null; SourceResolver resolver = null; + try { resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE); source = resolver.resolveURI(fileName); + return builder.parse(new InputSource(source.getInputStream())); - } - catch (Exception e) { + } catch (Exception e) { logger.warn("XMLResourceBundle: Non excalibur-source " + fileName, e); - } - finally { + } finally { resolver.release(source); manager.release(resolver); } + // Fallback try return builder.parse(fileName); } + /** * Gets the name of the bundle. * @@ -239,11 +239,14 @@ * Does the "key-cache" contain the value with such key? * * @param key the key to the value to be returned + * * @return true if contains, false otherwise */ private boolean cacheContains(String key) { - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug(name + ": cache contains: " + key); + } + return cache.containsKey(key); } @@ -251,11 +254,13 @@ * Does the "key-not-found-cache" contain such key? * * @param key the key to the value to be returned + * * @return true if contains, false otherwise */ private boolean cacheNotFoundContains(String key) { - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug(name + ": cache_not_found contains: " + key); + } return cacheNotFound.containsKey(key); } @@ -267,8 +272,10 @@ * @param value the value */ private void cacheKey(String key, Node value) { - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug(name + ": caching: " + key + " = " + value.toString()); + } + cache.put(key, value); } @@ -278,8 +285,9 @@ * @param key the key */ private void cacheNotFoundKey(String key) { - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug(name + ": caching not_found: " + key); + } cacheNotFound.put(key, ""); } @@ -288,88 +296,103 @@ * Gets the value by the key from the "key-cache". * * @param key the key + * * @return the value */ private Node getFromCache(String key) { Object value; - if (logger.isDebugEnabled()) + + if (logger.isDebugEnabled()) { logger.debug(name + ": returning from cache: " + key); + } value = cache.get(key); - if(value == null) + + if (value == null) { logger.debug("Could not find!"); + } - return (Node) value; + return (Node)value; } /** - * Steps through the bundle tree and stores all text element values - * in bundle's cache. Also stores attributes for all element nodes. + * Steps through the bundle tree and stores all text element values in bundle's cache. Also stores attributes for + * all element nodes. * * @param parent parent node, must be an element * @param pathToParent XPath to the parent node */ private void cacheAll(Node parent, String pathToParent) { - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Caching all messages"); + } NodeList children = parent.getChildNodes(); int childnum = children.getLength(); - for(int i = 0; i < childnum; i++) { + for (int i = 0; i < childnum; i++) { Node child = children.item(i); - if(child.getNodeType() == Node.ELEMENT_NODE) { - StringBuffer pathToChild = new StringBuffer(pathToParent) - .append('/').append(child.getNodeName()); + if (child.getNodeType() == Node.ELEMENT_NODE) { + StringBuffer pathToChild = new StringBuffer(pathToParent).append('/').append(child.getNodeName()); NamedNodeMap attrs = child.getAttributes(); - if(attrs != null) { + if (attrs != null) { Node temp = null; String pathToAttr = null; int attrnum = attrs.getLength(); - for(int j = 0; j < attrnum; j++) { + + for (int j = 0; j < attrnum; j++) { temp = attrs.item(j); - if (!temp.getNodeName().equalsIgnoreCase("xml:lang")) - pathToChild.append("[@").append(temp.getNodeName()) - .append("='").append(temp.getNodeValue()) + + if (!temp.getNodeName().equalsIgnoreCase("xml:lang")) { + pathToChild.append("[@").append(temp.getNodeName()).append("='").append(temp.getNodeValue()) .append("']"); } } + } cacheKey(pathToChild.toString(), child); } - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("What we've cached: " + child.toString()); } } + } /** * Get value by key. * * @param key the key + * * @return the value */ private Object _getObject(String key) { - if (key == null) return null; + if (key == null) { + return null; + } Node value = getFromCache(key); - if (value == null && !cacheNotFoundContains(key)) { - if (doc != null) + + if ((value == null) && !cacheNotFoundContains(key)) { + if (doc != null) { value = (Node)_getObject(this.doc.getDocumentElement(), key); + } if (value == null) { - if (this.parent != null) + if (this.parent != null) { value = (Node)this.parent._getObject(key); } + } - if (value != null) + if (value != null) { cacheKey(key, value); - else + } else { cacheNotFoundKey(key); } + } return value; } @@ -379,6 +402,7 @@ * * @param node the node * @param key the key + * * @return the value */ private Object _getObject(Node node, String key) { @@ -389,6 +413,7 @@ * Get the node with the supplied XPath key. * * @param key the key + * * @return the node */ private Node _getNode(String key) { @@ -396,11 +421,11 @@ } /** - * Get the node with the supplied XPath key, starting from concrete - * root node. + * Get the node with the supplied XPath key, starting from concrete root node. * * @param rootNode the root node * @param key the key + * * @return the node */ private Node _getNode(Node rootNode, String key) { @@ -409,25 +434,26 @@ } catch (Exception e) { logger.error("Error while locating resource with key: " + key, e); } + return null; } /** - * Return an Object by key. - * Implementation of the ResourceBundle abstract method. + * Return an Object by key. Implementation of the ResourceBundle abstract method. * * @param key the key + * * @return the object + * + * @throws MissingResourceException on error */ protected Object handleGetObject(String key) throws MissingResourceException { - return _getObject(key); } /** - * Return an enumeration of the keys. - * Implementation of the ResourceBundle abstract method. + * Return an enumeration of the keys. Implementation of the ResourceBundle abstract method. * * @return the enumeration of keys */
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]