You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2006/01/27 10:14:44 UTC

svn commit: r372794 - in /incubator/servicemix/trunk/servicemix-core: ./ src/main/java/org/apache/servicemix/jbi/jaxp/ src/test/java/org/apache/servicemix/jbi/jaxp/ src/test/resources/org/apache/servicemix/jbi/jaxp/

Author: gnodet
Date: Fri Jan 27 01:14:33 2006
New Revision: 372794

URL: http://svn.apache.org/viewcvs?rev=372794&view=rev
Log:
Fix xml unit tests on jdk 1.4

Modified:
    incubator/servicemix/trunk/servicemix-core/project.xml
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java
    incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java
    incubator/servicemix/trunk/servicemix-core/src/test/resources/org/apache/servicemix/jbi/jaxp/test.xml

Modified: incubator/servicemix/trunk/servicemix-core/project.xml
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/project.xml?rev=372794&r1=372793&r2=372794&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/project.xml (original)
+++ incubator/servicemix/trunk/servicemix-core/project.xml Fri Jan 27 01:14:33 2006
@@ -132,11 +132,6 @@
             <artifactId>xercesImpl</artifactId>
             <version>${xercesImpl_version}</version>
         </dependency>
-        <dependency>
-            <groupId>xerces</groupId>
-            <artifactId>xmlParserAPIs</artifactId>
-            <version>${xerces_version}</version>
-        </dependency>
 
         <dependency>
             <groupId>xml-apis</groupId>

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java?rev=372794&r1=372793&r2=372794&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java (original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java Fri Jan 27 01:14:33 2006
@@ -163,57 +163,56 @@
             return null;
         }
     }
-    
+
     public StreamSource toStreamSource(Source source) throws TransformerException {
-    	if (source instanceof StreamSource) {
-    		return (StreamSource) source;
-    	} else if (source instanceof DOMSource) {
+        if (source instanceof StreamSource) {
+            return (StreamSource) source;
+        } else if (source instanceof DOMSource) {
             return toStreamSourceFromDOM((DOMSource) source);
-        }
-        else if (source instanceof SAXSource) {
+        } else if (source instanceof SAXSource) {
             return toStreamSourceFromSAX((SAXSource) source);
-        }
-        else {
+        } else {
             return null;
         }
     }
 
     public StreamSource toStreamSourceFromSAX(SAXSource source) throws TransformerException {
-		InputSource inputSource = source.getInputSource();
-		if (inputSource != null) {
-			if (inputSource.getByteStream() != null) {
-				return new StreamSource(inputSource.getByteStream());
-			}
-			if (inputSource.getCharacterStream() != null) {
-				return new StreamSource(inputSource.getCharacterStream());
-			}
-		}
-		String result = toString(source);
-		return new StreamSource(new ByteArrayInputStream(result.getBytes()));
-	}
+        InputSource inputSource = source.getInputSource();
+        if (inputSource != null) {
+            if (inputSource.getByteStream() != null) {
+                return new StreamSource(inputSource.getByteStream());
+            }
+            if (inputSource.getCharacterStream() != null) {
+                return new StreamSource(inputSource.getCharacterStream());
+            }
+        }
+        String result = toString(source);
+        return new StreamSource(new ByteArrayInputStream(result.getBytes()));
+    }
 
 	public StreamSource toStreamSourceFromDOM(DOMSource source) throws TransformerException {
-		String result = toString(source);
-		return new StreamSource(new ByteArrayInputStream(result.getBytes()));
-	}
+        String result = toString(source);
+        return new StreamSource(new ByteArrayInputStream(result.getBytes()));
+    }
 
