org.apache.fop.util.XMLResourceBundle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.fop Show documentation
Show all versions of org.apache.fop Show documentation
The core maven build properties
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id: XMLResourceBundle.java 1734671 2016-03-12 05:39:53Z gadams $ */
package org.apache.fop.util;
import java.io.IOException;
import java.io.InputStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Stack;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xmlgraphics.util.QName;
/**
* This class is a ResourceBundle that loads its contents from XML files instead of properties
* files (like PropertiesResourceBundle).
*
* The XML format for this resource bundle implementation is the following
* (the same as Apache Cocoon's XMLResourceBundle):
*
* <catalogue xml:lang="en">
* <message key="key1">Message <br/> Value 1</message>
* <message key="key2">Message <br/> Value 1</message>
* ...
* </catalogue>
*
*/
public class XMLResourceBundle extends ResourceBundle {
//Note: Some code here has been copied and adapted from Apache Harmony!
private Properties resources = new Properties();
private Locale locale;
private static SAXTransformerFactory tFactory
= (SAXTransformerFactory)SAXTransformerFactory.newInstance();
/**
* Creates a resource bundle from an InputStream.
* @param in the stream to read from
* @throws IOException if an I/O error occurs
*/
public XMLResourceBundle(InputStream in) throws IOException {
try {
Transformer transformer = tFactory.newTransformer();
StreamSource src = new StreamSource(in);
SAXResult res = new SAXResult(new CatalogueHandler());
transformer.transform(src, res);
} catch (TransformerException e) {
throw new IOException("Error while parsing XML resource bundle: " + e.getMessage());
}
}
/**
* Gets a resource bundle using the specified base name, default locale, and class loader.
* @param baseName the base name of the resource bundle, a fully qualified class name
* @param loader the class loader from which to load the resource bundle
* @return a resource bundle for the given base name and the default locale
* @throws MissingResourceException if no resource bundle for the specified base name can be
* found
* @see java.util.ResourceBundle#getBundle(String)
*/
public static ResourceBundle getXMLBundle(String baseName, ClassLoader loader)
throws MissingResourceException {
return getXMLBundle(baseName, Locale.getDefault(), loader);
}
/**
* Gets a resource bundle using the specified base name, locale, and class loader.
* @param baseName the base name of the resource bundle, a fully qualified class name
* @param locale the locale for which a resource bundle is desired
* @param loader the class loader from which to load the resource bundle
* @return a resource bundle for the given base name and locale
* @throws MissingResourceException if no resource bundle for the specified base name can be
* found
* @see java.util.ResourceBundle#getBundle(String, Locale, ClassLoader)
*/
public static ResourceBundle getXMLBundle(String baseName, Locale locale, ClassLoader loader)
throws MissingResourceException {
if (loader == null) {
throw new NullPointerException("loader must not be null");
}
if (baseName == null) {
throw new NullPointerException("baseName must not be null");
}
assert locale != null;
ResourceBundle bundle;
if (!locale.equals(Locale.getDefault())) {
bundle = handleGetXMLBundle(baseName, "_" + locale, false, loader);
if (bundle != null) {
return bundle;
}
}
bundle = handleGetXMLBundle(baseName, "_" + Locale.getDefault(), true, loader);
if (bundle != null) {
return bundle;
}
throw new MissingResourceException(
baseName + " (" + locale + ")", baseName + '_' + locale, null);
}
static class MissingBundle extends ResourceBundle {
public Enumeration getKeys() {
return null;
}
public Object handleGetObject(String name) {
return null;
}
}
private static final ResourceBundle MISSING = new MissingBundle();
private static final ResourceBundle MISSINGBASE = new MissingBundle();
private static Map cache = new java.util.WeakHashMap();
//
© 2015 - 2025 Weber Informatics LLC | Privacy Policy