mrglavas 2004/02/05 13:30:09 Modified: java/src/org/apache/xerces/parsers AbstractSAXParser.java java/src/org/apache/xerces/jaxp SAXParserImpl.java Added: java/src/org/apache/xerces/impl/msg SAXMessages.properties java/src/org/apache/xerces/util SAXMessageFormatter.java Log: Finally... Localizing messages for SAX. Created a new message formatter for SAX messages and replaced all the clear text with message keys. Revision Changes Path 1.48 +47 -43 xml-xerces/java/src/org/apache/xerces/parsers/AbstractSAXParser.java Index: AbstractSAXParser.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/AbstractSAXParser.java,v retrieving revision 1.47 retrieving revision 1.48 diff -u -r1.47 -r1.48 --- AbstractSAXParser.java 3 Feb 2004 23:18:53 -0000 1.47 +++ AbstractSAXParser.java 5 Feb 2004 21:30:09 -0000 1.48 @@ -64,6 +64,7 @@ import org.apache.xerces.xs.PSVIProvider; import org.apache.xerces.util.EntityResolverWrapper; import org.apache.xerces.util.ErrorHandlerWrapper; +import org.apache.xerces.util.SAXMessageFormatter; import org.apache.xerces.util.SymbolHash; import org.apache.xerces.xni.Augmentations; import org.apache.xerces.xni.NamespaceContext; @@ -1479,10 +1480,9 @@ if (suffixLength == Constants.STRING_INTERNING_FEATURE.length() && featureId.endsWith(Constants.STRING_INTERNING_FEATURE)) { if (!state) { - // REVISIT: Localize this error message. -Ac throw new SAXNotSupportedException( - "PAR018 " + state + " state for feature \"" + featureId - + "\" is not supported.\n" + state + '\t' + featureId); + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "false-not-supported", new Object [] {featureId})); } return; } @@ -1512,12 +1512,16 @@ fConfiguration.setFeature(featureId, state); } catch (XMLConfigurationException e) { - String message = e.getMessage(); + String identifier = e.getIdentifier(); if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { - throw new SAXNotRecognizedException(message); + throw new SAXNotRecognizedException( + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "feature-not-recognized", new Object [] {identifier})); } else { - throw new SAXNotSupportedException(message); + throw new SAXNotSupportedException( + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "feature-not-supported", new Object [] {identifier})); } } @@ -1588,12 +1592,16 @@ return fConfiguration.getFeature(featureId); } catch (XMLConfigurationException e) { - String message = e.getMessage(); + String identifier = e.getIdentifier(); if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { - throw new SAXNotRecognizedException(message); + throw new SAXNotRecognizedException( + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "feature-not-recognized", new Object [] {identifier})); } else { - throw new SAXNotSupportedException(message); + throw new SAXNotSupportedException( + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "feature-not-supported", new Object [] {identifier})); } } @@ -1637,12 +1645,9 @@ setLexicalHandler((LexicalHandler)value); } catch (ClassCastException e) { - // REVISIT: Localize this error message. -ac throw new SAXNotSupportedException( - "PAR012 For propertyID \"" - +propertyId+"\", the value \"" - +value+"\" cannot be cast to LexicalHandler." - +'\n'+propertyId+'\t'+value+"\tLexicalHandler"); + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "incompatible-class", new Object [] {propertyId, "org.xml.sax.ext.LexicalHandler"})); } return; } @@ -1658,13 +1663,9 @@ setDeclHandler((DeclHandler)value); } catch (ClassCastException e) { - // REVISIT: Localize this error message. -ac throw new SAXNotSupportedException( - "PAR012 For propertyID \"" - +propertyId+"\", the value \"" - +value+"\" cannot be cast to DeclHandler." - +'\n'+propertyId+'\t'+value+"\tDeclHandler" - ); + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "incompatible-class", new Object [] {propertyId, "org.xml.sax.ext.DeclHandler"})); } return; } @@ -1680,11 +1681,9 @@ // if (suffixLength == Constants.DOM_NODE_PROPERTY.length() && propertyId.endsWith(Constants.DOM_NODE_PROPERTY)) { - // REVISIT: Localize this error message. -ac throw new SAXNotSupportedException( - "PAR013 Property \""+propertyId+"\" is read only." - +'\n'+propertyId - ); // read-only property + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "property-read-only", new Object [] {propertyId})); } // // Drop through and perform default processing @@ -1710,12 +1709,16 @@ fConfiguration.setProperty(propertyId, value); } catch (XMLConfigurationException e) { - String message = e.getMessage(); + String identifier = e.getIdentifier(); if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { - throw new SAXNotRecognizedException(message); + throw new SAXNotRecognizedException( + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "property-not-recognized", new Object [] {identifier})); } else { - throw new SAXNotSupportedException(message); + throw new SAXNotSupportedException( + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "property-not-supported", new Object [] {identifier})); } } @@ -1778,11 +1781,10 @@ // if (suffixLength == Constants.DOM_NODE_PROPERTY.length() && propertyId.endsWith(Constants.DOM_NODE_PROPERTY)) { - // REVISIT: Localize this error message. -Ac + // we are not iterating a DOM tree throw new SAXNotSupportedException( - "PAR014 Cannot getProperty(\""+propertyId - +"\". No DOM Tree exists.\n"+propertyId - ); // we are not iterating a DOM tree + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "dom-node-read-not-supported", null)); } // // Drop through and perform default processing @@ -1808,12 +1810,16 @@ return fConfiguration.getProperty(propertyId); } catch (XMLConfigurationException e) { - String message = e.getMessage(); + String identifier = e.getIdentifier(); if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { - throw new SAXNotRecognizedException(message); + throw new SAXNotRecognizedException( + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "property-not-recognized", new Object [] {identifier})); } else { - throw new SAXNotSupportedException(message); + throw new SAXNotSupportedException( + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "property-not-supported", new Object [] {identifier})); } } @@ -1842,11 +1848,10 @@ throws SAXNotRecognizedException, SAXNotSupportedException { if (fParseInProgress) { - // REVISIT: Localize this error message. -Ac throw new SAXNotSupportedException( - "PAR011 Feature: http://xml.org/sax/properties/declaration-handler" - +" is not supported during parse." - +"\nhttp://xml.org/sax/properties/declaration-handler"); + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "property-not-parsing-supported", + new Object [] {"http://xml.org/sax/properties/declaration-handler"})); } fDeclHandler = handler; @@ -1879,11 +1884,10 @@ throws SAXNotRecognizedException, SAXNotSupportedException { if (fParseInProgress) { - // REVISIT: Localize this error message. -Ac throw new SAXNotSupportedException( - "PAR011 Feature: http://xml.org/sax/properties/lexical-handler" - +" is not supported during parse." - +"\nhttp://xml.org/sax/properties/lexical-handler"); + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "property-not-parsing-supported", + new Object [] {"http://xml.org/sax/properties/lexical-handler"})); } fLexicalHandler = handler; 1.22 +28 -20 xml-xerces/java/src/org/apache/xerces/jaxp/SAXParserImpl.java Index: SAXParserImpl.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/jaxp/SAXParserImpl.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- SAXParserImpl.java 8 May 2003 20:11:58 -0000 1.21 +++ SAXParserImpl.java 5 Feb 2004 21:30:09 -0000 1.22 @@ -3,7 +3,7 @@ * The Apache Software License, Version 1.1 * * - * Copyright (c) 2000-2002 The Apache Software Foundation. All rights + * Copyright (c) 2000-2004 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -69,18 +69,20 @@ import org.xml.sax.SAXNotSupportedException; import org.xml.sax.XMLReader; +import org.apache.xerces.util.SAXMessageFormatter; + /** + * This is the implementation specific class for the + * <code>javax.xml.parsers.SAXParser</code>. + * * @author Rajiv Mordani * @author Edwin Goei + * * @version $Id$ */ - -/** - * This is the implementation specific class for the - * <code>javax.xml.parsers.SAXParser</code>. - */ public class SAXParserImpl extends javax.xml.parsers.SAXParser - implements JAXPConstants { + implements JAXPConstants { + private XMLReader xmlReader; private String schemaLanguage = null; // null means DTD @@ -200,20 +202,26 @@ Constants.SCHEMA_VALIDATION_FEATURE, false); } else { + // REVISIT: It would be nice if we could format this message + // using a user specified locale as we do in the underlying + // XMLReader -- mrglavas + throw new SAXNotSupportedException( + SAXMessageFormatter.formatMessage(null, "schema-not-supported", null)); + } + } + else if(JAXP_SCHEMA_SOURCE.equals(name)) { + String val = (String)getProperty(JAXP_SCHEMA_LANGUAGE); + if ( val != null && W3C_XML_SCHEMA.equals(val) ) { + xmlReader.setProperty(name, value); + } + else { throw new SAXNotSupportedException( - "Unsupported schema language"); + SAXMessageFormatter.formatMessage(null, + "jaxp-order-not-supported", + new Object[] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE})); } - } else if(JAXP_SCHEMA_SOURCE.equals(name)){ - String val = (String)getProperty(JAXP_SCHEMA_LANGUAGE); - if(val != null && W3C_XML_SCHEMA.equals(val)){ - xmlReader.setProperty(name, value); - }else - throw new SAXNotSupportedException( - "'http://java.sun.com/xml/jaxp/properties/schemaLanguage' "+ - "property should be set before setting "+ - "'http://java.sun.com/xml/jaxp/properties/schemaSource'"+ - " property"); - }else{ + } + else { xmlReader.setProperty(name, value); } } 1.1 xml-xerces/java/src/org/apache/xerces/impl/msg/SAXMessages.properties Index: SAXMessages.properties =================================================================== # This file stores localized messages for the Xerces # SAX implementation. # # The messages are arranged in key and value tuples in a ListResourceBundle. # # @version $Id: SAXMessages.properties,v 1.1 2004/02/05 21:30:09 mrglavas Exp $ BadMessageKey = The error message corresponding to the message key can not be found. FormatFailed = An internal error occurred while formatting the following message:\n # JAXP messages schema-not-supported = The specified schema language is not supported. jaxp-order-not-supported = Property ''{0}'' must be set before setting property ''{1}''. # feature messages feature-not-supported = Feature ''{0}'' is not supported. feature-not-recognized = Feature ''{0}'' is not recognized. true-not-supported = True state for feature ''{0}'' is not supported. false-not-supported = False state for feature ''{0}'' is not supported. # property messages property-not-supported = Property ''{0}'' is not supported. property-not-recognized = Property ''{0}'' is not recognized. property-read-only = Property ''{0}'' is read only. property-not-parsing-supported = Property ''{0}'' is not supported while parsing. dom-node-read-not-supported = Cannot read DOM node property. No DOM tree exists. incompatible-class = The value specified for property ''{0}'' cannot be casted to {1}. 1.1 xml-xerces/java/src/org/apache/xerces/util/SAXMessageFormatter.java Index: SAXMessageFormatter.java =================================================================== /* * The Apache Software License, Version 1.1 * * * Copyright (c) 2004 The Apache Software Foundation. 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 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 "Xerces" 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 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 the Apache Software Foundation and was * originally based on software copyright (c) 1999, International * Business Machines, Inc., http://www.apache.org. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.xerces.util; import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.PropertyResourceBundle; /** * Used to format SAX error messages using a specified locale. * * @author Michael Glavassevich, IBM * * @version $Id: SAXMessageFormatter.java,v 1.1 2004/02/05 21:30:09 mrglavas Exp $ */ public class SAXMessageFormatter { /** * Formats a message with the specified arguments using the given * locale information. * * @param locale The locale of the message. * @param key The message key. * @param arguments The message replacement text arguments. The order * of the arguments must match that of the placeholders * in the actual message. * * @return the formatted message. * * @throws MissingResourceException Thrown if the message with the * specified key cannot be found. */ public static String formatMessage(Locale locale, String key, Object[] arguments) throws MissingResourceException { ResourceBundle resourceBundle = null; if (locale != null) { resourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.SAXMessages", locale); } else { resourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.SAXMessages"); } // format message String msg; try { msg = resourceBundle.getString(key); if (arguments != null) { try { msg = java.text.MessageFormat.format(msg, arguments); } catch (Exception e) { msg = resourceBundle.getString("FormatFailed"); msg += " " + resourceBundle.getString(key); } } } // error catch (MissingResourceException e) { msg = resourceBundle.getString("BadMessageKey"); throw new MissingResourceException(key, msg, key); } // no message if (msg == null) { msg = key; if (arguments.length > 0) { StringBuffer str = new StringBuffer(msg); str.append('?'); for (int i = 0; i < arguments.length; i++) { if (i > 0) { str.append('&'); } str.append(String.valueOf(arguments[i])); } } } return msg; } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]