[jboss-svn-commits] JBoss Common SVN: r4825 - in jbossxb/trunk/src: main/java/org/jboss/xb/binding/parser and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Aug 4 09:25:43 EDT 2010
Author: alex.loubyansky at jboss.com
Date: 2010-08-04 09:25:43 -0400 (Wed, 04 Aug 2010)
New Revision: 4825
Added:
jbossxb/trunk/src/test/java/org/jboss/test/xml/WarnOnParserErrorsUnitTestCase.java
Modified:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/Unmarshaller.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallerFactory.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallerImpl.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/JBossXBParser.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/sax/SaxJBossXBParser.java
Log:
JBXB-252
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/Unmarshaller.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/Unmarshaller.java 2010-07-30 17:56:42 UTC (rev 4824)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/Unmarshaller.java 2010-08-04 13:25:43 UTC (rev 4825)
@@ -51,9 +51,28 @@
void setNamespaceAware(boolean namespaces) throws JBossXBException;
void setSchemaValidation(boolean validation) throws JBossXBException;
-
+
+ /**
+ * This property controls whether the (underlying) parser errors should be
+ * logged as warnings or should they terminate parsing with errors.
+ * The default is to terminate parsing by re-throwing parser errors.
+ */
+ void setWarnOnParserErrors(boolean value);
+
+ /**
+ * This property controls whether the (underlying) parser errors should be
+ * logged as warnings or should they terminate parsing with errors.
+ * The default is to terminate parsing by re-throwing parser errors.
+ *
+ * @return false if parser errors should be logged as warnings, otherwise - true
+ */
+ boolean getWarnOnParserErrors();
+
void setEntityResolver(EntityResolver entityResolver) throws JBossXBException;
+ /**
+ * @deprecated
+ */
void setErrorHandler(ErrorHandler errorHandler);
void mapFactoryToNamespace(ObjectModelFactory factory, String namespaceUri);
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallerFactory.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallerFactory.java 2010-07-30 17:56:42 UTC (rev 4824)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallerFactory.java 2010-08-04 13:25:43 UTC (rev 4825)
@@ -35,6 +35,7 @@
protected Map<String, Object> features;
protected Boolean validation;
protected Boolean namespaces;
+ protected Boolean warnOnParserErrors;
public static UnmarshallerFactory newInstance()
{
@@ -83,6 +84,27 @@
}
}
+ /**
+ * This property controls whether the (underlying) parser errors should be
+ * logged as warnings or should they terminate parsing with errors.
+ */
+ public void setWarnOnParserErrors(boolean value)
+ {
+ this.warnOnParserErrors = value;
+ }
+
+ /**
+ * This property controls whether the (underlying) parser errors should be
+ * logged as warnings or should they terminate parsing with errors.
+ * The default is to terminate parsing by re-throwing parser errors.
+ *
+ * @return false if parser errors should be logged as warnings, otherwise - true
+ */
+ public boolean getWarnOnParserErrors()
+ {
+ return warnOnParserErrors == null ? false : warnOnParserErrors;
+ }
+
// Inner
static class UnmarshallerFactoryImpl
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallerImpl.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallerImpl.java 2010-07-30 17:56:42 UTC (rev 4824)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallerImpl.java 2010-08-04 13:25:43 UTC (rev 4825)
@@ -92,6 +92,16 @@
// todo reader.setErrorHandler(errorHandler);
}
+ public void setWarnOnParserErrors(boolean value)
+ {
+ parser.setWarnOnParserErrors(value);
+ }
+
+ public boolean getWarnOnParserErrors()
+ {
+ return parser.getWarnOnParserErrors();
+ }
+
public void mapFactoryToNamespace(ObjectModelFactory factory, String namespaceUri)
{
if(builder == null)
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/JBossXBParser.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/JBossXBParser.java 2010-07-30 17:56:42 UTC (rev 4824)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/JBossXBParser.java 2010-08-04 13:25:43 UTC (rev 4825)
@@ -60,6 +60,22 @@
public void endDTD();
}
+ /**
+ * This property controls whether the (underlying) parser errors should be
+ * logged as warnings or should they terminate parsing with errors.
+ * The default is to terminate parsing by re-throwing parser errors.
+ */
+ void setWarnOnParserErrors(boolean value);
+
+ /**
+ * This property controls whether the (underlying) parser errors should be
+ * logged as warnings or should they terminate parsing with errors.
+ * The default is to terminate parsing by re-throwing parser errors.
+ *
+ * @return false if parser errors should be logged as warnings, otherwise - true
+ */
+ boolean getWarnOnParserErrors();
+
void setEntityResolver(EntityResolver entityResolver) throws JBossXBException;
void setProperty(String name, Object value);
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/sax/SaxJBossXBParser.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/sax/SaxJBossXBParser.java 2010-07-30 17:56:42 UTC (rev 4824)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/sax/SaxJBossXBParser.java 2010-08-04 13:25:43 UTC (rev 4825)
@@ -70,6 +70,7 @@
private final XMLReader reader;
private JBossXBParser.ContentHandler contentHandler;
private boolean trace;
+ private boolean warnOnParserErrors;
public SaxJBossXBParser()
throws JBossXBException
@@ -122,6 +123,23 @@
// JBossXBParser implementation
+ public void setWarnOnParserErrors(boolean value)
+ {
+ this.warnOnParserErrors = value;
+ }
+
+ /**
+ * This property controls whether the (underlying) parser errors should be
+ * logged as warnings or should they terminate parsing with errors.
+ * The default is to terminate parsing by re-throwing parser errors.
+ *
+ * @return false if parser errors should be logged as warnings, otherwise - true
+ */
+ public boolean getWarnOnParserErrors()
+ {
+ return this.warnOnParserErrors;
+ }
+
public void setEntityResolver(EntityResolver entityResolver)
throws JBossXBException
{
@@ -392,7 +410,10 @@
public void error(SAXParseException exception) throws SAXException
{
- throw new SAXException(formatMessage(exception));
+ if(this.warnOnParserErrors)
+ log.warn(formatMessage(exception));
+ else
+ throw new SAXException(formatMessage(exception));
}
public void fatalError(SAXParseException exception) throws SAXException
Added: jbossxb/trunk/src/test/java/org/jboss/test/xml/WarnOnParserErrorsUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/WarnOnParserErrorsUnitTestCase.java (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/WarnOnParserErrorsUnitTestCase.java 2010-08-04 13:25:43 UTC (rev 4825)
@@ -0,0 +1,165 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.xml;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.jboss.xb.binding.JBossXBException;
+import org.jboss.xb.binding.UnmarshallerFactory;
+import org.jboss.xb.binding.Unmarshaller;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.builder.JBossXBBuilder;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
+ * @version <tt>$Revision: 45337 $</tt>
+ */
+public class WarnOnParserErrorsUnitTestCase
+ extends AbstractJBossXBTest
+{
+ private static final String XSD = "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'" +
+ " elementFormDefault='qualified'" +
+ " version='1.0'>" +
+ "<xsd:element name='e'>" +
+ " <xsd:complexType>" +
+ " <xsd:sequence>" +
+ " <xsd:element name='value1' type='xsd:string'/>" +
+ " <xsd:element name='value2' type='xsd:string'/>" +
+ " </xsd:sequence>" +
+ " </xsd:complexType>" +
+ "</xsd:element>" +
+ "</xsd:schema>";
+
+ private static final String XML =
+ "<e xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation=''>" +
+ "<value2>2</value2>" +
+ "<value1>1</value1>" +
+ "</e>";
+
+ public WarnOnParserErrorsUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ private boolean useUnorderedSequence;
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ useUnorderedSequence = JBossXBBuilder.isUseUnorderedSequence();
+ JBossXBBuilder.setUseUnorderedSequence(true);
+ }
+
+ public void tearDown() throws Exception
+ {
+ super.tearDown();
+ JBossXBBuilder.setUseUnorderedSequence(useUnorderedSequence);
+ }
+
+ public void testWarn() throws Exception
+ {
+ SchemaBinding schema = JBossXBBuilder.build(E.class, true);
+
+ EntityResolver resolver = new EntityResolver(){
+ @Override
+ public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
+ {
+ return new InputSource(new StringReader(XSD));
+ }};
+
+ UnmarshallerFactory factory = UnmarshallerFactory.newInstance();
+
+ Unmarshaller unmarshaller = factory.newUnmarshaller();
+ unmarshaller.setSchemaValidation(true);
+ unmarshaller.setEntityResolver(resolver);
+ unmarshaller.setWarnOnParserErrors(true);
+
+ Object o = unmarshaller.unmarshal(new StringReader(XML), schema);
+ assertNotNull(o);
+ assertTrue(o instanceof E);
+ E e = (E)o;
+ assertEquals("1", e.getValue1());
+ assertEquals("2", e.getValue2());
+ }
+
+ public void testError() throws Exception
+ {
+ SchemaBinding schema = JBossXBBuilder.build(E.class, true);
+
+ EntityResolver resolver = new EntityResolver(){
+ @Override
+ public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
+ {
+ return new InputSource(new StringReader(XSD));
+ }};
+
+ UnmarshallerFactory factory = UnmarshallerFactory.newInstance();
+
+ Unmarshaller unmarshaller = factory.newUnmarshaller();
+ unmarshaller.setSchemaValidation(true);
+ unmarshaller.setEntityResolver(resolver);
+ unmarshaller.setWarnOnParserErrors(false);
+
+ try
+ {
+ unmarshaller.unmarshal(new StringReader(XML), schema);
+ fail("validation should have failed");
+ }
+ catch(JBossXBException e)
+ {
+ }
+ }
+
+ // Inner
+
+ @XmlRootElement(name="e")
+ public static final class E
+ {
+ private String value1;
+ private String value2;
+
+ public String getValue1()
+ {
+ return value1;
+ }
+
+ public void setValue1(String value)
+ {
+ this.value1 = value;
+ }
+
+ public String getValue2()
+ {
+ return value2;
+ }
+
+ public void setValue2(String value)
+ {
+ this.value2 = value;
+ }
+ }
+}
More information about the jboss-svn-commits
mailing list