-	public SAXSource toSAXSourceFromStream(StreamSource source) {
+    public SAXSource toSAXSourceFromStream(StreamSource source) {
         InputSource inputSource = new InputSource(source.getInputStream());
         inputSource.setSystemId(source.getSystemId());
         inputSource.setPublicId(source.getPublicId());
         return new SAXSource(inputSource);
     }
-	
-	public Reader toReaderFromSource(Source src) throws TransformerException {
-		StreamSource stSrc = toStreamSource(src);
-		Reader r = stSrc.getReader();
-		if (r == null) {
-			r = new InputStreamReader(stSrc.getInputStream());
-		}
-		return r;
-	}
 
-    public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException, IOException, SAXException {
+    public Reader toReaderFromSource(Source src) throws TransformerException {
+        StreamSource stSrc = toStreamSource(src);
+        Reader r = stSrc.getReader();
+        if (r == null) {
+            r = new InputStreamReader(stSrc.getInputStream());
+        }
+        return r;
+    }
+
+    public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException, IOException,
+            SAXException {
         DocumentBuilder builder = createDocumentBuilder();
         String systemId = source.getSystemId();
         Document document = null;
@@ -242,16 +241,27 @@
     public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException, SAXException, ParserConfigurationException {
         return new DOMSource(toDOMNodeFromSAX(source));
     }
-    
+
     public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException {
         SAX2DOM converter = new SAX2DOM(createDocument());
         XMLReader xmlReader = source.getXMLReader();
         if (xmlReader == null) {
-            xmlReader = XMLReaderFactory.createXMLReader();
+            xmlReader = createXMLReader();
         }
         xmlReader.setContentHandler(converter);
         xmlReader.parse(source.getInputSource());
         return converter.getDOM();
+    }
+
+    private XMLReader createXMLReader() throws SAXException {
+        // In JDK 1.4, the xml reader factory does not look for META-INF services
+        // If the org.xml.sax.driver system property is not defined, and exception will be thrown.
+        // In these cases, default to xerces parser
+        try {
+            return XMLReaderFactory.createXMLReader();
+        } catch (Exception e) {
+            return XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
+        }
     }
 
 

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java?rev=372794&r1=372793&r2=372794&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java (original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StaxSource.java Fri Jan 27 01:14:33 2006
@@ -15,6 +15,7 @@
  */
 package org.apache.servicemix.jbi.jaxp;
 
+import javax.xml.XMLConstants;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
@@ -34,107 +35,120 @@
 import org.xml.sax.helpers.AttributesImpl;
 
 public class StaxSource extends SAXSource implements XMLReader {
-	
-	private XMLStreamReader streamReader;
-	private ContentHandler contentHandler;
+
+    private XMLStreamReader streamReader;
+
+    private ContentHandler contentHandler;
+
     private char[] chars = new char[1024];
-	
-	public StaxSource(XMLStreamReader streamReader) {
-		this.streamReader = streamReader;
-		setInputSource(new InputSource());
-	}
-	
-	public XMLReader getXMLReader() {
-		return this;
-	}
-	
-	protected void parse() throws SAXException {
-		try {
-			contentHandler.startDocument();
-			while (true) {
-				switch (streamReader.getEventType()) {
-				case XMLStreamConstants.ATTRIBUTE:
-				case XMLStreamConstants.CDATA:
-					break;
-				case XMLStreamConstants.CHARACTERS:
-					if (!streamReader.isWhiteSpace()) {
-				        for (int textLength = streamReader.getTextLength();
-				             textLength > 0;
-				             textLength -= chars.length) {
-				        	int l = Math.min(textLength, chars.length);
-				        	streamReader.getTextCharacters(0, chars, 0, l);
-				        	contentHandler.characters(chars, 0, l);
-				        }
-					}
-					break;
-				case XMLStreamConstants.COMMENT:
-				case XMLStreamConstants.DTD:
-					break;
-				case XMLStreamConstants.END_DOCUMENT:
-					contentHandler.endDocument();
-					return;
-				case XMLStreamConstants.END_ELEMENT: {
-		            String uri = streamReader.getNamespaceURI();
-		            String localName = streamReader.getLocalName();
-		            String prefix = streamReader.getPrefix();
-		            String qname = prefix != null && prefix.length() > 0 ? prefix + ":" + localName : localName;
-					contentHandler.endElement(uri, localName, qname);
-					for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
-						contentHandler.endPrefixMapping(streamReader.getNamespaceURI(i));
-					}
-					break;
-				}
-				case XMLStreamConstants.ENTITY_DECLARATION:
-				case XMLStreamConstants.ENTITY_REFERENCE:
-				case XMLStreamConstants.NAMESPACE:
-				case XMLStreamConstants.NOTATION_DECLARATION:
-				case XMLStreamConstants.PROCESSING_INSTRUCTION:
-				case XMLStreamConstants.SPACE:
-				case XMLStreamConstants.START_DOCUMENT:
-					break;
-				case XMLStreamConstants.START_ELEMENT: {
-					for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
-						contentHandler.startPrefixMapping(streamReader.getNamespacePrefix(i), 
-														  streamReader.getNamespaceURI(i));
-					}
-		            String uri = streamReader.getNamespaceURI();
-		            String localName = streamReader.getLocalName();
-		            String prefix = streamReader.getPrefix();
-		            String qname = prefix != null && prefix.length() > 0 ? prefix + ":" + localName : localName;
-					contentHandler.startElement(uri, localName, qname, getAttributes());
-					break;
-				}
-				}
-				streamReader.next();
-			}
-		} catch (XMLStreamException e) {
-			throw new SAXParseException(e.getMessage(), 
-										null, 
-										null, 
-										e.getLocation().getLineNumber(), 
-										e.getLocation().getColumnNumber(), 
-										e);
-		}
-	}
-	
-	protected String getQualifiedName() {
-		String prefix = streamReader.getPrefix();
-		if (prefix != null && prefix.length() > 0) {
-			return prefix + ":" + streamReader.getLocalName();
-		} else {
-			return streamReader.getLocalName();
-		}
-	}
-	
-	protected Attributes getAttributes() {
+
+    public StaxSource(XMLStreamReader streamReader) {
+        this.streamReader = streamReader;
+        setInputSource(new InputSource());
+    }
+
+    public XMLReader getXMLReader() {
+        return this;
+    }
+
+    protected void parse() throws SAXException {
+        try {
+            contentHandler.startDocument();
+            while (true) {
+                switch (streamReader.getEventType()) {
+                case XMLStreamConstants.ATTRIBUTE:
+                case XMLStreamConstants.CDATA:
+                    break;
+                case XMLStreamConstants.CHARACTERS:
+                    if (!streamReader.isWhiteSpace()) {
+                        for (int textLength = streamReader.getTextLength(); textLength > 0; textLength -= chars.length) {
+                            int l = Math.min(textLength, chars.length);
+                            streamReader.getTextCharacters(0, chars, 0, l);
+                            contentHandler.characters(chars, 0, l);
+                        }
+                    }
+                    break;
+                case XMLStreamConstants.COMMENT:
+                case XMLStreamConstants.DTD:
+                    break;
+                case XMLStreamConstants.END_DOCUMENT:
+                    contentHandler.endDocument();
+                    return;
+                case XMLStreamConstants.END_ELEMENT: {
+                    String uri = streamReader.getNamespaceURI();
+                    String localName = streamReader.getLocalName();
+                    String prefix = streamReader.getPrefix();
+                    String qname = prefix != null && prefix.length() > 0 ? prefix + ":" + localName : localName;
+                    contentHandler.endElement(uri, localName, qname);
+                    for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
+                        contentHandler.endPrefixMapping(streamReader.getNamespaceURI(i));
+                    }
+                    break;
+                }
+                case XMLStreamConstants.ENTITY_DECLARATION:
+                case XMLStreamConstants.ENTITY_REFERENCE:
+                case XMLStreamConstants.NAMESPACE:
+                case XMLStreamConstants.NOTATION_DECLARATION:
+                case XMLStreamConstants.PROCESSING_INSTRUCTION:
+                case XMLStreamConstants.SPACE:
+                case XMLStreamConstants.START_DOCUMENT:
+                    break;
+                case XMLStreamConstants.START_ELEMENT: {
+                    // Do not use startPrefixMapping
+                    // See comment in getAttributes
+                    // for (int i = 0; i < streamReader.getNamespaceCount();
+                    // i++) {
+                    // contentHandler.startPrefixMapping(streamReader.getNamespacePrefix(i),
+                    // streamReader.getNamespaceURI(i));
+                    // }
+                    String uri = streamReader.getNamespaceURI();
+                    String localName = streamReader.getLocalName();
+                    String prefix = streamReader.getPrefix();
+                    String qname = prefix != null && prefix.length() > 0 ? prefix + ":" + localName : localName;
+                    contentHandler.startElement(uri, localName, qname, getAttributes());
+                    break;
+                }
+                }
+                streamReader.next();
+            }
+        } catch (XMLStreamException e) {
+            throw new SAXParseException(e.getMessage(), null, null, e.getLocation().getLineNumber(), e.getLocation()
+                    .getColumnNumber(), e);
+        }
+    }
+
+    protected String getQualifiedName() {
+        String prefix = streamReader.getPrefix();
+        if (prefix != null && prefix.length() > 0) {
+            return prefix + ":" + streamReader.getLocalName();
+        } else {
+            return streamReader.getLocalName();
+        }
+    }
+
+    protected Attributes getAttributes() {
         AttributesImpl attrs = new AttributesImpl();
+        // Adding namespace declaration as attributes is necessary because
+        // the xalan implementation that ships with SUN JDK 1.4 is bugged
+        // and does not handle the startPrefixMapping method
+        for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
+            String prefix = streamReader.getNamespacePrefix(i);
+            String uri = streamReader.getNamespaceURI(i);
+            // Default namespace
+            if (prefix == null || prefix.length() == 0) {
+                attrs.addAttribute("", XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE, "CDATA", uri);
+            } else {
+                attrs.addAttribute(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, prefix, XMLConstants.XMLNS_ATTRIBUTE + ":"
+                        + prefix, "CDATA", uri);
+            }
+        }
         for (int i = 0; i < streamReader.getAttributeCount(); i++) {
             String uri = streamReader.getAttributeNamespace(i);
             String localName = streamReader.getAttributeLocalName(i);
             String prefix = streamReader.getAttributePrefix(i);
             String qName;
             if (prefix != null && prefix.length() > 0) {
-            	qName = prefix + ':' + localName;
+                qName = prefix + ':' + localName;
             } else {
                 qName = localName;
             }
@@ -143,44 +157,57 @@
             attrs.addAttribute(uri, localName, qName, type, value);
         }
         return attrs;
-	}
-	
-	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
-		return false;
-	}
-	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
-	}
-	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
-		return null;
-	}
-	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
-	}
-	public void setEntityResolver(EntityResolver resolver) {
-	}
-	public EntityResolver getEntityResolver() {
-		return null;
-	}
-	public void setDTDHandler(DTDHandler handler) {
-	}
-	public DTDHandler getDTDHandler() {
-		return null;
-	}
-	public void setContentHandler(ContentHandler handler) {
-		this.contentHandler = handler;
-	}
-	public ContentHandler getContentHandler() {
-		return this.contentHandler;
-	}
-	public void setErrorHandler(ErrorHandler handler) {
-	}
-	public ErrorHandler getErrorHandler() {
-		return null;
-	}
-	public void parse(InputSource input) throws SAXException {
-		StaxSource.this.parse();
-	}
-	public void parse(String systemId) throws SAXException {
-		StaxSource.this.parse();
-	}
+    }
+
+    public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
+        return false;
+    }
+
+    public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
+    }
+
+    public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
+        return null;
+    }
+
+    public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
+    }
+
+    public void setEntityResolver(EntityResolver resolver) {
+    }
+
+    public EntityResolver getEntityResolver() {
+        return null;
+    }
+
+    public void setDTDHandler(DTDHandler handler) {
+    }
+
+    public DTDHandler getDTDHandler() {
+        return null;
+    }
+
+    public void setContentHandler(ContentHandler handler) {
+        this.contentHandler = handler;
+    }
+
+    public ContentHandler getContentHandler() {
+        return this.contentHandler;
+    }
+
+    public void setErrorHandler(ErrorHandler handler) {
+    }
+
+    public ErrorHandler getErrorHandler() {
+        return null;
+    }
+
+    public void parse(InputSource input) throws SAXException {
+        StaxSource.this.parse();
+    }
+
+    public void parse(String systemId) throws SAXException {
+        StaxSource.this.parse();
+    }
 
 }

