feat(jdk8): move files to new folder to avoid resources compiled.
This commit is contained in:
150
jdkSrc/jdk8/javax/xml/bind/util/JAXBResult.java
Normal file
150
jdkSrc/jdk8/javax/xml/bind/util/JAXBResult.java
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code 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 General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package javax.xml.bind.util;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.UnmarshallerHandler;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
|
||||
/**
|
||||
* JAXP {@link javax.xml.transform.Result} implementation
|
||||
* that unmarshals a JAXB object.
|
||||
*
|
||||
* <p>
|
||||
* This utility class is useful to combine JAXB with
|
||||
* other Java/XML technologies.
|
||||
*
|
||||
* <p>
|
||||
* The following example shows how to use JAXB to unmarshal a document
|
||||
* resulting from an XSLT transformation.
|
||||
*
|
||||
* <blockquote>
|
||||
* <pre>
|
||||
* JAXBResult result = new JAXBResult(
|
||||
* JAXBContext.newInstance("org.acme.foo") );
|
||||
*
|
||||
* // set up XSLT transformation
|
||||
* TransformerFactory tf = TransformerFactory.newInstance();
|
||||
* Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
|
||||
*
|
||||
* // run transformation
|
||||
* t.transform(new StreamSource("document.xml"),result);
|
||||
*
|
||||
* // obtain the unmarshalled content tree
|
||||
* Object o = result.getResult();
|
||||
* </pre>
|
||||
* </blockquote>
|
||||
*
|
||||
* <p>
|
||||
* The fact that JAXBResult derives from SAXResult is an implementation
|
||||
* detail. Thus in general applications are strongly discouraged from
|
||||
* accessing methods defined on SAXResult.
|
||||
*
|
||||
* <p>
|
||||
* In particular it shall never attempt to call the setHandler,
|
||||
* setLexicalHandler, and setSystemId methods.
|
||||
*
|
||||
* @author
|
||||
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
|
||||
*/
|
||||
public class JAXBResult extends SAXResult {
|
||||
|
||||
/**
|
||||
* Creates a new instance that uses the specified
|
||||
* JAXBContext to unmarshal.
|
||||
*
|
||||
* @param context The JAXBContext that will be used to create the
|
||||
* necessary Unmarshaller. This parameter must not be null.
|
||||
* @exception JAXBException if an error is encountered while creating the
|
||||
* JAXBResult or if the context parameter is null.
|
||||
*/
|
||||
public JAXBResult( JAXBContext context ) throws JAXBException {
|
||||
this( ( context == null ) ? assertionFailed() : context.createUnmarshaller() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance that uses the specified
|
||||
* Unmarshaller to unmarshal an object.
|
||||
*
|
||||
* <p>
|
||||
* This JAXBResult object will use the specified Unmarshaller
|
||||
* instance. It is the caller's responsibility not to use the
|
||||
* same Unmarshaller for other purposes while it is being
|
||||
* used by this object.
|
||||
*
|
||||
* <p>
|
||||
* The primary purpose of this method is to allow the client
|
||||
* to configure Unmarshaller. Unless you know what you are doing,
|
||||
* it's easier and safer to pass a JAXBContext.
|
||||
*
|
||||
* @param _unmarshaller the unmarshaller. This parameter must not be null.
|
||||
* @throws JAXBException if an error is encountered while creating the
|
||||
* JAXBResult or the Unmarshaller parameter is null.
|
||||
*/
|
||||
public JAXBResult( Unmarshaller _unmarshaller ) throws JAXBException {
|
||||
if( _unmarshaller == null )
|
||||
throw new JAXBException(
|
||||
Messages.format( Messages.RESULT_NULL_UNMARSHALLER ) );
|
||||
|
||||
this.unmarshallerHandler = _unmarshaller.getUnmarshallerHandler();
|
||||
|
||||
super.setHandler(unmarshallerHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmarshaller that will be used to unmarshal
|
||||
* the input documents.
|
||||
*/
|
||||
private final UnmarshallerHandler unmarshallerHandler;
|
||||
|
||||
/**
|
||||
* Gets the unmarshalled object created by the transformation.
|
||||
*
|
||||
* @return
|
||||
* Always return a non-null object.
|
||||
*
|
||||
* @exception IllegalStateException
|
||||
* if this method is called before an object is unmarshalled.
|
||||
*
|
||||
* @exception JAXBException
|
||||
* if there is any unmarshalling error.
|
||||
* Note that the implementation is allowed to throw SAXException
|
||||
* during the parsing when it finds an error.
|
||||
*/
|
||||
public Object getResult() throws JAXBException {
|
||||
return unmarshallerHandler.getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to throw exception from the middle of a contructor chained call
|
||||
* to this
|
||||
*/
|
||||
private static Unmarshaller assertionFailed() throws JAXBException {
|
||||
throw new JAXBException( Messages.format( Messages.RESULT_NULL_CONTEXT ) );
|
||||
}
|
||||
}
|
||||
272
jdkSrc/jdk8/javax/xml/bind/util/JAXBSource.java
Normal file
272
jdkSrc/jdk8/javax/xml/bind/util/JAXBSource.java
Normal file
@@ -0,0 +1,272 @@
|
||||
/*
|
||||
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code 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 General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package javax.xml.bind.util;
|
||||
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.DTDHandler;
|
||||
import org.xml.sax.EntityResolver;
|
||||
import org.xml.sax.ErrorHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXNotRecognizedException;
|
||||
import org.xml.sax.SAXParseException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
import org.xml.sax.helpers.XMLFilterImpl;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import org.xml.sax.XMLFilter;
|
||||
|
||||
/**
|
||||
* JAXP {@link javax.xml.transform.Source} implementation
|
||||
* that marshals a JAXB-generated object.
|
||||
*
|
||||
* <p>
|
||||
* This utility class is useful to combine JAXB with
|
||||
* other Java/XML technologies.
|
||||
*
|
||||
* <p>
|
||||
* The following example shows how to use JAXB to marshal a document
|
||||
* for transformation by XSLT.
|
||||
*
|
||||
* <blockquote>
|
||||
* <pre>
|
||||
* MyObject o = // get JAXB content tree
|
||||
*
|
||||
* // jaxbContext is a JAXBContext object from which 'o' is created.
|
||||
* JAXBSource source = new JAXBSource( jaxbContext, o );
|
||||
*
|
||||
* // set up XSLT transformation
|
||||
* TransformerFactory tf = TransformerFactory.newInstance();
|
||||
* Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
|
||||
*
|
||||
* // run transformation
|
||||
* t.transform(source,new StreamResult(System.out));
|
||||
* </pre>
|
||||
* </blockquote>
|
||||
*
|
||||
* <p>
|
||||
* The fact that JAXBSource derives from SAXSource is an implementation
|
||||
* detail. Thus in general applications are strongly discouraged from
|
||||
* accessing methods defined on SAXSource. In particular,
|
||||
* the setXMLReader and setInputSource methods shall never be called.
|
||||
* The XMLReader object obtained by the getXMLReader method shall
|
||||
* be used only for parsing the InputSource object returned by
|
||||
* the getInputSource method.
|
||||
*
|
||||
* <p>
|
||||
* Similarly the InputSource object obtained by the getInputSource
|
||||
* method shall be used only for being parsed by the XMLReader object
|
||||
* returned by the getXMLReader.
|
||||
*
|
||||
* @author
|
||||
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
|
||||
*/
|
||||
public class JAXBSource extends SAXSource {
|
||||
|
||||
/**
|
||||
* Creates a new {@link javax.xml.transform.Source} for the given content object.
|
||||
*
|
||||
* @param context
|
||||
* JAXBContext that was used to create
|
||||
* <code>contentObject</code>. This context is used
|
||||
* to create a new instance of marshaller and must not be null.
|
||||
* @param contentObject
|
||||
* An instance of a JAXB-generated class, which will be
|
||||
* used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must
|
||||
* not be null.
|
||||
* @throws JAXBException if an error is encountered while creating the
|
||||
* JAXBSource or if either of the parameters are null.
|
||||
*/
|
||||
public JAXBSource( JAXBContext context, Object contentObject )
|
||||
throws JAXBException {
|
||||
|
||||
this(
|
||||
( context == null ) ?
|
||||
assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTEXT ) ) :
|
||||
context.createMarshaller(),
|
||||
|
||||
( contentObject == null ) ?
|
||||
assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTENT ) ) :
|
||||
contentObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link javax.xml.transform.Source} for the given content object.
|
||||
*
|
||||
* @param marshaller
|
||||
* A marshaller instance that will be used to marshal
|
||||
* <code>contentObject</code> into XML. This must be
|
||||
* created from a JAXBContext that was used to build
|
||||
* <code>contentObject</code> and must not be null.
|
||||
* @param contentObject
|
||||
* An instance of a JAXB-generated class, which will be
|
||||
* used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must
|
||||
* not be null.
|
||||
* @throws JAXBException if an error is encountered while creating the
|
||||
* JAXBSource or if either of the parameters are null.
|
||||
*/
|
||||
public JAXBSource( Marshaller marshaller, Object contentObject )
|
||||
throws JAXBException {
|
||||
|
||||
if( marshaller == null )
|
||||
throw new JAXBException(
|
||||
Messages.format( Messages.SOURCE_NULL_MARSHALLER ) );
|
||||
|
||||
if( contentObject == null )
|
||||
throw new JAXBException(
|
||||
Messages.format( Messages.SOURCE_NULL_CONTENT ) );
|
||||
|
||||
this.marshaller = marshaller;
|
||||
this.contentObject = contentObject;
|
||||
|
||||
super.setXMLReader(pseudoParser);
|
||||
// pass a dummy InputSource. We don't care
|
||||
super.setInputSource(new InputSource());
|
||||
}
|
||||
|
||||
private final Marshaller marshaller;
|
||||
private final Object contentObject;
|
||||
|
||||
// this object will pretend as an XMLReader.
|
||||
// no matter what parameter is specified to the parse method,
|
||||
// it just parse the contentObject.
|
||||
private final XMLReader pseudoParser = new XMLReader() {
|
||||
public boolean getFeature(String name) throws SAXNotRecognizedException {
|
||||
if(name.equals("http://xml.org/sax/features/namespaces"))
|
||||
return true;
|
||||
if(name.equals("http://xml.org/sax/features/namespace-prefixes"))
|
||||
return false;
|
||||
throw new SAXNotRecognizedException(name);
|
||||
}
|
||||
|
||||
public void setFeature(String name, boolean value) throws SAXNotRecognizedException {
|
||||
if(name.equals("http://xml.org/sax/features/namespaces") && value)
|
||||
return;
|
||||
if(name.equals("http://xml.org/sax/features/namespace-prefixes") && !value)
|
||||
return;
|
||||
throw new SAXNotRecognizedException(name);
|
||||
}
|
||||
|
||||
public Object getProperty(String name) throws SAXNotRecognizedException {
|
||||
if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
|
||||
return lexicalHandler;
|
||||
}
|
||||
throw new SAXNotRecognizedException(name);
|
||||
}
|
||||
|
||||
public void setProperty(String name, Object value) throws SAXNotRecognizedException {
|
||||
if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
|
||||
this.lexicalHandler = (LexicalHandler)value;
|
||||
return;
|
||||
}
|
||||
throw new SAXNotRecognizedException(name);
|
||||
}
|
||||
|
||||
private LexicalHandler lexicalHandler;
|
||||
|
||||
// we will store this value but never use it by ourselves.
|
||||
private EntityResolver entityResolver;
|
||||
public void setEntityResolver(EntityResolver resolver) {
|
||||
this.entityResolver = resolver;
|
||||
}
|
||||
public EntityResolver getEntityResolver() {
|
||||
return entityResolver;
|
||||
}
|
||||
|
||||
private DTDHandler dtdHandler;
|
||||
public void setDTDHandler(DTDHandler handler) {
|
||||
this.dtdHandler = handler;
|
||||
}
|
||||
public DTDHandler getDTDHandler() {
|
||||
return dtdHandler;
|
||||
}
|
||||
|
||||
// SAX allows ContentHandler to be changed during the parsing,
|
||||
// but JAXB doesn't. So this repeater will sit between those
|
||||
// two components.
|
||||
private XMLFilter repeater = new XMLFilterImpl();
|
||||
|
||||
public void setContentHandler(ContentHandler handler) {
|
||||
repeater.setContentHandler(handler);
|
||||
}
|
||||
public ContentHandler getContentHandler() {
|
||||
return repeater.getContentHandler();
|
||||
}
|
||||
|
||||
private ErrorHandler errorHandler;
|
||||
public void setErrorHandler(ErrorHandler handler) {
|
||||
this.errorHandler = handler;
|
||||
}
|
||||
public ErrorHandler getErrorHandler() {
|
||||
return errorHandler;
|
||||
}
|
||||
|
||||
public void parse(InputSource input) throws SAXException {
|
||||
parse();
|
||||
}
|
||||
|
||||
public void parse(String systemId) throws SAXException {
|
||||
parse();
|
||||
}
|
||||
|
||||
public void parse() throws SAXException {
|
||||
// parses a content object by using the given marshaller
|
||||
// SAX events will be sent to the repeater, and the repeater
|
||||
// will further forward it to an appropriate component.
|
||||
try {
|
||||
marshaller.marshal( contentObject, (XMLFilterImpl)repeater );
|
||||
} catch( JAXBException e ) {
|
||||
// wrap it to a SAXException
|
||||
SAXParseException se =
|
||||
new SAXParseException( e.getMessage(),
|
||||
null, null, -1, -1, e );
|
||||
|
||||
// if the consumer sets an error handler, it is our responsibility
|
||||
// to notify it.
|
||||
if(errorHandler!=null)
|
||||
errorHandler.fatalError(se);
|
||||
|
||||
// this is a fatal error. Even if the error handler
|
||||
// returns, we will abort anyway.
|
||||
throw se;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook to throw exception from the middle of a contructor chained call
|
||||
* to this
|
||||
*/
|
||||
private static Marshaller assertionFailed( String message )
|
||||
throws JAXBException {
|
||||
|
||||
throw new JAXBException( message );
|
||||
}
|
||||
}
|
||||
83
jdkSrc/jdk8/javax/xml/bind/util/Messages.java
Normal file
83
jdkSrc/jdk8/javax/xml/bind/util/Messages.java
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code 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 General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package javax.xml.bind.util;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* Formats error messages.
|
||||
*/
|
||||
class Messages
|
||||
{
|
||||
static String format( String property ) {
|
||||
return format( property, null );
|
||||
}
|
||||
|
||||
static String format( String property, Object arg1 ) {
|
||||
return format( property, new Object[]{arg1} );
|
||||
}
|
||||
|
||||
static String format( String property, Object arg1, Object arg2 ) {
|
||||
return format( property, new Object[]{arg1,arg2} );
|
||||
}
|
||||
|
||||
static String format( String property, Object arg1, Object arg2, Object arg3 ) {
|
||||
return format( property, new Object[]{arg1,arg2,arg3} );
|
||||
}
|
||||
|
||||
// add more if necessary.
|
||||
|
||||
/** Loads a string resource and formats it with specified arguments. */
|
||||
static String format( String property, Object[] args ) {
|
||||
String text = ResourceBundle.getBundle(Messages.class.getName()).getString(property);
|
||||
return MessageFormat.format(text,args);
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
// Message resources
|
||||
//
|
||||
//
|
||||
static final String UNRECOGNIZED_SEVERITY = // 1 arg
|
||||
"ValidationEventCollector.UnrecognizedSeverity";
|
||||
|
||||
static final String RESULT_NULL_CONTEXT = // 0 args
|
||||
"JAXBResult.NullContext";
|
||||
|
||||
static final String RESULT_NULL_UNMARSHALLER = // 0 arg
|
||||
"JAXBResult.NullUnmarshaller";
|
||||
|
||||
static final String SOURCE_NULL_CONTEXT = // 0 args
|
||||
"JAXBSource.NullContext";
|
||||
|
||||
static final String SOURCE_NULL_CONTENT = // 0 arg
|
||||
"JAXBSource.NullContent";
|
||||
|
||||
static final String SOURCE_NULL_MARSHALLER = // 0 arg
|
||||
"JAXBSource.NullMarshaller";
|
||||
|
||||
}
|
||||
113
jdkSrc/jdk8/javax/xml/bind/util/ValidationEventCollector.java
Normal file
113
jdkSrc/jdk8/javax/xml/bind/util/ValidationEventCollector.java
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code 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 General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package javax.xml.bind.util;
|
||||
|
||||
import javax.xml.bind.ValidationEvent;
|
||||
import javax.xml.bind.ValidationEventHandler;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link javax.xml.bind.ValidationEventHandler ValidationEventHandler}
|
||||
* implementation that collects all events.
|
||||
*
|
||||
* <p>
|
||||
* To use this class, create a new instance and pass it to the setEventHandler
|
||||
* method of the Validator, Unmarshaller, Marshaller class. After the call to
|
||||
* validate or unmarshal completes, call the getEvents method to retrieve all
|
||||
* the reported errors and warnings.
|
||||
*
|
||||
* @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
|
||||
* @see javax.xml.bind.Validator
|
||||
* @see javax.xml.bind.ValidationEventHandler
|
||||
* @see javax.xml.bind.ValidationEvent
|
||||
* @see javax.xml.bind.ValidationEventLocator
|
||||
* @since JAXB1.0
|
||||
*/
|
||||
public class ValidationEventCollector implements ValidationEventHandler
|
||||
{
|
||||
private final List<ValidationEvent> events = new ArrayList<ValidationEvent>();
|
||||
|
||||
/**
|
||||
* Return an array of ValidationEvent objects containing a copy of each of
|
||||
* the collected errors and warnings.
|
||||
*
|
||||
* @return
|
||||
* a copy of all the collected errors and warnings or an empty array
|
||||
* if there weren't any
|
||||
*/
|
||||
public ValidationEvent[] getEvents() {
|
||||
return events.toArray(new ValidationEvent[events.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all collected errors and warnings.
|
||||
*/
|
||||
public void reset() {
|
||||
events.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this event collector contains at least one
|
||||
* ValidationEvent.
|
||||
*
|
||||
* @return true if this event collector contains at least one
|
||||
* ValidationEvent, false otherwise
|
||||
*/
|
||||
public boolean hasEvents() {
|
||||
return !events.isEmpty();
|
||||
}
|
||||
|
||||
public boolean handleEvent( ValidationEvent event ) {
|
||||
events.add(event);
|
||||
|
||||
boolean retVal = true;
|
||||
switch( event.getSeverity() ) {
|
||||
case ValidationEvent.WARNING:
|
||||
retVal = true; // continue validation
|
||||
break;
|
||||
case ValidationEvent.ERROR:
|
||||
retVal = true; // continue validation
|
||||
break;
|
||||
case ValidationEvent.FATAL_ERROR:
|
||||
retVal = false; // halt validation
|
||||
break;
|
||||
default:
|
||||
_assert( false,
|
||||
Messages.format( Messages.UNRECOGNIZED_SEVERITY,
|
||||
event.getSeverity() ) );
|
||||
break;
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
private static void _assert( boolean b, String msg ) {
|
||||
if( !b ) {
|
||||
throw new InternalError( msg );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user