SQE xml test for LSSerializer failed since build b25_2014-07-30-1719_1029. Seems there is some defect after recent xml parser updated, that causes LSSerializer doesn't remove the content of a node from the following xml, even LSSerializer has a filter, which rejects any node:
<?xml version="1.0" encoding="UTF-16"?><test>tt</test>
The xml will be converted to <?xml version="1.0" encoding="UTF-16"?>tt
Run the following java program:
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSParser;
import org.w3c.dom.ls.LSSerializer;
import org.w3c.dom.ls.LSSerializerFilter;
import org.w3c.dom.traversal.NodeFilter;
import org.xml.sax.SAXException;
public class Test
{
public static void main(String[] args) throws Exception
{
String result = prepare(XML_STRING, false);
System.out.println("xml-declaration false, result is: "+result + "\n it is expected: " + result.trim().equals(""));
result = prepare(XML_STRING, true);
System.out.println("xml-declaration true, result is: "+result + "\n it is expected: " + result.trim().equals("<?xml version=\"1.0\" encoding=\"UTF-16\"?>"));
}
private static String prepare(String source, boolean xmlDeclFlag) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder domParser = factory.newDocumentBuilder();
//final StringBufferInputStream is = new StringBufferInputStream(XML_STRING);
InputSource is = new InputSource(new StringReader(XML_STRING));
Document startDoc = domParser.parse(is);
DOMImplementation impl = startDoc.getImplementation();
implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSParser parser = implLS.createLSParser(
DOMImplementationLS.MODE_SYNCHRONOUS,
"http://www.w3.org/2001/XMLSchema");
LSInput src = getXmlSource(source);
LSSerializer writer = implLS.createLSSerializer();
DOMConfiguration conf = writer.getDomConfig();
conf.setParameter("xml-declaration", Boolean.valueOf(xmlDeclFlag));
// set filter
writer.setFilter(new LSSerializerFilter(){
public short acceptNode(Node enode) {
return FILTER_REJECT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ELEMENT;
}
});
Document doc = parser.parse(src);
return writer.writeToString(doc);
}
private static LSInput getXmlSource(String xml1) throws Exception
{
LSInput src = implLS.createLSInput();
src.setStringData(xml1);
return src;
}
private static String XML_STRING = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><test>tt</test>";
private static DOMImplementationLS implLS;
}
output:
xml-declaration false, result is: tt
it is expected: false
xml-declaration true, result is: <?xml version="1.0" encoding="UTF-16"?>tt
it is expected: false
It's not expected.
<?xml version="1.0" encoding="UTF-16"?><test>tt</test>
The xml will be converted to <?xml version="1.0" encoding="UTF-16"?>tt
Run the following java program:
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSParser;
import org.w3c.dom.ls.LSSerializer;
import org.w3c.dom.ls.LSSerializerFilter;
import org.w3c.dom.traversal.NodeFilter;
import org.xml.sax.SAXException;
public class Test
{
public static void main(String[] args) throws Exception
{
String result = prepare(XML_STRING, false);
System.out.println("xml-declaration false, result is: "+result + "\n it is expected: " + result.trim().equals(""));
result = prepare(XML_STRING, true);
System.out.println("xml-declaration true, result is: "+result + "\n it is expected: " + result.trim().equals("<?xml version=\"1.0\" encoding=\"UTF-16\"?>"));
}
private static String prepare(String source, boolean xmlDeclFlag) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder domParser = factory.newDocumentBuilder();
//final StringBufferInputStream is = new StringBufferInputStream(XML_STRING);
InputSource is = new InputSource(new StringReader(XML_STRING));
Document startDoc = domParser.parse(is);
DOMImplementation impl = startDoc.getImplementation();
implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSParser parser = implLS.createLSParser(
DOMImplementationLS.MODE_SYNCHRONOUS,
"http://www.w3.org/2001/XMLSchema");
LSInput src = getXmlSource(source);
LSSerializer writer = implLS.createLSSerializer();
DOMConfiguration conf = writer.getDomConfig();
conf.setParameter("xml-declaration", Boolean.valueOf(xmlDeclFlag));
// set filter
writer.setFilter(new LSSerializerFilter(){
public short acceptNode(Node enode) {
return FILTER_REJECT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ELEMENT;
}
});
Document doc = parser.parse(src);
return writer.writeToString(doc);
}
private static LSInput getXmlSource(String xml1) throws Exception
{
LSInput src = implLS.createLSInput();
src.setStringData(xml1);
return src;
}
private static String XML_STRING = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><test>tt</test>";
private static DOMImplementationLS implLS;
}
output:
xml-declaration false, result is: tt
it is expected: false
xml-declaration true, result is: <?xml version="1.0" encoding="UTF-16"?>tt
it is expected: false
It's not expected.