Modified: incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java?rev=372794&r1=372793&r2=372794&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java (original)
+++ incubator/servicemix/trunk/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/FragmentStreamReaderTest.java Fri Jan 27 01:14:33 2006
@@ -40,10 +40,11 @@
 	public void testStaxSource() throws Exception {
 		InputStream is = getClass().getResourceAsStream("test.xml");
 		XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is);
+        xsr = new ExtendedXMLStreamReader(xsr);
 		xsr.nextTag();
-		log.info(xsr.getLocalName());
+		log.info(xsr.getName());
 		xsr.nextTag();
-		log.info(xsr.getLocalName());
+		log.info(xsr.getName());
 		XMLStreamReader fsr = new FragmentStreamReader(xsr);
 		StaxSource ss = new StaxSource(fsr);
         StringWriter buffer = new StringWriter();

Modified: incubator/servicemix/trunk/servicemix-core/src/test/resources/org/apache/servicemix/jbi/jaxp/test.xml
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/test/resources/org/apache/servicemix/jbi/jaxp/test.xml?rev=372794&r1=372793&r2=372794&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/test/resources/org/apache/servicemix/jbi/jaxp/test.xml (original)
+++ incubator/servicemix/trunk/servicemix-core/src/test/resources/org/apache/servicemix/jbi/jaxp/test.xml Fri Jan 27 01:14:33 2006
@@ -1,6 +1,6 @@
 <?xml version="1.0"?>
 <test firstAtt="toto" secondAtt="titit" xmlns:u="uri:test3">
-  <u:hello>world</u:hello>
+  <u:hello u:id="3">world</u:hello>
   <!-- A comment -->
   <child xmlns="uri:test" atr1="val1" xmlns:t="uri:test2" t:atr2="val2">
     <child2> t i u </child2>