Polishing

This commit is contained in:
Juergen Hoeller 2013-02-11 21:51:02 +01:00 committed by unknown
parent 23925edc95
commit 93e6238f92
3 changed files with 68 additions and 76 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,24 +24,24 @@ import javax.xml.transform.sax.SAXResult;
import org.xml.sax.ContentHandler; import org.xml.sax.ContentHandler;
/** /**
* Implementation of the {@code Result} tagging interface for StAX writers. Can be constructed with a * Implementation of the {@code Result} tagging interface for StAX writers. Can be constructed with
* {@code XMLEventConsumer} or a {@code XMLStreamWriter}. * an {@code XMLEventConsumer} or an {@code XMLStreamWriter}.
* *
* <p>This class is necessary because there is no implementation of {@code Source} for StaxReaders in JAXP 1.3. * <p>This class is necessary because there is no implementation of {@code Source} for StaxReaders
* There is a {@code StAXResult} in JAXP 1.4 (JDK 1.6), but this class is kept around for back-ward compatibility * in JAXP 1.3. There is a {@code StAXResult} in JAXP 1.4 (JDK 1.6), but this class is kept around
* reasons. * for backwards compatibility reasons.
* *
* <p>Even though {@code StaxResult} extends from {@code SAXResult}, calling the methods of * <p>Even though {@code StaxResult} extends from {@code SAXResult}, calling the methods of
* {@code SAXResult} is <strong>not supported</strong>. In general, the only supported operation on this class is * {@code SAXResult} is <strong>not supported</strong>. In general, the only supported operation
* to use the {@code ContentHandler} obtained via {@link #getHandler()} to parse an input source using an * on this class is to use the {@code ContentHandler} obtained via {@link #getHandler()} to parse an
* {@code XMLReader}. Calling {@link #setHandler(org.xml.sax.ContentHandler)} will result in * input source using an {@code XMLReader}. Calling {@link #setHandler(org.xml.sax.ContentHandler)}
* {@code UnsupportedOperationException}s. * will result in {@code UnsupportedOperationException}s.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0
* @see XMLEventWriter * @see XMLEventWriter
* @see XMLStreamWriter * @see XMLStreamWriter
* @see javax.xml.transform.Transformer * @see javax.xml.transform.Transformer
* @since 3.0
*/ */
class StaxResult extends SAXResult { class StaxResult extends SAXResult {
@ -49,9 +49,9 @@ class StaxResult extends SAXResult {
private XMLStreamWriter streamWriter; private XMLStreamWriter streamWriter;
/** /**
* Constructs a new instance of the {@code StaxResult} with the specified {@code XMLStreamWriter}. * Construct a new instance of the {@code StaxResult} with the specified {@code XMLStreamWriter}.
*
* @param streamWriter the {@code XMLStreamWriter} to write to * @param streamWriter the {@code XMLStreamWriter} to write to
*/ */
StaxResult(XMLStreamWriter streamWriter) { StaxResult(XMLStreamWriter streamWriter) {
@ -60,8 +60,7 @@ class StaxResult extends SAXResult {
} }
/** /**
* Constructs a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}. * Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}.
*
* @param eventWriter the {@code XMLEventWriter} to write to * @param eventWriter the {@code XMLEventWriter} to write to
*/ */
StaxResult(XMLEventWriter eventWriter) { StaxResult(XMLEventWriter eventWriter) {
@ -70,9 +69,8 @@ class StaxResult extends SAXResult {
} }
/** /**
* Constructs a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter} and * Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}
* {@code XMLEventFactory}. * and {@code XMLEventFactory}.
*
* @param eventWriter the {@code XMLEventWriter} to write to * @param eventWriter the {@code XMLEventWriter} to write to
* @param eventFactory the {@code XMLEventFactory} to use for creating events * @param eventFactory the {@code XMLEventFactory} to use for creating events
*/ */
@ -81,35 +79,35 @@ class StaxResult extends SAXResult {
this.eventWriter = eventWriter; this.eventWriter = eventWriter;
} }
/** /**
* Returns the {@code XMLEventWriter} used by this {@code StaxResult}. If this {@code StaxResult} was * Return the {@code XMLEventWriter} used by this {@code StaxResult}. If this {@code StaxResult}
* created with an {@code XMLStreamWriter}, the result will be {@code null}. * was created with an {@code XMLStreamWriter}, the result will be {@code null}.
*
* @return the StAX event writer used by this result * @return the StAX event writer used by this result
* @see #StaxResult(javax.xml.stream.XMLEventWriter) * @see #StaxResult(javax.xml.stream.XMLEventWriter)
*/ */
XMLEventWriter getXMLEventWriter() { XMLEventWriter getXMLEventWriter() {
return eventWriter; return this.eventWriter;
} }
/** /**
* Returns the {@code XMLStreamWriter} used by this {@code StaxResult}. If this {@code StaxResult} was * Return the {@code XMLStreamWriter} used by this {@code StaxResult}. If this {@code StaxResult}
* created with an {@code XMLEventConsumer}, the result will be {@code null}. * was created with an {@code XMLEventConsumer}, the result will be {@code null}.
*
* @return the StAX stream writer used by this result * @return the StAX stream writer used by this result
* @see #StaxResult(javax.xml.stream.XMLStreamWriter) * @see #StaxResult(javax.xml.stream.XMLStreamWriter)
*/ */
XMLStreamWriter getXMLStreamWriter() { XMLStreamWriter getXMLStreamWriter() {
return streamWriter; return this.streamWriter;
} }
/** /**
* Throws a {@code UnsupportedOperationException}. * Throws an {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException always * @throws UnsupportedOperationException always
*/ */
@Override @Override
public void setHandler(ContentHandler handler) { public void setHandler(ContentHandler handler) {
throw new UnsupportedOperationException("setHandler is not supported"); throw new UnsupportedOperationException("setHandler is not supported");
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,24 +24,24 @@ import org.xml.sax.InputSource;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
/** /**
* Implementation of the {@code Source} tagging interface for StAX readers. Can be constructed with a * Implementation of the {@code Source} tagging interface for StAX readers. Can be constructed with
* {@code XMLEventReader} or a {@code XMLStreamReader}. * an {@code XMLEventReader} or an {@code XMLStreamReader}.
* *
* <p>This class is necessary because there is no implementation of {@code Source} for StAX Readers in JAXP 1.3. * <p>This class is necessary because there is no implementation of {@code Source} for StAX Readers
* There is a {@code StAXSource} in JAXP 1.4 (JDK 1.6), but this class is kept around for back-ward compatibility * in JAXP 1.3. There is a {@code StAXSource} in JAXP 1.4 (JDK 1.6), but this class is kept around
* reasons. * for backwards compatibility reasons.
* *
* <p>Even though {@code StaxSource} extends from {@code SAXSource}, calling the methods of * <p>Even though {@code StaxSource} extends from {@code SAXSource}, calling the methods of
* {@code SAXSource} is <strong>not supported</strong>. In general, the only supported operation on this class is * {@code SAXSource} is <strong>not supported</strong>. In general, the only supported operation
* to use the {@code XMLReader} obtained via {@link #getXMLReader()} to parse the input source obtained via {@link * on this class is to use the {@code XMLReader} obtained via {@link #getXMLReader()} to parse the
* #getInputSource()}. Calling {@link #setXMLReader(XMLReader)} or {@link #setInputSource(InputSource)} will result in * input source obtained via {@link #getInputSource()}. Calling {@link #setXMLReader(XMLReader)}
* {@code UnsupportedOperationException}s. * or {@link #setInputSource(InputSource)} will result in {@code UnsupportedOperationException}s.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0
* @see XMLEventReader * @see XMLEventReader
* @see XMLStreamReader * @see XMLStreamReader
* @see javax.xml.transform.Transformer * @see javax.xml.transform.Transformer
* @since 3.0
*/ */
class StaxSource extends SAXSource { class StaxSource extends SAXSource {
@ -49,11 +49,11 @@ class StaxSource extends SAXSource {
private XMLStreamReader streamReader; private XMLStreamReader streamReader;
/** /**
* Constructs a new instance of the {@code StaxSource} with the specified {@code XMLStreamReader}. The * Construct a new instance of the {@code StaxSource} with the specified {@code XMLStreamReader}.
* supplied stream reader must be in {@code XMLStreamConstants.START_DOCUMENT} or * The supplied stream reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
* {@code XMLStreamConstants.START_ELEMENT} state. * {@code XMLStreamConstants.START_ELEMENT} state.
*
* @param streamReader the {@code XMLStreamReader} to read from * @param streamReader the {@code XMLStreamReader} to read from
* @throws IllegalStateException if the reader is not at the start of a document or element * @throws IllegalStateException if the reader is not at the start of a document or element
*/ */
@ -63,10 +63,9 @@ class StaxSource extends SAXSource {
} }
/** /**
* Constructs a new instance of the {@code StaxSource} with the specified {@code XMLEventReader}. The * Construct a new instance of the {@code StaxSource} with the specified {@code XMLEventReader}.
* supplied event reader must be in {@code XMLStreamConstants.START_DOCUMENT} or * The supplied event reader must be in {@code XMLStreamConstants.START_DOCUMENT} or
* {@code XMLStreamConstants.START_ELEMENT} state. * {@code XMLStreamConstants.START_ELEMENT} state.
*
* @param eventReader the {@code XMLEventReader} to read from * @param eventReader the {@code XMLEventReader} to read from
* @throws IllegalStateException if the reader is not at the start of a document or element * @throws IllegalStateException if the reader is not at the start of a document or element
*/ */
@ -75,31 +74,30 @@ class StaxSource extends SAXSource {
this.eventReader = eventReader; this.eventReader = eventReader;
} }
/** /**
* Returns the {@code XMLEventReader} used by this {@code StaxSource}. If this {@code StaxSource} was * Return the {@code XMLEventReader} used by this {@code StaxSource}. If this {@code StaxSource}
* created with an {@code XMLStreamReader}, the result will be {@code null}. * was created with an {@code XMLStreamReader}, the result will be {@code null}.
*
* @return the StAX event reader used by this source * @return the StAX event reader used by this source
* @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader) * @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader)
*/ */
XMLEventReader getXMLEventReader() { XMLEventReader getXMLEventReader() {
return eventReader; return this.eventReader;
} }
/** /**
* Returns the {@code XMLStreamReader} used by this {@code StaxSource}. If this {@code StaxSource} was * Return the {@code XMLStreamReader} used by this {@code StaxSource}. If this {@code StaxSource}
* created with an {@code XMLEventReader}, the result will be {@code null}. * was created with an {@code XMLEventReader}, the result will be {@code null}.
*
* @return the StAX event reader used by this source * @return the StAX event reader used by this source
* @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader) * @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader)
*/ */
XMLStreamReader getXMLStreamReader() { XMLStreamReader getXMLStreamReader() {
return streamReader; return this.streamReader;
} }
/** /**
* Throws a {@code UnsupportedOperationException}. * Throws an {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException always * @throws UnsupportedOperationException always
*/ */
@Override @Override
@ -108,12 +106,12 @@ class StaxSource extends SAXSource {
} }
/** /**
* Throws a {@code UnsupportedOperationException}. * Throws an {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException always * @throws UnsupportedOperationException always
*/ */
@Override @Override
public void setXMLReader(XMLReader reader) { public void setXMLReader(XMLReader reader) {
throw new UnsupportedOperationException("setXMLReader is not supported"); throw new UnsupportedOperationException("setXMLReader is not supported");
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -45,14 +45,15 @@ import org.springframework.util.ClassUtils;
*/ */
public abstract class StaxUtils { public abstract class StaxUtils {
// JAXP 1.4 is only available on JDK 1.6+
private static boolean jaxp14Available = private static boolean jaxp14Available =
ClassUtils.isPresent("javax.xml.transform.stax.StAXSource", StaxUtils.class.getClassLoader()); ClassUtils.isPresent("javax.xml.transform.stax.StAXSource", StaxUtils.class.getClassLoader());
// Stax Source // Stax Source
/** /**
* Create a custom, non-JAXP 1.4 StAX {@link Source} for the given {@link XMLStreamReader}. * Create a custom, non-JAXP 1.4 StAX {@link Source} for the given {@link XMLStreamReader}.
*
* @param streamReader the StAX stream reader * @param streamReader the StAX stream reader
* @return a source wrapping the {@code streamReader} * @return a source wrapping the {@code streamReader}
*/ */
@ -62,9 +63,8 @@ public abstract class StaxUtils {
/** /**
* Create a StAX {@link Source} for the given {@link XMLStreamReader}. * Create a StAX {@link Source} for the given {@link XMLStreamReader}.
* * <p>If JAXP 1.4 is available, this method returns a {@link StAXSource};
* <p>If JAXP 1.4 is available, this method returns a {@link StAXSource}; otherwise it returns a * otherwise it returns a custom StAX Source.
* custom StAX Source.
* @param streamReader the StAX stream reader * @param streamReader the StAX stream reader
* @return a source wrapping the {@code streamReader} * @return a source wrapping the {@code streamReader}
* @see #createCustomStaxSource(XMLStreamReader) * @see #createCustomStaxSource(XMLStreamReader)
@ -80,7 +80,6 @@ public abstract class StaxUtils {
/** /**
* Create a custom, non-JAXP 1.4 StAX {@link Source} for the given {@link XMLEventReader}. * Create a custom, non-JAXP 1.4 StAX {@link Source} for the given {@link XMLEventReader}.
*
* @param eventReader the StAX event reader * @param eventReader the StAX event reader
* @return a source wrapping the {@code eventReader} * @return a source wrapping the {@code eventReader}
*/ */
@ -90,9 +89,8 @@ public abstract class StaxUtils {
/** /**
* Create a StAX {@link Source} for the given {@link XMLEventReader}. * Create a StAX {@link Source} for the given {@link XMLEventReader}.
* * <p>If JAXP 1.4 is available, this method returns a {@link StAXSource};
* <p>If JAXP 1.4 is available, this method returns a {@link StAXSource}; otherwise it returns a * otherwise it returns a custom StAX Source.
* custom StAX Source.
* @param eventReader the StAX event reader * @param eventReader the StAX event reader
* @return a source wrapping the {@code eventReader} * @return a source wrapping the {@code eventReader}
* @throws XMLStreamException in case of StAX errors * @throws XMLStreamException in case of StAX errors
@ -116,11 +114,11 @@ public abstract class StaxUtils {
return (source instanceof StaxSource || (jaxp14Available && Jaxp14StaxHandler.isStaxSource(source))); return (source instanceof StaxSource || (jaxp14Available && Jaxp14StaxHandler.isStaxSource(source)));
} }
// Stax Result // Stax Result
/** /**
* Create a custom, non-JAXP 1.4 StAX {@link Result} for the given {@link XMLStreamWriter}. * Create a custom, non-JAXP 1.4 StAX {@link Result} for the given {@link XMLStreamWriter}.
*
* @param streamWriter the StAX stream writer * @param streamWriter the StAX stream writer
* @return a source wrapping the {@code streamWriter} * @return a source wrapping the {@code streamWriter}
*/ */
@ -130,9 +128,8 @@ public abstract class StaxUtils {
/** /**
* Create a StAX {@link Result} for the given {@link XMLStreamWriter}. * Create a StAX {@link Result} for the given {@link XMLStreamWriter}.
* * <p>If JAXP 1.4 is available, this method returns a {@link StAXResult};
* <p>If JAXP 1.4 is available, this method returns a {@link StAXResult}; otherwise it returns a * otherwise it returns a custom StAX Result.
* custom StAX Result.
* @param streamWriter the StAX stream writer * @param streamWriter the StAX stream writer
* @return a result wrapping the {@code streamWriter} * @return a result wrapping the {@code streamWriter}
* @see #createCustomStaxResult(XMLStreamWriter) * @see #createCustomStaxResult(XMLStreamWriter)
@ -148,7 +145,6 @@ public abstract class StaxUtils {
/** /**
* Create a custom, non-JAXP 1.4 StAX {@link Result} for the given {@link XMLEventWriter}. * Create a custom, non-JAXP 1.4 StAX {@link Result} for the given {@link XMLEventWriter}.
*
* @param eventWriter the StAX event writer * @param eventWriter the StAX event writer
* @return a source wrapping the {@code eventWriter} * @return a source wrapping the {@code eventWriter}
*/ */
@ -158,7 +154,6 @@ public abstract class StaxUtils {
/** /**
* Create a StAX {@link Result} for the given {@link XMLEventWriter}. * Create a StAX {@link Result} for the given {@link XMLEventWriter}.
*
* <p>If JAXP 1.4 is available, this method returns a {@link StAXResult}; otherwise it returns a * <p>If JAXP 1.4 is available, this method returns a {@link StAXResult}; otherwise it returns a
* custom StAX Result. * custom StAX Result.
* @param eventWriter the StAX event writer * @param eventWriter the StAX event writer
@ -177,8 +172,8 @@ public abstract class StaxUtils {
/** /**
* Indicate whether the given {@link javax.xml.transform.Result} is a StAX Result. * Indicate whether the given {@link javax.xml.transform.Result} is a StAX Result.
* @return {@code true} if {@code result} is a custom Stax Result or JAXP * @return {@code true} if {@code result} is a custom Stax Result or JAXP 1.4
* 1.4 {@link StAXResult}; {@code false} otherwise. * {@link StAXResult}; {@code false} otherwise.
*/ */
public static boolean isStaxResult(Result result) { public static boolean isStaxResult(Result result) {
return (result instanceof StaxResult || (jaxp14Available && Jaxp14StaxHandler.isStaxResult(result))); return (result instanceof StaxResult || (jaxp14Available && Jaxp14StaxHandler.isStaxResult(result)));
@ -327,6 +322,7 @@ public abstract class StaxUtils {
return new XMLEventStreamWriter(eventWriter, eventFactory); return new XMLEventStreamWriter(eventWriter, eventFactory);
} }
/** /**
* Inner class to avoid a static JAXP 1.4 dependency. * Inner class to avoid a static JAXP 1.4 dependency.
*/ */
@ -349,11 +345,11 @@ public abstract class StaxUtils {
} }
private static boolean isStaxSource(Source source) { private static boolean isStaxSource(Source source) {
return source instanceof StAXSource; return (source instanceof StAXSource);
} }
private static boolean isStaxResult(Result result) { private static boolean isStaxResult(Result result) {
return result instanceof StAXResult; return (result instanceof StAXResult);
} }
private static XMLStreamReader getXMLStreamReader(Source source) { private static XMLStreamReader getXMLStreamReader(Source source) {