From: <bo...@us...> - 2009-05-12 04:29:36
|
Revision: 316 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=316&view=rev Author: bodewig Date: 2009-05-12 04:29:34 +0000 (Tue, 12 May 2009) Log Message: ----------- wrap checked exception's Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-11 10:13:41 UTC (rev 315) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-12 04:29:34 UTC (rev 316) @@ -17,6 +17,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; +import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.net.URI; @@ -30,6 +31,8 @@ import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; +import net.sf.xmlunit.exceptions.ConfigurationException; +import net.sf.xmlunit.exceptions.XMLUnitRuntimeException; import org.w3c.dom.Document; public class Input { @@ -94,32 +97,46 @@ return fromStream(new ByteArrayInputStream(b)); } - public static Builder fromURL(URL url) throws Exception { - InputStream in = null; + public static Builder fromURL(URL url) { try { - in = url.openStream(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int read = -1; - byte[] buf = new byte[4096]; - while ((read = in.read(buf)) >= 0) { - if (read > 0) { - baos.write(buf, 0, read); + InputStream in = null; + try { + in = url.openStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + int read = -1; + byte[] buf = new byte[4096]; + while ((read = in.read(buf)) >= 0) { + if (read > 0) { + baos.write(buf, 0, read); + } } + return fromMemory(baos.toByteArray()); + } finally { + if (in != null) { + in.close(); + } } - return fromMemory(baos.toByteArray()); - } finally { - if (in != null) { - in.close(); - } + } catch (IOException ex) { + throw new XMLUnitRuntimeException(ex); } } - public static Builder fromURI(URI uri) throws Exception { - return fromURL(uri.toURL()); + public static Builder fromURI(URI uri) { + try { + return fromURL(uri.toURL()); + } catch (java.net.MalformedURLException ex) { + throw new IllegalArgumentException("uri " + uri + " is not an URL", + ex); + } } - public static Builder fromURI(String uri) throws Exception { + public static Builder fromURI(String uri) { + try { return fromURI(new URI(uri)); + } catch (java.net.URISyntaxException ex) { + throw new IllegalArgumentException("uri " + uri + " is not an URI", + ex); + } } public static interface TransformationBuilder extends Builder { @@ -168,8 +185,10 @@ } t.transform(source, r); return new DOMSource(r.getNode()); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (javax.xml.transform.TransformerConfigurationException e) { + throw new ConfigurationException(e); + } catch (javax.xml.transform.TransformerException e) { + throw new XMLUnitRuntimeException(e); } } } Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java 2009-05-12 04:29:34 UTC (rev 316) @@ -0,0 +1,24 @@ +/* + 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.exceptions; + +/** + * Exception thrown when anything inside JAXP throws a + * *ConfigurationException. + */ +public class ConfigurationException extends XMLUnitRuntimeException { + public ConfigurationException(Throwable cause) { + super(cause); + } +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java 2009-05-12 04:29:34 UTC (rev 316) @@ -0,0 +1,47 @@ +/* + 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.exceptions; + +/** + * Base class of any Exception thrown within XMLUnit. + */ +public class XMLUnitRuntimeException extends RuntimeException { + /** + * Inititializes the exeption. + * + * @param message the detail message + * @param cause the root cause of the exception + */ + public XMLUnitRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Inititializes an exeption without cause. + * + * @param message the detail message + */ + public XMLUnitRuntimeException(String message) { + this(message, null); + } + + /** + * Inititializes an exeption using the wrapped exception's message. + * + * @param message the detail message + */ + public XMLUnitRuntimeException(Throwable cause) { + this(cause != null ? cause.getMessage() : null, cause); + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2009-05-18 15:40:17
|
Revision: 320 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=320&view=rev Author: bodewig Date: 2009-05-18 15:40:13 +0000 (Mon, 18 May 2009) Log Message: ----------- rename XMLUnitRuntimeException to XMLUnitException Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitException.java Removed Paths: ------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-12 12:31:40 UTC (rev 319) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-18 15:40:13 UTC (rev 320) @@ -32,7 +32,7 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import net.sf.xmlunit.exceptions.ConfigurationException; -import net.sf.xmlunit.exceptions.XMLUnitRuntimeException; +import net.sf.xmlunit.exceptions.XMLUnitException; import org.w3c.dom.Document; public class Input { @@ -117,7 +117,7 @@ } } } catch (IOException ex) { - throw new XMLUnitRuntimeException(ex); + throw new XMLUnitException(ex); } } @@ -188,7 +188,7 @@ } catch (javax.xml.transform.TransformerConfigurationException e) { throw new ConfigurationException(e); } catch (javax.xml.transform.TransformerException e) { - throw new XMLUnitRuntimeException(e); + throw new XMLUnitException(e); } } } Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java 2009-05-12 12:31:40 UTC (rev 319) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java 2009-05-18 15:40:13 UTC (rev 320) @@ -17,7 +17,7 @@ * Exception thrown when anything inside JAXP throws a * *ConfigurationException. */ -public class ConfigurationException extends XMLUnitRuntimeException { +public class ConfigurationException extends XMLUnitException { public ConfigurationException(Throwable cause) { super(cause); } Copied: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitException.java (from rev 319, trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java) =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitException.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitException.java 2009-05-18 15:40:13 UTC (rev 320) @@ -0,0 +1,47 @@ +/* + 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.exceptions; + +/** + * Base class of any Exception thrown within XMLUnit. + */ +public class XMLUnitException extends RuntimeException { + /** + * Inititializes the exeption. + * + * @param message the detail message + * @param cause the root cause of the exception + */ + public XMLUnitException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Inititializes an exeption without cause. + * + * @param message the detail message + */ + public XMLUnitException(String message) { + this(message, null); + } + + /** + * Inititializes an exeption using the wrapped exception's message. + * + * @param message the detail message + */ + public XMLUnitException(Throwable cause) { + this(cause != null ? cause.getMessage() : null, cause); + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitException.java ___________________________________________________________________ Added: svn:mergeinfo + Added: svn:eol-style + native Deleted: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java 2009-05-12 12:31:40 UTC (rev 319) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java 2009-05-18 15:40:13 UTC (rev 320) @@ -1,47 +0,0 @@ -/* - 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.exceptions; - -/** - * Base class of any Exception thrown within XMLUnit. - */ -public class XMLUnitRuntimeException extends RuntimeException { - /** - * Inititializes the exeption. - * - * @param message the detail message - * @param cause the root cause of the exception - */ - public XMLUnitRuntimeException(String message, Throwable cause) { - super(message, cause); - } - - /** - * Inititializes an exeption without cause. - * - * @param message the detail message - */ - public XMLUnitRuntimeException(String message) { - this(message, null); - } - - /** - * Inititializes an exeption using the wrapped exception's message. - * - * @param message the detail message - */ - public XMLUnitRuntimeException(Throwable cause) { - this(cause != null ? cause.getMessage() : null, cause); - } -} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2009-05-25 15:05:38
|
Revision: 333 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=333&view=rev Author: bodewig Date: 2009-05-25 15:05:35 +0000 (Mon, 25 May 2009) Log Message: ----------- validation infrastructure Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/Languages.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ValidationProblem.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ValidationResult.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/Validator.java Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/Languages.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/Languages.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/Languages.java 2009-05-25 15:05:35 UTC (rev 333) @@ -0,0 +1,50 @@ +/* + 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.validation; + +import javax.xml.XMLConstants; + +/** + * Contants for the languages supported by XMLUnit's schema + * validation. More languages may be supported depending on your JAXP + * environment. + */ +public final class Languages { + private Languages() {} + + /** + * W3C XML Schema. + * + * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI + */ + public static final String W3C_XML_SCHEMA_NS_URI = + XMLConstants.W3C_XML_SCHEMA_NS_URI; + + /** + * DTD + * + * @see javax.xml.XMLConstants#XML_DTD_NS_URI + */ + public static final String XML_DTD_NS_URI = XMLConstants.XML_DTD_NS_URI; + + /** + * RELAX NG + * + * <p>This is most likely only supported if you use additional + * libraries and configure JAXP to use them.</p> + * + * @see javax.xml.XMLConstants#RELAXNG_NS_URI + */ + public static final String RELAXNG_NS_URI = XMLConstants.RELAXNG_NS_URI; +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/Languages.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ValidationProblem.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ValidationProblem.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ValidationProblem.java 2009-05-25 15:05:35 UTC (rev 333) @@ -0,0 +1,63 @@ +/* + 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.validation; + +/** + * A validation "problem" which may be an error or a warning. + */ +public class ValidationProblem { + public static enum ProblemType {ERROR, WARNING}; + + public static final int UNKNOWN = -1; + + private final int line, column; + private final ProblemType type; + private final String message; + + public ValidationProblem(String message, int line, int column, + ProblemType type) { + this.message = message; + this.line = line; + this.column = column; + this.type = type; + } + + /** + * The line where the problem occured or {@link #UNKNOWN UNKNOWN}. + */ + public int getLine() { + return line; + } + + /** + * The column where the problem occured or {@link #UNKNOWN UNKNOWN}. + */ + public int getColumn() { + return column; + } + + /** + * Whether this is an error or a warning. + */ + public ProblemType getType() { + return type; + } + + /** + * The problem's message. + */ + public String getMessage() { + return message; + } +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ValidationProblem.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ValidationResult.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ValidationResult.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ValidationResult.java 2009-05-25 15:05:35 UTC (rev 333) @@ -0,0 +1,41 @@ +/* + 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.validation; + +/** + * The result of a validation. + */ +public class ValidationResult { + private final boolean valid; + private final Iterable<ValidationProblem> problems; + + public ValidationResult(boolean valid, Iterable<ValidationProblem> problems) { + this.valid = valid; + this.problems = problems; + } + + /** + * Has the validation been successful? + */ + public boolean isValid() { + return valid; + } + + /** + * Retrieves the problems that have been found. + */ + public Iterable<ValidationProblem> getProblems() { + return problems; + } +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/ValidationResult.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/Validator.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/Validator.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/Validator.java 2009-05-25 15:05:35 UTC (rev 333) @@ -0,0 +1,83 @@ +/* + 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.validation; + +import javax.xml.transform.Source; + +/** + * Validates a piece of XML against a schema given in a supported + * language or the defintion of such a schema itself. + */ +public abstract class Validator { + private String schemaURI; + private Source[] sourceLocations; + + /** + * The URI (or for example the System ID in case of a DTD) that + * identifies the schema to validate or use during validation. + */ + public void setSchemaURI(String uri) { + this.schemaURI = uri; + } + + protected String getSchemaURI() { + return schemaURI; + } + + /** + * Where to find the schema. + */ + public void setSchemaSources(Source[] s) { + if (s != null) { + sourceLocations = new Source[s.length]; + System.arraycopy(s, 0, sourceLocations, 0, s.length); + } else { + sourceLocations = null; + } + } + + /** + * Where to find the schema. + */ + public final void setSchemaSource(Source s) { + setSchemaSources(s == null ? null : new Source[] {s}); + } + + protected Source[] getSchemaSources() { + return sourceLocations; + } + + /** + * Validates a schema. + * + * @throws UnsupportedOperationException if the language's + * implementation doesn't support schema validation + */ + public abstract ValidationResult validateSchema(); + + /** + * Validates an instance against the schema. + */ + public abstract ValidationResult validateInstance(Source instance); + + + /** + * Factory that obtains a Validator instance based on the schema language. + * + * @see Languages + */ + public static Validator forLanguage(String language) { + return null; + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/validation/Validator.java ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <bo...@us...> - 2010-04-29 06:44:48
|
Revision: 363 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=363&view=rev Author: bodewig Date: 2010-04-29 06:44:42 +0000 (Thu, 29 Apr 2010) Log Message: ----------- docs - and don't hard code Source implementations SAXSource can deal with Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java 2010-04-29 06:14:31 UTC (rev 362) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java 2010-04-29 06:44:42 UTC (rev 363) @@ -25,12 +25,22 @@ import net.sf.xmlunit.exceptions.XMLUnitException; import org.xml.sax.InputSource; +/** + * Conversion methods. + */ public final class Convert { private Convert() { } + /** + * Creates a SAX InputSource from a TraX Source. + * + * <p>May use an XSLT identity transformation if SAXSource cannot + * convert it directly.</p> + */ public static InputSource toInputSource(Source s) { try { - if (!(s instanceof SAXSource) && !(s instanceof StreamSource)) { + InputSource is = SAXSource.sourceToInputSource(s); + if (is == null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamResult r = new StreamResult(bos); TransformerFactory fac = TransformerFactory.newInstance(); @@ -38,8 +48,9 @@ t.transform(s, r); s = new StreamSource(new ByteArrayInputStream(bos .toByteArray())); + is = SAXSource.sourceToInputSource(s); } - return SAXSource.sourceToInputSource(s); + return is; } catch (javax.xml.transform.TransformerConfigurationException e) { throw new ConfigurationException(e); } catch (javax.xml.transform.TransformerException e) { Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java 2010-04-29 06:14:31 UTC (rev 362) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java 2010-04-29 06:44:42 UTC (rev 363) @@ -31,6 +31,9 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; +/** + * Simplified access to JAXP's XPath API. + */ public class XPathEngine { private final XPath xpath; @@ -42,10 +45,18 @@ } } + /** + * Create an XPathEngine that uses JAXP's default XPathFactory + * under the covers. + */ public XPathEngine() { this(XPathFactory.newInstance()); } + /** + * Returns a potentially empty collection of Nodes matching an + * XPath expression. + */ public Iterable<Node> selectNodes(String xPath, Source s) { try { NodeList nl = (NodeList) xpath.evaluate(xPath, @@ -57,6 +68,9 @@ } } + /** + * Evaluates an XPath expression and stringifies the result. + */ public String evaluate(String xPath, Source s) { try { return xpath.evaluate(xPath, Convert.toInputSource(s)); @@ -65,6 +79,11 @@ } } + /** + * Establish a namespace context. + * + * @param prefix2Uri maps from prefix to namespace URI. + */ public void setNamespaceContext(Map<String, String> prefix2Uri) { xpath.setNamespaceContext(new NC(prefix2Uri)); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-04-29 10:01:44
|
Revision: 368 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=368&view=rev Author: bodewig Date: 2010-04-29 10:01:38 +0000 (Thu, 29 Apr 2010) Log Message: ----------- extract reusable helpers Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/IterableNodeList.java Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java 2010-04-29 09:38:19 UTC (rev 367) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java 2010-04-29 10:01:38 UTC (rev 368) @@ -15,6 +15,14 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Iterator; +import java.util.Map; +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; @@ -57,4 +65,57 @@ throw new XMLUnitException(e); } } + + /** + * Creates a JAXP NamespaceContext from a Map prefix => Namespace URI. + */ + public static NamespaceContext + toNamespaceContext(Map<String, String> prefix2URI) { + final Map<String, String> copy = + new LinkedHashMap<String, String>(prefix2URI); + return new NamespaceContext() { + public String getNamespaceURI(String prefix) { + if (prefix == null) { + throw new IllegalArgumentException("prefix must not be null"); + } + if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { + return XMLConstants.XML_NS_URI; + } + if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { + return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; + } + String uri = copy.get(prefix); + return uri != null ? uri : XMLConstants.NULL_NS_URI; + } + + public String getPrefix(String uri) { + Iterator i = getPrefixes(uri); + return i.hasNext() ? (String) i.next() : null; + } + + public Iterator getPrefixes(String uri) { + if (uri == null) { + throw new IllegalArgumentException("uri must not be null"); + } + Collection<String> c = new HashSet<String>(); + boolean done = false; + if (XMLConstants.XML_NS_URI.equals(uri)) { + c.add(XMLConstants.XML_NS_PREFIX); + done = true; + } + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { + c.add(XMLConstants.XMLNS_ATTRIBUTE); + done = true; + } + if (!done) { + for (Map.Entry<String, String> entry : copy.entrySet()) { + if (uri.equals(entry.getValue())) { + c.add(entry.getKey()); + } + } + } + return c.iterator(); + } + }; + } } \ No newline at end of file Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/IterableNodeList.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/IterableNodeList.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/IterableNodeList.java 2010-04-29 10:01:38 UTC (rev 368) @@ -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.util.Iterator; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * Provides an iterable view to a NodeList, the Iterator that can be + * obtained from this Iterable will be read-only. + */ +public final class IterableNodeList implements Iterable<Node> { + private final NodeList nl; + private final int length; + + public IterableNodeList(NodeList nl) { + this.nl = nl; + length = nl.getLength(); + } + + public Iterator<Node> iterator() { + return new NodeListIterator(); + } + + private class NodeListIterator implements Iterator<Node> { + private int current = 0; + public void remove() { + throw new UnsupportedOperationException(); + } + public Node next() { + return nl.item(current++); + } + public boolean hasNext() { + return current < length; + } + } +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/IterableNodeList.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java 2010-04-29 09:38:19 UTC (rev 367) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java 2010-04-29 10:01:38 UTC (rev 368) @@ -13,13 +13,7 @@ */ package net.sf.xmlunit.xpath; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; import java.util.Map; -import javax.xml.XMLConstants; -import javax.xml.namespace.NamespaceContext; import javax.xml.transform.Source; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; @@ -28,6 +22,7 @@ import net.sf.xmlunit.exceptions.ConfigurationException; import net.sf.xmlunit.exceptions.XMLUnitException; import net.sf.xmlunit.util.Convert; +import net.sf.xmlunit.util.IterableNodeList; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -59,10 +54,10 @@ */ public Iterable<Node> selectNodes(String xPath, Source s) { try { - NodeList nl = (NodeList) xpath.evaluate(xPath, - Convert.toInputSource(s), - XPathConstants.NODESET); - return new IterableNodeList(nl); + return new IterableNodeList( + (NodeList) xpath.evaluate(xPath, Convert.toInputSource(s), + XPathConstants.NODESET) + ); } catch (XPathExpressionException ex) { throw new XMLUnitException(ex); } @@ -85,81 +80,7 @@ * @param prefix2Uri maps from prefix to namespace URI. */ public void setNamespaceContext(Map<String, String> prefix2Uri) { - xpath.setNamespaceContext(new NC(prefix2Uri)); + xpath.setNamespaceContext(Convert.toNamespaceContext(prefix2Uri)); } - private static class NC implements NamespaceContext { - private final Map<String, String> prefix2Uri; - - private NC(Map<String, String> prefix2Uri) { - this.prefix2Uri = prefix2Uri; - } - - public String getNamespaceURI(String prefix) { - if (prefix == null) { - throw new IllegalArgumentException("prefix must not be null"); - } - if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { - return XMLConstants.XML_NS_URI; - } - if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { - return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; - } - String uri = prefix2Uri.get(prefix); - return uri != null ? uri : XMLConstants.NULL_NS_URI; - } - - public String getPrefix(String uri) { - Iterator i = getPrefixes(uri); - return i.hasNext() ? (String) i.next() : null; - } - - public Iterator getPrefixes(String uri) { - if (uri == null) { - throw new IllegalArgumentException("uri must not be null"); - } - Collection<String> c = new HashSet<String>(); - boolean done = false; - if (XMLConstants.XML_NS_URI.equals(uri)) { - c.add(XMLConstants.XML_NS_PREFIX); - done = true; - } - if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { - c.add(XMLConstants.XMLNS_ATTRIBUTE); - done = true; - } - if (!done) { - for (Map.Entry<String, String> entry : prefix2Uri.entrySet()) { - if (uri.equals(entry.getValue())) { - c.add(entry.getKey()); - } - } - } - return c.iterator(); - } - } - - private static class IterableNodeList implements Iterable<Node> { - private final NodeList nl; - private final int length; - private int current = 0; - private IterableNodeList(NodeList nl) { - this.nl = nl; - length = nl.getLength(); - } - public Iterator<Node> iterator() { - return new Iterator<Node>() { - public void remove() { - throw new UnsupportedOperationException(); - } - public Node next() { - return nl.item(current++); - } - public boolean hasNext() { - return current < length; - } - }; - } - } - } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2013-02-03 08:35:18
|
Revision: 504 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=504&view=rev Author: bodewig Date: 2013-02-03 08:35:12 +0000 (Sun, 03 Feb 2013) Log Message: ----------- Actually, the NS lookup method is not needed when DOM3 is there Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Nodes.java Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java 2013-02-03 06:37:37 UTC (rev 503) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java 2013-02-03 08:35:12 UTC (rev 504) @@ -667,15 +667,17 @@ private static QName valueAsQName(Attr attribute) { String[] pieces = attribute.getValue().split(":"); if (pieces.length < 2) { - pieces = new String[] { "", pieces[0] }; + pieces = new String[] { null, pieces[0] }; } else if (pieces.length > 2) { pieces = new String[] { pieces[0], attribute.getValue().substring(pieces[0].length() + 1) }; } - return new QName(Nodes.findNamespaceURIForPrefix(attribute, pieces[0]), - pieces[1], pieces[0]); + if ("".equals(pieces[0])) { + pieces[0] = null; + } + return new QName(attribute.lookupNamespaceURI(pieces[0]), pieces[1]); } private static class Attributes { Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Nodes.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Nodes.java 2013-02-03 06:37:37 UTC (rev 503) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Nodes.java 2013-02-03 08:35:12 UTC (rev 504) @@ -113,35 +113,6 @@ } /** - * Looks up the namespace URI associated with a given prefix on a given node. - * @param onNode the reference node - * @param prefix the prefix to look for - * @return the URI or null of it cannot be found - */ - public static String findNamespaceURIForPrefix(Node onNode, String prefix) { - if (onNode != null && onNode instanceof Attr) { - onNode = ((Attr) onNode).getOwnerElement(); - } - while (onNode != null && onNode.getNodeType() != Node.ELEMENT_NODE) { - onNode = onNode.getParentNode(); - } - if (onNode == null) { - return null; - } - - if (prefix == null || "".equals(prefix)) { - prefix = XMLConstants.XMLNS_ATTRIBUTE; - } - Map<QName, String> attrs = getAttributes(onNode); - String uri = attrs.get(new QName(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, - prefix)); - if (uri != null) { - return uri; - } - return findNamespaceURIForPrefix(onNode.getParentNode(), prefix); - } - - /** * Trims textual content of this node, removes empty text and * CDATA children, recurses into its child nodes. * @param normalize whether to normalize whitespace as well This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |