jefft 01/12/09 21:05:44
Modified: src/java/org/apache/avalon/framework/configuration
AbstractConfiguration.java Configuration.java
DefaultConfiguration.java
DefaultConfigurationSerializer.java
NamespacedSAXConfigurationHandler.java
Log:
- tighten the getNamespace() and getPrefix() contracts. Now, both methods will
return non-null strings, defaulting to "" (no namespace). If the
Configuration builder does not set the namespace or prefix internal field, a
ConfigurationException will be thrown when getNamespace() or getPrefix() is
called.
- Fix the Serializer NPE bug when parsing non-namespace XML (eg "<foo/>").
- Fix a Serializer bug where no xmlns declarations are made. Ie, <x:foo
xmlns:x="http://bar"/> would previously be serialized into <x:foo/>.
For further details, see
http://marc.theaimsgroup.com/?l=avalon-dev&m=100790337926263&w=2
Revision Changes Path
1.11 +5 -2
jakarta-avalon/src/java/org/apache/avalon/framework/configuration/AbstractConfiguration.java
Index: AbstractConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/AbstractConfiguration.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- AbstractConfiguration.java 2001/11/19 16:59:15 1.10
+++ AbstractConfiguration.java 2001/12/10 05:05:44 1.11
@@ -15,7 +15,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Pierpaolo Fumagalli</a>
- * @version CVS $Revision: 1.10 $ $Date: 2001/11/19 16:59:15 $
+ * @version CVS $Revision: 1.11 $ $Date: 2001/12/10 05:05:44 $
*/
public abstract class AbstractConfiguration
implements Configuration
@@ -24,8 +24,11 @@
* Returns the prefix of the namespace. This is only used as a
serialization
* hint, therefore is not part of the client API. It should be included
in
* all Configuration implementations though.
+ * @return A non-null String (defaults to "")
+ * @throws ConfigurationException if no prefix was defined (prefix is
+ * <code>null</code>.
*/
- protected abstract String getPrefix();
+ protected abstract String getPrefix() throws ConfigurationException;
/**
* Returns the value of the configuration element as an <code>int</code>.
1.9 +1 -1
jakarta-avalon/src/java/org/apache/avalon/framework/configuration/Configuration.java
Index: Configuration.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/Configuration.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Configuration.java 2001/12/09 04:53:33 1.8
+++ Configuration.java 2001/12/10 05:05:44 1.9
@@ -134,7 +134,7 @@
* @since 4.1
* @return a String identifying the namespace of this Configuration.
*/
- String getNamespace();
+ String getNamespace() throws ConfigurationException;
/**
* Return a new <code>Configuration</code> instance encapsulating the
1.12 +33 -6
jakarta-avalon/src/java/org/apache/avalon/framework/configuration/DefaultConfiguration.java
Index: DefaultConfiguration.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/DefaultConfiguration.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- DefaultConfiguration.java 2001/11/26 09:28:43 1.11
+++ DefaultConfiguration.java 2001/12/10 05:05:44 1.12
@@ -44,6 +44,13 @@
/**
* Create a new <code>DefaultConfiguration</code> instance.
+ * @param name config node name
+ * @param location Builder-specific locator string
+ * @param ns Namespace string (typically a URI). Should not be null; use
""
+ * if no namespace.
+ * @param prefix A short string prefixed to element names, associating
+ * elements with a longer namespace string. Should not be null; use ""
if no
+ * namespace.
*/
public DefaultConfiguration( final String name,
final String location,
@@ -53,7 +60,7 @@
m_name = name;
m_location = location;
m_namespace = ns;
- m_prefix = prefix; // only used as a serialization hint
+ m_prefix = prefix; // only used as a serialization hint. Cannot be
null
}
/**
@@ -67,17 +74,37 @@
/**
* Returns the namespace of this configuration element
*/
- public String getNamespace()
+ public String getNamespace() throws ConfigurationException
{
- return m_namespace;
- }
+ if( null != m_namespace )
+ {
+ return m_namespace;
+ }
+ else
+ {
+ throw new ConfigurationException( "No namespace (not even
default \"\") is associated with the "+
+ "configuration element \"" +
getName() +
+ "\" at " + getLocation() );
+ }
+ }
/**
* Returns the prefix of the namespace
+ * @throws ConfigurationException if prefix is not present
(<code>null</code>).
*/
- protected String getPrefix()
+ protected String getPrefix() throws ConfigurationException
{
- return m_prefix;
+ if( null != m_prefix )
+ {
+ return m_prefix;
+ }
+ else
+ {
+ throw new ConfigurationException( "No prefix (not even default
\"\") is associated with the "+
+ "configuration element \"" +
getName() +
+ "\" at " + getLocation() );
+ }
+
}
/**
1.7 +26 -5
jakarta-avalon/src/java/org/apache/avalon/framework/configuration/DefaultConfigurationSerializer.java
Index: DefaultConfigurationSerializer.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/DefaultConfigurationSerializer.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- DefaultConfigurationSerializer.java 2001/11/19 16:59:15 1.6
+++ DefaultConfigurationSerializer.java 2001/12/10 05:05:44 1.7
@@ -83,7 +83,7 @@
* be set before calling this method.
*/
protected void serialize( final Configuration source )
- throws SAXException
+ throws SAXException, ConfigurationException
{
m_namespaceSupport.reset();
m_handler.startDocument();
@@ -95,7 +95,7 @@
* Serialize each Configuration element. This method is called
recursively.
*/
protected void serializeElement( final Configuration element )
- throws SAXException
+ throws SAXException, ConfigurationException
{
m_namespaceSupport.pushContext();
@@ -106,8 +106,12 @@
{
for (int i = 0; i < attrNames.length; i++)
{
- attr.addAttribute("", attrNames[i], attrNames[i], "CDATA",
- element.getAttribute(attrNames[i], ""));
+ attr.addAttribute("", // namespace URI
+ attrNames[i], // local name
+ attrNames[i], // qName
+ "CDATA", // type
+ element.getAttribute(attrNames[i], "") //
value
+ );
}
}
@@ -118,14 +122,31 @@
{
nsPrefix = ((AbstractConfiguration) element).getPrefix();
}
+ // nsPrefix is guaranteed to be non-null at this point.
boolean nsWasDeclared = false;
- // Is this namespace already declared?
final String existingURI = m_namespaceSupport.getURI( nsPrefix );
+
+ // ie, there is no existing URI declared for this prefix or we're
+ // remapping the prefix to a different URI
if ( existingURI == null || !existingURI.equals( nsURI ) )
{
nsWasDeclared = true;
+ if (nsPrefix.equals("") && nsURI.equals(""))
+ {
+ // implicit mapping; don't need to declare
+ }
+ else if (nsPrefix.equals(""))
+ {
+ // (re)declare the default namespace
+ attr.addAttribute("", "xmlns", "xmlns", "CDATA", nsURI);
+ }
+ else
+ {
+ // (re)declare a mapping from nsPrefix to nsURI
+ attr.addAttribute("", "xmlns:"+nsPrefix, "xmlns:"+nsPrefix,
"CDATA", nsURI);
+ }
m_handler.startPrefixMapping( nsPrefix, nsURI );
m_namespaceSupport.declarePrefix( nsPrefix, nsURI );
}
1.2 +2 -1
jakarta-avalon/src/java/org/apache/avalon/framework/configuration/NamespacedSAXConfigurationHandler.java
Index: NamespacedSAXConfigurationHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/NamespacedSAXConfigurationHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NamespacedSAXConfigurationHandler.java 2001/12/07 13:44:54 1.1
+++ NamespacedSAXConfigurationHandler.java 2001/12/10 05:05:44 1.2
@@ -122,7 +122,8 @@
final String
namespaceURI,
final String
location )
{
- final String prefix = m_namespaceSupport.getPrefix( namespaceURI );
+ String prefix = m_namespaceSupport.getPrefix( namespaceURI );
+ if (prefix == null) prefix = "";
return new DefaultConfiguration( localName, location, namespaceURI,
prefix );
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>