From: <bo...@us...> - 2010-04-29 06:13:40
|
Revision: 361 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=361&view=rev Author: bodewig Date: 2010-04-29 06:13:34 +0000 (Thu, 29 Apr 2010) Log Message: ----------- extract Source => InputSource Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ParsingValidator.java Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java 2010-04-29 06:13:34 UTC (rev 361) @@ -0,0 +1,49 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package net.sf.xmlunit.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import net.sf.xmlunit.exceptions.ConfigurationException; +import net.sf.xmlunit.exceptions.XMLUnitException; +import org.xml.sax.InputSource; + +public final class Convert { + private Convert() { } + + public static InputSource toInputSource(Source s) { + try { + if (!(s instanceof SAXSource) && !(s instanceof StreamSource)) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + StreamResult r = new StreamResult(bos); + TransformerFactory fac = TransformerFactory.newInstance(); + Transformer t = fac.newTransformer(); + t.transform(s, r); + s = new StreamSource(new ByteArrayInputStream(bos + .toByteArray())); + } + return SAXSource.sourceToInputSource(s); + } catch (javax.xml.transform.TransformerConfigurationException e) { + throw new ConfigurationException(e); + } catch (javax.xml.transform.TransformerException e) { + throw new XMLUnitException(e); + } + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ParsingValidator.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ParsingValidator.java 2010-04-27 14:31:03 UTC (rev 360) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ParsingValidator.java 2010-04-29 06:13:34 UTC (rev 361) @@ -17,9 +17,9 @@ import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.Source; -import javax.xml.transform.sax.SAXSource; import net.sf.xmlunit.exceptions.ConfigurationException; import net.sf.xmlunit.exceptions.XMLUnitException; +import net.sf.xmlunit.util.Convert; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; @@ -50,7 +50,7 @@ @Override public ValidationResult validateSchema() { throw new XMLUnitException("Schema validation is not supported by" - + " ParsinValidator"); + + " ParsingValidator"); } @Override public ValidationResult validateInstance(Source s) { @@ -69,7 +69,7 @@ if (Languages.W3C_XML_SCHEMA_NS_URI.equals(language)) { InputSource[] schemaSource = new InputSource[source.length]; for (int i = 0; i < source.length; i++) { - schemaSource[i] = toInputSource(source[i]); + schemaSource[i] = Convert.toInputSource(source[i]); } parser.setProperty(Properties.SCHEMA_SOURCE, schemaSource); @@ -77,7 +77,7 @@ handler.setSchemaSystemId(source[0].getSystemId()); } } - InputSource input = toInputSource(s); + InputSource input = Convert.toInputSource(s); try { parser.parse(input, handler); } catch (SAXException e) { @@ -101,11 +101,6 @@ } } - // TODO factor out to a common class, will be needed by other parts as well - private static InputSource toInputSource(Source s) throws SAXException { - return SAXSource.sourceToInputSource(s); - } - private static class Properties { static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |