feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,495 @@
/*
* 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.helpers;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.attachment.AttachmentMarshaller;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.Schema;
import java.io.UnsupportedEncodingException;
import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
// J2SE1.4 feature
// import java.nio.charset.Charset;
// import java.nio.charset.UnsupportedCharsetException;
/**
* Partial default <tt>Marshaller</tt> implementation.
*
* <p>
* This class provides a partial default implementation for the
* {@link javax.xml.bind.Marshaller} interface.
*
* <p>
* The only methods that a JAXB Provider has to implement are
* {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.transform.Result)},
* {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.stream.XMLStreamWriter)}, and
* {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.stream.XMLEventWriter)}.
*
* @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
* @see javax.xml.bind.Marshaller
* @since JAXB1.0
*/
public abstract class AbstractMarshallerImpl implements Marshaller
{
/** handler that will be used to process errors and warnings during marshal */
private ValidationEventHandler eventHandler =
new DefaultValidationEventHandler();
//J2SE1.4 feature
//private Charset encoding = null;
/** store the value of the encoding property. */
private String encoding = "UTF-8";
/** store the value of the schemaLocation property. */
private String schemaLocation = null;
/** store the value of the noNamespaceSchemaLocation property. */
private String noNSSchemaLocation = null;
/** store the value of the formattedOutput property. */
private boolean formattedOutput = false;
/** store the value of the fragment property. */
private boolean fragment = false;
public final void marshal( Object obj, java.io.OutputStream os )
throws JAXBException {
checkNotNull( obj, "obj", os, "os" );
marshal( obj, new StreamResult(os) );
}
public void marshal(Object jaxbElement, File output) throws JAXBException {
checkNotNull(jaxbElement, "jaxbElement", output, "output" );
try {
OutputStream os = new BufferedOutputStream(new FileOutputStream(output));
try {
marshal( jaxbElement, new StreamResult(os) );
} finally {
os.close();
}
} catch (IOException e) {
throw new JAXBException(e);
}
}
public final void marshal( Object obj, java.io.Writer w )
throws JAXBException {
checkNotNull( obj, "obj", w, "writer" );
marshal( obj, new StreamResult(w) );
}
public final void marshal( Object obj, org.xml.sax.ContentHandler handler )
throws JAXBException {
checkNotNull( obj, "obj", handler, "handler" );
marshal( obj, new SAXResult(handler) );
}
public final void marshal( Object obj, org.w3c.dom.Node node )
throws JAXBException {
checkNotNull( obj, "obj", node, "node" );
marshal( obj, new DOMResult(node) );
}
/**
* By default, the getNode method is unsupported and throw
* an {@link java.lang.UnsupportedOperationException}.
*
* Implementations that choose to support this method must
* override this method.
*/
public org.w3c.dom.Node getNode( Object obj ) throws JAXBException {
checkNotNull( obj, "obj", Boolean.TRUE, "foo" );
throw new UnsupportedOperationException();
}
/**
* Convenience method for getting the current output encoding.
*
* @return the current encoding or "UTF-8" if it hasn't been set.
*/
protected String getEncoding() {
return encoding;
}
/**
* Convenience method for setting the output encoding.
*
* @param encoding a valid encoding as specified in the Marshaller class
* documentation
*/
protected void setEncoding( String encoding ) {
this.encoding = encoding;
}
/**
* Convenience method for getting the current schemaLocation.
*
* @return the current schemaLocation or null if it hasn't been set
*/
protected String getSchemaLocation() {
return schemaLocation;
}
/**
* Convenience method for setting the schemaLocation.
*
* @param location the schemaLocation value
*/
protected void setSchemaLocation( String location ) {
schemaLocation = location;
}
/**
* Convenience method for getting the current noNamespaceSchemaLocation.
*
* @return the current noNamespaceSchemaLocation or null if it hasn't
* been set
*/
protected String getNoNSSchemaLocation() {
return noNSSchemaLocation;
}
/**
* Convenience method for setting the noNamespaceSchemaLocation.
*
* @param location the noNamespaceSchemaLocation value
*/
protected void setNoNSSchemaLocation( String location ) {
noNSSchemaLocation = location;
}
/**
* Convenience method for getting the formatted output flag.
*
* @return the current value of the formatted output flag or false if
* it hasn't been set.
*/
protected boolean isFormattedOutput() {
return formattedOutput;
}
/**
* Convenience method for setting the formatted output flag.
*
* @param v value of the formatted output flag.
*/
protected void setFormattedOutput( boolean v ) {
formattedOutput = v;
}
/**
* Convenience method for getting the fragment flag.
*
* @return the current value of the fragment flag or false if
* it hasn't been set.
*/
protected boolean isFragment() {
return fragment;
}
/**
* Convenience method for setting the fragment flag.
*
* @param v value of the fragment flag.
*/
protected void setFragment( boolean v ) {
fragment = v;
}
static String[] aliases = {
"UTF-8", "UTF8",
"UTF-16", "Unicode",
"UTF-16BE", "UnicodeBigUnmarked",
"UTF-16LE", "UnicodeLittleUnmarked",
"US-ASCII", "ASCII",
"TIS-620", "TIS620",
// taken from the project-X parser
"ISO-10646-UCS-2", "Unicode",
"EBCDIC-CP-US", "cp037",
"EBCDIC-CP-CA", "cp037",
"EBCDIC-CP-NL", "cp037",
"EBCDIC-CP-WT", "cp037",
"EBCDIC-CP-DK", "cp277",
"EBCDIC-CP-NO", "cp277",
"EBCDIC-CP-FI", "cp278",
"EBCDIC-CP-SE", "cp278",
"EBCDIC-CP-IT", "cp280",
"EBCDIC-CP-ES", "cp284",
"EBCDIC-CP-GB", "cp285",
"EBCDIC-CP-FR", "cp297",
"EBCDIC-CP-AR1", "cp420",
"EBCDIC-CP-HE", "cp424",
"EBCDIC-CP-BE", "cp500",
"EBCDIC-CP-CH", "cp500",
"EBCDIC-CP-ROECE", "cp870",
"EBCDIC-CP-YU", "cp870",
"EBCDIC-CP-IS", "cp871",
"EBCDIC-CP-AR2", "cp918",
// IANA also defines two that JDK 1.2 doesn't handle:
// EBCDIC-CP-GR --> CP423
// EBCDIC-CP-TR --> CP905
};
/**
* Gets the corresponding Java encoding name from an IANA name.
*
* This method is a helper method for the derived class to convert
* encoding names.
*
* @exception UnsupportedEncodingException
* If this implementation couldn't find the Java encoding name.
*/
protected String getJavaEncoding( String encoding ) throws UnsupportedEncodingException {
try {
"1".getBytes(encoding);
return encoding;
} catch( UnsupportedEncodingException e ) {
// try known alias
for( int i=0; i<aliases.length; i+=2 ) {
if(encoding.equals(aliases[i])) {
"1".getBytes(aliases[i+1]);
return aliases[i+1];
}
}
throw new UnsupportedEncodingException(encoding);
}
/* J2SE1.4 feature
try {
this.encoding = Charset.forName( _encoding );
} catch( UnsupportedCharsetException uce ) {
throw new JAXBException( uce );
}
*/
}
/**
* Default implementation of the setProperty method handles
* the four defined properties in Marshaller. If a provider
* needs to handle additional properties, it should override
* this method in a derived class.
*/
public void setProperty( String name, Object value )
throws PropertyException {
if( name == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
}
// recognize and handle four pre-defined properties.
if( JAXB_ENCODING.equals(name) ) {
checkString( name, value );
setEncoding( (String)value );
return;
}
if( JAXB_FORMATTED_OUTPUT.equals(name) ) {
checkBoolean( name, value );
setFormattedOutput((Boolean) value );
return;
}
if( JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name) ) {
checkString( name, value );
setNoNSSchemaLocation( (String)value );
return;
}
if( JAXB_SCHEMA_LOCATION.equals(name) ) {
checkString( name, value );
setSchemaLocation( (String)value );
return;
}
if( JAXB_FRAGMENT.equals(name) ) {
checkBoolean(name, value);
setFragment((Boolean) value );
return;
}
throw new PropertyException(name, value);
}
/**
* Default implementation of the getProperty method handles
* the four defined properties in Marshaller. If a provider
* needs to support additional provider specific properties,
* it should override this method in a derived class.
*/
public Object getProperty( String name )
throws PropertyException {
if( name == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
}
// recognize and handle four pre-defined properties.
if( JAXB_ENCODING.equals(name) )
return getEncoding();
if( JAXB_FORMATTED_OUTPUT.equals(name) )
return isFormattedOutput()?Boolean.TRUE:Boolean.FALSE;
if( JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(name) )
return getNoNSSchemaLocation();
if( JAXB_SCHEMA_LOCATION.equals(name) )
return getSchemaLocation();
if( JAXB_FRAGMENT.equals(name) )
return isFragment()?Boolean.TRUE:Boolean.FALSE;
throw new PropertyException(name);
}
/**
* @see javax.xml.bind.Marshaller#getEventHandler()
*/
public ValidationEventHandler getEventHandler() throws JAXBException {
return eventHandler;
}
/**
* @see javax.xml.bind.Marshaller#setEventHandler(ValidationEventHandler)
*/
public void setEventHandler(ValidationEventHandler handler)
throws JAXBException {
if( handler == null ) {
eventHandler = new DefaultValidationEventHandler();
} else {
eventHandler = handler;
}
}
/*
* assert that the given object is a Boolean
*/
private void checkBoolean( String name, Object value ) throws PropertyException {
if(!(value instanceof Boolean))
throw new PropertyException(
Messages.format( Messages.MUST_BE_BOOLEAN, name ) );
}
/*
* assert that the given object is a String
*/
private void checkString( String name, Object value ) throws PropertyException {
if(!(value instanceof String))
throw new PropertyException(
Messages.format( Messages.MUST_BE_STRING, name ) );
}
/*
* assert that the parameters are not null
*/
private void checkNotNull( Object o1, String o1Name,
Object o2, String o2Name ) {
if( o1 == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, o1Name ) );
}
if( o2 == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, o2Name ) );
}
}
public void marshal(Object obj, XMLEventWriter writer)
throws JAXBException {
throw new UnsupportedOperationException();
}
public void marshal(Object obj, XMLStreamWriter writer)
throws JAXBException {
throw new UnsupportedOperationException();
}
public void setSchema(Schema schema) {
throw new UnsupportedOperationException();
}
public Schema getSchema() {
throw new UnsupportedOperationException();
}
public void setAdapter(XmlAdapter adapter) {
if(adapter==null)
throw new IllegalArgumentException();
setAdapter((Class)adapter.getClass(),adapter);
}
public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
throw new UnsupportedOperationException();
}
public <A extends XmlAdapter> A getAdapter(Class<A> type) {
throw new UnsupportedOperationException();
}
public void setAttachmentMarshaller(AttachmentMarshaller am) {
throw new UnsupportedOperationException();
}
public AttachmentMarshaller getAttachmentMarshaller() {
throw new UnsupportedOperationException();
}
public void setListener(Listener listener) {
throw new UnsupportedOperationException();
}
public Listener getListener() {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,435 @@
/*
* 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.helpers;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.w3c.dom.Node;
import javax.xml.bind.JAXBException;
import javax.xml.bind.PropertyException;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.attachment.AttachmentUnmarshaller;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import java.io.File;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Partial default <tt>Unmarshaller</tt> implementation.
*
* <p>
* This class provides a partial default implementation for the
* {@link javax.xml.bind.Unmarshaller}interface.
*
* <p>
* A JAXB Provider has to implement five methods (getUnmarshallerHandler,
* unmarshal(Node), unmarshal(XMLReader,InputSource),
* unmarshal(XMLStreamReader), and unmarshal(XMLEventReader).
*
* @author <ul>
* <li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li>
* </ul>
* @see javax.xml.bind.Unmarshaller
* @since JAXB1.0
*/
public abstract class AbstractUnmarshallerImpl implements Unmarshaller
{
/** handler that will be used to process errors and warnings during unmarshal */
private ValidationEventHandler eventHandler =
new DefaultValidationEventHandler();
/** whether or not the unmarshaller will validate */
protected boolean validating = false;
/**
* XMLReader that will be used to parse a document.
*/
private XMLReader reader = null;
/**
* Obtains a configured XMLReader.
*
* This method is used when the client-specified
* {@link SAXSource} object doesn't have XMLReader.
*
* {@link Unmarshaller} is not re-entrant, so we will
* only use one instance of XMLReader.
*/
protected XMLReader getXMLReader() throws JAXBException {
if(reader==null) {
try {
SAXParserFactory parserFactory;
parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
// there is no point in asking a validation because
// there is no guarantee that the document will come with
// a proper schemaLocation.
parserFactory.setValidating(false);
reader = parserFactory.newSAXParser().getXMLReader();
} catch( ParserConfigurationException e ) {
throw new JAXBException(e);
} catch( SAXException e ) {
throw new JAXBException(e);
}
}
return reader;
}
public Object unmarshal( Source source ) throws JAXBException {
if( source == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) );
}
if(source instanceof SAXSource)
return unmarshal( (SAXSource)source );
if(source instanceof StreamSource)
return unmarshal( streamSourceToInputSource((StreamSource)source));
if(source instanceof DOMSource)
return unmarshal( ((DOMSource)source).getNode() );
// we don't handle other types of Source
throw new IllegalArgumentException();
}
// use the client specified XMLReader contained in the SAXSource.
private Object unmarshal( SAXSource source ) throws JAXBException {
XMLReader r = source.getXMLReader();
if( r == null )
r = getXMLReader();
return unmarshal( r, source.getInputSource() );
}
/**
* Unmarshals an object by using the specified XMLReader and the InputSource.
*
* The callee should call the setErrorHandler method of the XMLReader
* so that errors are passed to the client-specified ValidationEventHandler.
*/
protected abstract Object unmarshal( XMLReader reader, InputSource source ) throws JAXBException;
public final Object unmarshal( InputSource source ) throws JAXBException {
if( source == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) );
}
return unmarshal( getXMLReader(), source );
}
private Object unmarshal( String url ) throws JAXBException {
return unmarshal( new InputSource(url) );
}
public final Object unmarshal( URL url ) throws JAXBException {
if( url == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "url" ) );
}
return unmarshal( url.toExternalForm() );
}
public final Object unmarshal( File f ) throws JAXBException {
if( f == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "file" ) );
}
try {
// copied from JAXP
String path = f.getAbsolutePath();
if (File.separatorChar != '/')
path = path.replace(File.separatorChar, '/');
if (!path.startsWith("/"))
path = "/" + path;
if (!path.endsWith("/") && f.isDirectory())
path = path + "/";
return unmarshal(new URL("file", "", path));
} catch( MalformedURLException e ) {
throw new IllegalArgumentException(e.getMessage());
}
}
public final Object unmarshal( java.io.InputStream is )
throws JAXBException {
if( is == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "is" ) );
}
InputSource isrc = new InputSource( is );
return unmarshal( isrc );
}
public final Object unmarshal( Reader reader ) throws JAXBException {
if( reader == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "reader" ) );
}
InputSource isrc = new InputSource( reader );
return unmarshal( isrc );
}
private static InputSource streamSourceToInputSource( StreamSource ss ) {
InputSource is = new InputSource();
is.setSystemId( ss.getSystemId() );
is.setByteStream( ss.getInputStream() );
is.setCharacterStream( ss.getReader() );
return is;
}
/**
* Indicates whether or not the Unmarshaller is configured to validate
* during unmarshal operations.
* <p>
* <i><b>Note:</b> I named this method isValidating() to stay in-line
* with JAXP, as opposed to naming it getValidating(). </i>
*
* @return true if the Unmarshaller is configured to validate during
* unmarshal operations, false otherwise
* @throws JAXBException if an error occurs while retrieving the validating
* flag
*/
public boolean isValidating() throws JAXBException {
return validating;
}
/**
* Allow an application to register a validation event handler.
* <p>
* The validation event handler will be called by the JAXB Provider if any
* validation errors are encountered during calls to any of the
* <tt>unmarshal</tt> methods. If the client application does not register
* a validation event handler before invoking the unmarshal methods, then
* all validation events will be silently ignored and may result in
* unexpected behaviour.
*
* @param handler the validation event handler
* @throws JAXBException if an error was encountered while setting the
* event handler
*/
public void setEventHandler(ValidationEventHandler handler)
throws JAXBException {
if( handler == null ) {
eventHandler = new DefaultValidationEventHandler();
} else {
eventHandler = handler;
}
}
/**
* Specifies whether or not the Unmarshaller should validate during
* unmarshal operations. By default, the <tt>Unmarshaller</tt> does
* not validate.
* <p>
* This method may only be invoked before or after calling one of the
* unmarshal methods.
*
* @param validating true if the Unmarshaller should validate during
* unmarshal, false otherwise
* @throws JAXBException if an error occurred while enabling or disabling
* validation at unmarshal time
*/
public void setValidating(boolean validating) throws JAXBException {
this.validating = validating;
}
/**
* Return the current event handler or the default event handler if one
* hasn't been set.
*
* @return the current ValidationEventHandler or the default event handler
* if it hasn't been set
* @throws JAXBException if an error was encountered while getting the
* current event handler
*/
public ValidationEventHandler getEventHandler() throws JAXBException {
return eventHandler;
}
/**
* Creates an UnmarshalException from a SAXException.
*
* This is an utility method provided for the derived classes.
*
* <p>
* When a provider-implemented ContentHandler wants to throw a
* JAXBException, it needs to wrap the exception by a SAXException.
* If the unmarshaller implementation blindly wrap SAXException
* by JAXBException, such an exception will be a JAXBException
* wrapped by a SAXException wrapped by another JAXBException.
* This is silly.
*
* <p>
* This method checks the nested exception of SAXException
* and reduce those excessive wrapping.
*
* @return the resulting UnmarshalException
*/
protected UnmarshalException createUnmarshalException( SAXException e ) {
// check the nested exception to see if it's an UnmarshalException
Exception nested = e.getException();
if(nested instanceof UnmarshalException)
return (UnmarshalException)nested;
if(nested instanceof RuntimeException)
// typically this is an unexpected exception,
// just throw it rather than wrap it, so that the full stack
// trace can be displayed.
throw (RuntimeException)nested;
// otherwise simply wrap it
if(nested!=null)
return new UnmarshalException(nested);
else
return new UnmarshalException(e);
}
/**
* Default implementation of the setProperty method always
* throws PropertyException since there are no required
* properties. If a provider needs to handle additional
* properties, it should override this method in a derived class.
*/
public void setProperty( String name, Object value )
throws PropertyException {
if( name == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
}
throw new PropertyException(name, value);
}
/**
* Default implementation of the getProperty method always
* throws PropertyException since there are no required
* properties. If a provider needs to handle additional
* properties, it should override this method in a derived class.
*/
public Object getProperty( String name )
throws PropertyException {
if( name == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
}
throw new PropertyException(name);
}
public Object unmarshal(XMLEventReader reader) throws JAXBException {
throw new UnsupportedOperationException();
}
public Object unmarshal(XMLStreamReader reader) throws JAXBException {
throw new UnsupportedOperationException();
}
public <T> JAXBElement<T> unmarshal(Node node, Class<T> expectedType) throws JAXBException {
throw new UnsupportedOperationException();
}
public <T> JAXBElement<T> unmarshal(Source source, Class<T> expectedType) throws JAXBException {
throw new UnsupportedOperationException();
}
public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> expectedType) throws JAXBException {
throw new UnsupportedOperationException();
}
public <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class<T> expectedType) throws JAXBException {
throw new UnsupportedOperationException();
}
public void setSchema(Schema schema) {
throw new UnsupportedOperationException();
}
public Schema getSchema() {
throw new UnsupportedOperationException();
}
public void setAdapter(XmlAdapter adapter) {
if(adapter==null)
throw new IllegalArgumentException();
setAdapter((Class)adapter.getClass(),adapter);
}
public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
throw new UnsupportedOperationException();
}
public <A extends XmlAdapter> A getAdapter(Class<A> type) {
throw new UnsupportedOperationException();
}
public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) {
throw new UnsupportedOperationException();
}
public AttachmentUnmarshaller getAttachmentUnmarshaller() {
throw new UnsupportedOperationException();
}
public void setListener(Listener listener) {
throw new UnsupportedOperationException();
}
public Listener getListener() {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,131 @@
/*
* 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.helpers;
import org.w3c.dom.Node;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.ValidationEventLocator;
import java.net.URL;
/**
* <p>
* JAXB 1.0 only default validation event handler. This is the default
* handler for all objects created from a JAXBContext that is managing
* schema-derived code generated by a JAXB 1.0 binding compiler.
*
* <p>
* This handler causes the unmarshal and validate operations to fail on the first
* error or fatal error.
*
* <p>
* This handler is not the default handler for JAXB mapped classes following
* JAXB 2.0 or later versions. Default validation event handling has changed
* and is specified in {@link javax.xml.bind.Unmarshaller} and
* {@link javax.xml.bind.Marshaller}.
*
* @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
* @see javax.xml.bind.Unmarshaller
* @see javax.xml.bind.Validator
* @see javax.xml.bind.ValidationEventHandler
* @since JAXB1.0
*/
public class DefaultValidationEventHandler implements ValidationEventHandler {
public boolean handleEvent( ValidationEvent event ) {
if( event == null ) {
throw new IllegalArgumentException();
}
// calculate the severity prefix and return value
String severity = null;
boolean retVal = false;
switch ( event.getSeverity() ) {
case ValidationEvent.WARNING:
severity = Messages.format( Messages.WARNING );
retVal = true; // continue after warnings
break;
case ValidationEvent.ERROR:
severity = Messages.format( Messages.ERROR );
retVal = false; // terminate after errors
break;
case ValidationEvent.FATAL_ERROR:
severity = Messages.format( Messages.FATAL_ERROR );
retVal = false; // terminate after fatal errors
break;
default:
assert false :
Messages.format( Messages.UNRECOGNIZED_SEVERITY,
event.getSeverity() );
}
// calculate the location message
String location = getLocation( event );
System.out.println(
Messages.format( Messages.SEVERITY_MESSAGE,
severity,
event.getMessage(),
location ) );
// fail on the first error or fatal error
return retVal;
}
/**
* Calculate a location message for the event
*
*/
private String getLocation(ValidationEvent event) {
StringBuffer msg = new StringBuffer();
ValidationEventLocator locator = event.getLocator();
if( locator != null ) {
URL url = locator.getURL();
Object obj = locator.getObject();
Node node = locator.getNode();
int line = locator.getLineNumber();
if( url!=null || line!=-1 ) {
msg.append( "line " + line );
if( url!=null )
msg.append( " of " + url );
} else if( obj != null ) {
msg.append( " obj: " + obj.toString() );
} else if( node != null ) {
msg.append( " node: " + node.toString() );
}
} else {
msg.append( Messages.format( Messages.LOCATION_UNAVAILABLE ) );
}
return msg.toString();
}
}

View File

@@ -0,0 +1,97 @@
/*
* 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.helpers;
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 INPUTSTREAM_NOT_NULL = // 0 args
"AbstractUnmarshallerImpl.ISNotNull";
static final String MUST_BE_BOOLEAN = // 1 arg
"AbstractMarshallerImpl.MustBeBoolean";
static final String MUST_BE_STRING = // 1 arg
"AbstractMarshallerImpl.MustBeString";
static final String SEVERITY_MESSAGE = // 3 args
"DefaultValidationEventHandler.SeverityMessage";
static final String LOCATION_UNAVAILABLE = // 0 args
"DefaultValidationEventHandler.LocationUnavailable";
static final String UNRECOGNIZED_SEVERITY = // 1 arg
"DefaultValidationEventHandler.UnrecognizedSeverity";
static final String WARNING = // 0 args
"DefaultValidationEventHandler.Warning";
static final String ERROR = // 0 args
"DefaultValidationEventHandler.Error";
static final String FATAL_ERROR = // 0 args
"DefaultValidationEventHandler.FatalError";
static final String ILLEGAL_SEVERITY = // 0 args
"ValidationEventImpl.IllegalSeverity";
static final String MUST_NOT_BE_NULL = // 1 arg
"Shared.MustNotBeNull";
}

View File

@@ -0,0 +1,85 @@
/*
* 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.helpers;
import javax.xml.bind.ValidationEventLocator;
/**
* Default implementation of the NotIdentifiableEvent interface.
*
* <p>
* JAXB providers are allowed to use whatever class that implements
* the ValidationEvent interface. This class is just provided for a
* convenience.
*
* @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
* @see javax.xml.bind.NotIdentifiableEvent
* @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 NotIdentifiableEventImpl
extends ValidationEventImpl
implements javax.xml.bind.NotIdentifiableEvent {
/**
* Create a new NotIdentifiableEventImpl.
*
* @param _severity The severity value for this event. Must be one of
* ValidationEvent.WARNING, ValidationEvent.ERROR, or
* ValidationEvent.FATAL_ERROR
* @param _message The text message for this event - may be null.
* @param _locator The locator object for this event - may be null.
* @throws IllegalArgumentException if an illegal severity field is supplied
*/
public NotIdentifiableEventImpl( int _severity, String _message,
ValidationEventLocator _locator) {
super(_severity, _message, _locator);
}
/**
* Create a new NotIdentifiableEventImpl.
*
* @param _severity The severity value for this event. Must be one of
* ValidationEvent.WARNING, ValidationEvent.ERROR, or
* ValidationEvent.FATAL_ERROR
* @param _message The text message for this event - may be null.
* @param _locator The locator object for this event - may be null.
* @param _linkedException An optional linked exception that may provide
* additional information about the event - may be null.
* @throws IllegalArgumentException if an illegal severity field is supplied
*/
public NotIdentifiableEventImpl( int _severity, String _message,
ValidationEventLocator _locator,
Throwable _linkedException) {
super(_severity, _message, _locator, _linkedException);
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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.helpers;
import javax.xml.bind.ParseConversionEvent;
import javax.xml.bind.ValidationEventLocator;
/**
* Default implementation of the ParseConversionEvent interface.
*
* <p>
* JAXB providers are allowed to use whatever class that implements
* the ValidationEvent interface. This class is just provided for a
* convenience.
*
* @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
* @see javax.xml.bind.ParseConversionEvent
* @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 ParseConversionEventImpl
extends ValidationEventImpl
implements ParseConversionEvent {
/**
* Create a new ParseConversionEventImpl.
*
* @param _severity The severity value for this event. Must be one of
* ValidationEvent.WARNING, ValidationEvent.ERROR, or
* ValidationEvent.FATAL_ERROR
* @param _message The text message for this event - may be null.
* @param _locator The locator object for this event - may be null.
* @throws IllegalArgumentException if an illegal severity field is supplied
*/
public ParseConversionEventImpl( int _severity, String _message,
ValidationEventLocator _locator) {
super(_severity, _message, _locator);
}
/**
* Create a new ParseConversionEventImpl.
*
* @param _severity The severity value for this event. Must be one of
* ValidationEvent.WARNING, ValidationEvent.ERROR, or
* ValidationEvent.FATAL_ERROR
* @param _message The text message for this event - may be null.
* @param _locator The locator object for this event - may be null.
* @param _linkedException An optional linked exception that may provide
* additional information about the event - may be null.
* @throws IllegalArgumentException if an illegal severity field is supplied
*/
public ParseConversionEventImpl( int _severity, String _message,
ValidationEventLocator _locator,
Throwable _linkedException) {
super(_severity, _message, _locator, _linkedException);
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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.helpers;
import javax.xml.bind.PrintConversionEvent;
import javax.xml.bind.ValidationEventLocator;
/**
* Default implementation of the PrintConversionEvent interface.
*
* <p>
* JAXB providers are allowed to use whatever class that implements
* the ValidationEvent interface. This class is just provided for a
* convenience.
*
* @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
* @see javax.xml.bind.PrintConversionEvent
* @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 PrintConversionEventImpl
extends ValidationEventImpl
implements PrintConversionEvent {
/**
* Create a new PrintConversionEventImpl.
*
* @param _severity The severity value for this event. Must be one of
* ValidationEvent.WARNING, ValidationEvent.ERROR, or
* ValidationEvent.FATAL_ERROR
* @param _message The text message for this event - may be null.
* @param _locator The locator object for this event - may be null.
* @throws IllegalArgumentException if an illegal severity field is supplied
*/
public PrintConversionEventImpl( int _severity, String _message,
ValidationEventLocator _locator) {
super(_severity, _message, _locator);
}
/**
* Create a new PrintConversionEventImpl.
*
* @param _severity The severity value for this event. Must be one of
* ValidationEvent.WARNING, ValidationEvent.ERROR, or
* ValidationEvent.FATAL_ERROR
* @param _message The text message for this event - may be null.
* @param _locator The locator object for this event - may be null.
* @param _linkedException An optional linked exception that may provide
* additional information about the event - may be null.
* @throws IllegalArgumentException if an illegal severity field is supplied
*/
public PrintConversionEventImpl( int _severity, String _message,
ValidationEventLocator _locator,
Throwable _linkedException) {
super(_severity, _message, _locator, _linkedException);
}
}

View File

@@ -0,0 +1,175 @@
/*
* 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.helpers;
import java.text.MessageFormat;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventLocator;
/**
* Default implementation of the ValidationEvent interface.
*
* <p>
* JAXB providers are allowed to use whatever class that implements
* the ValidationEvent interface. This class is just provided for a
* convenience.
*
* @author <ul><li>Kohsuke Kawaguchi, 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 ValidationEventImpl implements ValidationEvent
{
/**
* Create a new ValidationEventImpl.
*
* @param _severity The severity value for this event. Must be one of
* ValidationEvent.WARNING, ValidationEvent.ERROR, or
* ValidationEvent.FATAL_ERROR
* @param _message The text message for this event - may be null.
* @param _locator The locator object for this event - may be null.
* @throws IllegalArgumentException if an illegal severity field is supplied
*/
public ValidationEventImpl( int _severity, String _message,
ValidationEventLocator _locator ) {
this(_severity,_message,_locator,null);
}
/**
* Create a new ValidationEventImpl.
*
* @param _severity The severity value for this event. Must be one of
* ValidationEvent.WARNING, ValidationEvent.ERROR, or
* ValidationEvent.FATAL_ERROR
* @param _message The text message for this event - may be null.
* @param _locator The locator object for this event - may be null.
* @param _linkedException An optional linked exception that may provide
* additional information about the event - may be null.
* @throws IllegalArgumentException if an illegal severity field is supplied
*/
public ValidationEventImpl( int _severity, String _message,
ValidationEventLocator _locator,
Throwable _linkedException ) {
setSeverity( _severity );
this.message = _message;
this.locator = _locator;
this.linkedException = _linkedException;
}
private int severity;
private String message;
private Throwable linkedException;
private ValidationEventLocator locator;
public int getSeverity() {
return severity;
}
/**
* Set the severity field of this event.
*
* @param _severity Must be one of ValidationEvent.WARNING,
* ValidationEvent.ERROR, or ValidationEvent.FATAL_ERROR.
* @throws IllegalArgumentException if an illegal severity field is supplied
*/
public void setSeverity( int _severity ) {
if( _severity != ValidationEvent.WARNING &&
_severity != ValidationEvent.ERROR &&
_severity != ValidationEvent.FATAL_ERROR ) {
throw new IllegalArgumentException(
Messages.format( Messages.ILLEGAL_SEVERITY ) );
}
this.severity = _severity;
}
public String getMessage() {
return message;
}
/**
* Set the message field of this event.
*
* @param _message String message - may be null.
*/
public void setMessage( String _message ) {
this.message = _message;
}
public Throwable getLinkedException() {
return linkedException;
}
/**
* Set the linked exception field of this event.
*
* @param _linkedException Optional linked exception - may be null.
*/
public void setLinkedException( Throwable _linkedException ) {
this.linkedException = _linkedException;
}
public ValidationEventLocator getLocator() {
return locator;
}
/**
* Set the locator object for this event.
*
* @param _locator The locator - may be null.
*/
public void setLocator( ValidationEventLocator _locator ) {
this.locator = _locator;
}
/**
* Returns a string representation of this object in a format
* helpful to debugging.
*
* @see Object#equals(Object)
*/
public String toString() {
String s;
switch(getSeverity()) {
case WARNING: s="WARNING";break;
case ERROR: s="ERROR";break;
case FATAL_ERROR: s="FATAL_ERROR";break;
default: s=String.valueOf(getSeverity());break;
}
return MessageFormat.format("[severity={0},message={1},locator={2}]",
new Object[]{
s,
getMessage(),
getLocator()
});
}
}

View File

@@ -0,0 +1,273 @@
/*
* 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.helpers;
import java.net.URL;
import java.net.MalformedURLException;
import java.text.MessageFormat;
import javax.xml.bind.ValidationEventLocator;
import org.w3c.dom.Node;
import org.xml.sax.Locator;
import org.xml.sax.SAXParseException;
/**
* Default implementation of the ValidationEventLocator interface.
*
* <p>
* JAXB providers are allowed to use whatever class that implements
* the ValidationEventLocator interface. This class is just provided for a
* convenience.
*
* @author <ul><li>Kohsuke Kawaguchi, 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 ValidationEventLocatorImpl implements ValidationEventLocator
{
/**
* Creates an object with all fields unavailable.
*/
public ValidationEventLocatorImpl() {
}
/**
* Constructs an object from an org.xml.sax.Locator.
*
* The object's ColumnNumber, LineNumber, and URL become available from the
* values returned by the locator's getColumnNumber(), getLineNumber(), and
* getSystemId() methods respectively. Node, Object, and Offset are not
* available.
*
* @param loc the SAX Locator object that will be used to populate this
* event locator.
* @throws IllegalArgumentException if the Locator is null
*/
public ValidationEventLocatorImpl( Locator loc ) {
if( loc == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "loc" ) );
}
this.url = toURL(loc.getSystemId());
this.columnNumber = loc.getColumnNumber();
this.lineNumber = loc.getLineNumber();
}
/**
* Constructs an object from the location information of a SAXParseException.
*
* The object's ColumnNumber, LineNumber, and URL become available from the
* values returned by the locator's getColumnNumber(), getLineNumber(), and
* getSystemId() methods respectively. Node, Object, and Offset are not
* available.
*
* @param e the SAXParseException object that will be used to populate this
* event locator.
* @throws IllegalArgumentException if the SAXParseException is null
*/
public ValidationEventLocatorImpl( SAXParseException e ) {
if( e == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "e" ) );
}
this.url = toURL(e.getSystemId());
this.columnNumber = e.getColumnNumber();
this.lineNumber = e.getLineNumber();
}
/**
* Constructs an object that points to a DOM Node.
*
* The object's Node becomes available. ColumnNumber, LineNumber, Object,
* Offset, and URL are not available.
*
* @param _node the DOM Node object that will be used to populate this
* event locator.
* @throws IllegalArgumentException if the Node is null
*/
public ValidationEventLocatorImpl(Node _node) {
if( _node == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "_node" ) );
}
this.node = _node;
}
/**
* Constructs an object that points to a JAXB content object.
*
* The object's Object becomes available. ColumnNumber, LineNumber, Node,
* Offset, and URL are not available.
*
* @param _object the Object that will be used to populate this
* event locator.
* @throws IllegalArgumentException if the Object is null
*/
public ValidationEventLocatorImpl(Object _object) {
if( _object == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "_object" ) );
}
this.object = _object;
}
/** Converts a system ID to an URL object. */
private static URL toURL( String systemId ) {
try {
return new URL(systemId);
} catch( MalformedURLException e ) {
// TODO: how should we handle system id here?
return null; // for now
}
}
private URL url = null;
private int offset = -1;
private int lineNumber = -1;
private int columnNumber = -1;
private Object object = null;
private Node node = null;
/**
* @see javax.xml.bind.ValidationEventLocator#getURL()
*/
public URL getURL() {
return url;
}
/**
* Set the URL field on this event locator. Null values are allowed.
*
* @param _url the url
*/
public void setURL( URL _url ) {
this.url = _url;
}
/**
* @see javax.xml.bind.ValidationEventLocator#getOffset()
*/
public int getOffset() {
return offset;
}
/**
* Set the offset field on this event locator.
*
* @param _offset the offset
*/
public void setOffset( int _offset ) {
this.offset = _offset;
}
/**
* @see javax.xml.bind.ValidationEventLocator#getLineNumber()
*/
public int getLineNumber() {
return lineNumber;
}
/**
* Set the lineNumber field on this event locator.
*
* @param _lineNumber the line number
*/
public void setLineNumber( int _lineNumber ) {
this.lineNumber = _lineNumber;
}
/**
* @see javax.xml.bind.ValidationEventLocator#getColumnNumber()
*/
public int getColumnNumber() {
return columnNumber;
}
/**
* Set the columnNumber field on this event locator.
*
* @param _columnNumber the column number
*/
public void setColumnNumber( int _columnNumber ) {
this.columnNumber = _columnNumber;
}
/**
* @see javax.xml.bind.ValidationEventLocator#getObject()
*/
public Object getObject() {
return object;
}
/**
* Set the Object field on this event locator. Null values are allowed.
*
* @param _object the java content object
*/
public void setObject( Object _object ) {
this.object = _object;
}
/**
* @see javax.xml.bind.ValidationEventLocator#getNode()
*/
public Node getNode() {
return node;
}
/**
* Set the Node field on this event locator. Null values are allowed.
*
* @param _node the Node
*/
public void setNode( Node _node ) {
this.node = _node;
}
/**
* Returns a string representation of this object in a format
* helpful to debugging.
*
* @see Object#equals(Object)
*/
public String toString() {
return MessageFormat.format("[node={0},object={1},url={2},line={3},col={4},offset={5}]",
getNode(),
getObject(),
getURL(),
String.valueOf(getLineNumber()),
String.valueOf(getColumnNumber()),
String.valueOf(getOffset()));
}
}