bloritsch 01/10/29 06:58:48
Modified: src/java/org/apache/avalon/framework/configuration
DefaultConfigurationSerializer.java Namespace.java
SAXConfigurationHandler.java
Log:
apply fixes from Sylvain Wallez to Namespace support
Revision Changes Path
1.3 +35 -12
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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DefaultConfigurationSerializer.java 2001/10/02 16:24:55 1.2
+++ DefaultConfigurationSerializer.java 2001/10/29 14:58:48 1.3
@@ -14,6 +14,7 @@
import java.net.URL;
import java.util.Properties;
import org.xml.sax.helpers.AttributesImpl;
+import org.xml.sax.helpers.NamespaceSupport;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import javax.xml.transform.OutputKeys;
@@ -35,7 +36,7 @@
private TransformerHandler m_handler;
private OutputStream m_out;
private Properties m_format = new Properties();
- private Namespace m_currentNamespace =
Namespace.getNamespace( null );
+ private NamespaceSupport m_namespaceSupport = new
NamespaceSupport();
/**
* Build a ConfigurationSerializer
@@ -83,6 +84,7 @@
protected void serialize( final Configuration source )
throws SAXException
{
+ this.m_namespaceSupport.reset();
this.m_handler.startDocument();
this.serializeElement(source);
this.m_handler.endDocument();
@@ -94,6 +96,8 @@
protected void serializeElement( final Configuration element )
throws SAXException
{
+ m_namespaceSupport.pushContext();
+
AttributesImpl attr = new AttributesImpl();
String[] attrNames = element.getAttributeNames();
@@ -101,20 +105,38 @@
{
for (int i = 0; i < attrNames.length; i++)
{
- attr.setAttribute(i, "", attrNames[i], attrNames[i], "CDATA",
+ attr.addAttribute("", attrNames[i], attrNames[i], "CDATA",
element.getAttribute(attrNames[i], ""));
}
}
+
+ final Namespace namespace = element.getNamespace();
+ final String nsURI = namespace.getURI();
+ final String nsPrefix = namespace.getPrefix();
+ boolean nsWasDeclared = false;
+
+ // Is this namespace already declared?
+ final String existingURI = m_namespaceSupport.getURI( nsPrefix );
+ if ( existingURI == null || !existingURI.equals( nsURI ) )
+ {
+ nsWasDeclared = true;
+ this.m_handler.startPrefixMapping( nsPrefix, nsURI );
+ this.m_namespaceSupport.declarePrefix( nsPrefix, nsURI );
+ }
- Namespace oldNamespace = null;
- if ( m_currentNamespace != element.getNamespace() )
+ String localName = element.getName();
+ String qName = element.getName();
+ if ( nsPrefix == null || nsPrefix.length() == 0 )
{
- oldNamespace = m_currentNamespace;
- m_currentNamespace = element.getNamespace();
- this.m_handler.startPrefixMapping(
m_currentNamespace.getPrefix(), m_currentNamespace.getURI() );
+ qName = localName;
}
+ else
+ {
+ qName = prefix + ":" + localName;
+ }
- this.m_handler.startElement("", element.getName(),
element.getName(), attr);
+ this.m_handler.startElement(nsURI, localName, qName, attr);
+
String value = element.getValue(null);
if (null == value)
@@ -131,13 +153,14 @@
this.m_handler.characters(value.toCharArray(), 0,
value.length());
}
- this.m_handler.endElement("", element.getName(), element.getName());
+ this.m_handler.endElement(nsURI, localName, qName);
- if ( null != oldNamespace )
+ if ( nsWasDeclared )
{
- this.m_handler.endPrefixMapping( m_currentNamespace.getPrefix()
);
- m_currentNamespace = oldNamespace;
+ this.m_handler.endPrefixMapping( nsPrefix );
}
+
+ this.m_namespaceSupport.popContext();
}
/**
1.2 +1 -11
jakarta-avalon/src/java/org/apache/avalon/framework/configuration/Namespace.java
Index: Namespace.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/Namespace.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Namespace.java 2001/10/02 16:26:04 1.1
+++ Namespace.java 2001/10/29 14:58:48 1.2
@@ -18,8 +18,6 @@
*/
public final class Namespace implements Serializable
{
- private final static HashMap cache = new HashMap();
-
private final String m_prefix;
private final String m_uri;
@@ -186,15 +184,7 @@
{
loc = "";
}
-
- Namespace ns = (Namespace) Namespace.cache.get( pre + loc );
-
- if ( null == ns )
- {
- ns = new Namespace( pre, loc );
- Namespace.cache.put( pre + loc, ns );
- }
- return ns;
+ return new Namespace( pre, loc );
}
}
1.6 +15 -19
jakarta-avalon/src/java/org/apache/avalon/framework/configuration/SAXConfigurationHandler.java
Index: SAXConfigurationHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/SAXConfigurationHandler.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- SAXConfigurationHandler.java 2001/10/02 16:24:55 1.5
+++ SAXConfigurationHandler.java 2001/10/29 14:58:48 1.6
@@ -8,13 +8,13 @@
package org.apache.avalon.framework.configuration;
import java.util.ArrayList;
-import java.util.Stack;
import org.xml.sax.Attributes;
import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
+import org.xml.sax.helpers.NamespaceSupport;
/**
* A SAXConfigurationHandler helps build Configurations out of sax events.
@@ -26,10 +26,10 @@
extends DefaultHandler
implements ErrorHandler
{
- private final ArrayList m_elements = new ArrayList();
+ private final ArrayList m_elements = new
ArrayList();
private Configuration m_configuration;
private Locator m_locator;
- private Stack m_namespaceStack = new Stack();
+ private NamespaceSupport m_namespaceSupport = new
NamespaceSupport();
public Configuration getConfiguration()
{
@@ -50,7 +50,7 @@
public void startDocument()
throws SAXException
{
- m_namespaceStack.push(Namespace.getNamespace(null));
+ m_namespaceSupport.reset();
super.startDocument();
}
@@ -58,7 +58,7 @@
throws SAXException
{
super.endDocument();
- m_namespaceStack.clear();
+ m_namespaceSupport.reset();
}
public void characters( final char[] ch, int start, int end )
@@ -97,12 +97,17 @@
{
m_configuration = (Configuration)object;
}
+
+ m_namespaceSupport.popContext();
}
protected DefaultConfiguration createConfiguration( final String
localName,
+ final String
namespaceURI,
final String
location )
{
- return new DefaultConfiguration( localName, location, (Namespace)
this.m_namespaceStack.peek() );
+ final String prefix = m_namespaceSupport.getPrefix(
namespaceURI );
+ final Namespace namespace = Namespace.getNamespace( prefix,
namespaceURI );
+ return new DefaultConfiguration( localName, location, namespace );
}
public void startElement( final String namespaceURI,
@@ -111,8 +116,10 @@
final Attributes attributes )
throws SAXException
{
+ m_namespaceSupport.pushContext();
+
final DefaultConfiguration configuration =
- createConfiguration( rawName, getLocationString() );
+ createConfiguration( localName, namespaceURI,
getLocationString() );
final int size = m_elements.size() - 1;
if( size > -1 )
@@ -187,18 +194,7 @@
public void startPrefixMapping(String prefix, String uri)
throws SAXException
{
- m_namespaceStack.push( Namespace.getNamespace( prefix, uri ) );
+ m_namespaceSupport.declarePrefix( prefix, uri );
super.startPrefixMapping( prefix, uri );
- }
-
- public void endPrefixMapping(String prefix)
- throws SAXException
- {
- Namespace ns = (Namespace) m_namespaceStack.pop();
- if ( !( ns.getPrefix().equals( prefix ) ) )
- {
- throw new SAXException( "Uneven namespace mapping for prefix: "
+ prefix );
- }
- super.endPrefixMapping( prefix );
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>